Arithmetic and Logic
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.
Memory to memory operations are not allowed.
The instructions can affect ZF, CF, SF, AF, PF bits of the flag
register.
Example
EXAMPLE: ADD / ADC (ver. 1)
Write a program to calculate the total sum of 5
bytes of data. Each byte represents the daily
wages of a worker. This person does not make
more than $255 (FFH) a day. The decimal data is
as follows: 125, 235, 197, 91, and 48
The 80x86 Microprocessors 1.5 Assembly Language
The 80x86 Microprocessors 1.6 Assembly Language
Example
Numbers are converted to hex by the assembler: 125 =
7DH, 235 = OEBH, 197 = OC5H, 91 = 5BH, 48 = 30H.
1st iteration of the loop, 7DH is added to AL with CF = 0
and AH = 00. CX = 04 and ZF = O.
2nd iteration of the loop, EBH is added to AL, which results
in AL = 68H and CF = 1. Since a carry occurred, AH is
incremented, AH=1. CX = 03 and ZF = O.
3rd iteration, C5H is added to AL, which makes AL = 2DH.
Again a carry occurred, AH is incremented again, AH = 2.
CX = 02 and ZF = O.
This process continues until CX = 00 and the zero flag
becomes 1
The 80x86 Microprocessors 1.7 Assembly Language
Example Continue
ADC AH, 00 means
AH = AH + 00 + CF
The 80x86 Microprocessors 1.8 Assembly Language
EXAMPLE: ADD / ADC
COUNT EQU 05
DATA DB 125, 235, 197, 91, 48
SUM DW ?
… … … … … …
MOV CX,COUNT
MOV SI, OFFSET DATA
MOV AX,00
CLC Why?
BACK: ADD AL,[SI]
ADC AH,00
INC SI
DEC CX
JNZ BACK
MOV SUM,AX
MOV AH, 4CH
INT 21H
The 80x86 Microprocessors 1.9 Assembly Language
EXAMPLE: Addition of words
Write a program to calculate the total sum of five
words of data. Each data value represents the yearly
wages of a worker. This person does not make more
than $65,555 (FFFFH) a year. The data: 27345,
28521, 29533, 30105, and 32375.
The 80x86 Microprocessors 1.10 Assembly Language
The 80x86 Microprocessors 1.11 Assembly Language
EXAMPLE: Addition of multiword Numbers
Write a program that adds the following two
multiword numbers and saves the result:
DATA1 = 548FB9963CE7H and
DATA2 = 3FCD4FA23B8DH.
548F B996 3CE7
3FCD 4FA2 3B8D
The 80x86 Microprocessors 1.12 Assembly Language
The 80x86 Microprocessors 1.13 Assembly Language
Subtraction of Unsigned Numbers
SUB destination, source ; destination = destination - source
2's complement is used in subtraction.
The 80x86 uses internal adder circuitry to perform the subtraction command.
The steps of the hardware in executing the SUB instruction for unsigned numbers,
as follows.
1. Take the 2's complement of the source operand.
2. Add it to the destination operand.
3. Invert the carry.
Example: Show the steps involved in the following:
MOV AL,3FH ; AL = 3FH = 0011 1111
MOV BH,23H ; BH = 23H = 00100011
SUB AL,BH ;AL = 0011 1111 – 00100011 = 0011 1111 +
1101 1101
10001 1100
CF = 0, ZF = 0, AF = 0, PF = 0, and SF = 0.
The programmer must look at the carry flag (not the sign flag) to determine if the
result is positive (CF = 0) or negative (CF = 1) .
The 80x86 Microprocessors 1.14 Assembly Language
Example: Analyze the following program
;From the data segment:
DATA1 DB 4CH
DATA2 DB 6EH
DATA3 DB ?
;From the code segment:
MOV DH,DATA1
SUB DH,DATA2
JNC NEXT
NOT DH
INC DH
NEXT: MOV DATA3,DH
Solution:
DH = 4C ➔ 01001100 ➔ 0100 1100
-6E ➔ -01101110 ➔ +1001 0010
DH = -22 1101 1110 ➔ 00100010 ➔ -22
CF=1 the result is negative
The 80x86 Microprocessors 1.15 Assembly Language
SBB (Subtract with Borrow)
SBB destination, source ; destination = destination - source - CF
Analyze the following program:
; From the data segment:
DATA1 DD 62562FAH
DATA2 DD 412963BH
RESULT DD ?
; From the code segment:
MOV AX, WORD PTR DATA1
SUB AX, WORD PTR DATA2
MOV WORD PTR RESULT, AX
MOV AX, WORD PTR DATA1+2
SBB AX, WORD PTR DATA2+2
MOV WORD PTR RESULT+2, AX
Solution:
DATA1 = 62562FAH = 0110001001010110001011111010
DATA2 = 412963BH = 0100000100101001011000111011
After the SUB ➔ AX = 62FA - 963B = CCBF ➔ CF = 1.
Since CF = 1, when SBB is executed, AX = 625 - 412- 1 = 212.
Therefore, the value stored in RESULT is 0212CCBF.
The 80x86 Microprocessors 1.16 Assembly Language
Multiplication of unsigned numbers
Byte Byte
One of the operands must be in the AL register
The second operand can be either in a register or in memory
After the multiplication, the result is in AX.
Example:
Multiply 25H by 65H and store the result in memory
Solution:
; From the data segment:
RESULT DW ?
; From the code segment:
MOV AL,25H
MOV BL,65H
MUL BL
MOV RESULT,AX
The 80x86 Microprocessors 1.17 Assembly Language
Example: Byte Byte Multiplication
; From the data segment
DATA1 DB 25H
DATA2 DB 65H
RESULT DW ?
; From the code segment
MOV AL,DATA1
MOV BL,DATA2
MUL BL ; register addressing mode
MOV RESULT,AX
OR
MOV AL,DATA1
MUL DATA2 ; direct memory addressing mode
MOV RESULT,AX
OR
MOV AL,DATA1
MOV SI,OFFSET DATA2
MUL BYTE PTR [SI] ; register indirect addressing mode
MOV RESULT,AX
The 80x86 Microprocessors 1.18 Assembly Language
Multiplication of unsigned numbers
word x word
One operand must be in AX
The second operand can be in a register or memory
After the multiplication, registers DX and AX will contain the result.
AX will hold the lower word and
DX the higher word.
Example:
Multiply 2378H by 2F79H and store the result in memory
; From the data segment:
DATA1 DW 2378H
DATA2 DW 2F79H
RESULT DW 2 DUP(?)
; From the code segment:
MOV AX,DATA1
MUL DATA2
MOV RESULT, AX
MOV RESULT+2, DX
The 80x86 Microprocessors 1.19 Assembly Language
Multiplication of unsigned numbers
word x byte
AL contain the byte
AH must be zero
The second operand can be in a register or memory.
After the multiplication, registers DX and AX will contain the result.
AX will hold the lower word and DX the higher word.
Example:
Multiply 2378H by 79H and store the result in memory
; From the data segment:
DATA1 DW 2378H
DATA2 DB 79H
RESULT DW 2 DUP(?)
; From the code segment:
MOV AL,DATA1
MOV AH,0
MUL DATA2
MOV BX, OFFSET RESULT
MOV [BX], AX
MOV [BX]+2, DX Assembly Language
The 80x86 Microprocessors 1.20
Unsigned Multiplication Summary
Multiplication Operand 1 Operand 2 Result
byte byte AL register or memory AX
word word AX register or memory DX, AX
word byte AL = byte, AH = 0 register or memory DX, AX
Flags ➔ Affected OF, CF
Unpredictable SF, ZF, AF, PF
The 80x86 Microprocessors 1.21 Assembly Language
Division of unsigned numbers
Byte over Byte
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
;using register indirect addressing mode
MOV AL,DATA1
SUB AH,AH
MOV BX,OFFSET DATA2
DIV BYTE PTR [BX]
MOV QUOT,AL
MOV
The 80x86 Microprocessors
REM,AH 1.24 Assembly Language
Word / Word
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.
MOV AX,10050 ;AX holds numerator
SUB DX,DX ;DX must be cleared
MOV BX,100 ;BX used for denominator
DIV BX
MOV QUOT,AX ;quotient = AX = 64H = 100
MOV REM,DX ;remainder = DX = 32H = 50
The 80x86 Microprocessors 1.25 Assembly Language
Word / Byte
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
The 80x86 Microprocessors 1.26 Assembly Language
Double Word / Word
The numerator is in DX and AX
The most significant word in DX and the least significant word in AX.
The denominator can be in a register or in memory.
After the DIV instruction, the quotient will be in AX, the remainder in DX.
;from the data segment:
DATA1 DD 105432
DATA2 DW 10000
QUOT DW ?
REMAIN DW ?
;from the code segment:
MOV AX,WORD PTR DATA1 ;AX holds lower word
MOV DX,WORD PTR DATA1+2 ;DX higher word of numerator
DIV DATA2
MOV QUOT,AX ;AX holds quotient
MOV REMAIN,DX ;DX holds remainder
The 80x86 Microprocessors 1.27 Assembly Language
Unsigned Division Summary
Division Numerator Denominator Quotient Rem
byte/byte AL = byte, AH = 0 register or memory AL AH
word/word AX = word, DX = 0 register or memory AX DX
word/byte AX = word register or memory AL AH
doubleword/word DX AX = doubleword register or memory AX DX
Flags ➔ Unpredictable OF, SF, ZF, AF, PF, CF
The 80x86 Microprocessors 1.28 Assembly Language
LOGIC INSTRUCTIONS AND
AND destination, source
This instruction will perform a bitwise logical AND on the operands and place
the result in the destination.
The destination operand can be a register or in memory.
The source operand can be a register, in memory, or immediate.
AND will automatically change the CF and OF to zero
Show the results of the following:
MOV BL,35H
AND BL,0FH
Solution:
35H ➔ 0011 0101
0FH ➔ 0000 1111
05H 0000 0101
Flag settings will be: SF = 0, ZF = 0, PF = 1, CF = 0, OF = 0
The 80x86 Microprocessors 1.29 Assembly Language
The Usage of AND
AND can be used to mask certain bits of the operand.
MOV DH,F0H
AND AH,DH
It can also be used to test for a zero operand:
AND DH,DH
JZ XXX
...
XXX: ...
After executing AND DH,DH, the content of DH does not change,
but, the ZF will be affected.
The 80x86 Microprocessors 1.30 Assembly Language
LOGIC OR
OR destination, source
The destination and source operands are ORed and the result is placed in
the destination.
The destination operand can be a register or in memory.
The source operand can be a register, in memory, or immediate.
OR can be used to set certain bits of an operand to 1.
OR DH,00000011B
CF and OF will be reset to zero
SF, ZF, and PF will be set according to the result.
All other flags are not affected.
The OR instruction can also be used to test for a zero operand.
OR DH,0H
OR DH,DH
JZ XXX
XXX: ...
The 80x86 Microprocessors 1.31 Assembly Language
The 80x86 Microprocessors 1.32 Assembly Language
XOR
XOR destination, source
The XOR instruction will eXclusive-OR the operands and place the result
in the destination.
XOR sets the result bits to 1 if they are not equal; otherwise, they are
reset to 0.
CF = 0 and OF = 0 are set internally
The rest are changed according to the result of the operation.
Show the results of the following:
MOV DH,54H
XOR DH,78H
54H ➔ 0101 0100
78H ➔ 0111 1000
2CH 0010 1100
Flag settings will be: SF = 0, ZF = 0, PF = 0, CF = OF = 0.
The 80x86 Microprocessors 1.33 Assembly Language
XOR
The 80x86 Microprocessors 1.34 Assembly Language
SHIFT
There are two kinds of shift
Logical shift: for unsigned operands
Arithmetic shift: for signed operands.
Using shift instructions shifts the contents of a register or memory
location right or left.
The number of times (or bits) that the operand is shifted can be specified
directly if it is once only, or through the CL register if it is more than once.
Flags ➔ Affected: OF, SF, ZF, PF, CF
Unpredictable: AF
The 80x86 Microprocessors 1.35 Assembly Language
Logical Shift Right
0 CF
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
The 80x86 Microprocessors 1.36 Assembly Language
Logical Shift Right (Cont.)
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
The 80x86 Microprocessors 1.37 Assembly Language
Logical Shift Lift
CF 0
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 80x86 Microprocessors 1.38 Assembly Language
COMPARE of unsigned numbers
CMP destination, source
The CMP instruction compares two operands and changes the flags
according to the result of the comparison.
The operands themselves remain unchanged.
The destination operand can be a register or in memory.
The source operand can be a register, in memory, or immediate.
Although all the CF, AF, SF, PF, ZF, and OF flags reflect the result of the
comparison, only the CF and ZF are used
Compare operands CF ZF
destination > source 0 0
destination = source 0 1
destination < source 1 0
The 80x86 Microprocessors 1.39 Assembly Language
Example
The 80x86 Microprocessors 1.40 Assembly Language
Unsigned J instructions
JA ; Jump Above Jump if CF=0 and ZF=0
JAE ; Jump Above or Equal Jump if CF=0
JB ; Jump Below Jump if CF=1
JBE ; Jump Below or Equal Jump if CF=1 and ZF=1
JE ; Jump Equal Jump if ZF=1
JNE ; Jump Not Equal Jump if ZF=0
The 80x86 Microprocessors 1.41 Assembly Language
Example
Assume that there is a class of five people
with the following grades: 69, 87, 96, 45,
and 75. Find the highest grade.
The 80x86 Microprocessors 1.42 Assembly Language
The 80x86 Microprocessors 1.43 Assembly Language
Example
Use the CMP instruction to determine if
an ASCII character is uppercase or
lowercase. Note that small and capital
letters in ASCII have the following values:
The 80x86 Microprocessors 1.44 Assembly Language
Example
There is a relationship between the
pattern of lowercase and uppercase
letters, as shown below for A and a:
A 0100 0001 41H
a 0110 0001 61H
The 80x86 Microprocessors 1.45 Assembly Language
The 80x86 Microprocessors 1.46 Assembly Language
ROTATE INSTRUCTIONS
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.)
Flags ➔ Affected: OF, CF
The 80x86 Microprocessors 1.47 Assembly Language
ROR rotate right
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
The 80x86 Microprocessors 1.48 Assembly Language
ROL rotate left
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.
MOV BH,72H ;BH = 0111 0010 CF
ROL BH,1 ;BH = 11100100 CF=0
ROL BH,1 ;BH = 11001001 CF=1
ROL BH,1 ;BH = 1001 0011 CF=1
ROL BH,1 ;BH = 00100111 CF=1
MOV BH,72H ;BH=0111 0010
MOV CL,4 ;CL=4 number of times to rotate
ROL BH,CL ;BH=00100111 CF=1
The 80x86 Microprocessors 1.49 Assembly Language
Example
The 80x86 Microprocessors 1.50 Assembly Language
RCL
RCR
STC CLC
The 80x86 Microprocessors 1.51 Assembly Language
Example
The 80x86 Microprocessors 1.52 Assembly Language
Example
The 80x86 Microprocessors 1.53 Assembly Language
HW
1, 2, 4, 5, 6, 7, 9, 18, 19, 20, 21, 26
The 80x86 Microprocessors 1.54 Assembly Language