CSE331 Lecture-08
CSE331 Lecture-08
Instruction Set
There are over a hundred instructions in the instruction set
for the 8086 CPU; there are also instructions designed
especially for the more advanced processors.
8086 supports 8 types of instructions:
Data Transfer Instructions
Arithmetic Instructions
Logical Instructions
String manipulation Instructions
Program Execution Transfer Instructions (Branch &
Loop Instructions)
Process Control Instructions
Iteration Control Instructions
Interrupt Instructions
2
DAA
The DAA (decimal adjust after addition) instruction converts the binary
result of an ADD or ADC operation to packed decimal format.
The value to be adjusted must be in AL.
If lower nibble of AL > 9 or AF = 1 then : AL=AL+6 & AF = 1
else if upper nibble of AL > 9 or CF = 1 then : AL = AL + 60h & CF = 1
Example-2:
Let AL = 88 BCD, and BL = 49 BCD
ADD AL, BL AL = D1H; AF = 1, add 06H to AL
DAA AL = D7H; upper nibble > 9, add 60H to AL
AL = 37 BCD, CF = 1
AH=AH+1
3
Subtraction Instructions
Following figure shows set of instruction provided for implementing
subtraction:
8000h
- 0001h
7FFFh = 0111 1111 1111 1111b
SF = 0 because the MSB is 0.
PF = 1 because there are 8 (even number) one bits in the low byte.
ZF = 0 because the result is nonzero.
CF = 0 because a smaller unsigned number is being subtracted from a
larger one.
OF = 1 because a positive number is being subtracted from a negative
one (like adding two negatives), and the result is positive.
5
SBB Instruction
The subtract with borrow (SBB) instruction is similar to SUB,
however it also subtracts the contents of the carry flag.
(D)←(D)-(S)-(CF)
6
NEG Instruction
Negate. Makes operand negative (two's complement).
Invert all bits of the operand
Add 1 to inverted operand
• MOV AX, -5
AX= -5= FFFBh, none of the flags are affected
• NEG AX, AX=8000h
8000h = 1000 0000 0000 0000
2’s complement = 1000 0000 0000 0000
= 8000h
CF=1, OF=1, PF=1, ZF=0, SF=1
10
AAS
Case-III: Subtract ‘4’ from ‘15’.
Suppose AX contains 3135H, and BL contains 34H, what will be the
out put for the following instructions:
Code AH AL Comments
MOV AX,3135H 00110001 00110101 AX=3135H
MOV BX,34H 00000000 00110100 BL=0034H
SUB AL, BL 00110001 00000001 AX=3101H
LN<0
AH AL AAS 00110001 00000001 AX=3101H
31H 01H OR AX,3030 00110001 00110001 AX=3131H
11
DAS
Decimal adjust After Subtraction. Corrects the result of subtraction of
two packed BCD values.
Algorithm:
If low nibble of AL > 9 or AF = 1 then: AL = AL - 6 & AF = 1
Else If upper nibble of AL > 9h or CF = 1 then: AL = AL - 60h & CF = 1
Case-I: Subtract AL = 86 BCD, from BL = 57 BCD
12
DAS
Case-II: Subtract AL = 49 BCD, from BL = 72 BCD
13
Multiplication & Division Instructions
Following figure shows set of instruction provided for implementing
multiplication and division:
Mnemonic Meaning Format Operation Flags affected
MUL Multiply MUL S (AL) • (S8) → (AX) OF, CF
(unsigned) (AX) • (S16) → (DX),(AX) SF, ZF, AF, PF undefined
DIV Division DIV S (1) Q((AX)/(S8)) → (AL) R((AX)/(S8)) → OF, SF, ZF, AF, PF, CF
(unsigned) (AH)
(2) Q((DX,AX)/(S16)) → (AX) Undefined
R((DX,AX)/(S16)) → (DX) If Q is FF16
in case (1) or FFFF16 in case (2), then
type 0 interrupt occurs
IMUL Multiply (AL) • (S8) → (AX) OF, CF
(signed) (AX) • (S16) → (DX),(AX) SF, ZF, AF, PF undefined
IDIV Division (1) Q((AX)/(S8)) → (AL) OF, CF
(signed) R((AX)/(S8)) → (AH) SF, ZF, AF, PF undefined
(2) Q((DX,AX)/(S16)) → (AX) OF, SF, ZF, AF, PF, CF
R((DX,AX)/(S16)) → (DX) undefined
f Q is positive and exceeds 7FFF16 or if Q is
negative and becomes less than 800116, then
type 0 interrupt occurs
Multiplication & Division Instructions
Mnemonic Meaning Format Operation Flags affected
Allowed operands.
Integer Multiplication
• Contrary to addition, the multiplication operation depends on the
interpretation:
no interpretation: FFh x 2h = ??
unsigned interp.: 255 x 2 = 510
signed interpret.: -1 x 2 = -2
16
MUL Instruction
The instruction performs unsigned multiplication.
If source is of 8 bits, it will multiply with AL and result will
be stored in AX register.
If source is of 16 bits, it will multiply with AX and result
will be stored in DX-AX registers.
Algorithm:
when operand is a byte: AX = AL * operand.
when operand is a word: (DX ,AX) = AX * operand
Syntax
MUL Register
MUL memory
17
IMUL Instruction
The instruction performs signed multiplication.
If source is of 8 bits, it will multiply with AL and result will
be stored in AX register.
If source is of 16 bits, it will multiply with AX and result
will be stored in DX-AX registers.
Algorithm:
when operand is a byte: AX = AL * operand.
when operand is a word: (DX ,AX) = AX * operand
Syntax
IMUL Register
IMUL memory
18
MUL & IMUL Instruction
Example:
MOV AL, 200 ; AL = 0C8h
MOV BL, 4
MUL BL ; AX = 0320h (800)
Example:
MOV AL, -2
MOV BL, -4
IMUL BL ; AX = 8
19
MUL & IMUL Instruction
The 2’s-complement signed data contents of AL equal 1 and the contents of CL
are 2. What result is produced in AX by executing the following instructions?
MUL CL
and
IMUL CL
Solution
As binary data, the contents of AL and CL are:
(AL)=-1 (as 2’s complement)= 111111112 =FF16
(CL)=-2 (as 2’s complement)= 111111102 = FE16
Executing the MUL instruction gives
(AX)=111111112 =111111102 =11111101000000102 =FD0216
The second instruction multiplies the two numbers as signed numbers to generate
the signed result. That is,
(AX)=-116 ×-216
=216 =0002H
20
Examples of MUL and IMUL
• Say that AX = 1h and BX = FFFFh, then:
23
Integer Division
• Notation for integer division:
– Ex: 7 ¸ 2 = (3, 1) not_equal to 7 / 2 = 3.5
– dividend ¸ divisor = (quotient, remainder)
24
DIV Instruction
The instruction performs unsigned division.
It divides a word by byte and a double word by word.
If divisor is of 8 bits, it will divides with AX and result of quotient will
be there in AL register and reminder will be stored in AH register.
If divisor is of 16 bits, it will divides with DX-AX double word and
result of quotient will be there in AX register and reminder will be
stored in DX register.
Algorithm:
When operand is a byte:
Syntax
AL = AX / operand
DIV Register
AH = remainder (modulus)
When operand is a word: DIV memory
AX = (DX AX) / operand
DX = remainder (modulus)
25
IDIV Instruction
The instruction performs signed division.
It divides a word by byte and a double word by word.
If divisor is of 8 bits, it will divides with AX and result of quotient will
be there in AL register and reminder will be stored in AH register.
If divisor is of 16 bits, it will divides with DX-AX double word and
result of quotient will be there in AX register and reminder will be
stored in DX register.
Algorithm:
When operand is a byte:
Syntax
AL = AX / operand
IDIV Register
AH = remainder (modulus)
When operand is a word: IDIV memory
AX = (DX AX) / operand
DX = remainder (modulus)
26
DIV and IDIV Instruction
Example:
MOV AX, 203 ; AX = 00CBh
MOV BL, 4
DIV BL ; AL = 50 (32h), AH = 3
RET
Example:
MOV AX, -203 ; AX = 0FF35h
MOV BL, 4
IDIV BL ; AL = -50 (CEh), AH = -3 (FDh)
RET
27
Examples of DIV and IDIV
• DX = 0000h, AX = 0005h, BX = FFFEh:
28
Examples of DIV and IDIV (cont.)
• AX = 0007, BX = FFFEh:
– Instruction Quot. Rem. AL AH
DIV BL 0 7 00 07
IDIV BL -3 1 FD 01
• AX = 00FBh, BX = 0CFFh:
– Instruction Quot. Rem. AL AH
DIV BL 0 251 00 FB
IDIV BL DIVIDE OVERFLOW
29
Exercise 2
• Give the hexadecimal content of AX immediately
after the execution of each instruction below or
indicate if there is a divide overflow
30
CBW Instruction
Convert sign byte (AL) into sign word (AX).
The CBW instruction copies the sign bit (D7) of AL register into AH
register. If high bit of AL = 1 then: AH = 255 (0FFh) Else : AH = 0
Example:
MOV AL, 60H ; AL = 01100000=+96
CBW ; AX=00000000 01100000=+96
; AH=00000000 AL=01100000
Example:
MOV AL, 80H ; AL = 10000000=-128
CBW ; AX=11111111 1000000=FF80H-128
; AH=11111111 AL=10000000
31
CWD Instruction
Convert sign word (AX) into sign double word (DX-AX).
The CWD instruction copies the sign bit (D15) of AX register into DX
register. If High bit of AX=1 then : DX = 65535 (0FFFFh) else : DX = 0
Example:
MOV AX, 0104H ; AX = 0000 0001 0000 0100=+260
CWD ; AX=0000 0001 0000 0100=0104h
; DX=0000 0000 0000 0000=0000h
Example:
MOV AX, 8002H ; AX = 1000 0000 0000 0010=-32766
CWD ; AX=1000 0000 0000 0010=8002h
; DX=1111 1111 1111 1111=FFFFh
32
Example
What is the result of executing the following sequence of instructions?
MOV AL, 0A1H
CBW
CWD
Example:
The first instruction loads AL with A1H. This gives
(AL)=A116=101000012
Executing the second instruction extends the most significant bit of AL, 1, into
all bits of AH. The result is
(AH)=111111112=FF16 Or (AX)=11111111101000012=FFA116
This completes conversion of the byte in AL to a word in AX.
The last instruction loads each bit of DX with the most significant bit of AX.
This bit is also 1. Therefore, we get
(DX)= 11111111111111112= FFFF16
Now the word in AX has been extended to a double word.
(AX) =FFA116
(DX)=FFFF16
33
AND, OR, XOR, and NOT Instructions
34
NOT Instructions
37
TEST Instructions
This instruction performs logic AND operation between
source and destination.
Result is not stored anywhere. It only updates flag
register without changing destination.
Destination can be register or memory location.
Source can be register or memory location or data.
For example, if you are required to check the status of 7th
bit in a 16-bit register, you can write
TEST AX, 0080H;
ZF will be set if 7th bit in AX is zero, ZF will be zero if
7th bit in AX is 1.
Only ZF is updated, keeping AX unchanged.
38
OR Instructions
This instruction performs logic OR operation between
source and destination.
Destination can be register or memory location.
Source can be register or memory location or data.
OR AL, BL
If AL= 47H=0100 0111 and BL=36H=0011 0110
After execution of instruction,
AL=0111 0111=77H
PF, SF, and ZF affected, CF=OF=0 and AF is
undefined
39
XOR Instructions
This instruction performs logic XOR operation
between source and destination.
Destination can be register or memory location.
Source can be register or memory location or data.
XOR AL, BL
If AL= 47H=0100 0111 and BL=36H=0011 0110
After execution of instruction,
AL=0111 0001=71H
PF, SF, and ZF affected, CF=OF=0 and AF is
undefined
40
MASK Operations
To modify only selective bits in destination, we
construct a source bit pattern known as mask.
To choose mask use the following properties
B and 1 B (unchanged)
B and 0 0 (clear)
B OR 1 1 (set)
B OR 0 B (Unchanged)
B XOR 0 B (unchanged)
B XOR 1 B’ (Complement)
41
MASK Operations-Clear
The AND instruction
It may be used to clear specific destination bits while
preventing others
A 0 mask bit clear the corresponding destination bit.
A 1 mask bit preserves the corresponding destination
bit.
For example: Clear the sign bit of AL while leaving
the others bits unchanged
Solution: AND AL, 7FH
Where, 7FH(0111 1111) is the mask.
42
MASK Operations-Set
The OR instruction
It may be used to set specific destination bits while
preventing others
A 1 mask bit sets the corresponding destination bit.
A 0 mask bit preserves the corresponding destination
bit.
For example: Set the MSB and LSB of AL while
preserving the other bits.
Solution: OR AL, 81H
Where, 81H(1000 0001) is the mask.
43
MASK Operations-Complement
The XOR instruction
It may be used to complement specific destination
bits while preventing others
A 1 mask bit complements the corresponding
destination bit.
A 0 mask bit preserves the corresponding destination
bit.
For example: Change the sign bit of DX.
Solution: XOR DX, 8000H
Where, 8000H (1000 0000 0000 0000) is the
mask.
44