Chapter 5

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 74

Microprocessors 1

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

Multiplication and Division

Basic Logic Instructions


Shift and Rotate
1- Addition
 Addition (ADD) appears in many forms in the
microprocessor. The first section details the use of
the ADD instruction for 8-, 16-, and 32-bit binary
addition.
 A second form of addition, called add-with-carry, is
introduced with the ADC instruction.
 The increment instruction (INC) is presented.
Increment is a special type of addition that adds a
one to a number.
 Exchange and Add (XADD): Anew type of
addition called exchange and add, appers in the
80486 istruction set and contains through the
core2
Addition
 (A) Examples of the ADD instructions.
Assembly Language Operation
ADD AL, BL AL = AL + BL
ADD CX, DI CX = CX + Dl
ADD EBP, EAX EBP = EBP + EAX
ADD CL, 44H CL = CL + 44H
ADD EDX, 12345H EDX = EDX + 00012345H
AL adds to the contents of the
data segment memory location
ADD [BX], AL addressed by BX with the sum
stored in the same memory
location
(B)Addition-With-Carry
An addition-with-carry instruction (ADC)
adds the bit in the carry flag (C) to the
operand data.
Assembly Language Operation
ADC AL, AH AL = AL + AH + carry
ADC CX. BX CX = CX + BX + carry
ADC EBX. EDX EBX = EBX + EDX + carry
ADC DH, [BX] The byte contents of the data segment memory
location addressed by BX add to DH with carry
with the sum stored in DH
ADC BX, [BP + 2] The word contents of the stack segment memory
location addressed by BP plus 2 add to BX with
carry with the sum stored in BX
ADC ECX, [EBX] The doubleword contents of the data segment
memory location addressed by EBX add to ECX
with carry with the sum stored in ECX
( C ) Increment Addition
Increment addition (INC) adds 1 to
a register or a memory location. The
INC instruction can add 1 to any
register or memory location,
With indirect memory increments,
the size of the data must be
described by using the BYTE PTR,
WORD PTR, or DWORD PTR
directives.
( c ) Increment Addition
Assembly Language Operation
INC BL BL = BL + 1
INC SP SP = SP + 1
INC EAX EAX = EAX + 1
INC BYTE PTR [BX] Adds 1 to the byte contents of the data segment
memory location addressed by BX
INC WORD PTR [SI] Adds 1 to the word contents of the data segment
memory location addressed by SI
INC DWORD PTR [ECX] Adds 1 to the doubleword contents of the data
segment memory location addressed by ECX
INC DATA1 Increments the contents of data segment memory
location DATA1
( D ) Exchange and Add for the
80486-core2 processors
2- Subtraction
Many forms of subtraction (SUB)
appear in the instruction set.
These forms use any addressing
mode with 8-, 16-, or 32-bit data.
A special form of subtraction
(decrement, or DEC) subtracts a 1
from any register or memory
location.
2- Subtraction
 (A) Examples of the SUB instructions.
Assembly Language Operation
SUB CL, BL CL = CL - BL
SUB AX, SP AX = AX - SP
SUB ECX, EBP ECX = ECX - EBP
SUB DH, 6FH DH = DH - 6FH
SUB AX, CCCCH AX = AX - CCCCH
SUB ESI, 2000300H ESI = ESI - 02000300H
SUB [DI], CH Subtracts the contents of CH from
the contents of the data segment
memory location addressed by Dl
SUB CH, [BP] Subtracts the byte contents of the
stack segment memory location
addressed by BP from CH
(B) Decrement Subtraction:
• Decrement subtraction (DEC) subtracts a
1 from a register or the contents of a
memory location.
Assembly Language Operation
DEC BH BH = BH - 1
DEC CX CX = CX - 1
DEC EDX EDX = EDX - 1
DEC BYTE PTR [Dl] Subtracts 1 from the byte contents of
the data segment memory location
DEC WORD PTR[BP] addressed 1byfrom
Subtracts Dl the word contents of
the stack segment memory location
DEC DWORD PTR[EBX] Subtracts
addressed 1byfrom
BP the doubleword
contents of the data segment memory
DEC NUMB location addressed
Subtracts bycontents
1 from the EBX of the
data segment memory location NUMB
( C ) Subtract-with-Borrow
A subtraction-with-borrow (SBB) instruction
functions as a regular subtraction, except that
the carry flag (C), which holds the borrow, also
subtracts from the difference.
Assembly Language Operation
SBB AH, AL AH = AH - AL - carry
SBB AX, BX AX = AX - BX - carry
SBB EAX, ECX EAX = EAX- ECX -carry
SBB CL, 2H CL = CL – 2H - carry
SBB BYTE PTR[DI], 3H Both a 3H and carry subtract from
the contents of the data segment
memory location addressed by Dl
( C ) Subtract-with-Borrow

