Chapter 5
Chapter 5
Chapter 5
Chapter 5
Arithmetic And Logic
Instructions
prepared by
Dr: Mohamed EL-Bouridy Dr : Reda EL-Sheshtawy
[email protected] [email protected]
Chapter Overview
Addition, Subtraction, and Comparison
One’s complement
Each 0 1
Each 1 0
AX = 1011000011110101
One’s( AX)= 0100111100001010
two's complement = One’s + 1
Multiplication and Division
Only modern microprocessors contain
multiplication and division instructions.
Most
Most Least
Least
Most
Most Least
Least
8-bit Multiplication
With 8-bit multiplication, the multiplicand is
always in the AL register, whether signed or
unsigned. The multiplier can be any 8-bit
register or any memory location.
Assembly
Operation
Language
T= A . B
AND
Assembly Language Operation
AND AL, BL AL = AL AND BL
AND CX , DX CX = CX AND DX
AND ECX, EDI ECX = ECX AND EDI
AND CL, 33H CL = CL AND 33H
AND DI, 4FFFH Dl = Dl AND 4FFFH
AND ESI, 34H ESI = ESI AND 00000034H
AND AX, [DI] AX is Anded with the word contents of the data
segment memory location addressed by Dl
The byte contents of the data segment memory
AND ARRAY [SI] , AL location addressed by the sum of ARRAY plus SI is
Anded with AL; the result moves to memory
CL is Anded with the byte contents of the data
AND [EAX] , CL segment memory location addressed by EAX; the
result moves to memory.
OR
• The OR operation performs logical addition and is often called the Inclusive-OR
function.
T= A + B
OR
Assembly Language Operation
OR AH, BL AH = AH OR BL
OR SI, DX SI = SI OR DX
OR EAX, EBX EAX = EAX OR EBX
OR DH, A3H DH= DH OR A3H
OR SP, 990DH SP = SP OR 990DH
OR EBP, 10 EBP = EBP OR 0000000AH
DX is Ored with the word contents
OR DX, [BX] of the data segment memory
location addressed by BX
The byte contents of the data
segment memory location
OR DATES[DI+2],AL
addresses by the sum of DATES,
DI, and 2 are Ored with AL
XOR
• The Exclusive-OR instruction (XOR) differs
from Inclusive-OR (OR).
T= AB+ AB
XOR
Assembly Language Operation
XOR CH, DL CH = CH XOR DL
XOR SI, BX SI = SI XOR BX
XOR EBX, EDI EBX = EBX XOR EDI
XOR AH, EEH AH = AH XOR EEH
XOR DI, DDH Dl = Dl XOR OODDH
XOR ESI,100 ESI = ESI XOR 00000064H
DX is Exclusive-Ored with the word
XOR DX, [SI] contents of the data segment memory
location addressed by SI
AL is Exclusive-Ored with the byte
contents of the data segment memory
XOR DATES[DI+2], AL
location addressed by the sum of DATES,
DI,and 2
NOT
• Logical inversion, or the one’s complement (NOT);
and arithmetic sign inversion,
Assembly Language Operation
NOT CH CH is one's complemented
NOT EBX EBX is one's complemented
The contents of the data
NOT TEMP segment memory location TEMP
is one’s complemented
The byte contents of the data
segment memory location
NOT BYTE PTR[BX]
addressed by BX is one’s
complemented
Shift
• Shift instructions position or move numbers to the left or right
within a register or memory location.
• They also perform simple arithmetic such as multiplication by
powers of 2+n (left shift) and division by powers of 2-n (right
shift).
• Example of Shift instructions.
Assembly
Language
Operation
SHL AX, 1 AX is logically shifted left 1 place
SHR BX,12 BX is logically shifted right 12 places
SHR ECX,10 ECX is logically shifted right 10 places
SHL RAX,50 RAX is logically shifted left 50 places(64-bit mode)
The contents of the data segments memory
location DATA1 are arithmetically shifted left
SAL DATA1, CL
the number of places specified by CL (64-bit
mode)
SAR Sl, 2 SI is arithmetically shifted right 2 places
SAR EDX, 14 EDX is arithmetically shifted right 14 places
Rotate
•Examples of Rotate instructions.
Assembly Language Operation
a) ADD AX, BX
b) ADD AL, 12H
c) ADD EDI,EBP
d) ADD CX, 22H
e) ADD AL, [SI]
f) ADD FROG, CX
If AX = 1001H and DX = 20FFH, list the sum
and the contents of each flag register bit
(C, S, Z, and O) after the ADD AX, DX
instruction executes.
AX 1001H 0001 0000 0000 0001 B
+ DX +2 0 F F H 0010 0000 1111 1111 B
3100H 0011 0001 0000 0000 B
C = 0, S = 0, Z = 0, O=0
Develop a short sequence of instructions
that adds AL, BL, CL, DL, and AH. Save the
sum in the DH register.
ADD AL, BL
ADC AL, CL
ADC AL,DL
ADC AL, AH
MOV DH, AL
Develop a short sequence of instructions
that adds AX, BX, CX, DX, and SP. Save the
sum in the DI register.
ADD AX, BX
ADC AX, CX
ADC AX,DX
ADC AX, SP
MOV DI, AX
Select a subtract instruction that will:
a) subtract BX from CX
b) subtract EEH from DH
c) subtract DI from SI
d) subtract 3322H from EBP
e) subtract the data address by SI from CH
f) subtract the data stored 10 words after the
location addressed by SI from DX
a) SUB CX, BX
b) SUB DH, EEH
c) SUB SI, DI
d) SUB EBP, 00003322H
e) SUB CH, [SI]
f) SUB DX, [SI + 20]
Write a short sequence of instructions that
subtracts the numbers in DI, SI, and BP from
the AX register. Store the difference in
register BX.
SUB AX, DI
SUB AX, SI
SUB AX, BP
MOV BX, AX
Write a short sequence of instructions that
adds BX, CX, and DX. Save the sum in the
AX register.
ADD BX, CX
ADD BX, DX
MOV AX, BX
Write a short sequence of instructions that
adds data in memory locations NUMB and
NUMB+1. Save the sum in the AX register.
MOV DI, OFFSET NUMB
MOV AL, 00H
MOV AH, 00H
ADD AL, [DI]
ADD AL, [DI+1]
OR
MOV DI, OFFEST NUMB
MOV AX, 0000H
ADD AL, [DI]
INC DI
ADD AL, [DI]
Write a sequence of instructions that cube
the 8-bit number found in DL. Load DL with a
5 H initially, and make sure that your result
is a 16-bit number.
ADD AX, CX
ADC BX, DX
Write a sequence of instructions that adds
two 64-bit numbers. The first number stored
in EBX and EAX registers and the second
number stored in EDX and ECX registers.
Store the result in EBX and EAX registers.
The most significant 32-bit are in EBX and
EDX.
A sequence of instructions that sums EBX-EAX and EDX-ECX
The result is returned in EBX-EAX.
MOV AL, BL
MOV AH, 00H
DIV CL
MOV BH, 02
MUL BH
MOV DX, AX
Select an AND instruction that will:
1. AND BX with DX and save the result in BX.
2. AND 0EAH with DH.
3. AND DI with BP and save the result in DI.
4. AND 1122H with EAX.
5. AND the data addressed by BP with CX and save
the result in memory.
1. AND BX, DX
2. AND DH, EAH
3. AND DI, BP
4. AND EAX, 00001122H
5. AND [BP], CX
Develop a short sequence of instructions that
clears (0) the three leftmost bits of DH without
changing the remainder DH and stores the
result in BH.
DH xxxx xxxx B
MUSK ×0001 1111 B
000x xxxx B
Develop a short sequence of instructions that
sets (1) the rightleast five bits of DI without
changing the remaining bits of DI. Save the
results in SI.
MOV SI, DI
OR SI, 001FH
1) SHR DI, 3
2) SHL AL, 1
3) ROL AL, 3
4) RCR EDX, 1
5) SAR DH, 1
Write an assembly language program to divide
166H by 25H (unsigned 16-bit numbers). Store
the quotient at the beginning of memory
location 0200 and the reminder at the
beginning of memory location 0202.
(2) INC AX
AX 8076 H
+0001 H
8077 H
Results
Solution
(3) NEG DX (2,S complement )
DX FFF8
CX - 007F
FF79
Results
Solution
(6) ADC AX, BX
AX 8077
BX +0080
+ 0
Carry C 80F7
CX 0003
(20) MOV AX, BX
Then CX= 0004
SI = 0010 Then SI = 0014
DI = 0001 Then DI = 0005
CX= 0004 Then CX = 0000
AX = 0080
BX = 0080
Then AX= 0080 Results
No AX No BX No CX No DX
8000 H 0080 H 0080 H 0008 H
1 8076 H 11 0080 H 4 007F H 3 FFF8 H
2 8077 H 14 0400 H 12 00FF H 5 FF79 H
6 80F7 H 15 2000 H 13 0003 H 7 0040 H
7 7B80 H 16 0400 H 18 0004 H 8 007A H
8 81FA H 17 0080 H 19 0000 H 9 0000 H
9 81FA H 69
10 0080 H
68
20 0080 H
CS DS SS ES 67
19 0014 H 19 0005 H
64
SP CF ZF SF
63
1008 H 0 0 0
The
End