ACOE251 1 Assembly Language Arithmetic and Logic Instructions
ACOE251 2
Arithmetic Instructions: (Addition)
A. Addition: (Flags affected: A,C,O,P,S,Z)
– ADD AL,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
ACOE251 3
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
ACOE251 4
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
ACOE251 5
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
ACOE251 6
Example: Fill up the trace table given below.
Instructions AX
MOV AX,847AH
BX CX SI Cy S Z Address Data
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
010A
010B
010C
010D
010E
3F
78
5A
C8
93
59
4F
A3
7E
F4
09
8A
5C
6A
45
SUB CX,CX
MOV BX,5CE8H
ADD AL,BH
ADC AH,BL
MOV CL,AL
ADD CH,BL
MOV SI,0108H
SUB BL,[SI]
SBB BH,[SI+4]
ADD AL,[010CH]
MOV AX,[0106H]
INC AL
DEC BX
ADD CX,[S1+4]
ACOE251 7
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
– IMUL BX ; DX,AX AX * BX
– IMUL BYTE PTR [BX] ; AX AL * [BX]
– IMUL WORD PTR [SI] ; DX,AX AX * [SI]
ACOE251 8
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
ACOE251 9
Example: Fill up the trace table given below.
Instructions AX
MOV AX,42C1H
BX DX SI Cy S Z Address Data
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
010A
010B
010C
010D
010E
3F
78
5A
C8
93
59
4F
A3
7E
F4
09
8A
5C
6A
45
SUB DX,DX
MOV BX,2456H
MUL BL
MUL BX
MOV AX,6A42H
MOV CH,32H
DIV CH
MOV SI,0103H
MUL BYTE PTR [SI]
IMUL BYTE PTR [SI]
MOV AX,[0109H]
IMUL AH
MOV BL,2
DIV BL
ACOE251 10
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
ACOE251 11
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
• TEST AL,30H ;Perform AL AND30H and set the flags. AL is unchanged.
ACOE251 12
Example: Fill up the trace table given below.
Instructions AX
MOV AX,847AH
BX CX SI Cy S Z Address Data
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
010A
010B
010C
010D
010E
3F
78
5A
C8
93
59
4F
A3
7E
F4
09
8A
5C
6A
45
XOR BX,BX
MOV CX,5CE8H
AND AL,BH
AND AH,92H
OR CL,AL
XOR CH,BL
MOV SI,0108H
OR BL,[SI]
AND BH,[SI+2]
XOR AL,[010CH]
NOT AX
NEG BL
TEST BL,14H
TEST AL,3FH
ACOE251 13
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
ACOE251 14
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
ACOE251 15
Example: Fill up the trace table given below.
Instructions AX
MOV AX,547AH
BX Cy S Z
MOV BX,9C8FH
ADD AL,BH
JC SKIP1
INC AL
ADD AH,BL
JC SKIP2
DEC AL
SUB BH,AL
JC SKIP3
DEC AX
SUB BL,AH
JNC SKIP4
INC BX
SKIP1:
SKIP2:
SKIP3:
SKIP4:
INC BX
ACOE251 16
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..
Hex
00
Unsigned
00
01
02
126
127
128
129
130
253
254
255
01
02
7E
7F
80
81
82
FD
FE
FF
Signed
00
+01
+02
+126
+127
-128
-127
-126
-03
-02
-01
ACOE251 17
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
ACOE251 18
Example: Fill up the trace table given below.
Instructions AH
MOV AX,NUM
CMP AH,77H
JA SKIP1
INC AL
JA SKIP2
INC AL
JL SKIP3
INC AH
JL SKIP4
INC AL
SKIP1:
SKIP2:
SKIP3:
SKIP4:
INC AL
NUM=4500H
AL
CMP AH,89H
CMP AH,77H
CMP AH,97H
NUM=9500H NUM=C500H
ACOE251 19
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.
ACOE251 21
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
CF0
CF 0
ACOE251 22
Arithmetic shifts
• SAL AL, BL ;Identical to SHL
• SAR AL, BL ;Shift right, but the most significant bit fills the leftmost position again
CF
ACOE251 25
Example: Fill up the trace table given below. Instructions AX
MOV AX,547AH
BX Cy S Z
MOV BX,9C8FH
SHR BL, 1
SHL AL, 1
MOV AL, 4H
SHL BH, AL
ROL AH, 1
ROR BL, 3
SUB BH,AL
SAR BH, 1
RCR BH, AL
SHL AH, AL
SUB AL, 3
RCL BL, AL
INC AL