Assembly Language Operation


SBB [DI], AL Both AL and carry subtract from
the data segment memory
location addressed by Dl
SBB DI, [BP + 2] Both carry and the word contents
of the stack segment memory
location addressed by the sum of
BP and 2 subtract from Dl
SBB AL,[EBX + ECX] Both carry and the byte contents
of the data segment memory
location addressed by the sum of
EBX and ECX subtract from AL
Comparison
The comparison instruction (CMP) is a
subtraction that changes only the flag
bits; the destination never changes.

 A comparison is useful for checking


the entire contents of a register or a
memory location against another value.
Comparison
• Examples of the CMP instructions.

Assembly Language Operation


CMP CL, BL CL - BL
CMP AX, SP AX - SP
CMP EBP, ESI EBP - ESI
CMP AX, 2000H AX - 2000H
CMP [DI], 0CH 0CH subtracts from the
contents of the data
segment memory location
addressed by Dl
Comparison
• Examples of the CMP instructions.
Assembly Language Operation
CMP CL, [BP] The byte contents of the
stack segment memory
location addressed by BP
subtract from CL
CMP AH, TEMP The byte contents of the
data segment memory
location TEMP subtract
from AH
Negate
 Negate byte or word

Neg it means the two's complement

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.

 Earlier 8-bit microprocessors could not


multiply or divide without the use of a
program that multiplied or divided by
using a series of shifts and additions or
subtractions.
MUL Instruction
• The MUL (unsigned multiply) instruction multiplies
an 8, 16, or 32 bit operand by either AL, AX, or EAX.

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.

The multiplication instruction contains one


operand because it always multiplies the
operand times the contents of register AL.
8-bit Multiplication
Examples of 8-bit multiplication instructions.
Assembly Language Operation
MUL CL AL is multiplied by CL; the unsigned
product is in AX

IMUL DH AL is multiplied by DH; the signed


product is in AX

IMUL BYTE PTR[BX] AL is multiplied by the byte contents of


the data segment memory addressed
by BX; the signed product is in AX
16-bit Multiplication
Word multiplication is very similar to byte
multiplication. The difference is that AX
contains the multiplicand instead of AL, and
the product appears in DX-AX instead of AX.

The DX register always contains the most-


significant 16 bits of the product, and AX
contains the least-significant 16 bits.
16-bit Multiplication
• Examples of 16-bit multiplication instructions.

Assembly Language Operation


MUL CX  AX is multiplied by CX; the
unsigned product is in DX-AX
 AX is multiplied by Dl; the signed
IMUL DI
product is in DX(most)-AX(least)
MUL WORD PTR[SI]  AX is multiplied by the word
contents of the data segment
addressed by SI; the unsigned
product is in DX(most)-AX(least)
32-bit Multiplication
As with 8- and 16-bit multiplication, 32-bit
multiplication can be signed or unsigned by
using the IMUL and MUL instructions.
With 32-bit multiplication, the contents of
EAX are multiplied by the operand specified
with the instruction.
The product (64 bits wide) is found in EDX-
EAX, where EAX contains the least-
significant 32 bits of the product, and EDX
contains the most-significant 32 bits of the
product
32-bit Multiplication:
Examples of 32-bit multiplication instructions.

Assembly Language Operation


