Chapter3 - Arithmetic and Logic Instructions
Chapter3 - Arithmetic and Logic Instructions
Instructions
Dr. Ali Mohamed
Faculty of Engineering at Aswan
Aswan University
Addition and Subtraction of Unsigned Numbers
Unsigned numbers are data in which all bits are used to represent data
and no bit is used to represent sign.
Operands can be between 00H to FFH for 8-bit data
The form of ADD instruction is,
The instructions ADD and ADC are used to add numbers where the
destination can be in a register or memory and the source should be in a
register, in memory or immediate.
Solution:
DH = 4C ➔ 01001100 ➔ 0100 1100
-6E ➔ -01101110 ➔ +1001 0010
DH = -22 1101 1110 ➔ 00100010 ➔ -22
CF=1 the result is negative
MOV AL,25H
MOV BL,65H
MUL BL
MOV RESULT,AX
32
5
The numerator must be in the AL register (AL 32)
AH must be set to zero (AH 0)
The denominator cannot be immediate but can be in a
register or memory.
(DL 5)
After the DIV instruction is performed (DIV DL),
the quotient is in AL (AL 6)
the remainder is in AH (AH 2)
The 80x86 Microprocessors 1.22 Assembly Language
DIV & Addressing Modes
DATA1 DB 95
DATA2 DB 10
QUOT DB ?
REM DB ?
; using immediate addressing mode will give an error
MOV AL,DATA1 ;move data into AL
SUB AH,AH ;clear AH
DIV 10 ;immediate mode not allowed!!
;using direct mode
MOV AL,DATA1 ;AL holds numerator
SUB AH,AH ;AH must be cleared
DIV DATA2 ;divide AX by DATA2
MOV QUOT,AL ;quotient = AL = 09
MOV REM,AH ;remainder = AH = 05
The 80x86 Microprocessors 1.23 Assembly Language
DIV & Addressing Modes (Cont.)
;using register addressing mode
MOV AL,DATA1
SUB AH,AH
MOV BH,DATA2
DIV BH
MOV QUOT,AL
MOV REM,AH
The numerator is in AX
DX must be cleared
The denominator can be in a register or memory.
After the DIV, AX will have the quotient and the remainder will be
in DX.
The numerator is in AX
The denominator can be in a register or memory.
After the DIV instruction, AL will contain the quotient, and AH will contain
the remainder.
The following program divides AX = 2055 by CL= 100.
Then AL = 14H (20 decimal) is the quotient and AH = 37H (55 decimal) is
the remainder.
MOV AX,2055 ;AX holds numerator
MOV CL,100 ;CL used for denominator
DIV CL
MOV QUOT,AL ;AL holds quotient
MOV REM,AH ;AH holds remainder
XXX: ...
The 80x86 Microprocessors 1.31 Assembly Language
The 80x86 Microprocessors 1.32 Assembly Language
XOR
The operand is shifted right bit by bit, and for every shift
the LSB will go to the carry flag (CF) and
the MSB is filled with 0
MOV AL,9AH
MOV CL,3
SHR AL,CL
Solution:
9AH ➔ 10011010
01001101 CF = 0
00100110 CF = 1
00010011 CF = 0
Although SHR does affect the OF, SF, PF, and ZF flags, they are not
important in this case.
The operand to be shifted can be in a register or in memory, but
immediate addressing mode is not allowed for shift instructions.
SHR 25,CL ; this instruction will cause an error.
Show the results of SHR in the following:
;from the data segment:
DATA1 DW 7777H
;from the code segment:
TIMES EQU 4
MOV CL,TIMES
SHR DATA1,CL
Solution
0111011101110111 ➔ 0011101110111011 ➔ 0001110111011101
➔ 0000111011101110 ➔ 0000011101110111
After every shift, the LSB is filled with 0 and the MSB goes to CF.
Show the effects of SHL in the following:
MOV DH,6
MOV CL,4
SHL DH,CL
Solution:
Initially CF 00000110
CF=0 00011000
CF=0 00110000
CF=0 01100000
CF=0 11000000
After the four shifts left, the DH register has 60H and CF = 0.
The rotation instructions ROR, ROL and RCR, RCL are designed
specifically to perform a bitwise rotation of an operand.
They allow a program to rotate an operand right or left.
The operand can be in a register or memory.
If the number of times an operand is to be rotated is more than 1, this is
indicated by CL. (This is similar to the shift instructions.)
CF
In rotate right, as bits are shifted from left to right they exit from the right
end (LSB) and enter the left end (MSB).
In ROR the LSB is moved to the MSB and is also copied to CF
If the operand is to be rotated once, the 1 is coded
If it is to be rotated more than once, register CL is used to hold the
number of times it is to be rotated.
MOV AL,36H ; AL = 00110110
ROR AL,1 ; AL = 00011011 CF=0
ROR AL,1 ; AL = 10001101 CF=1
ROR AL,1 ; AL = 11000110 CF=1
MOV CL,3
ROR AL,CL
In rotate left, bits are shifted from right to left they exit the left end (MSB)
and enter the right end (LSB).
In ROL the MSB is moved to the LSB and is also copied to CF
If the operand is to be rotated once, the 1 is coded.
Otherwise, the number of times it is to be rotated is in CL.
RCR
STC CLC
The 80x86 Microprocessors 1.51 Assembly Language
Example