2Module_8086_Instruction_Set_&_Programming
2Module_8086_Instruction_Set_&_Programming
Microprocessor
CSC405
Module - 2
Instruction Set and Programming
By Prof. Sunil Katkar
Department of Computer Engineering, VCET
Module - 2
Instruction Set and Programming
Objective
To emphasize on instruction set and logic to build assembly
language programs.
Outcome
At the end of the course student will be able to:
Write assembly, mixed language programs using instruction
set of 8086 and analyze updated values of control flag after
execution of assembly language program.
Module -2
Instruction Set and Programming
1. Register Addressing
Group I : Addressing modes for
2. Immediate Addressing register and immediate data
3. Direct Addressing
5. Based Addressing
Group II : Addressing modes for
6. Indexed Addressing memory data
7. Based Index Addressing
8. String Addressing
8. String Addressing
6
8086 Microprocessor Group I : Addressing modes for
Addressing Modes register and immediate data
1. Register Addressing
In immediate addressing mode, an 8-bit or 16-bit
2. Immediate Addressing data is specified as part of the instruction
3. Direct Addressing
Example:
4. Register Indirect Addressing
MOV DL, 08H
5. Based Addressing
The 8-bit data (08H) given in the instruction is
6. Indexed Addressing moved to DL
8. String Addressing
MOV AX, 0A9FH
9. Direct I/O port Addressing
The 16-bit data (0A9FH) given in the instruction is
10. Indirect I/O port Addressing
moved to AX register
11. Relative Addressing
(AX) 0A9FH
12. Implied Addressing
7
8086 Microprocessor
Addressing Modes : Memory Access
1. Register Addressing
2. Immediate Addressing
Here, the effective address of the memory
3. Direct Addressing
location at which the data operand is stored is
4. Register Indirect Addressing given in the instruction.
12. Implied Addressing This addressing mode is called direct because the
displacement of the operand from the segment
base is specified directly in the instruction.
11
8086 Microprocessor Group II : Addressing modes
Addressing Modes for memory data
12
8086 Microprocessor Group II : Addressing modes
Addressing Modes for memory data
(AL) (MA)
(AH) (MA + 1) 13
8086 Microprocessor Group II : Addressing modes
Addressing Modes for memory data
(CL) (MA)
(CH) (MA + 1)
14
8086 Microprocessor Group II : Addressing modes
Addressing Modes for memory data
15
8086 Microprocessor Group II : Addressing modes
Addressing Modes for memory data
1. Register Addressing
2. Immediate Addressing
1. Register Addressing
2. Immediate Addressing
3. Direct Addressing
5. Based Addressing
6. Indexed Addressing
Instructions using this mode have no operands.
The instruction itself will specify the data to be
7. Based Index Addressing
operated by the instruction.
8. String Addressing
Example: CLC
9. Direct I/O port Addressing
This clears the carry flag to zero.
10. Indirect I/O port Addressing
19
INSTRUCTION SET
Rules while executing the Instruction
21
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
3. Logical Instructions
22
8086 Microprocessor
Instruction Set
23
Data Transfer Instructions
24
MOV destination register, source register
MOV AX, BX
25
MOV memory. accumulator
MOV [SI], AL
Let, CS → 2105 H
IP → 187A H, DS → 2314 H,
AL → 20H, SI → 1023
26
MOV accumulator, memory
Let, CS → 2105 H
IP → 187A H, DS → 2314 H,
Temp_Result → FF H,
AH → 00 H and AL → 0A H
27
MOV register, memory
# MOV CX, COUNT [DI]
Let, CS → 2105 H
IP → 187A H, DS → 2314 H,
DI → 1100 H,
AH → AA H and AL → 2B H
COUNT offset → 1000 H
28
# MOV memory, register # MOV register, immediate
EA = COUNT + DI
PA = DS + EA
29
# MOV seg-reg, reg-16 # MOV seg-reg, mem-16
30
PUSH – Push word onto stack
# PUSH Source
SP = SP – 2
SS : [SP]
PUSH AX
CS → 2105H, IP → 187AH,
31
POP – Pop word off stack
32
XCHG – Exchange byte or word
XCHG AX, BX
33
XLAT/XLATB : Translate or Replace Byte
34
35
36
Addition
37
ADD register, register
38
ADD register, memory
Let,
DS → 2314 H
Offset → 2048
Physical Address
23140 + 2048
=25188
2005 + 1024 =
3029
39
ADD memory, register
ADD [2048], AX
Let,
DS → 2314 H
Offset → 2048
Physical Address
23140 + 2048
=25188
2005 + 1024 =
3029
40
ADD register, immediate
ADD accumulator, immediate
ADD AL, 74 H
74 + 24 = 98
41
ADD memory, immediate data
ADD [5214], 75
Let,
DS → 2314 H
Offset → 5214
Physical Address
23140 + 5214
=28354
75 H + 14 H = 89 H
42
ADC – Add with carry
ADC AL, BL
20 H + 34 H + 01 = 55 H
43
INC – Increment byte or word by 1
INC Destination
INC AL
INC CX
44
DAA – Decimal adjust for addition
01011001+ 00110100
= 1000 1101
= 8D BCD
Lower nibble = 1101 = F > 9
therefore,
10001101 + 00000110
= 1001 0011
= 93 BCD 45
Address Object Instructions
46
LEA – Load Effective Address
# LEA register, source
Reg. = Address of
memory (offset)
47
LDS – Load pointer with DS
DS = Second word
IP → 187AH, ES → 2010H
48
LDS – Load pointer with DS
49
50
SUB Register, Register
SUB BL, CL H 07 - 04 = 03
SUB CH, BL
CF = 1, OF = 0, SF = 1
SUB CH, BL
ZF=AF=0, PF = 1, OF = 1, SF = 1, CF = 1
51
52
Flag Register
0 1 1 0 1 0 1 0
0 1 1 1 1 0 1 0
1 1 1 1
1 1 1 1 1 0 0 0 0
ZF=AF=0, PF = 1, OF = 1, SF = 1, CF = 1
53
SUB memory, register
SUB [2048], AX
Let,
CS → 2105 H,
IP → 187A H
DS → 2314 H
Offset → 2048H
Physical Address
23140 + 2048
=25188
2024 - 1005 =
101FH
54
SUB Register, memory
55
SUB Register, immediate
SUB accumulator, immediate
Register register – immediate data
56
SUB memory, immediate data
57
SBB Subtract byte or word with borrow
58
DEC - Decrement byte or word by 1
DEC destination
destination destination – 1
DEC AL
AL AL – 1
Let, AL = 09H
59
AAS – ASCII adjust for subtraction
AAS
If lower nibble of AL > 9 or AF = 1, then Let AL = 0011 1001
AL = AL – 06H = 39H = ASCII 9
AH = AH – 1 Let BL = 0011 0101
AF = 1 = 35H = ASCII 5
CF = 1 SUB AL, BL
AAS
else
= 0011 1001 – 0011 0101
AF = 0 = 0 0000 0100
CF = 0 = 04 BCD
In both cases clear the high nibble of AL
60
DAS – Decimal adjust for subtraction
62
CMP – Compare byte or word
CMP destination, source
destination – source
If source > destination, then
CF = 1, ZF = 0, SF = 1
If source < destination, then
CF = 0, ZF = 0, SF = 0
If source = destination, then
CF = 0, ZF = 1, SF = 0
63
Multiplication
64
MUL - Multiply byte or word (unsigned)
MUL CX
MUL source
(DX:AX) = AX * CX
When operand is a byte:
MSB will be stored in DX
AX = AL * operand
LSB will be stored in AX
When operand is a word
Let, AX = 0009H, CX = 0054H,
(DX:AX) = AX * operand
DX = FFFFH (initial data),
CS = 2105H, IP = 187AH
0009H * 0054H = 02F4H
65
IMUL - Multiply byte or word (signed)
66
Division Instruction
67
DIV – Divide byte or word unsigned
DIV source
DIV BH
When operand is a byte:
Let, AX = 37D7 H = 14295 decimal
AL = AX / operand (Quotient)
BH = 97H = 151 decimal
AH = remainder (Modulus)
AL = AX / BH = 5E H = 94 decimal
When operand is a word
AH = 65H = 101 decimal
AX = (DS:AX) / operand (Quotient)
DX = remainder (Modulus)
68
IDIV – Integer divide byte or word
IDIV source
IDIV BL
When operand is a byte:
Let, AX =03AB H, BL= 00D3 H
AL = AX / operand (Quotient)
AL = AX /BL = EC H
AH = remainder (Modulus)
AH = Remainder = 27 H
When operand is a word
AX = (DS:AX) / operand (Quotient)
DX = remainder (Modulus)
69
CBW – Convert byte or word
70
CWD – Convert word to double word
CWD
If MSB bit of AX = 1 then
DX = 65535 (FFFF H)
If DX = 4AA3 H and AX = 00A3 H
Else
DX = 0
71
Logical Group
72
NOT – NOT byte or word
NOT destination
If bit is 1 turn it to 0
If bit is 0 turn it to 1
NOT AX
73
AND – And byte or word
AND destination, source
Destination = destination AND source
AND AL, BL
74
OR –Inclusive OR byte or word
OR destination, source
Destination = destination OR source
OR AL, BL
75
XOR – Exclusive OR byte or word
XOR destination, source
Destination = destination XOR source
XOR AL, BL
76
TEST – Test byte or word
77
Shifts
78
SHL/SAL – Shift logical/arithmetic left byte
or word
79
SHR – Shift logical right byte or word
80
SAR – Shift arithmetic byte or word
81
Rotates
82
ROL – Rotate left byte or word
83
RCR – rotate through carry right byte or
word
84
Mixed Language Programming
using Assembly and C
85
Combining Assembly and C
86
Combining Assembly and C
87
AAA – ASCII adjust for addition
88
DAA – Decimal Adjust Accumulator
89
AAS – ASCII adjust for Subtraction
90
DAS – Decimal Adjust for Subtraction
91
8086 Microprocessor
Instruction Set
92
8086 Microprocessor
Instruction Set
94
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADD A, data
95
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADDC A, data
96
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SUB A, data
97
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SBB A, data
98
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
99
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
100
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
101
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
102
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
103
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
104
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
CMP A, data
105
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
106
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
107
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
108
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
109
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
110
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
111
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
112
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL …
113
8086 Microprocessor
Instruction Set
114
8086 Microprocessor
Instruction Set
REP
115
8086 Microprocessor
Instruction Set
MOVS
(MAE) (MA)
116
8086 Microprocessor
Instruction Set
CMPS
117
8086 Microprocessor
Instruction Set
LODS
119
8086 Microprocessor
Instruction Set
STOS
120
8086 Microprocessor
Instruction Set
CLC Clear CF 0
NOP No operation
Mnemonics Explanation
CALL reg/ mem/ disp16 Call subroutine
122
8086 Microprocessor
Instruction Set
Checks flags
123
8086 Microprocessor
Instruction Set
Mnemonics Explanation
JC disp8 Jump if CF = 1
JP disp8 Jump if PF = 1
JO disp8 Jump if OF = 1
JS disp8 Jump if SF = 1
125
Assembler directives
8086 Microprocessor
Assemble Directives
Used to :
› specify the start and end of a program
› attach value to variables
› allocate storage locations to input/ output data
› define start and end of segments, procedures, macros etc..
127
8086 Microprocessor
Assemble Directives
DB Define Byte
PROC
FAR Example:
NEAR LIST DB 7FH, 42H, 35H
ENDP
Three consecutive memory locations are reserved for
SHORT the variable LIST and each data specified in the
instruction are stored as initial value in the reserved
MACRO memory location
ENDM 128
8086 Microprocessor
Assemble Directives
DB Define Word
PROC
FAR Example:
NEAR ALIST DW 6512H, 0F251H, 0CDE2H
ENDP
Six consecutive memory locations are reserved for
SHORT the variable ALIST and each 16-bit data specified in
the instruction is stored in two consecutive memory
MACRO location.
ENDM 129
8086 Microprocessor
Assemble Directives
ORG
END Segnam SEGMENT
EVEN
…
EQU … Program code
… or
PROC … Data Defining Statements
…
FAR …
NEAR
ENDP Segnam ENDS
SHORT
ORG
User defined name of
END Segment Register
the segment
EVEN
EQU
PROC Example:
FAR
NEAR ASSUME CS: ACODE, DS:ADATA Tells the compiler that the
ENDP instructions of the program are
stored in the segment ACODE and
data are stored in the segment
SHORT ADATA
MACRO
ENDM 131
8086 Microprocessor
Assemble Directives
ORG
Examples:
END
EVEN ORG 1000H Informs the assembler that the statements
EQU following ORG 1000H should be stored in
memory starting with effective address
1000H
PROC
FAR
LOOP EQU 10FEH Value of variable LOOP is 10FEH
NEAR
ENDP
_SDATA SEGMENT In this data segment, effective address of
SHORT ORG 1200H memory location assigned to A will be 1200H
A DB 4CH and that of B will be 1202H and 1203H.
EVEN
MACRO B DW 1052H
ENDM _SDATA ENDS 132
8086 Microprocessor
Assemble Directives
General form
ASSUME
ORG
procname PROC[NEAR/ FAR]
END
EVEN …
… Program statements of the
EQU procedure
…
DB
Examples:
DW
RET
ORG
ADD64 ENDP
END
EVEN
EQU CONVERT PROC FAR The subroutine/ procedure named CONVERT
is declared as FAR and so the assembler will
… code the CALL and RET instructions involved
PROC … in this procedure as far call and return
ENDP …
FAR
RET
NEAR CONVERT ENDP
SHORT
MACRO
ENDM 134
8086 Microprocessor
Assemble Directives
PROC
ENDP
FAR
NEAR
SHORT
MACRO
ENDM 135
8086 Microprocessor
Assemble Directives
PROC
ENDP
FAR User defined name of
NEAR the macro
SHORT
MACRO
ENDM 136
Interfacing memory and i/o ports
8086 Microprocessor
Memory
Processor Memory
▪ Registers inside a microcomputer
▪ Store data and results temporarily
▪ No speed disparity
▪ Cost
Secondary Memory
▪ Storage media comprising of slow
devices such as magnetic tapes and
disks
▪ Hold large data files and programs:
Operating system, compilers,
databases, permanent programs etc. 138
8086 Microprocessor
Memory organization in 8086
8086 : 16-bit
Bank 0 : A0 = 0 Even
addressed memory bank
139
8086 Microprocessor
Memory organization in 8086
141
8086 Microprocessor
Interfacing SRAM and EPROM
142
8086 Microprocessor
Interfacing SRAM and EPROM
143
8086 Microprocessor
Interfacing SRAM and EPROM
144
8086 Microprocessor
Interfacing SRAM and EPROM
Monitor Programs
Programing 8279 for keyboard scanning and display
refreshing
Initialization of stack
145
8086 Microprocessor
Interfacing I/O and peripheral devices
I/O devices
For communication between microprocessor and
outside world
Ports / Buffer IC’s
Microprocessor I/ O devices
(interface circuitry)
Interrupt driven I/ O
I/O device interrupts the
processor and initiate data
transfer
Direct memory access
Data transfer is achieved by 146
bypassing the microprocessor
147
148
149
150
151
152