MUL ECX  EAX is multiplied by ECX; the
unsigned product is in EDX-EAX
IMUL EDI  EAX is multiplied by EDI; the
signed product is in EDX-EAX
MUL DWORD PTR[ECX]  EAX is multiplied by the double
word contents of the data
segment addressed by ECX; the
product is in EDX-EAX
DIV Instruction
 The DIV (unsigned divide) instruction performs 8-bit,
16-bit, and 32-bit division

 A single operand is supplied (register or memory


operand), which is assumed to be the divisor
8-bit Division
An 8-bit division uses the AX register to
store the dividend that is divided by the
contents of any 8-bit register or memory
location.
The quotient moves into AL after the division
with AH containing a whole number
remainder.
For a signed division, the quotient is positive
or negative; the remainder always assumes
the sign of the dividend and is always an
integer.
8-bit Division
Examples of 8-bit Division instructions.

Assembly
Operation
Language

DIV CL AX is divided by CL; the unsigned quotient


is in AL and the remainder is in AH

IDIV BL AX is divided by BL; the signed quotient is


in AL and the remainder is in AH

DIV BYTEPTR[BP] AX is divided by the byte contents of the


stack segment memory location addressed
by BP; the unsigned quotient in AL and the
remainder in AH
16-bit Division
• 16-bit division is similar to 8-bit division, except that instead of dividing into
AX, the 16-bit number is divided into DX-AX.
• The quotient appears in AX and the remainder appears in DX after a 16-bit
division.

Assembly Language Operation

DIV CX DX-AX is divided by CX; the unsigned quotient is


in AX and the remainder is in DX
IDIV SI DX-AX is divided by SI; the signed quotient is in
AX and the remainder is in DX
Basic Logic Instructions

 The basic logic instructions include AND,


OR, Exclusive OR(XOR), and NOT.

 The NEG instruction is similar to the


NOT+INC instruction.
AND
• The AND operation performs logical
multiplication. The AND operation clears bits of
a binary number.
• The task of clearing a bit in a binary number is
called masking.

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

ROL SI, 14 SI rotates left 14 places

RCL BL, 6 BL rotates left through carry 6 places

ROL ECX, 18 ECX rotates left 18 places

RCR AH, CL AH rotates right through carry the


number of places specified by CL

ROR WORD PTR[BP], 2 The word contents of the stack


segment memory location
addressed by BP rotate right 2
places
Questions and
Answers
8086 Flag Registers Format.
Select an ADD instruction that will:
(a) add BX to AX
(b) add l2H to AL
(c) add EDI and EBP
(d) add 22H to CX
(e) add the data addressed by SI to AL
(f) add CX to the data stored at memory
location FROG

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.

MOV DL, 05H


MOV AL, DL
MUL DL
MUL DL
Write a sequence of instructions that adds
the contents of array elements 3, 5, and 7.

MOV AL, 00 H ;clear sum


MOV SI, 03 H ;address element 3
ADD AL, ARRAY[SI] ;add element 3
ADD AL, ARRAY [SI+2] ;add element 5
ADD AL, ARRAY [SI+4] ;add element 7
Write a sequence of instructions that adds two
32-bit numbers. The first number stored in BX
and AX registers and the second number
stored in DX and CX registers. Store the result
in BX and AX registers. The most significant
16-bit are in BX and DX.
 A sequence of instructions that sums BX-AX and DX-CX
The result is returned in BX-AX.

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.

ADD EAX, ECX


ADC EBX, EDX
Write a short sequence of instructions that
divides the number in BL by the number in
CL, and then multiplies the result by 2. The
final answer must be a 16-bit number stored
in the DX register.

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.

AND DH, 1FH


MOV BH, DH

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

SI xxxx xxxx xxxx xxxx B


MUSK +0000 0000 0001 1111
Bxxxx xxxx xxx1 1111B
Develop a sequence of instructions that sets (1)
in the rightleast four bits of AX; clears (0) of
the leftmost three bits of AX; and inverts bits
7,8 and 9 of AX.
OR AX, 000FH AX xxxx xxxx xxxx xxxx B
MUSK +0000 0000 0000 1111
AND AX, 1FFFH AX Bxxxx xxxx xxxx 1111B

