Chapter 3
Chapter 3
Addressing Modes
&
Instruction Set
Of 8086
UNIVERSITY OF GONDAR
INSTITUTE OF TECHNOLOGY
COMPILED BY: TIGABU Y.
@2021 1
Introduction
Program
A set of instructions written to solve a problem.
Instruction
Directions which a microprocessor
follows to execute a task or part of a
task.
Computer language
High Level
Low Level
Machine Language
Assembly Language
Binary bits English Alphabets
‘Mnemonics’
Assembler Mnemonics
Machine Language
2
Introduction... Cont’d
Program is a set of instructions written to solve a problem. Instructions are the
directions which a microprocessor follows to execute a task or part of a task.
Broadly, computer language can be divided into two parts as high-level language
and low level language. Low level language are machine specific. Low level
language can be further divided into machine language and assembly language.
3. Direct Addressing
9. String Addressing
• Example 1:
MOV DL, 08H
• Example 2:
MOV AX, 0A9FH
EA = (BX)
BA = (DS) x 10 H where:
MA = BA + EA DS = Data Segment
EA = Effective Address
(AX) (MA) or,
BA = Base Address
(AL) (MA) MA = (Physical) Memory Address
(AH) (MA +1)
14
5.Indexed Addressing Mode
• SI or DI register is used to hold an index value for memory data
and a signed 8-bit or unsigned 16-bit displacement will be
specified in the instruction.
• Example: MOV CX, [SI + A2H] ; indexing addressing mode with displacement
Operations:
EA = (SI) + FFA2H
BA = (DS) x 10 H
MA = BA + EA
(CL) (MA)
(CH) (MA + 1)
15
6. Based Addressing Mode
• In Based Addressing, BX or BP is used to hold the base value for effective
address and a signed 8-bit or unsigned 16-bit displacement will be specified
in the instruction.
• Example: MOV AX, [BX + 08H]; Based Addressing Mode with displacement
Operations:
(AL) (MA)
(AH) (MA + 1)
16
7.BASED & INDEX ADDRESSING MODES
• In Based Index Addressing, the effective address is computed
from the sum of a base register (BX or BP), an index register
(SI or DI).
Operations:
EA = (BX) + (SI)
BA = (DS) x 10 H
MA = BA + EA
(DL) (MA)
(DH) (MA + 1)
17
8.BASED & INDEX with Displacement ADDRESSING
MODES
• In Based Index with displacement Addressing, the effective
address is computed from the sum of a base register (BX or
BP), an index register (SI or DI) and a displacement.
Operations:
(DL) (MA)
(DH) (MA + 1)
18
9. Strings Addressing Mode
• Employed in string operations to operate on string data.
• The effective address (EA) of source data is stored in SI register and the OA of
destination is stored in DI register.
• Segment register for calculating base address of source data is DS and that of the
destination data is ES.
19
9. Strings Addressing Mode
• Example: MOVS BYTE ; in another way [ES:DI] [DS:SI]
Operations:
20
10. Direct I/O Port Addressing
• These addressing modes are used to access data from
standard I/O mapped devices or ports.
Operations:
PORTaddr = 09H
(AL) (PORT)
21
11. Indirect I/O Port Addressing
• In indirect port addressing mode, the instruction will
specify the name of the register which holds the port
address. In 8086, the 16-bit port address is stored in
the DX register.
• Example: OUT [DX], AX
Operations:
PORTaddr = (DX)
(PORT) (AX)
22
12. Implied Addressing Mode
• Instructions using this mode have no operands.
The instruction itself will specify the data to be
operated by the instruction.
• Example 1: STC
• Example 2: CLC
• Example 3: CLD
24
Addressing Modes (cont’d)
• Program Memory Addressing Modes
o According to the flow of instruction execution, the
instructions may be categorized as
1. Sequential Control Flow Instructions
2. Control Transfer (BRANCH) Instructions
o Sequential control flow instructions are the instructions
which after execution, transfer control to the next
instruction appearing immediately after it (in the sequence)
in the program.
For example the arithmetic, logic, data transfer and processor
control instructions are Sequential control flow instructions.
o The control transfer instructions on the other hand transfer
control to some predefined address or the address
somehow specified in the instruction, after their execution.
For example INT, CALL, RET & JMP instructions fall under this
category. 25
Addressing Modes in 8086
• Program Memory Addressing Modes
This addressing mode is required for instructions
that cause a branch in the program
These instructions are: JMP & CALL
If the branch is within the segment, it is called as an
Intra-Segment Branch or a Near Branch.
o The destination location is within the current segment
If the branch is in a different segment, it’s called as an
Inter-segment Branch or a Far Branch.
o The destination location is from a different segment (it
can be any memory location within the entire memory
locations)
In both instructions the name of a memory address, called a
label is specified ins the instruction instead of address.
26
Addressing Modes in 8086
• Program Memory Addressing Modes
• For the control transfer instructions, the addressing modes depend upon
whether the destination location is within the same segment or in a different
one. It also depends upon the method of passing the destination address to the
processor
• Inter-Segment: Address to which the control is to be transferred is in a different
segment
30
Addressing Modes in 8086
• Program Memory Addressing Modes
Intra-Segment Indirect Addressing Mode
31
Addressing Modes in 8086
• Program Memory Addressing Modes
Inter-Segment Direct Addressing Mode
o The new branch location is specified directly in
the instruction
o Both CS and IP get new values, as this is an
inter-segment branch.
32
Addressing Modes in 8086
• Program Memory Addressing Modes
Inter-Segment Indirect Addressing Mode
o The new branch location is specified indirectly in
the instruction through a register or a memory
location ( in DS only)
o Both IP and CS get new values, as this is an inter-
segment branch
o Eg: JMP DWORD PTR [BX]; IP {DS:[BX], DS:[BX + 1]}
; CS {DS:[BX +2], DS:[BX + 3]}
33
Addressing Modes in 8086
• Stack Memory Addressing Modes
The STACK is a portion of read/write memory set
aside by the user for the purpose of storing
information temporarily.
When the information is written on the stack, the
operation is called PUSH.
When the information is read from stack, the
operation is called POP.
The Microprocessor stores the information,
much like stacking plates. If it is desired to take
out the first stacked plate, we will have to
remove all plates above the first plate in the
reverse order
34
Addressing Modes in 8086
• Stack Memory Addressing Modes
PUSH POP Operations…
36
Addressing Modes in 8086
• Stack Memory Addressing Modes
Function of PUSH and POP operations…
o Temporarily stores the contents of 16-bit register
or memory location or program status word, and
retrieves when required.
o When the programmer realizes the shortage of the
registers, he stores the present contents of the
registers in the stack with the help of PUSH
instruction and then uses these registers for other
functions.
o After completion of other function, programmer
loads the previous contents of the register from
the stack with the help of POP Instruction. 37
Addressing Modes in 8086
• Stack Memory Addressing Modes
PUSH Operation…
o The PUSH instruction decrements stack pointer by
two and copies a word from some source to the
location in the stack where the stack pointer
points. ( here the source must be a word = 16-bit)
o The source of the word can be a general purpose
register, a segment register, or memory.
o The next slide shows the map of the stack before
and after execution of PUSH AX and PUSH CX
Instructions.
38
Addressing Modes in 8086
• Stack Memory Addressing Modes
PUSH Operation…
o Higher order byte of the source operand stored @ SP-1
o Lower order byte of the source operand stored @ SP-2
Where SP is stack Pointer, and lets assume stack was empty
39
Addressing Modes in 8086
• Stack Memory Addressing Modes
POP Operation…
o The POP instruction copies a word from some the
stack location pointed to by the stack pointer (SP)
to the destination.
o The destination can be a general purpose register,
a segment register, or memory.
o After the word is copied to the specified
destination, the stack pointer is automatically
incremented by 2.
o The next slide shows the map of the stack before
and after execution of POPH DX and POP BX
Instructions. 40
Addressing Modes in 8086
• Stack Memory Addressing Modes
POP Operation…
o The byte @ SP is transferred to lower order byte position.
o Then, the byte @ SP+1 will be transferred to higher order
byte position. Then again SP will be incremented again.
43
INSTRUCTIO
N SET of
8086 44
Instruction set basics
45
Instruction set basics
46
Instruction set basics
• Instruction:- An instruction is a binary pattern designed
inside a microprocessor to perform a specific function.
47
Instruction set basics
• A program consists of a number of CPU instructions.
• Each instruction consist of an
o instruction code (Opcode) and
o one or more operands (data which the instruction manipulates).
• 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.
o E.g. 08 is the opcode for instruction “MOV X,Y”.
• Operand:- We can also say it as data on which operation should act.
o operands may be register values or memory values.
o The CPU executes the instructions using information present in this field.
It may be 8-bit data or 16-bit data.
• The instruction code specifies to the CPU what to do, where the data
is located and where the output data will be put. 48
Types of instruction set of 8086
microprocessor
1. Data Transfer/Copy Instructions
2. Arithmetic Instructions
3. Bit Manipulation Instructions
I. Logical Instructions
II. Shift & Rotate Instructions
4. Control Transfer (Branch) Instructions
5. Iteration Control (Loop) Instructions
6. Flag Manipulation & Machine (Processor) Control
Instructions
I. Flag Manipulation Instructions
II. Processor Control Instructions
7. String Instructions
8. Interrupt Instructions 49
(1). Data copy/transfer instructions.
Instructions that are used to copy/transfer data/
address in to registers, memory locations and I/O
ports.
Generally involve two operands: Source operand
and Destination operand of the same size.
Source: Register or a memory location or an
immediate data
Destination : Register or a memory location.
The size should be a either a byte or a word.
A 8-bit data can only be moved to 8-bit register/
memory and a 16-bit data can be moved to 16-bit
register/ memory.
50
(1). Data copy/transfer instructions.
Mnemonics:
MOV, PUSH, POP, XCHG, IN, OUT, XLAT, LEA, LDS,
LES, LAHE, SAHE, PUSHF, POPF
Instruction to transfer word:
MOV, PUSH, POP, XCHG, XLAT,
53
(1). Data copy/transfer instructions.
(1). MOV Destination, Source
54
BEFORE AFTER
EXECUTION EXECUTION
AX 2000H MOV BX,AX BX 2000H
BEFORE AFTER
EXECUTION EXECUTION
A AL M A AL
H
MOV CL,M H
B BL
H B BL
40 40
C CL H
H
C CL 40
D DL
H H 55
Stack Organization
Holds data temporarily and stores return addresses used
by procedures
It is Last IN First Out (LIFO) buffer
Describes the way data are stored and removed
from the stack
Data are placed on the stack with a PUSH
instruction; removed with a POP instruction.
• SS:SP points to top of the stack.
• Last filled location in the stack.
• Expands down.
• Fills from higher to lower addresses
56
Stack Pointer
It is a 16-bit register, contains the address of the
data item currently on top of the stack.
Stack operation includes pushing (providing)
data on to the stack and popping (taking) data
from the stack.
Pushing operation decrements stack pointer
and Popping operation increments stack
pointer. i.e. there is a last in first out (LIFO)
operation.
57
(2). Push Source
Source can be register, segment register or memory.
This instruction pushes the contents of specified source on
to the stack.
In this stack pointer is decremented by 2, i.e.,
o The higher byte data is pushed first into (SP-1).
o Then lower byte data is pushed into (SP-2).
o Then, SP SP-2
58
(2). Push Source
59
(2). Push Source
60
(2). Push Source
Let
And
DS = 1122 H
assume
ES = 3344 H
the stack
Push DS Push ES
61
INITIAL POSITION
(1) STACK
POINTER
DECREMENTS SP & STORES HIGHER
BYTE
62
BEFORE EXECUTION
SP 2002H
2000H
BH BL
2001H
CH 10 CL 50
DH DL
2002H
PUSH CX
AFTER EXECUTION
2000H 50
SP 2000H
BH BL
2001H 10
CH 10 CL 50
DH DL 2002H
63
(3) POP Destination
Destination can be register, segment register or memory.
This instruction pops (takes) the contents of specified
destination.
o In this stack pointer is incremented by 2, this
means
The lower byte data (the value @ SP) is popped first.
Then higher byte data (the value @ SP+1) is popped
next.
Then, SP SP+2
64
(3) POP Destination
65
(3) POP Destination
66
(3) POP Destination
Assume we have a
given stack
67
INITIAL POSITION AND READS LOWER
BYTE
INCREMENTS SP
LOWER BYTE
HIGHER BYTE
(3) STACK POINTER
68
BEFORE EXECUTION
2000H 30
SP 2000H
2001H 50
BH BL
2002H
POP BX
AFTER EXECUTION
2000H 30
SP 2002H 2001H 50
BH 5 BL 30 2002H
0
69
(4). XCHG Destination, source;
This instruction exchanges contents of Source
with destination.
Source can be register or memory location.
Destination can be memory location or register
However, exchange of data contents of two
memory locations is not permitted.
70
(4). XCHG Destination, source;
71
BEFORE EXECUTION AFTER EXECUTION
AH 20 AL 40 AH 70 AL 80
BH 70 BL 80 BH 20 BL 40
XCHG AX,BX
72
(5)IN AL/AX, 8-bit/16-bit port address
Used to read a byte or word from the provided
port to the accumulator.
It reads data from the specified port address.
It copies data to accumulator from a port with 8-
bit or 16-bit address.
Direct Addressing modes Mode:
In this mode, 8-bit address of the port is a part
of the instruction
73
(5)IN AL/AX, 8-bit/16-bit port address
Indirect Addressing modes Mode:
In this addressing mode, the address of the port is
referred from DX register.
o DX is the only register allowed to carry port
address.
Since DX is 16 bit register, the port address can be
any number between 0000H to FFFFH.
o It is possible to address up to 65,536 ports in this
mode.
74
(5)IN AL/AX, 8-bit/16-bit port address
75
BEFORE EXECUTION
PORT 10 AL
80H
IN AL,80H
AFTER EXECUTION
PORT 10 AL 10
80H
76
OUT 8-bit/16-bit port address, AL/AX
Used to send out a byte or word from the accumulator to the
provided port
It writes a byte from AL or word from AX to the specified
port address.
It copies contents of accumulator to the port with 8-bit or
16-bit address.
Direct Addressing Mode:
In this addressing mode, 8-bit address of the port is a
part of the instruction.
77
OUT 8-bit/16-bit port address, AL/AX
Indirect Addressing modes Mode:
In this addressing mode, the address of the port is
referred from DX register.
o DX is the only register allowed to carry port
address.
Since DX is 16 bit register, the port address can be
any number between 0000H to FFFFH.
o It is possible to access up to ports in this mode.
78
OUT 8-bit/16-bit port address, AL/AX
79
BEFORE EXECUTION
PORT 10 AL 40
50H
OUT 50H,AL
AFTER EXECUTION
PORT 40 AL 40
50H
80
(7) XLAT
Also known as translate instruction.
Used to translate a byte in AL using a table in the
memory.
E.g. XLAT; Translate a byte in AL
This instruction replaces a byte in AL register with a
byte from a lookup table in memory
BX register stores the offset of the starting address of
the lookup table and AL register stores the byte
number from the lookup table.
This instruction copies byte from address pointed by
[BX+AL] back into AL.
81
(7) XLAT
Also known as translate instruction.
Used to translate a byte in AL using a table in the
memory.
It is used to find out codes in case of code conversion.
i.e. it translates code of the key pressed to
the corresponding 7-segment code.
After execution this instruction contents of AL register
always gets replaced.
E.g. XLAT;
82
8. LEA 16-bit register (dest.), address (source)
83
(9). LDS 16-bit register (source), address (dest.);
LDS Also known as Load Data Segment (LDS).
• Used to load DS register and other provided register
with words from memory.
• LDS REG, Mem
This instruction loads new values into the specified register,
REG, and into the DS register from four successive memory
locations.
o This instruction copies words from two memory locations into
the register, REG, specified in the instruction.
o It then copies a word form the next two memory locations into
the DS register.
• E.g.
84
(10). LES 16-bit register (source), address (dest.);
LES Also known as Load Extra Segment (LES).
• Used to load ES register and other provided register
with words from memory.
• LES REG, Mem
This instruction loads new values into the specified register,
REG, and into the ES register from four successive memory
locations.
o The word from the first two memory locations is copied into the
specified register, REG, specified in the instruction.
o It then copies a word from the next two memory location is
copied into ES register.
• E.g.
85
LEA, LES, LDS
86
(1) LDS BX,5000H;
(2) LES BX,5000H;
15 0 7 0
BX 20 10 10 5000H
20
5001H
30
DS/ES 40 30 5002H
40 5003H
87
(11). LAHF
• Load lower byte of flag register in AH
This instruction loads the AH register from the
contents of lower byte of the flag register.
• This command is used to observe the status of the
all conditional flags of flag register.
(12). SAHF
89
(2). Arithmetic Instructions
These instructions perform the
operations like:
Addition,
Subtraction,
Increment,
Decrement,
Negation,
Comparison,
Multiplication,
Division,
BCD Arithmetic
ASCII Arithmetic 90
(2). Arithmetic Instructions
(1). ADD destination, source;
This instruction adds the contents of source operand with the
contents of destination operand.
The source may be immediate data, memory location or register.
The destination may be memory location or register.
However, both the source and destination operands cannot be memory
operands.
That means memory to memory addition is not possible.
• Also the contents of the segment registers cannot be added using this
instruction
The result is stored in destination operand.
AX is the default destination register.
Affected Flags: AF, CF, OF, PF, SF, ZF
AH 30 AL 30
AH 10 AL 10 ADD AX,2020H
1010
+2020
3030
AH 10 AL 10 AH 30 AL 30
ADD AX,BX
BH 20 BL 20 BH 20 BL 20
93
ADC destination, source
This instruction adds the contents of source operand with the
contents of destination operand and the carry flag bit (which may
be set as a result of the previous calculations) .
95
ADD destination, source
ADC destination, source
96
ADD destination, source
ADC destination, source
97
(3) INC source
This instruction increases the contents of source operand by 1.
The source may be memory location or register.
The source can not be immediate data.
The result is stored in the same place.
Flags Affected: AF, OF, PF, SF, ZF
Note: CF is not affected
If contents of 8-bit register are FFH and 16-bit register are
FFFFH, after INC instruction contents of registers will be zero
without affecting carry flag.
98
(3) INC source
99
BEFORE EXECUTION AFTER EXECUTION
AH 10 AL 11 INC AX AH 10 AL 12
100
4. DEC source
This instruction decreases the contents of source operand
by 1.
The source may be memory location or register.
The source can not be immediate data.
The result is stored in the same place.
Flags Affected: AF, OF, PF, SF, ZF
Note: CF is not affected
If the contents of 8-bit register are 00H and 16-bit register
are 0000H, after DEC instruction contents of register will
be FFH and FFFFH respectively without affecting carry
flag.
101
4. DEC source
102
BEFORE EXECUTION AFTER EXECUTION
AH 10 AL 11 DEC AX AH 10 AL 10
103
5. NEG source
This instruction replaces the number in a
destination with 2’s complement of that
number.
This instruction can be implemented by
inverting each bit and adding 1(one) to it.
The source may be memory location or
register.
The source can not be immediate data.
The result is stored in the same place.
Flags Affected: AF, CF, OF, PF, SF, ZF
104
(6) 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.
Flags Affected: AF, CF, OF, PF, SF, ZF
106
BEFORE EXECUTION AFTER EXECUTION
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
107
(7). SBB destination, source;
Also known as Subtract with Borrow.
This instruction subtracts the contents of the source
operand & the borrow flag (CF) which may reflect the
result of the previous calculations, from contents of
destination operand.
Subtraction with borrow, here means subtracting 1
from the subtraction obtained by SUB, if carry
(borrow) flag is set.
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.
E.g. (1). SBB AX,1000H;
(2). SBB AX,BX;
108
(7). SBB destination, source;
109
BEFORE EXECUTION AFTER EXECUTION
B 1 SBB AX,1000H
AH 20 AL 20 AH 10 AL 1F
2020
- 1000
1020 -1 = 101F
BEFORE EXECUTION AFTER EXECUTION
B 1
AH 20 AL 20 AH 10 AL 1F
SBB AX,BX
BH 10 BL 10 BH 10 BL 10
110
(6) SUB destination, source;
(7). SBB destination, source;
111
(6) SUB destination, source;
(7). SBB destination, source;
112
(8). CMP destination, source
This instruction compares the contents of source
operand with the contents of destination operands.
For comparison, it subtracts the source operand from
the destination operand but does not store the result
anywhere.
The source may be immediate data, memory
location or register.
The destination may be memory location or
register.
The source and destination both can’t be memory
location.
113
(8). CMP destination, source
The flags are affected depending upon the result of the
subtraction:
If both of the operands are equal, zero flag is set.
If the source operand is greater than the destination
operand, carry flag is set or else, carry flag is reset.
Then resulting carry & zero flag will be set or reset.
Example:
114
(8). CMP destination, source
115
(8). CMP destination, source
116
(8). CMP destination, source
117
D=S: CY=0,Z=1
BEFORE EXECUTION D>S:
CY=0,Z=0 AFTER EXECUTION
D<S: CY=1,Z=0
AH 10 AL 00
CMP AX,BX CY 0 Z 1
BH 10 BL 00
118
BCD Numbers
Binary-Coded Decimal(BCD): is a class of binary encodings of decimals
numbers where each decimal digit is represented by a fixed number of
bits, usually four or eight.
As most computers deal with data in 8-bit bytes, it’s possible to use
one of the following methods to encode a BCD number.
o Unpacked: Each numeral is encoded into one byte, with four
bits representing the numeral and the remaining bits having no
significance.
o Packed: Two numerals are encoded into a single byte, with one
numeral in the least significant nibble ( bits 0 through 3) and the
other numeral in the most significant nibble ( bits 4 through 7).
119 119
BCD Examples
Binary-Coded Decimal(BCD)
o Decimal 9 1
o Binary 0000 1001 0000 0001
Using Packed BCD, the same number would fit into a single
byte.
o Decimal 9 1
o Binary 1001 0001
120 120
(9) BCD Arithmetic
The 8086 provides two instructions to support BCD arithmetic.
It is used to make sure that the result of adding two BCD
numbers is adjusted to be a correct BCD number.
It only works on AL register.
124 124
(9) BCD Arithmetic… cont’d
DAS Instruction works as follows
127 127
ASCII
AAA (ASCII Adjust after Addition):
The data entered from the terminal is in ASCII
format.
In ASCII, 0 – 9 are represented by 30H – 39H.
This instruction allows us to add the ASCII
codes.
This instruction does not have any operand.
129 129
(10) ASCII Arithmetic … cont’d
AAA (ASCII Adjust after Addition):
130 130
(10) ASCII Arithmetic … cont’d
AAS (ASCII Adjust after Subtract):
The data entered from the terminal is in ASCII
format.
In ASCII, 0 – 9 are represented by 30H – 39H.
This instruction allows us to add the ASCII
codes.
This instruction does not have any operand.
131 131
(10) ASCII Arithmetic … cont’d
AAS (ASCII Adjust after Subtract):
The data entered from the terminal is in ASCII
format.
In ASCII, 0 – 9 are represented by 30H – 39H.
This instruction allows us to add the ASCII
codes.
This instruction does not have any operand.
132 132
(10) ASCII Arithmetic… cont’d
AAM (ASCII Adjust after
Multiplication):
The data entered from the terminal is in ASCII
format.
In ASCII, 0 – 9 are represented by 30H – 39H.
This instruction allows us to add the ASCII
codes.
This instruction does not have any operand.
133 133
(10) ASCII Arithmetic… cont’d
AAM (ASCII Adjust after
Multiplication):
134 134
(10) ASCII Arithmetic… cont’d
AAD (ASCII Adjust before Division):
135 135
(10) ASCII Arithmetic… cont’d
AAD (ASCII Adjust before Division):
136 136
Multiplication
This group of instructions consists of
following group of instructions
MUL: Unsigned Multiplication
IMUL: Signed Multiplication
137
MUL Instruction
Syntax: MUL source
This instruction multiplies an unsigned byte from source
and an unsigned byte in AL register, or an unsigned word
from source and an unsigned word in AX register.
The source can be a register or a memory location.
When the byte is multiplied by the contents of AL, the
result is stored in AX.
o The most significant byte is stored in AH and the least
significant byte is stored in AL.
When a word is multiplied by the contents of AX, the most
significant word of the result is stored in DX and least
significant word of result is stored in AX.
E.g.
(2). MUL BH // AX= AL*BH;
(3). MUL CX //DX AX=AX*CX;
(4). MUL WORD PTR [BX} //DXAX = AX times word in
//DS pointed by [BX]
• If the upper byte of a 16-bit result or the upper word of a 32-bit result contains only
copies of the sign bit ( all 0’s or all 1’s), then the CF and the OF will be both 0’s.
141
• The AF, PF, SF and ZF flags are all undefined after IMUL.
Division Instruction
This group of instructions consists of
following group of instructions
DIV: Unsigned Division
IDIV: Signed Division
142
DIV Instruction… cont’d
Syntax: DIV source
This is used to divide an unsigned word by a byte
or to divide an unsinged double word by a word.
CASE ONE:
o When dividing a word by a byte, the word
must be in AX register.
After division, AL will contain an 8-bit quotient
and AH will contain an 8-bit remainder.
If an attempt is made to divide by 0 or the quotient
is too large to fit in AL register (greater than FFH),
the 8086 will automatically execute a type 0
interrupt.
143
DIV Instruction… cont’d
Syntax: DIV source
CASE TWO:
o When a double word is divided by a word, the
most significant word of the double word must be
in DX and the least-significant word of the double
word must be in AX.
After division, AX will contain a 16-bit quotient
and DX will contain a 16-bit remainder.
If an attempt is made to divide by 0 or the quotient
is too large to fit in AX register (greater than
FFFFH), the 8086 will automatically execute a type
0 interrupt.
o The source can be a register or a memory location.
144
DIV Instruction… cont’d
Syntax: DIV source
To divide a byte by a byte
o It is necessary to put the dividend byte in
AL and fill AH with all 0’s.
To divide a word by a word
o It is necessary to put the dividend word in
AX and fill DX with all 0’s
145
DIV Instruction… cont’d
Syntax: DIV source
To divide a byte by a byte
o It is necessary to put the dividend byte in
AL and fill AH with all 0’s.
To divide a word by a word
o It is necessary to put the dividend word in
AX and fill DX with all 0’s
E.g.
MOV AX, 0203 // AX=0203
MOV BL, 04 // BL=04
DIV BL // AL=0203/04=50 (i.e. AL=50 & AH=03)
146
DIV Instruction… cont’d
147
IDIV Instruction
148
IDIV Instruction … cont’d
149
Multiplication and Division Examples
150
BEFORE EXECUTION
AH 00 AL 05
BH 00 BL 03
CH CL
AH 00 AL 0F
BH BL
CH CL
DH 00 DL 00
151
BEFORE EXECUTION
AH 00 AL 0F
BH 00 BL 02
CH CL AX=Quotient {0007}
DX=Reminder {0001}
DIV BX 000F =7 1
AFTER EXECUTION 0002 2
AH 00 AL 07
BH BL
CH CL
DH 00 DL 01 152
153
154
155
Bit Manipulation Instructions
These instructions are used to perform operations where
data bits are involved.
These instructions are used at the bit level.
These instructions are grouped into:
Logical Instructions
Shift & Rotate Instructions
Logical Instructions
Instructions to perform Logical operations
o NOT
o AND
o OR
o XOR
o TEST
156
Bit Manipulation Instructions
Shift and Rotate Instructions
Instructions to perform Shift operations
o SHL… Shift Left
o SAL… Shift Arithmetic Left
o SHR… Shift Right
o SAR… Shift Arithmetic Right
Instructions to perform Rotate operations
o ROL… Rotate Left
o ROR… Rotate Right
o RCL… Rotate Left through Carry flag
o RCR… Rotate Right through Carry flag
157
AND
Syntax: AND destination, source
Used for adding each bit in a s source byte/word with the
corresponding bit in a destination byte/word.
Stores result in the destination
The source may be an immediate number, a register, or a
memory location
The destination may be a register or a memory location
The source and destination both can’t be memory
locations.
AND operation with two inputs produces result logic 1 only
when both inputs are logic 1.
The CF and OF are both 0 after AND, the PF, SF, and ZF
are affected, AF is undefined.
158
AND
AND A, DATA
AND AL, DATA8 (AL) (AL) & DATA8
160
OR
Syntax: OR destination, source
Used to multiply each bit in a source byte/word with the
corresponding bit in a destination byte/word.
Stores result in the destination
The source may be an immediate number, a register, or a
memory location
The destination may be a register or a memory location
The source and destination both can’t be memory
locations.
OR operation with two inputs produces result logic 0 only
when both inputs are logic 0.
The CF and OF are both 0 after OR, the PF, SF, and ZF are
affected, AF is undefined.
161
OR
OR A, DATA
163
XOR
Syntax: XOR destination, source
Used to perform Exclusive-OR operation over each bit in a
source byte/word with the corresponding bit in a
destination byte/word.
Stores result in the destination
The source may be an immediate number, a register, or a
memory location
The destination may be a register or a memory location
The source and destination both can’t be memory locations.
XOR operation with two inputs produces result logic 0 only
when both inputs are logic 0.
The CF and OF are both 0 after XOR, the PF, SF, and ZF
are affected, AF is undefined.
164
XOR
XOR A, DATA
166
NOT instruction
Syntax: NOT destination
167
TEST and Bit Test Instruction
Syntax: TEST destination, source
169
TEST and Bit Test Instruction
Flags after TEST operation
PF, SF and ZF will be updating to show the results of
the ANDing.
PF has meaning only for the lower 8-bits of the
destination.
AF will be undefined.
• OR
– Used in setting certain bits
xxxx xxxx OR 0000 1111 = xxxx 1111
(Set the upper four bits)
172 172
XOR
– Used in Inverting bits
173 173
BEFORE EXECUTION AFTER EXECUTION
AH FF AL AH 11 AL 11
FF AND AX,BXH
BH 11 BL 11
BH 11 BL 11
174
BEFORE EXECUTION AFTER EXECUTION
AH FF AL FF AH FF AL FF
OR AX,BXH
BH 11 BL 11 BH 11 BL 11
175
BEFORE EXECUTION AFTER EXECUTION
AH FF AL AH EE AL EE
FF XOR AX,BXH
BH 11 BL 11
BH 11 BL 11
176
BEFORE EXECUTION AFTER EXECUTION
AH 00 AL 00
AH FF AL FF NOT AXH
177
Shift & Rotate Instructions
Shift Instructions
These instructions position or move binary data to
the left or right by shifting them within the register or
memory location.
They also perform multiplication by powers of (left
shift) and division by powers of (right shift)
The shift operations can be classified as:
o Logical shifts
o Arithmetic shifts
The Logical Shifts move a 0 into the rightmost bit position
for a logical left shift (SHL) and 0 into the leftmost bit
position for a logical right shift (SHR).
178
Shift & Rotate Instructions
Shift Instructions … continued
The Logical Left Shift (SHL) and Arithmetic Logical Shift (SAL)
operations are identical.
However, Logical Right Shift (SHR) and Arithmetic Right Shift ( SAR)
operations are different, because;
o SAR copies the sign bit through the number, while SHR copies a 0
through the number.
Logical Shift operations are used with unsigned numbers; they perform
multiplication or division of unsigned numbers.
On the other hand, Arithmetic Shift operations are used with signed
numbers; they perform multiplication or division of signed numbers.
179
Shift & Rotate Instructions
Shift Instructions … continued
180
SHL/ SAL
Syntax: SHL/SAL destination, count
SHL and SAL are two mnemonics for the same instruction.
This instruction shifts each bit in the specified destination to the left and
0 is stored at LSB position.
The MSB is shifted into the carry flag.
The destination can be a byte or a word in a register or memory
The number of shifts are indicated by count.
If the number of shifts are greater than 1, then shift count must be
loaded in CL register and CL must be placed in the count position of the
instruction.
FLAGS: All flags are affected.
181
SHL/ SAL
Diagram bellow shows SHL/SAL instruction for byte operation
182
SHL Instruction
The SHL (shift left) instruction performs a logical
left shift on the destination operand, filling the
lowest bit with 0.
0
CF
mov dl,5d
shl dl,1
183 183
SHL
184
SHR Instruction
Syntax: SHR destination, count
This instruction shifts each bit in the specified destination to the right
and 0 is stored at MSB position.
The LSB is shifted into the carry flag.
The destination can be a byte or a word in a register or in a memory
location.
The number of shifts are indicated by count.
If the number of shifts are greater than 1, then shift count must be
loaded in CL register and CL must be placed in the count position of the
instruction.
FLAGS: All flags are affected.
185
SHR Instruction
Diagram blow shows SHR instruction for byte operation.
186
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
MOV DL,80d
SHR DL,1 ; DL = 40
SHR DL,2 ; DL = 10
187 187
SHR
188
SAR Instruction
Syntax: SAR destination, count
This instruction shifts each bit in the specified destination some number
of bit positions to the right.
As a bit is shifted out of the MSB position, a copy of the old MSB is put
in the MSB position.
The LSB will be shifted into CF
In case of multiple shifts , CF will contain the bit most recently shifted in
from the LSB.
Bits Shifted into CF previously will be lost.
The destination cab be a byte /word in a register or in a memory.
189
SAR Instruction
Syntax: SAR destination, count
The number of shifts are indicated by count.
If number of shifts required is one, you can place one (1) in the count
position.
If the number of shifts are greater than 1, then shift count must be
loaded in CL register and CL must be placed in the count position of the
instruction.
FLAGS: All flags are affected.
190
SAR Instruction
Diagram blow shows SAR instruction for byte operation.
191
SAR
Instruction
SAR (shift arithmetic right) performs a
right arithmetic shift on the destination
operand.
CF
For example, 80 / 23 = 10
After: 00001010 = 10
shl dl,1
193 193
Shift & Rotate Instructions
Rotate Instructions
These instructions position or move binary data by rotating the
information in a register or memory location, either from one end to
another or through the carry flag.
194
ROL Instruction
Syntax: ROL destination, count
This instruction rotates all bits in a specified byte or word to the left some
number of bit positions.
MSB is placed as new LSB and a new CF.
The destination can be a byte or a word in a register or in a memory location.
The number of shifts are indicated by count.
If number of shifts required is one, you can place one (1) in the count position.
If the number of shifts are greater than 1, then shift count must be loaded in CL
register and CL must be placed in the count position of the instruction.
195
ROL Instruction
Diagram blow shows ROL instruction for byte operation.
196
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
197 197
ROR Instruction
Syntax: ROR destination, count
This instruction rotates all bits in a specified byte or word to the right some
number of bit positions.
LSB is placed as new MSB and a new CF.
The destination can be a byte or a word in a register or in a memory location.
The number of shifts are indicated by count.
If number of shifts required is one, you can place one (1) in the count position.
If the number of shifts are greater than 1, then shift count must be loaded in CL
register and CL must be placed in the count position of the instruction.
198
ROR Instruction
Diagram blow shows ROR instruction for byte operation.
199
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
C
F
MOV AL,11110000b
ROR AL,1 ; AL = 01111000b
MOV DL,3Fh
ROR DL,4 ; DL = F3h
200 200
RCL Instruction
Syntax: RCL destination, count
This instruction rotates all bits in a specified byte or word some number of bit
positions to the left along with the carry flag .
MSB is placed as new Carry and previous Carry is placed as a new LSB. The
destination can be a byte or a word in a register or in a memory location.
The number of shifts are indicated by count.
If number of shifts required is one, you can place one (1) in the count position.
If the number of shifts are greater than 1, then shift count must be loaded in CL
register and CL must be placed in the count position of the instruction.
201
RCL Instruction
Diagram blow shows RCL instruction for byte operation.
202
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 ; CF = 0
MOV BL,88H ; CF,BL = 0 10001000b
RCL BL,1 ; CF,BL = 1 00010000b
RCL BL,1 ; CF,BL = 0 00100001b
203 203
RCL
Instruction
204 204
RCR Instruction
Syntax: RCR destination, count
This instruction rotates all bits in a specified byte or word some number of bit
positions to the right along with the carry flag .
LSB is placed as new Carry and previous Carry is placed as a new MSB.
The destination can be a byte or a word in a register or in a memory location.
The number of shifts are indicated by count.
If number of shifts required is one, you can place one (1) in the count position.
If the number of shifts are greater than 1, then shift count must be loaded in CL
register and CL must be placed in the count position of the instruction.
205
RCR Instruction
Diagram blow shows RCR instruction for byte operation.
206
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 ; CF = 1
MOV AH,10H ; CF,AH = 00010000 1
RCR AH,1 ; CF,AH = 10001000 0
207 207
RCR
Instruction
208
SHL Instruction
The SHL (shift left) instruction performs a logical
left shift on the destination operand, filling the
lowest bit with 0.
0
CF
BEFORE
EXECUTION
0 0 0 0 0 1 0 1 =05H
CF
0 0 0 0 1 0 1 0 =0AH
AFTER 0
EXECUTION
209
SHR Instruction
0
CF
BEFORE
EXECUTION
0 0 0 0 0 1 0 1 =05H
CF
AFTER 0 0 0 0 0 0 1 0
EXECUTION
1
=02H
210
ROL Instruction
CF
BEFORE
EXECUTION 0 0 0 0 0 1 0 1 =05H
CF
AFTER 0 0 0 0 0 1 0 1 0 =0AH
EXECUTION
211
ROR Instruction
CF
0 0 0 0 0 1 0 1 =05H
BEFORE
EXECUTION
CF
AFTER 1 0 0 0 0 0 1 0 1 =82H
EXECUTION
212
Branching Instructions
(or) Program execution
Transfer Instructions
These instructions cause change in the sequence of the
execution of instruction.
This change can be through a condition or
sometimes unconditional.
The conditions are represented by flags.
213
Program Control Transfer
(Branch) Instructions
Aka program execution transfer instructions.
These instructions transfer the flow of execution of the
program to a new address specified in the instruction
directly or in directly
Depending upon the addressing modes, the CS may or may
not be modified. These type of instructions are classified as:
I. Unconditional Transfer instructions – CALL, RET, JMP
II. Conditional Transfer Instructions – J cond.
In the assembly language programming, more than one
data/ code/ stack segments can be defined. But only one
segment of each type can be accessed at any time.
214
Program Control Transfer
(Branch) Instructions
• Depending upon the addressing modes, the CS may or may not
be modified. These type of instructions are classified as:
I. Unconditional Transfer instructions – CALL, RET, JMP
o In case of these instructions, the execution control is
transferred to the specified location independent of any
status or condition.
o The CS and IP are unconditionally modified to the new
CS and IP.
II. Conditional Transfer Instructions – J cond.
o In case of these instructions, the execution control is
transferred to the specified location provided the result of
the previous operation satisfies a particular condition,
otherwise, the execution continues in normal flow
sequence.
215
Program Control Transfer Instructions
CALL
Used to call a procedure and save their return
address to stack.
Procedure
o aka subroutine.
o is a group of instructions stored as a separate program
in the memory and it is called/invoked from the main
program whenever required.
• Types of Procedure
depends on where the procedure is stored in the main
memory.
Near Procedure & Far Procedure
o If it is in the same code segment where the main program is
stored it is referred to as near procedure, otherwise it is
called far procedure. 216
Program Control Transfer Instructions
• CALL
• Types of Procedures
I. Near Procedure (i.e., 32 KB displacement)
o for near procedures, CALL instruction pushes only
the IP register contents on the stack, since CS register
contents remains unchanged for main program and
procedure.
II. Far Procedure (any where outside the current segment)
o But for far procedures, CALL instruction pushes IP
and CS on the stack.
• The modes for them are respectively called as intrasegment
and intersegment addressing modes.
217
Program Control Transfer Instructions
• CALL
• Types of CALLs
I. Near CALL
o is a call to a procedure which is in the same code
segment as the CALL instruction.
o When a near call is being executed, the SP is
decremented by 2 and then the address (offset address
on IP) of the next instruction after the CALL
instruction, is copied onto the stack.
o IP is then get loaded with the offset of the first
instruction of the procedure in same segment.
II. Far CALL
o is a call to a procedure which is in a different segment
from that which contains the CALL instruction.
218
Program Control Transfer Instructions
• CALL
• Types of CALLs
I. Near CALL
II. Far CALL
o When a far call is being executed; SP will be decremented
by 2 and then the contents of the CS register will be copied
onto the stack.
o SP will be decremented by 2 again, and then the offset of
the instruction after CALL instruction will be copied on to
the stack.
o Finally, CS will be get loaded with the base of the
destination segment which contains the procedure and IP
will be get loaded with offset of the first instruction of the
procedure in the destination segment.
219
Program Control Transfer Instructions
• CALL Instruction
220
Program Control Transfer Instructions
• RET Instruction:
RET: Return from the procedure/subroutine.
will return execution from a procedure to the next
instruction after the CALL instruction in the calling
program.
If the procedure is a near procedure, then the return will
be done by replacing the IP register with a word from the
top of the stack.
If the procedure is a far procedure;
o then IP register will be loaded with the word at the top of
the stack.
o The SP will then be incremented by two.
o The CS register is then get loaded with a word form the
new top of the stack. Then SP is again incremented by 2.
No flags are affected with RET instruction.
221
SUBROUTINE & SUBROUTINE HANDILING
INSTRUCTIONS
Main
program
Subroutine
A
First Instruction
Call subroutine A
Next instruction
Return
Call subroutine A
Next instruction
222
Program Control Transfer Instructions
• JMP Instruction
This instruction causes an instruction is to be
fetched from the location specified in the JMP
instruction rather than from the next location
after the JMP instruction.
The JMP instructions are basically classified as
unconditional jump and conditional jump instructions.
A Conditional Jump instruction allows the programmer to
make decisions based up on numerical tests.
The results of the numerical tests are held in the flag bits,
which are then tested by conditional jump instructions.
223
Program Control Transfer Instructions
• JMP Instruction
The JMP instructions are further classified as short, near, and far
jump instructions.
Short JUMP
o Allows jumps or branches to memory locations within +127 and -
128 bytes from the address of the instruction following the jump.
NEAR JUMP
o Allows a jump or branch to memory locations within ±32 Kbytes (or
anywhere in the current code segment) from the address of the
instruction following the jump.
o Only IP is changed, the contents of the CS remains the same.
Far JUMP
o Allows a jump or branch to any memory location within the real
memory system.
o Destination location is from a different segment.
o Both IP and CS changed.
224
Program Control Transfer Instructions
• JMP Instruction
The SHORT and NEAR jumps are often called
intrasegment jumps, and the FAR jumps are often called
intersegment jumps.
NEAR and FAR jumps are further described as either
direct or indirect.
o If the address for the jump is specified directly within the
instruction, then the jump is described as direct.
o If the destination address for the jump is contained in a
register, or memory location, the jump is referred as
indirect.
A positive displacement usually means jump is ‘ahead’ in
the program, and a negative displacement usually means
jump is ‘backward’ in the program.
225
Program Control Transfer Instructions
• JMP Instruction
Assume the following state of 8086: (CS)=2000H, (IP)=0300H, (SI)=A00H,
(DS)=400H, (DS:A00)=10H, (DS:A01)=B3H, (DS:A02)=22H, (DS:A03)=1AH.
To what address is program control passed if each of the following JMP
instruction is execute?
226
Program Control Transfer Instructions
• JMP Instruction
227
Program Control Transfer Instructions
• JMP Instruction
228
Program Control Transfer Instructions
• JMP Instruction
Conditional Jumps
are always short jumps in 8086.
These instructions will cause a jump to a label given in
the instruction if the desired condition(s) occurs in the
program before the execution of the instruction.
Test the following flag bits: SF, ZF, CF, PF and OF
o If the condition under test is true, a branch to the
label associated with jump instruction occurs.
o If the condition is false, execution simply goes on the
next instruction.
The destination must be in the range of -128 bytes to +127
bytes from the address of the instruction after the conditional
transfer instruction.
229
Program Control Transfer Instructions
• JMP Instruction
Conditional Jumps
230
Program Control Transfer Instructions
• JMP Instruction
Unsigned Conditional Jumps
231
Program Control Transfer Instructions
• JMP Instruction
Unsigned Conditional Jumps
232
Program Control Transfer Instructions
• JMP Instruction
Conditional Jumps
Example 2: Write a program to move a block of 100 consecutive bytes of data
starting at offset address 400H in memory to another block of memory
locations starting at offset address 600H. Assume both block at the same data
segment 3000H. Use conditional jump instruction.
Solution:
MOV AX, 3000H
MOV DS, AX DS=3000H
MOV SI, 0400H
ES=3000H
MOV DI, 0600H
MOV CX, 64H
NXT: MOV AH, [SI] SI=0400H
MOV [DI], AH DI=0600H
INC SI Counter=CX=64H=100byte
INC DI
DEC CX
JNZ NXT
HLT 233
Program Control Transfer Instructions
• JMP Instruction
Conditional Jumps
Example 3: Write a program to add (50)H numbers stored at memory
locations start at 4000:0100H , then store the result at address 200H in the
same data segment.
Direction Flag=0, CLD
Solution: DS=4000H
MOV AX , 4000H ES=4000
MOV DS , AX SI=0100H
MOV CX , 0050H DI= 0200
MOV SI, 0100H Counter=CX=50H
XOR AX, AX ; OR (MOV AL, 00H)
Again: ADD AX, [BX]
INC SI
DEC CX
JNZ Again
MOV [0200], AX ; OR (MOV [DI] , AX)
NB: Re-read: After CALL & JUMP instructions , Program Memory
234
addressing Modes.
Iteration Control (LOOP) Instructions
• These instructions are used to execute a series of
instructions some number of times.
• The number is specified in the CX register.
• The CX register is automatically decremented by one,
each time after the execution of loop instruction.
• Until CX = 0, execution will jump to a destination
specified by a label in the instruction.
• The destination address for the jump must be in the
range of -128 bytes to +127 bytes from the address of
the instruction after the iteration control instruction.
235
Iteration Control (LOOP) Instructions
• There is one condition for exit from loop, see table
• If the condition is false , execution simply goes onto the
next instruction after the iteration control instruction
236
Iteration Control (LOOP) Instructions
Example
Write a program to move a block of 100 consecutive bytes of data starting at
offset address 400H in memory to another block of memory locations starting
at offset address 600H. Assume both block at the same data segment 3000H.
Use LOOP instruction.
Solution:
MOV AX, 3000H
MOV DS, AX
MOV SI, 0400H DS=3000H
MOV DI, 0600H ES=3000H
MOV CX, 64H
NXT: MOV AH, [SI] SI=0400H
MOV [DI], AH DI=0600H
INC SI
Counter=CX=64H=100byte
INC DI
LOOP NXT
HLT
NB: Compare and contrast this program with the one on slide 233
237
STRING MANIPULATION INSTRUCTIONS
String : Sequence of bytes or words
is a series of the same type of data items in sequential memory locations
8086 instruction set includes instruction for string movement, comparison,
scan, load and store.
MOVS/MOVSB/MOVSW
LODS/LODSB/LODSW
STOS/STOSB/STOSW
CMPS/COMPSB/COMPSW
SCAS/SCASB/SCASW
239
STRING MANIPULATION INSTRUCTIONS
MOVS/MOVSB/MOVSW instruction
These instructions copy a byte or word form a location in
the data segment to a location in the extra segment
SI must contain the offset of the source byte or word in the
data segment
DI must contain the offset of the destination in the extra
segment.
For multiple byte or multiple word moves, the number of
elements to be moved is put in CX register. CX is function as
a counter.
After the byte or word is moved, SI and DI are
automatically adjusted to point to the next source and next
destination.
If DF = 0, SI and DI will be incremented by 1 after a byte
move, and they will be incremented by 2 after a word move.
240
STRING MANIPULATION INSTRUCTIONS
MOVS/MOVSB/MOVSW instruction
IfDF = I, SI and DI will be decremented by 1 after a byte
move, and they will be decremented by 2 after a word move.
MOVSB--- move string as bytes, MOVSW--- move string as
a word
MOVS affects no flag
241
STRING MANIPULATION INSTRUCTIONS
REP/REPE/REPZ/REPNE/REPNZ instruction
REP isa prefix which is written before one of the string
instructions to repeat execution of it until CX = 0
Mnemonic Meaning Condition for exit
REP Repeat CX=0
REPE/REPZ Repeat while equal/ CX= 0 or ZF = 0
Repeat while Zero
REPNE/ Repeat while not equal/ CX = 0 or ZF = 1
REPNZ Repeat while not zero
242
STRING MANIPULATION INSTRUCTIONS
LODS/LODSB/LODSW instruction
This instruction copies a byte from a string location
pointed to by SI to AL, a word from string location
pointed to by SI to AX
LODS does not affect any flag.
LODSB copies a byte, LODSW copies a word.
243
STRING MANIPULATION INSTRUCTIONS
STOS/STOSB/STOSW instruction
This instruction copies a byte from AL or a word from AX to
memory location in the extra segment.
DI is used to hold the offset of the memory location
After copy DI is automatically incremented or decremented
to point to the next string element in the memory.
If DF = 0, then DI will automatically be incremented by one
for a byte string or incremented by 2 for a word string.
244
STRING MANIPULATION INSTRUCTIONS
STOS/STOSB/STOSW instruction
IfDF = 01 then DI will automatically be decremented by one
for a byte string or decremented by 2 for a word string.
STOSB copies byte and STOSW copies a word.
STOS doesn’t affect any flag.
245
STRING MANIPULATION INSTRUCTIONS
CMPS/COMPSB/COMPSW instruction
Can be used to compare a byte in one string with a
byte in another string or a word in one string with a
word in another string.
SI is used to hold offset of a byte or word in the source
string
DI is used to hold the offset of a byte or word in the
destination string
The comparison is done by subtracting the byte or
word pointed to by DI from the byte or word pointed
to by SI
The AF, CF, OF, ZF, PF and SF flags are affected but
neither operand is affected
246
STRING MANIPULATION INSTRUCTIONS
CMPS/COMPSB/COMPSW instruction
After the comparison, SI and DI will be automatically incremented or
decremented according to DF. (If DF = 0, SI and DI otherwise )
CX function as a Counter which is decremented after each comparison is a
counter . This will go until CX = 0
247
STRING MANIPULATION INSTRUCTIONS
SCAS/SCASB/SCASW instruction
SCAS compares a string byte with a byte in AL or a string
word with word in AX.
The instruction affects the flags, but it does not change
either the operand in AL(AX) or the operand in the string.
The string to be scanned must be in the extra segment.
DI must contain the offset of the byte or word to be
compared
The AF, CF, OF, ZF, PF and SF flags are affected.
248
STRING MANIPULATION INSTRUCTIONS
CMPS/COMPSB/COMPSW instruction
After the comparison, SI and DI will be automatically incremented or
decremented according to DF. (If DF = 0, SI and DI otherwise )
CX function as a Counter which is decremented after each comparison is a
counter . This will go until CX = 0
249
REP
250
MOVS
251
MOVS
252
CMPS
253
SCAS
254
SCAS
255
LODS
256
STOS
257
1. Copying a string (MOV SB)
MOV CX,0003 copy 3 memory locations
MOV SI,1000
MOV DI,2000
L1 CLD
MOV SB
DEC CX decrement CX
JNZ L1
HLT
258
2. Find & Replace
259
REP (Repeat):
260 260
Flag Manipulation and Processor Control
Instructions
THESE INSTRUCTIONS CONTROL THE
FUNCTIONING OF THE AVAILABLE
HARDWARE INSIDE THE PROCESSOR CHIP
TWO TYPES:
I. FLAG MANIPULATION
II. MACHINE(PROCESSOR ) CONTROL
INSTRUCTIONS
261
Flag Manipulation and Processor Control
Instructions
• FLAG MANIPULATION Instructions
The flag manipulation instructions directly
modify some of the flags of 8086.
These instructions modify the carry,
direction and interrupt flags
262
Flag Manipulation and Processor Control
Instructions
• Machine Control Instructions
These instructions control the bus usage and
execution.
Don't require any operand
263
Flag Manipulation and Processor Control
Instructions
• Machine Control Instructions
264
Processor Control Instructions
These instructions control the processor itself.
265 265
Flag manipulation and Processor Control Instructions
266
STC
It sets the carry flag to 1.
CLC
267 267
STD:
It sets the direction flag to 1.
CLD:
It clears the direction flag to 0.
268 268
HLT instruction – HALT processing
The HLT instruction will cause the 8086 to stop fetching and
executing instructions.
NOP instruction
this instruction simply takes up three clock cycles and does
no processing.
LOCK instruction
this is a prefix to an instruction. This prefix makes sure that
during execution of the instruction, control of system bus is not
taken by other microprocessor.
WAIT instruction
this instruction takes 8086 to an idle condition. The
CPU will not do any processing during this.
269 269
Interrupts Instructions
INT instruction: INT type
This instruction causes the 8086 to call a far
procedure.
The term type in the instruction refers to a
number between 0-255 which identifies the
interrupt.
The address of the procedure is taken the
memory whose address is four times the type
270
number.
Interrupts Instructions
INT instruction: INT type
271
Interrupts Instructions
IRET instruction:
273
Interrupts Instructions
INTO instruction:
If the overflow flag (OF) is set, this instruction will
cause the 8086 to do an indirect far call to a procedure
you write to handle overflow condition.
To do call the 8086 will read a new value for IP from
address 00010H and a new values of CS from address
00012H.
274
INSTRUCTION SET-summary
1.DATA TRANSFER INSTRUCTIONS
Mnemonic Meaning Format Operation
POP pops the operand from top of POP BX Copy top of stack to 16
stack to Des. bit reg
sp=sp+2
ADC Add with carry ADC D,S (S)+(D)+(CF) (D) carry (CF)
170
277
3. Bit Manipulation Instructions(Logical Instructions)
3.2. Shift & Rotate Instructions
Mnemonic Meaning Format
SAL/SHL Shift arithmetic Left/ SAL/SHL D, Count
Shift Logical left
278
4. Branching or PROGRAM
EXECUTION TRANSFER INSTRUCTIONS
• CALL - call a subroutine
• RET - returns the control from procedure to calling
program
• JMP Des – Unconditional Jump
• Jxx Des – conditional Jump (ex: JC 8000)
• Loop Des
279
5. Iteration Control Instructions
• Loop Des
LOOP
LOOPE/LOOPZ
LOOPNE/LOOPNZ
280
6. STRING INSTRUCTIONS
• CMPS Des, Src - compares the string bytes
• SCAS String - scans a string
• MOVS / MOVSB / MOVSW - moving of byte or
word
• REP (Repeat) - repetition of the instruction
281
7. Flag Manipulations
and
PROCESSOR CONTROL INSTRUCTIONS
• STC – set the carry flag (CF=1)
• CLC – clear the carry flag (CF=0)
• STD – set the direction flag (DF=1)
• CLD – clear the direction flag (DF=0)
• HLT – stop fetching & execution
• NOP – no operation(no processing)
• LOCK - control of system bus is not taken by other µP
• WAIT - CPU will not do any processing
• ESC - µP does NOP or access a data from memory for
coprocessor 282
8. Interrupt Instructions
• INTN
• IRET
• INTO
283