8086 Instruction Set
8086 Instruction Set
8086 Instruction Set
8086 Microprocessor
By
A.Sanyasi Rao
Assoc. Prof, Dept. of ECE
Software
The sequence of commands used to tell a microcomputer what to
do is called a program,
Each command in a program is called an instruction
8088 understands and performs operations for 117 basic
instructions
The native language of the IBM PC is the machine language of the
8088
A program written in machine language is referred to as machine
code
In 8088 assembly language, each of the operations is described by
alphanumeric symbols instead of 0-1s.
ADD AX, BX
(Opcode)
(Destination operand)
(Source operand )
2
Instructions
LABEL: INSTRUCTION
Address identifier
Ex.
; COMMENT
Does not generate any machine code
; copy BX into AX
Applications
One of the most beneficial uses of
assembly language programming is
real-time applications.
Real time means the task required by the
application must be completed before any other
input to the program that will alter its operation can
occur
For example the device service routine which
controls the operation of the floppy disk drive is a
good example that is usually written in assembly
language
5
Meaning
Format
Operation
Flags affected
MOV
Move
Mov D,S
(S) (D)
None
Destination
Source
Memory
Accumulator
Accumulator
Memory
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Seg reg
Reg 16
Seg reg
Mem 16
Reg 16
Seg reg
Memory
Seg reg
NO MOV
Memory
Immediate
Segment Register
EX:
Memory
Segment Register
Segment Register
MOV AL, BL
Meaning
Format
XCHG
Exchange
XCHG D,S
Destination
Operation
(S)
(D)
Flags affected
None
Source
Accumulator Reg 16
Memory
Register
Register
Register
Register
Memory
NO XCHG
MEMs
SEG REGs
10
Meaning
Format
LEA
Load
Effective
Address
LEA Reg16,EA
LDS
Load
Register
And DS
LDS Reg16,MEM32
Load
Register
and ES
LES Reg16,MEM32
LES
Operation
EA
(Reg16)
(MEM32) (Reg16)
Flags
affected
None
None
(Mem32+2) (DS)
(MEM32) (Reg16)
None
(Mem32+2) (DS)
Meaning
Format
XLAT
Translate
XLAT
Operation
((AL)+(BX)+(DS)0)
Flags
(AL)
None
Example:
Assume (DS) = 0300H, (BX)=0100H, and (AL)=0DH
XLAT replaces contents of AL by contents of memory location with
PA=(DS)0 +(BX) +(AL)
= 03000H + 0100H + 0DH = 0310DH
Thus
(0310DH) (AL)
12
Meaning
Format
Operation
Flags
affected
ADD
Addition
ADD D,S
(S)+(D) (D)
carry
(CF)
ALL
ADC
Add with
carry
ADC D,S
INC
Increment by
one
INC D
AAA
ASCII adjust
for addition
AAA
AF,CF
DAA
Decimal
adjust for
addition
DAA
ALL
(S)+(D)+(CF)
carry
(D)+1
(D)
(CF)
ALL
(D)
ALL but CY
13
Examples:
Ex.1
ADD AX,2
ADC AX,2
Ex.2 INC BX
INC WORD PTR [BX]
Ex.3 ASCII CODE 0-9 = 30-39h
MOV AX,38H
ADD AL,39H
AAA
ADD AX,3030H
Ex.4 AL contains 25 (packed BCD)
BL contains 56 (packed BCD)
ADD AL, BL
DAA
25
+ 56
-------7B 81
14
Meaning
Format
Operation
SUB
Subtract
SUB D,S
SBB
Subtract
with
borrow
SBB D,S
DEC
Decrement
by one
DEC D
NEG
Negate
NEG D
DAS
Decimal
adjust for
subtraction
DAS
All
AAS
ASCII
adjust for
subtraction
AAS
(AL) difference
(AH) dec by 1 if borrow
CY,AC
(D) - (S)
Borrow
(D) - (S) - (CF)
(D) - 1
Flags
affected
(D)
(CF)
(D)
(D)
All
All
All but CF
All
15
Examples: DAS
MOV BL, 28H
MOV AL, 83H
SUB AL,BL
DAS
; AL=5BH
; adjust as AL=55H
16
17
18
Multiplicand
Operand
(Multiplier)
Result
Byte*Byte
AL
Register or memory
AX
Word*Word
AX
Register or memory
DX :AX
Dword*Dword
EAX
Register or memory
EAX :EDX
Division
(DIV or IDIV)
Dividend
Operand
(Divisor)
Quotient: Remainder
Word/Byte
AX
Register or Memory
AL : AH
Dword/Word
DX:AX
Register or Memory
AX : DX
Qword/Dword
EDX: EAX
Register or Memory
EAX : EDX
19
Ex1:
Assume that each instruction starts from these values:
AL = 85H, BL = 35H, AH = 0H
1. MUL BL AL . BL = 85H * 35H = 1B89H AX = 1B89H
2. IMUL BL AL . BL = 2S AL * BL = 2S (85H) * 35H
= 7BH * 35H = 1977H 2s comp E689H AX.
AH
0085
H
3. DIV BL AX =
= 02 (85-02*35=1B)
1B
35 H
BL
AH AL
AX 0085 H
4. IDIV BL
=
=
1B 02
BL
35 H
AL
02
20
Ex2:
00 F 3H
00 F 3H
AX
3.IDIV BL
=
=
= 2 (00F3 2*6F=15H)
6
FH
2
'
S
(
91
H
)
BL
AH
AL
15
R
02
Q
POS
NEG 2s(02) = FEH
NEG
00 F 3H
AX
4. DIV BL
=
= 01(F3-1*91=62)
91
H
BL
AH
15
AL
FE
AH
62
AL
01
21
DX
8713
AX
B000
DX
AX
06FE
B000
F 000 H
3. DIV BL =
= B6DH More than FFH Divide Error.
15 H
22
Ex4:
AX 1250 H
1250 H
POS
POS
1250 H
1. IDIV BL
=
=
=
=
=
BL
NEG 2' sNEG 2' s(90 H )
70 H
90 H
= 29H (Q) (1250 29 * 70) = 60H (REM)
29H ( POS) 2S (29H) = D7H
R
60H
Q
D7H
1250 H
AX
2. DIV BL
=
= 20H1250-20*90 =50H
90
H
BL
R
50H
AH
Q
20H
AL
23
Logical Instructions
Mnemonic
Meaning
Format
Operation
Flags Affected
AND
Logical AND
AND D,S
OR
Logical Inclusive
OR
OR D,S
(S)+(D) (D)
XOR
Logical Exclusive
OR
XOR D,S
NOT
LOGICAL NOT
Destination
Source
Register
Register
Memory
Register
Memory
Accumulator
Register
Memory
Register
Immediate
Immediate
Immediate
NOT D
(S) +
(D)(D)
_
(D) (D)
Destination
Register
Memory
24
LOGICAL Instructions
AND
XOR
Used in Inverting bits
xxxx xxxx XOR 0000 1111 = xxxxxxxx
1111 1100B
1100 0000B
0010 0000B
26
27
CF
28
Shift Instructions
Mnemo
-nic
SAL/SH
L
SHR
SAR
Meaning
Format
Shift
SAL/SHL D, Count
arithmetic
Left/shift
Logical left
Shift
logical
right
Shift
arithmetic
right
SHR D, Count
SAR D, Count
Operation
Flags
Affected
CF,PF,SF,ZF
AF undefined
OF undefined
if count 1
CF,PF,SF,ZF
AF undefined
OF undefined
if count 1
CF,PF,SF,ZF
AF undefined
OF undefined
if count 1
29
Allowed operands
Destination
Count
Register
Register
CL
Memory
Memory
CL
30
31
SHL Instruction
The SHL (shift left) instruction performs a logical
left shift on the destination operand, filling the
lowest bit with 0.
0
CF
Operand types:
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
32
Fast Multiplication
Shifting left 1 bit multiplies a number by 2
mov dl,5
Before:
00000101
=5
shl dl,1
After:
00001010
= 10
; DL = 20
33
Ex.
; Multiply
AX by 10
SHL AX, 1
MOV BX, AX
MOV CL,2
SHL AX,CL
ADD AX, BX
34
SHR Instruction
The SHR (shift right) instruction performs a logical
right shift on the destination operand. The highest
bit position is filled with a zero.
0
CF
; DL = 40
; DL = 10
35
SAR Instruction
SAR (shift arithmetic right) performs a right
arithmetic shift on the destination operand.
CF
; DL = -40
; DL = -10
36
Rotate Instructions
Mnem
-onic
Meaning
Format
Operation
Flags Affected
ROL
Rotate
Left
ROL D,Count
ROR
Rotate
Right
CF
OF undefined
if count 1
RCL
Rotate
Left
through
Carry
RCL D,Count
CF
OF undefined
if count 1
RCR
Rotate
right
through
Carry
CF
OF undefined
if count 1 37
ROL Instruction
ROL (rotate) shifts each bit to the left
The highest bit is copied into both the Carry
flag and into the lowest bit
No bits are lost
CF
MOV Al,11110000b
ROL Al,1
; AL = 11100001b
MOV Dl,3Fh
ROL Dl,4
; DL = F3h
38
ROR Instruction
ROR (rotate right) shifts each bit to the right
The lowest bit is copied into both the Carry flag and
into the highest bit
No bits are lost
CF
MOV AL,11110000b
ROR AL,1
; AL = 01111000b
MOV DL,3Fh
ROR DL,4
; DL = F3h
39
RCL Instruction
RCL (rotate carry left) shifts each bit to the left
Copies the Carry flag to the least significant bit
Copies the most significant bit to the Carry flag
CF
CLC
MOV BL,88H
RCL BL,1
RCL BL,1
;
;
;
;
CF = 0
CF,BL = 0 10001000b
CF,BL = 1 00010000b
CF,BL = 0 00100001b
40
RCR Instruction
RCR (rotate carry right) shifts each bit to the right
Copies the Carry flag to the most significant bit
Copies the least significant bit to the Carry flag
CF
STC
MOV AH,10H
RCR AH,1
; CF = 1
; CF,AH = 00010000 1
; CF,AH = 10001000 0
41
Rotate Instructions
Destination
Count
Register
Register
CL
Memory
Memory
CL
42
MEANING
OPERATION
Flags
Affected
CLC
CF
STC
(CF) 1
CF
CMC
Complement
Carry Flag
(CF) (CF)l
CF
CLD
Clear Direction
Flag
(DF) 0
SI & DI will be auto incremented while
string instructions are executed.
DF
Set Direction
Flag
(DF) 1
SI & DI will be auto decremented
while string instructions are executed.
DF
CLI
Clear Interrupt
Flag
(IF) 0
IF
STI
Set Interrupt
Flag
(IF) 1
IF
STD
43
Format
Operation
CMP
CMP D,S
Compare
Flags
Affected
Allowed Operands
(D) = (S)
; ZF=0
Destination
Source
Register
Register
; ZF=0, CF=0
Register
Memory
; ZF=0, CF=1
Memory
Register
Register
Immediate
Memory
Immediate
Accumulator
Immediate
44
String?
An array of bytes or words located in
memory
Supported String Operations
Copy (move, load)
Search (scan)
Store
Compare
45
46
String Instructions
Instruction prefixes
Prefix
REP
Used with
Meaning
MOVS
STOS
REPE/REPZ
CMPS
SCAS
REPNE/REP
NZ
CMPS
SCAS
Instructions
MnemoNic
meaning
format
Operation
Flags
effect
-ed
MOVS
Move string
DS:SI
ES:DI
CMPS
Compare
string
DS:SI
ES:DI
All
status
flags
48
MnemoNic
meaning
format
Operation
SCAS
Scan string
AX ES:DI
SCASB/
SCASW
LODS
STOS
((ES)0+(DI)) (AL or A) 1 or 2
(DI) (DI) 1 or 2
49
Conditional
jumps
Unconditional
jump
Iteration
instructions
CALL
instructions
Return
instructions
50
Return
Call subroutine A
Next instruction
51
Inter Segment
At starting CS and IP placed in a stack.
New values are loaded in to CS and IP given by the
operand.
After execution original CS, IP values placed as it is.
Far-proc
Memptr32
These two words (32 bits) are loaded directly into IP and
CS with execution at CALL instruction.
First 16 IP
Next 16 CS
53
Mnem- Meaning
onic
CALL
Format
Operation
Flags
Affected
Far proc
Memptr 16
Regptr 16
Memptr 32
54
RETURN
Every subroutine must end by executing an instruction that returns control
to the main program. This is the return (RET) instruction.
By execution the value of IP or IP and CS that were saved in the stack to
be returned back to their corresponding regs. (this time (SP) (SP)+2 )
Mnem Meaning
-onic
Format
RET
RET or
Return
RET operand program
(and CS
operands
added to
SP.
Return
Operation
Flags
Affected
to
the
main None
by restoring IP
for far-proc). If
is present, it is
the contents of
Operand
None
Disp16
55
Loop Instructions
These instructions are used to repeat a set of instructions several
times.
Format:
LOOP Short-Label
Operation: (CX) (CX)-1
Jump is initialized to location defined by short label if CX0.
otherwise, execute next sequential instruction.
Instruction LOOP works w.r.t contents of CX. CX must be
preloaded with a count that represents the number of times the
loop is to be repeat.
Whenever the loop is executed, contents at CX are first
decremented then checked to determine if they are equal to zero.
If CX=0, loop is complete and the instruction following loop is
executed.
If CX 0, content return to the instruction at the label specified in
the loop instruction.
56
It is a 2 byte instruction.
Used for backward jump only.
Maximum distance for backward jump is only 128 bytes.
LOOP AGAIN is almost same as:
DEC CX
JNZ AGAIN
Mnemonic meaning
format
Operation
LOOP
Loop
Loop short-label
(CX) (CX) 1
Jump to location given by
short-label if CX 0
LOOPE/
LOOPZ
Loop while
equal/ loop
while zero
LOOPE/LOOPZ
short-label
(CX) (CX) 1
Jump to location given by
short-label if CX 0 and
ZF=1
LOOPNE/LOOPNZ
short-label
(CX) (CX) 1
Jump to location given by
short-label if CX 0 and
ZF=0
58
JMP AA
Unconditional JMP
Part 2
Skipped part
Part 3
AA
XXXX
Next instruction
Unconditional Jump
Unconditional Jump Instruction
Near Jump or
Far Jump or
Operands
Short label
Near label
Far label
Memptr16
Regptr16
memptr32
60
Conditional Jump
Part 1
Jcc AA
Conditional Jump
Part 2
NO
XXXX
condition
Skipped part
YES
Part 3
AA
XXXX
Next instruction
61
Jumps based on
a single flag
Jumps based on
more than one flag
62
Mnemonic :
Jcc
Meaning :
Conditional Jump
Format :
Jcc operand
Operation :
63
TYPES
Mnemonic
meaning
condition
JA
Above
JB
Above or Equal
CF=0
JB
Below
CF=1
JBE
Below or Equal
CF=1 or ZF=1
JC
Carry
CF=1
JCXZ
CX register is Zero
(CF or ZF)=0
JE
Equal
ZF=1
JG
Greater
JGE
Greater or Equal
SF=OF
JL
Less
64
Mnemonic
meaning
condition
JLE
Less or Equal
JNA
Not Above
CF =1 or Zf=1
JNAE
CF = 1
JNB
Not Below
CF = 0
JNBE
CF = 0 and ZF = 0
JNC
Not Carry
CF = 0
JNE
Not Equal
ZF = 0
JNG
Not Greater
JNGE
JNL
Not Less
SF = OF
65
Mnemonic
meaning
condition
JNLE
ZF = 0 and SF = OF
JNO
Not Overflow
OF = 0
JNP
Not Parity
PF = 0
JNZ
Not Zero
ZF = 0
JNS
Not Sign
SF = 0
JO
Overflow
OF = 1
JP
Parity
PF = 1
JPE
Parity Even
PF = 1
JPO
Parity Odd
PF = 0
JS
Sign
SF = 1
JZ
Zero
ZF = 1
66
JNZ
JS
JNS
JC
JNC
r8 ;Jump if No Carry
JP
JNP
JO
JNO
JE SAME
Should be
<=127
bytes
ADD CX, DX
;Executed if Z = 0
;Executed if Z = 1
(if SI = DI)
69
Should be
<= 128
bytes
; executed if Z = 1
(if SI = DI)
:
CMP SI, DI
JE BACK
ADD CX, DX
;executed if Z = 0
(if SI not equal to DI)
70
What if
>127
bytes
Requirement
Then do this!
CMP SI, DI
CMP SI, DI
JE SAME
JNE NEXT
ADD CX, DX
JMP SAME
:
SAME: SUB BX, AX
Range for JMP (unconditional jump) can be +215 = + 32K JMP instruction
discussed in detail later
71
JBE or
JNA
Jump if
No Jump if
Ex.
Cy = 1 OR Z= 1
Cy = 0 AND Z = 0
CMP BX, CX
Below OR Equal
Surely Above
JBE BX_BE
JNBE or
JA
Jump if
No Jump if
Ex.
Cy = 0 AND Z= 0
Cy = 1 OR Z = 1
CMP BX, CX
Surely Above
Below OR Equal
JA BXabove
74
No Jump if
S = 1 AND V = 0
(surely negative)
OR (S = 0 AND V = 1)
(wrong answer positive!)
OR Z = 1 (equal)
S = 0 AND V = 0
(surely positive)
OR (S = 1 AND V = 1)
(wrong answer negative!)
AND Z = 0 (not equal)
i.e. S XOR V = 1 OR Z = 1
75
No Jump if
S = 0 AND V = 0
(surely positive)
OR (S = 1 AND V = 1)
(wrong answer negative!)
AND Z = 0 (not equal)
S = 1 AND V = 0
(surely negative)
OR (S = 0 AND V = 1)
(wrong answer positive!)
OR Z = 1 (equal)
i.e. S XOR V = 1 OR Z = 1
76
No Jump if
S = 1 AND V = 0
(surely negative)
OR (S = 0 AND V = 1)
(wrong answer positive!)
S = 0 AND V = 0
(surely positive)
OR (S = 1 AND V = 1)
(wrong answer negative!)
i.e. S XOR V = 1
i.e. S XOR V = 0
77
No Jump if
S = 0 AND V = 0
(surely positive)
OR (S = 1 AND V = 1)
(wrong answer negative!)
S = 1 AND V = 0
(surely negative)
OR (S = 0 AND V = 1)
(wrong answer positive!)
i.e. S XOR V = 0
i.e. S XOR V = 1
78
Near Jump
Near Jump
Direct Jump
(common)
Indirect Jump
(uncommon)
Short Jump
Long Jump
2 bytes
3 bytes
EB r8
E9 r16
range + 27
range +215
2 or more bytes
Starting with FFH
Range: complete
segment
3 Near Jump and 2 Far Jump instructions have the same mnemonic
JMP but different opcodes
79
Short Jump
2 byte (EB r8) instruction Range: -128 to +127 bytes
Backward jump: Assembler knows the quantum of jump.
Generates Short Jump code if <=128 bytes is the required jump
Generates code for Long Jump if >128 bytes is the required jump
SAME
:
:
SAME:
MOV CX, DX
81
Long Jump
3-byte (E9 r16) instruction Range: -32768 to +32767 bytes
CS:0000H
Long Jump can
handle it as jump
quantum is <=32767
CS:8000H
JMP FRWD
:
:
FRWD = CS:FFFFH
82
BKWD = CS:0000H
CS:8000H
JMP BKWD
:
:
CS:FFFFH
83
CS:0000H
:
:
Can be treated
as a small
(20H) backward
branch!
CS:000DH
JMP FRWD
CS:0010H
Jump distance
=FFE0H. Too
very long
forward jump
:
FRWD= CS:FFF0H
CS:FFFFH
84
CS:0000H
:
:
Can be treated
as a small
(20H) forward
branch!
BKWD= CS:0010H
:
:
Jump distance
=FFE0H. Too
very long
backward jump
JMP BKWD
CS:FFF0H
CS:FFFFH
85
DS:3234H 5678H
Branches to
DS:3236H AB22H
CS:5678H
86
Far Jump
Far Jump
Direct Jump
(common)
Indirect Jump
(uncommon)
5 bytes
EA,2 byte offset, 2 byte segment
Range: anywhere
2 or more bytes
Starting with FFH
Range: anywhere
3 Near Jump and 2 Far Jump instructions have the same mnemonic
JMP but different opcodes
87
88
89
1234H
Branches to
ABCDH:5678H
DS:3234H
5678H
DS:3236H
ABCDH
It is a 4-byte instruction
90