XOR AX, 0380H AX xxxx xxxx xxxx 1111B


MUSK ×0001 1111 1111
AX 1111B
000x xxxx xxxx 1111 B
MUSK 0000 0011 1000 0000B
Select the correct instruction to perform each of
the following tasks:
1) Shift DI right three places, with zeros moved into the
leftmost bit
2) Move all bits in AL left one place, making sure that a 0
move into the rightmost bit position
3) Rotate all the bits of AL left three places
4) Rotate carry right one place through EDX
5) Move the DH register right one place, making sure that
the sign of the result is the same as the sign of the
original number

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.

MOV AX, 166H


MOV BL, 25H
DIV BL
MOV [0200], AL
MOV [0202], AH
Show the detailed result for the following instructions,
indicate the group of each instruction
(1) ADD AX, 0076H
(2) INC AX AX BX CX DX
(3) NEG DX
(4) DEC CX 8000 H 0080 H 0080 H 0008 H
(5) SUB DX, CX CS DS SS ES
(6) ADC AX, BX
0000 H 4000 H 8000 H B000 H
(7) MUL BX
(8) DIV CX BP IP SI DI
(9) CWD(Convert Word to 1000 H 1000 H 0010 H 0001 H
Double)
(10) AND AX, BX SP CF ZF SF
(11) OR BX, AX 1008 H 0 0 0
(12) XOR CX, BX
(13) MOV CL, 03 H
(14) SHL BX, CL (18) MOV CX, 0004H
(15) ROL BX, CL (19) MOVS B
(16) SHR BX, CL (20) MOV AX, BX
Solution

(1) ADD AX, 0076H


AX 8000 H
+0076 H
8076 H

(2) INC AX
AX 8076 H
+0001 H
8077 H

Results
Solution
(3) NEG DX (2,S complement )

DX 0000 0000 0000 1000


1,S 1111 1111 1111 0111
+ 1
1111 1111 1111 1000
(4) DEC CX
F F F 8

(5) SUB DX, CX CX


DX=DX-CX 0080
- 0001
007F

DX FFF8
CX - 007F
FF79
Results
Solution
(6) ADC AX, BX
AX 8077
BX +0080
+ 0
Carry C 80F7

(7) MUL BX AX 80F7


BX ×0080
0000
407B8
0000
0000
0407B80
DX= 0040 AX= 7B80
Results
Solution
(8) DIV CX
0081FA (Quotient) in AX
007F 0407B80
3F8
FB
7F
7C8
771
570
(9) CWD 4F6
(10)AND AX, BX 7A (Reminder) in DX
Then AX= 81FA Then DX= 0000

AX 1000 0001 1111 1010


BX 0000 0000 1000 0000
0000 0000 1000 0000
Then AX= 0000 0000 1000 0000
0 0 8 0
Results
Solution
(11)OR BX, AX
BX 0000 0000 1000 0000
AX 0000 0000 1000 0000
0000 0000 1000 0000
Then BX= 0000 0000 1000 0000
0 0 8 0
(12) XOR CX, BX
CX 0000 0000 0111 1111
BX 0000 0000 1000 0000
0000 0000 1111 1111

Then CX= 0000 0000 1111 1111


0 0 F F
Results
Solution
(13)MOV CL, 03

(14) SHL BX, CL Then CX= 0003

(15) ROL BX, CL


BX 0000 0000 1000 0000

(16) SHR BX, CL


Then BX= 0000 0100 0000 0000
0 4 0 0

BX 0000 0100 0000 0000


Then BX= 0010 0000 0000 0000
2 0 0 0

BX 0010 0000 0000 0000

Then BX= 0000 0100 0000 0000 Results


0 4 0 0
Solution
(17)ROR BX, CL

BX 0000 0100 0000 0000


(18) MOV CX, 0004H
Then BX= 0000 0000 1000 0000
0 0 8 0
(19) MOVS B

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

0000 H 4000 H 8000 H B000 H 66


BP IP SI DI
1000 H 0010 H 0001 H 65

19 0014 H 19 0005 H
64
SP CF ZF SF
63
1008 H 0 0 0
The
End

You might also like