ACOE2511 Assembly Language Arithmetic and Logic Instructions
ACOE2512 Arithmetic Instructions: (Addition) A.Addition: (Flags affected: A,C,O,P,S,Z) –ADDAL,BL ; AL AL + BL, BL unchanged –ADD CX,DI ; CX CX + DI, DI unchanged –ADD AH,45H ; AH AH + 45H –ADD [BX],AL ; [BX] [BX] + AL –ADD CX,[BX] ; CX CX + [BX] –ADD AL,CX ; INVALID B. Add with Carry: (Flags affected: A,C,O,P,S,Z) –ADC AH,BH ; AH AH + BH + Carry –ADC AX,CX ; AX AX + CX + Carry –ADC AL,[BX+SI] ; AL AL + [BX+SI] + Carry
ACOE2513 Arithmetic Instructions: (Subtraction) C. Subtraction: (Flags affected: A,C,O,P,S,Z) –SUB AL,BL ; AL AL - BL ;BL unchanged –SUB CX,DI ; CX CX - DI ;DI unchanged –SUB AH,45H ; AH AH - 45H –SUB BL,ARRAY ; BL BL - [ARRAY] –SUB [BX],AL ; [BX] [BX] - AL –SUB AL,CX ; INVALID D. Subtract with Borrow: (Flags affected: A,C,O,P,S,Z) –SBB AH,BH ; AH AH - BH - Carry –SBB AX,CX ; AX AX - CX - Carry –SBB AL,[BX+SI] ; AL AL - [BX+SI] - Carry
ACOE2514 Arithmetic Instructions: (Increment, Decrement) E. Increment: (Flags affected: A,O,P,S,Z) –INC AL ; AL AL + 1 –INC SP ; SP SP + 1 –INC COUNT1 ; [COUNT1] [COUNT1] + 1 –INC BYTE PTR[BX] ; [BX] [BX] + 1 –INC WORD PTR[BX] ; [BX] [BX] + 1 F. Decrement: (Flags affected: A,O,P,S,Z) –DEC AL ; AL AL - 1 –DEC SP ; SP SP - 1 –DEC COUNT1 ; [COUNT1] [COUNT1] - 1 –DEC BYTE PTR[BX] ; [BX] [BX] - 1 –DEC WORD PTR[BX] ; [BX] [BX] - 1
ACOE2515 Examples What will be the values of the carry, overflow, sign and zero flags after the execution of each of the following instructions: MOV DX,0 DEC DX MOV AX,720H SUB AX, 0E6H MOV DX,0 DEC DX
ACOE2516 Example: Fill up the trace table given below.
ACOE2517 Arithmetic Instructions: (Multiplication) G. Multiplication: (Flags affected: C,O, (A,P,S,Z are undefined)) Unsigned multiplication: –MUL CL ; AX AL * CL –MUL CX ; DX,AX AX * CX –MUL BYTE PTR [BX]; AX AL * [BX] –MUL WORD PTR [SI] ; DX,AX AX * [SI] Signed multiplication (2's complement): –IMUL BL ; AX AL * BL –IMULBX ; DX,AX AX * BX –IMUL BYTE PTR [BX]; AX AL * [BX] –IMUL WORD PTR [SI] ; DX,AX AX * [SI]
ACOE2518 Arithmetic Instructions: (Division) H. Division: (Flags affected: A,C,O,P,S,Z (all undefined)) Unsigned Division: –DIV CL ; AL Quotient of AX/CL ; AH Remainder of AX/CL –DIV CX ; AX Quotient of DX,AX/CX ; DX Remainder of DX,AX/CX Signed Division: –IDIV CL ; AL Quotient of AX/CL ; AH Remainder of AX/CL –IDIV CX ; AX Quotient of DX,AX/CX ; DX Remainder of DX,AX/CX
ACOE2519 Example: Fill up the trace table given below.
ACOE25110 Arithmetic Instructions: (BCD and ASCII Operations) I. BCD and ASCII Arithmetic: –DAA ; Decimal Adjust for Addition –DAS ; Decimal Adjust for Subtraction –AAA ; ASCII Adjust for Addition –AAS ; ASCII Adjust for Subtraction –AAM ; ASCII Adjust for Multiplication –AAD ; ASCII Adjust for Division
ACOE25111 Logic Instructions:( AND, OR, XOR, NOT, NEG and TEST ) Logic Instructions: AND AL,BL ; AL AL AND BL (Always clears C and O flags) AND CL,33H ; CL CL AND 33H AND AX,[DI] ; AX AX AND [DI] OR AL,BL ; AL AL OR BL OR AX,1234H ; AX AX OR 1234H XOR AL,CL ; AL AL EX-OR CL XOR BH,0FH ; BH BH EX-OR 0FH NOT CH ; CH 1's complement of CH (No flags affected) NOT AX ; AX 1's complement of AX NEG CH ; CH 2's complement of CH (ALWAYS SETS CF) NEG BX ; BX 2's complement of BX TESTAL,30H ;Perform AL AND30H and set the flags. AL is unchanged.
ACOE25112 Example: Fill up the trace table given below.
ACOE25113 Program Control Instructions :(Jump and Call) Unconditional jump (JMP): –The JMP instruction specifies the address of the next instruction to be executed. There are three types of unconditional jump instructions: the SHORT, the NEAR, and the FAR. A SHORT jump is specified with only one byte which represents the displacement between the current instruction to the next instruction. The next instruction can be located at a distance from +127 to -128 memory locations away from the current instruction. A NEAR jump specifies the address of the next instruction within the current Code Segment. A FAR jump specifies the exact address of the next instruction by specifying the values of the CS and IP registers. –Examples: JMP NEXT JMP SHORT AGAIN JMP NEAR AGAIN JMP FAR AGAIN
ACOE25114 Program Control Instructions :(Jump and Call) Conditional jump: –Conditional jumps are executed only if the specified conditions are true. Usually the condition specified by a conditional jump instruction is the state of a flag. A list of the conditional jump instructions that check the state of flags is given below: Instruction Flags tested Action JC C = 1 Jump if carry set JZ Z = 1 Jump if equal or zero JS S = 1 Jump on sign (Negative) JO O = 1 Jump on overflow JNC C = 0 Jump if not carry JNZ Z = 0 Jump if not equal or 0 JNS S = 0 Jump if not sign (Positive) JNO O = 0 Jump if not overflow
ACOE25115 Example: Fill up the trace table given below.
ACOE25116 Program Control Instructions :(Jump and Call) Conditional jump using the Compare instruction: –Conditional jump instruction can be used after the compare ( CMP ) instruction. –Comparison of unsigned numbers is done using the Above or Below conditions. For example 81H is Above 7EH since 129>128. –Comparison of unsigned numbers is done using the Greater or Less conditions. For example 81H is Less than 7EH since -127<128. –The programmer can choose between the Above/Below or Greater/Less according to the application. If the values used in a program are always positive then these values are treated as unsigned numbers, and the Above /Below conditions are used, otherwise signed numbers are used and the Greater/Less conditions are used..
ACOE25117 Program Control Instructions :(Jump and Call) A list of the conditional jump instructions used with the Compare instruction is given below: Instruction Flags tested Action JA C = 0 & Z = 0 Jump if above JAE C = 0 Jump if above or equal JB C = 1 Jump if below JBE C = 1 or Z = 1 Jump if below or equal JE or JZ Z = 1 Jump if equal or zero JG O = Z AND S Jump if greater JGE S = O Jump if greater or equal JL S = O Jump if less JLE Z = 1 or S = O Jump if less or equal JNE or JNZ Z = 0 Jump if not equal or 0
ACOE25118 Example: Fill up the trace table given below.
ACOE25119 Program Control Instructions :(Jump and Call) Loops: –The LOOP instruction is a combination of the conditional jump and the decrement CX instructions. It will decrement the contents of CX and, if CX is not zero, jump to the label associated with the LOOP. If CX becomes zero, then the next sequential instruction is executed. –The LOOP instruction can also have conditional forms LOOPE (LOOPZ), and LOOPNE (LOOPNZ). –The conditional jump instruction JCXZ (Jump if CX = 0) can also be used. Procedures: –Procedures are implemented by using the PROC directive. The last instruction in a procedure must be the RET instruction. A procedure can be called by using the CALL instruction.
ACOE25120 Example: Fill up the trace table given below.
ACOE25121 Shift instructions SHL AL, BL ;Shifts the contents of AL to the left as many times as the value of BL, filling the lowest bit with zero and moving the highest to the carry flag SHR AL, BL ; The same principle, but now a right shift
ACOE25122 Arithmetic shifts SAL AL, BL ;Identical to SHL SAR AL, BL ;Shift right, but the most significant bit fills the leftmost position again
ACOE25123 Rotate instructions ROL ROR
ACOE25124 Rotate with carry instructions RCL RCR
ACOE25125 Example: Fill up the trace table given below.
ACOE25126 IN AND OUT INSTRUCTIONS IN AL, 3C ;Input byte from port 3Ch OUT 3Ch, AL ;Output byte to port 3Ch Use AX if you want to transfer 16 bits and EAX if you want to transfer 32 bits.