Instruction Set of 8086 Microprocessor
Instruction Set of 8086 Microprocessor
1
Instruction set basics
• Instruction:- An instruction is a binary pattern designed inside a
microprocessor to perform a specific function.
• Opcode:-
• It stands for operational code.
• It specifies the type of operation to be performed by CPU.
• It is the first field in the machine language instruction format.
• E.g. 08 is the opcode for instruction “MOV X,Y”.
• Operand:-
• We can also say it as data on which operation should act.
• Operands may be register values or memory values.
• The CPU executes the instructions using information present in this field. It
may be 8-bit data or 16-bit data. 2
Instruction set basics
3
Assignment of codes to Registers
4
Types of instruction set of 8086
microprocessor
(1). Data Copy/Transfer instructions.
7
Stack Pointer
• It is a 16-bit register, contains the address of the data
item currently on top of the stack.
8
(1). Data copy/transfer instructions.
• E.g.:
• (1). PUSH AX;
• (2). PUSH DS;
• (3). PUSH [5000H]; 9
INITIAL POSITION
LOWER BYTE
(3) STACK POINTER
HIGHER BYTE
10
BEFORE EXECUTION
SP 2002H
2000H
BH BL
2001H
CH 10 CL 50
DH DL
2002H
PUSH CX
AFTER EXECUTION
50
SP 2000H 2000H
BH BL
2001H 10
CH 10 CL 50
DH DL 2002H
11
(1). Data copy/transfer instructions.
• E.g.
• (1). POP AX;
• (2). POP DS;
• (3). POP [5000H];
12
INITIAL POSITION AND READS LOWER BYTE
(1) STACK POINTER LOWER BYTE
INCREMENTS SP
LOWER BYTE
HIGHER BYTE
2000H 30
SP 2000H
2001H
50
BH BL
2002H
POP BX
AFTER EXECUTION
2000H 30
SP 2002H 2001H 50
BH 50 BL 30 2002H
14
(1). Data copy/transfer instructions.
•E.g.
(1). XCHG BX, AX;
(2). XCHG [5000H],AX;
15
BEFORE EXECUTION AFTER EXECUTION
AH 20 AL 40 AH 70 AL 80
BH 70 BL 80 BH 20 BL 40
XCHG AX,BX;
16
(1). Data copy/transfer instructions.
PORT 80H 10 AL
IN AL,80H;
AFTER EXECUTION
PORT 80H 10 AL 10
18
(1). Data copy/transfer instructions.
19
BEFORE EXECUTION
PORT 50H 10 AL 40
OUT 50H,AL;
AFTER EXECUTION
PORT 50H 40 AL 40
20
(1). Data copy/transfer instructions.
• (7). XLAT;
Mnemonic Meaning Format Operation Flags
XLAT Translate XLAT ((AL)+(BX)+(DS)0) (AL) None
22
(1). Data copy/transfer instructions.
• E.g.
(1). LEA BX,Address;
(2). LEA SI,Address[BX];
23
(1). Data copy/transfer instructions.
• E.g.
(1). LDS BX,5000H;
(2). LES BX,5000H;
24
(1). LDS BX,5000H;
(2). LES BX,5000H;
15 0 7 0
10
BX 20 10 5000H
20
5001H
30
DS/ES 40 30 5002H
40
5003H
25
(1). Data copy/transfer instructions.
• Addition,
• Subtraction,
• Increment,
• Decrement.
28
(2). Arithematic Instructions
•(1). ADD destination, source;
1010
+2020
3030
2050 30
(2). Arithematic Instructions
• (2). ADC destination, source
• This instruction adds the contents of source operand with the
contents of destination operand with carry flag bit.
• The source may be immediate data, memory location or register.
• The destination may be memory location or register.
• The result is stored in destination operand.
• AX is the default destination register.
ADC AX,2020H
CY 1
AH 10 AL 10 AH 30 AL 31
1010
+2020
3030+1=3031
BEFORE EXECUTION AFTER EXECUTION
CY 1
AH 10 AL 10 AH 30 AL 31
BH 20 BL 20 ADC AX,BX BH 20 BL 20
2050 32
(2). Arithematic Instructions
• (3). INC source
AH 10 AL 11
AH 10 AL 10 INC AX
1011
5000H 1010 INC [5000H] 5000H
34
(2). Arithematic Instructions
• (4). DEC source;
AH 10 AL 0F
AH 10 AL 10 DEC AX
100F
5000H 1010 DEC [5000H] 5000H
36
(2). Arithematic Instructions
• (5). SUB destination, source;
• This instruction subtracts the contents of source operand from
contents of destination.
• The source may be immediate data, memory location or
register.
• The destination may be memory location or register.
• The result is stored in the destination place.
AH 20 AL 00 SUB AX,1000H AH 10 AL 00
2000
-1000
=1000
AH 20 AL 00 AH 10 AL 00
BH 10 BL 00 SUB AX,BX BH 10 BL 00
38
(2). Arithematic Instructions
• (6). SBB destination, source;
• Also known as Subtract with Borrow.
• This instruction subtracts the contents of source operand &
borrow from contents of destination operand.
• The source may be immediate data, memory location or register.
• The destination may be memory location or register.
• The result is stored in the destination place.
SBB AX,1000H
B 1
AH 20 AL 20 AH 10 AL 19
2020
- 1000
1020-1=1019
BEFORE EXECUTION AFTER EXECUTION
B 1
AH 20 AL 20 AH 10 AL 19
BH 10 BL 10 SBB AX,BX BH 10 BL 10
2050 40
(2). Arithematic Instructions
• (7). CMP destination, source
• Also known as Compare.
• This instruction compares the contents of source operand with
the contents of destination operands.
• The source may be immediate data, memory location or
register.
• The destination may be memory location or register.
• Then resulting carry & zero flag will be set or reset.
42
(2). Arithematic Instructions
•(8). AAA
• Also known as ASCII Adjust After Addition.
• This instruction is executed after ADD instruction.
(1). IF lower bits of AL<=09 then,
• Higher bits of AL should loaded with zeroes.
• There should be no change in lower bits of AL.
• AH also must be cleared (AH=0000 0000).
(2). IF lower bits of AL>09 then,
• Bits of AL must be incremented by 06 (i.e. AL+0110).
• Bits of AH must be incremented by 01 (i.e. AH+0001).
• Then higher bits of AL should be loaded with 0000.
43
(1). FOR AL<=09H
AL 6 7 BEFORE EXECUTION
Hb Lb
AL 0 7 AFTER EXECUTION
Hb Lb
Hb=Higher bits,
Lb=Lower bits.
(1). FOR AL>09H
AL 6 A BEFORE EXECUTION
Hb Lb
(A)1010 +(06)0110=0001 0000
HB LB
AL 0 0 AFTER EXECUTION
Hb Lb
44
(2). Arithematic Instructions
•(9). AAS
• Also known as ASCII Adjust After Subtraction.
• This instruction is executed after SUB instruction.
(1). IF lower bits of AL<=09 then,
• Higher bits of AL should loaded with zeroes.
• There should be no change in lower bits of AL.
• AH also must be cleared (AH=0000 0000).
(2). IF lower bits of AL>09 then,
• Bits of AL must be decremented by 06 (i.e. AL-0110).
• Bits of AH must be decremented by 01 (i.e. AH-0001).
• Then higher bits of AL should be loaded with 0000.
45
(1). FOR AL<=09H
AL 6 7 BEFORE EXECUTION
Hb Lb
AL 0 7 AFTER EXECUTION
Hb Lb
Hb=Higher bits,
Lb=Lower bits.
(1). FOR AL>09H
AL 6 A BEFORE EXECUTION
Hb Lb
(A)1010 -(06)0110=0000 0100
Hb Lb
AL 0 4 AFTER EXECUTION
Hb Lb
46
(2). Arithematic Instructions
• (10). AAM
• Also known as ASCII Adjust After Multiplication.
• This instruction is executed after MUL instruction.
• Then AH=AL/10 & AL=Remainder.
48
(2). Arithematic Instructions
•(12). DAA;
• Decimal Adjust Accumulator.
• IF lower bits of AL>09.
• Then AL=AL+06.
59
• Follow the rules as given below:-
(1). 1 NOT = 0
(2). 0 NOT = 1
60
•
(3). Logical Instructions
•(4). TEST destination,source
• Both operands may be register, memory location or immediate data.
• This instruction performs bit by bit logical AND operation for flags
only (i.e. only flags will be affected).
• If the corresponding 0th bit of result contains ‘1’ then result will be
non-zero & zero flag will be cleared/reset (i.e. ZF=0).
• If the corresponding 0th bit of result contains ‘0’ then result will be
zero & zero flag will be set (i.e. ZF=1)..
62
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
63
Fast Multiplication
• Shifting left 1 bit multiplies a number by 2
mov dl,5
Before: 0 0 0 0 0 1 0 1 =5
shl dl,1
After: 0 0 0 0 1 0 1 0 = 10
64
• Ex.; Multiply AX by 10
• SHL AX, 1
• MOV BX, AX
• MOV CL,2
• SHL AX,CL
• ADD AX, BX
65
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
CF
67
(3). Logical Instructions
RCR
• Also known as Rotate Right through Carry.
• Each binary bit of the operand is rotated towards right
by one position through Carry flag.
• Least Significant Bit (LSB) i.e. B0 is placed in the Carry
flag.
• Then carry flag bit is placed in the Most Significant Bit
(MSB) position B15.
68
BEFORE EXECUTION
B B B B B B B B B B B B B B B B CF
1 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0
15 7 0
AFTER EXECUTION
B B B B B B B B B B B B B B B B CF
0 1 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1
15 7 0
69
(3). Logical Instructions
•(6). RCL
• Also known as Rotate Left through Carry.
• Each binary bit of the operand is rotated towards left
by one position through Carry flag.
• Least Significant Bit (LSB) of operand i.e. B0 is placed in
the B1.
• Then Most Significant Bit (MSB) of operand is placed in
carry flag bit.
70
BEFORE EXECUTION
CF B B B B B B B B B B B B B B B B
1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1
0
AFTER EXECUTION
CF B B B B B B B B B B B B B B B B
0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0
1
71
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
72
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
73
String?
74
String Instruction Basics
• Source DS:SI, Destination ES:DI
75
String Instructions
Instruction prefixes
77
Mnemo- meaning format Operation
Nic
SCAS Scan string SCASB/ Set flags as per
AX – ES:DI SCASW (AL or AX) - ((ES)0+(DI))
(DI) (DI) ± 1 or 2
78
(4). String Manipulation Instructions
•(1). REP
• Also known as Repeat instruction prefix.
• This instruction executed repeatedly until the ‘CX’
register becomes zero.
• When ‘CX’ becomes zero then program control passes
to next instruction.
• There are following sub types of ‘REP’ instruction,
• (i). REPE:- REPeat instruction while Equal.
• (ii). REPZ:- REPeat instruction while Zero.
• (iii). REPNE:- REPeat instruction while Not Equal.
• (iv). REPNZ:- REPeat instruction while Not Zero.
79
(4). String Manipulation Instructions
•(2). CMPS
• Also known as Compare String Byte or String Word.
• The length of the string must be stored in register CX.
• If both the byte or word are equal then zero flag will be
set (i.e. ZF=1) otherwise it will be reset (i.e. ZF=0).
• When zero flag will be set then ‘CX’=0.
• There are following sub types,
• (i). CMPSB:- Compare String Byte.
• (ii). CMPSW:- Compare String Word.
80
(5). Branching Instructions
• (1). CALL
• Also known as unconditional call.
• Under unconditional call, the execution control is
transferred to the specified location independent of
any status or condition.
• This instruction is used to call subroutine from a main
program.
• There are following sub types,
• (i). NEAR CALL:- It pushes only IP into the stack.
• (ii). FAR CALL:- It pushes IP & CS into the stack. 81
(5). Branching Instructions
•(2). JMP
• Also known as unconditional jump.
• Under unconditional jump, the execution control is transferred to the
specified location using 8-bit or 16-bit displacement.
• There are following three formats of jump instruction,
• (i). JMP 8-bit Displacement
82
Conditional Jump instructions
83
Conditional Jump Instructions
Mnemonic : Jcc
Meaning : Conditional Jump
Format : Jcc operand
Operation : If condition is true jump to the address specified by operand.
Otherwise the next instruction is executed.
Flags affected : None
84
TYPES
JB Below CF=1
JC Carry CF=1
JE Equal ZF=1
86
Mnemonic meaning condition
JNLE Not Less nor Equal 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
87
Jumps Based on a single flag
JZ r8 ;Jump if zero flag set to 1 (Jump if result is zero)
JNZ r8 ;Jump if Not Zero (Z flag = 0 i.e. result is nonzero)
JS r8 ;Jump if Sign flag set to 1 (result is negative)
JNS r8 ;Jump if Not Sign (result is positive)
88
JZ r8 ; JE (Jump if Equal) also means same.
JNZ r8 ; JNE (Jump if Not Equal) also means same.
JC r8 ;JB (Jump if below) and JNAE (Jump if Not
Above or Equal) also mean same.
89
(6). Flag Manipulation Instructions
•(1). CMC
• Also known as Complement Carry Flag.
• It inverts contents of carry flag.
• if CF = 1 then CF will be = 0.
• if CF = 0 then CF will be = 1.
• E.g. CMC
90
(6). Flag Manipulation Instructions
•(2). STC
• Also known as Set Carry Flag.
• It makes carry flag in set condition.
• After execution CF = 1.
• E.g. STC
91
(6). Flag Manipulation Instructions
•(3). CLI
• Also known as Clear Interrupt Flag.
• It makes interrupt flag in reset condition.
• After execution IF = 0.
• E.g. CLI
92
(6). Flag Manipulation Instructions
•(4). CLD
• Also known as Clear Direction Flag.
• It makes direction flag in reset condition.
• After execution DF = 0.
• E.g. CLD
93
(7). Machine Control Instructions
•(1). HLT
• Also known as Halt
• It makes the processor to be in stable (do nothing)
condition.
• E.g. HLT
94
(7). Machine Control Instructions
•(2). NOP
• Also known as No Operation.
• It tells about further there will be no operation to be
performed.
• E.g. NOP
95