Вы находитесь на странице: 1из 26

Assembly Language

Arithmetic and Logic Instructions

ACOE251 1
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 2
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 3
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 4
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 MOV AX,720H MOV DX,0


DEC DX SUB AX, 0E6H DEC DX

ACOE251 5
Example: Fill up the trace table given below.
Instructions AX BX CX SI Cy S Z Address Data
MOV AX,847AH 0100 3F
SUB CX,CX 0101 78
MOV BX,5CE8H 0102 5A
ADD AL,BH 0103 C8
ADC AH,BL 0104 93
MOV CL,AL 0105 59
ADD CH,BL 0106 4F
MOV SI,0108H 0107 A3
SUB BL,[SI] 0108 7E
SBB BH,[SI+4] 0109 F4
ADD AL,[010CH] 010A 09
MOV AX,[0106H] 010B 8A
INC AL 010C 5C
DEC BX 010D 6A
ADD CX,[S1+4] 010E 45

ACOE251 6
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 7
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 8
Example: Fill up the trace table given below.
Instructions AX BX DX SI Cy S Z Address Data
MOV AX,42C1H 0100 3F
SUB DX,DX 0101 78
MOV BX,2456H 0102 5A
MUL BL 0103 C8
MUL BX 0104 93
MOV AX,6A42H 0105 59
MOV CH,32H 0106 4F
DIV CH 0107 A3
MOV SI,0103H 0108 7E
MUL BYTE PTR [SI] 0109 F4
IMUL BYTE PTR [SI] 010A 09
MOV AX,[0109H] 010B 8A
IMUL AH 010C 5C
MOV BL,2 010D 6A
DIV BL 010E 45

ACOE251 9
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 10
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 11
Example: Fill up the trace table given below.
Instructions AX BX CX SI Cy S Z Address Data
MOV AX,847AH 0100 3F
XOR BX,BX 0101 78
MOV CX,5CE8H 0102 5A
AND AL,BH 0103 C8
AND AH,92H 0104 93
OR CL,AL 0105 59
XOR CH,BL 0106 4F
MOV SI,0108H 0107 A3
OR BL,[SI] 0108 7E
AND BH,[SI+2] 0109 F4
XOR AL,[010CH] 010A 09
NOT AX 010B 8A
NEG BL 010C 5C
TEST BL,14H 010D 6A
TEST AL,3FH 010E 45

ACOE251 12
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 13
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 14
Example: Fill up the trace table given below.
Instructio ns AX BX Cy S Z
MOV AX,547AH
MOV BX,9C8FH
ADD AL,BH
JC SKIP1
INC AL
SKIP1: ADD AH,BL
JC SKIP2
DEC AL
SKIP2: SUB BH,AL
JC SKIP3
INC BX
SKIP3: SUB BL,AH
JNC SKIP4
INC BX
SKIP4: DEC AX

ACOE251 15
Program Control Instructions :(Jump and Call)
Conditional jump using the Compare instruction: Hex Unsigned Signed

– Conditional jump instruction can be used after the 00 00 00


compare ( CMP ) instruction. 01 01 +01

– Comparison of unsigned numbers is done using the 02 02 +02


Above or Below conditions. For example 81H is Above
7EH since 129>128.
7E 126 +126
– Comparison of unsigned numbers is done using the
7F 127 +127
Greater or Less conditions. For example 81H is Less
than 7EH since -127<128. 80 128 -128
81 129 -127
– The programmer can choose between the
Above/Below or Greater/Less according to the 82 130 -126

application. If the values used in a program are always


positive then these values are treated as unsigned
FD 253 -03
numbers, and the Above /Below conditions are used,
otherwise signed numbers are used and the FE 254 -02
Greater/Less conditions are used.. FF 255 -01

ACOE251 16
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 17
Example: Fill up the trace table given below.
NUM=4500H NUM=9500H NUM=C500H
Instructions AH AL
MOV AX,NUM
CMP AH,77H
JA SKIP1
INC AL
SKIP1: CMP AH,89H
JA SKIP2
INC AL
SKIP2: CMP AH,77H
JL SKIP3
INC AL
SKIP3: CMP AH,97H
JL SKIP4
INC AL
SKIP4: INC AH

ACOE251 18
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 19
Example: Fill up the trace table given below.

ACOE251 20
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
CF 0

• SHR AL, BL ; The same principle, but now a


right shift
0 CF

ACOE251 21
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 22
Rotate instructions
• ROL
CF

• ROR
CF

ACOE251 23
Rotate with carry instructions
• RCL
CF

• RCR
CF

ACOE251 24
Example: Fill up the trace table given below.

ACOE251 25
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.

ACOE251 26

Вам также может понравиться