Chapter 4
Chapter 4
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.
Machine language is the only language which a machine can understand. Instructions in this
language are written in binary bits as a specific bit pattern. The computer interprets this bit
pattern as an instruction to perform a particular task. The entire program is a sequence of
binary numbers. This is a machine-friendly language but not user friendly. Debugging is
another problem associated with machine language.
To overcome these problems, programmers develop another way in which instructions are
written in English alphabets. This new language is known as Assembly language. The
instructions in this language are termed mnemonics.
As microprocessor can only understand the machine language so mnemonics are translated
into machine language either manually or by a program known as assembler.
Efficient software development for the microprocessor requires a complete familiarity with the
instruction set, their format and addressing modes.
3
Instruction set basics
• Instruction:- An instruction is a binary pattern designed
inside a microprocessor to perform a specific function.
• The entire group of instructions that a microprocessor supports
is called Instruction Set.
• 8086 has more than 20,000 instructions.
5
Instruction set basics
• An instruction set is the set of instructions, and It is a part of
computer architecture.
• It specifies the operations, operands, and sequence for the
processing of instructions.
• Microprocessor understands only Machine language
o Cannot understand Assembly Language
• Assembler:- it converts the instruction into sequence of
binary bits, so that this bits can be read by the processor.
• Mnemonics:- these are the symbolic codes for either
instructions or commands to perform a particular
function.
o E.g. MOV, ADD, SUB etc.
6
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. B8 is the opcode for instruction “MOV AX, immediate ”.
• 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.
7
Types of instruction set of 8086 Microprocessor
The instruction set of 8086 is divided into 8 major groups.
1. Data Transfer/Copy Instructions
2. Arithmetic Instructions
3. Bit Manipulation Instructions
I. Logical Instructions
II. Shift & Rotate Instructions
4. Control Transfer (Branch) Instructions
Call, JMP, RET, J condition
5. Iteration Control (Loop) Instructions
6. Flag Manipulation & Machine (Processor) Control
Instructions
I. Flag Manipulation Instructions
II. Processor Control Instructions
7. String Instructions---LODS, MOVS, CMPS, SCAS, STOS,
REP
8
(1). Data movement instructions.
Study about Various instructions that are used for
transferring data within the 8086 microprocessor.
Instructions that are used to copy/transfer data/ address in to
registers, memory locations and I/O ports.
12
Push Source
Let
And
DS = 1122 H
assume
ES = 3344 H
the stack
Push DS Push ES
13
POP Destination---Syntax
This instruction copies/retrieves word contents from the stack
memory pointed by SP into the specified destination.
Destination can be register, segment register or memory.
Whenever data is removed from the stack the byte from the
stack segment memory location addressed by SP moves into
the MSB of the destination register and the byte from the stack
segment memory location addressed by SP+1 moves into the
LSB of the destination register.
o In this stack pointer is incremented by 2, this means
The lower byte data (the value @ SP) is popped first=MSB
Then higher byte data (the value @ SP+1) is popped next.=LSB
Then, SP SP+2
14
POP Destination
Assume we have a
given stack
CS [SS : SP]
SP SP + 2
15
PUSHF
16
3. XCHG Destination, Source
This instruction exchanges contents of Source with
destination.
The syntax of instructions is: XCHG Destination, Source
17
XCHG Destination, source;
XCHG AX,BX
BEFORE EXECUTION AFTER EXECUTION
AH 20 AL 40 AH 70 AL 80
BH 70 BL 80 BH 20 BL 40
18
(4)IN AL/AX, 8-bit/16-bit port address
This instruction is used to transfer data from the
input unit to accumulator.
Input/ reads a byte or word from port to AL or AX
19
5. OUT 8-bit/16-bit port address, AL/AX
This instruction is used to transfer data from accumulator to
the output unit.
Used to send out a byte or word from the accumulator to the
provided port
It writes a byte from AL or a word from AX to the specified
port address.
It copies contents of accumulator to the port with 8-bit or
16-bit address.
20
(6) XLAT: Translate
XLAT instruction transfer a byte from the memory location
into the AL register.
XLAT instruction replaces a byte in the AL register with a
byte from a lookup table in memory.
BX register stores the offset of 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.
It adds the contents of AL to BX to form a memory address
within the data segment. AL DS:[BX+AL]
It then copies the contents of this address into AL.
This is the only instruction that adds an 8-bit number to a l6-bit number.
21
It is an example of implied addressing mode
XLAT: Translate instruction is used for finding out the Codes in case of
code conversion problem using lookup table technique.
22
8. LEA 16-bit register, address (source)
23
(9). LDS Destination, source;
This instruction will load the register that is defined in the
instruction and the data segment (DS) from the source.
This instruction loads new values into the specified register,
REG, and into the DS register from four successive memory
locations. It loads the DS register with the content of memory
location specified as source in the instruction
o This instruction copies words from two memory locations into the
register, specified in the instruction.
o It then copies a word form the next two memory locations into the
DS register.
24
(10). LES 16-bit register (dest.), address (Source);
LES Also known as Load Extra Segment (LES).
• Used to load ES register and other provided register with words from
memory.
• LES REG, Mem
• It loads the ES register with the content of memory location
specified as source in the instruction
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 And the word from the next two memory location is copied into
ES register.
• E.g.
25
LEA, LES, LDS
These instructions load a segment register and general purpose
registers with an address directly from memory
The general forms of these registers
26
(11). LAHF- It stands for 'Load AH from Flag register'. This
instruction load the content of lower byte of the flag register into AH
register.
30
ADC destination, source
Mov AX, 12FFH
Mov BX, 0001H if you want to add this two 16 bit numbers as two 8
ADC AX, BX bit numbers independently, i.e. lower byte first and
then higher byte for some reason, we must use add with carry.
This is the carry before the instruction is placed.
First we must add the lower byte because the carry of lower byte
must be added in the higher byte.
So when we want to add the lower byte we use simple addition (ADD).
The higher byte is computed with ADC, because the carry of the
previous computation (lower byte) must be added.
We must know when to use ADD and ADC.
They have their own roles in assembly language programing.
Suppose the program is written for the 80286 to add the 32 bit numbers in the 8086
AX and BX registers? So we must add independently as 16 bits. 31
INC Destination
This instruction increases the contents of Destination operand by
1.
The Destination may be memory location or register.
The Destination can not be immediate data. Why?
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.
Mov AX, FFFFH Mov AL, FFH
INC AX INC AL
Why CF is not affected here??– Because the carry is preserved
across loop iterations & has impact only on Addition and subtraction
32
BEFORE EXECUTION AFTER EXECUTION
AH 10 AL 11 INC AX AH 10 AL 12
33
Subtraction instructions (Sub, SBB, DEC, CMP,
NEG)
35
e.g.
36
SUB destination, source;
SBB destination, source;
37
DEC destination
This instruction decreases the contents of destination operand
by 1.
The destination may be memory location or register.
The destination 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.
38
NEG destination
This instruction replaces the number in a
destination with 2’s complement of that number.
For obtaining 2’s complement it subtracts the
contents of destination from zero.
This instruction can be implemented by inverting
each bit and adding 1(one) to it.
The destination may be memory location or register.
The destination can not be immediate data.
The result is stored in the same place.
Flags Affected: AF, CF, OF, PF, SF, ZF
39
CMP destination, source
Itcompares two specified bytes or words.
This instruction compares the contents of source operand
with the contents of destination operands.
For comparison, it internally subtracts the source operand
from the destination operand but does not store the result
anywhere or value of source and destination does not change.
But the flags are modified to indicate the result
The source may be immediate data, memory location or
register.
The destination may be constant/immediate, memory
location or register.
The source and destination both can’t be memory location.
40
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
(CF=0 for less than).
Then resulting carry & zero flag will be set or reset.
Example:
41
CMP destination, source
We can check this instruction by using 2’s compliment method.
Destination-Source
1. Do 2’s compliment of the source or negative number
2. Add the destination to the 2’s compliment
3. If CF=1, ZF=0, SF=1 on step 2 then destination is greater
than the source or result is positive.
4. If no final carry occurs and ZF=0, SF=0 then destination is
smaller than the source or result is negative
5. If no final carry occurs and ZF=1 SF=0 both of the operands
are equal, zero flag is set.
42
CMP destination, source
43
CMP destination, source
44
CMP destination, source
45
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
46
Multiplication Instruction (MUL & IMUL)
There are 3 different cases
1. When two bytes are multiplied
Multiplicand is in AL register
Multiplier is in a byte in memory or another register
product is stored in AX
AL*8bit number =AX
2. When two one word values are multiplied
Multiplicand is in AX register
Multiplier is in a word in memory or another register
Product is double word which is stored higher word (leftmost)
portion in DX and lower order word (right most) portion in AX
AX*16bit number = DX AX
3. When two double word values are multiplied
EAX*32bit number = EDX EAX
47
Multiplication Instruction (MUL & IMUL)
50
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.
o Type 0 interrupts: This interrupt is also known as the
divide by zero interrupt. For cases where the quotient
becomes particularly large to be placed or 0 divisor /
adjusted an error might occur.
51
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
MOV AX, 0203 // AX=0203
E.g. MOV BL, 04 // BL=04
DIV BL // AL=0203/04=50 (i.e. AL=50 & AH=03)
52
DIV Instruction… cont’d
53
IDIV Instruction
54
IDIV Instruction … cont’d
55
Multiplication and Division Examples
56
57
3. 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
58
3. Bit Manipulation Instructions
Shift and Rotate Instructions
Instructions to perform Shift operations
o SHL… Shift Left/SAL… Shift Arithmetic Left
o SHR-Shift Right they are different
o SAR-Shift Arithmetic Right
Immediate operand is not allowed in any of the shift instructions.
Instructions to perform Rotate operations
o ROL… Rotate Left=MSB is pushed into CF and simultaneously it is
transferred into the LSB position at each operation
o ROR… Rotate Right=LSB is pushed into CF and simultaneously it is
transferred into the MSB position at each operation
o RCL… Rotate Left through Carry flag= CF pushed to MSB & the LSB
is pushed to CF
o RCR… Rotate Right through Carry flag = CF pushed to LSB & the
59
MSB is pushed to CF
Shift Instructions
SHL…Shift Left/SAL…Shift Arithmetic Left
These instructions shift the operand word or Byte bit by
bit to the left and insert zeros in the newly introduced least
significant bits.
All flags are affected depending upon the result
The shift operation is through carry flag.
60
Shift Instructions
SHR… Shift Right Instructions
These instructions performs bit-wise right shifts on the
operand word or Byte that may reside in a register or
memory location and insert zeros in the shifted positions.
The result is stored in the destination operand.
All flags are affected depending upon the result
The shift operation is through carry flag.
61
Shift Instructions
SAR… Shift Arithmetic Right
This instructions performs bit-wise right shifts on the operand
word or Byte that may reside in a register or memory location
It inserts the most significant bit of the operand in the newly
inserted positions
The result is stored in the destination operand.
All the condition flags are affected depending upon the result.
The shift operation is through carry flag.
62
Rotate Instructions
63
Rotate Instructions
64
Rotate Instructions Examples
65
4. Iteration Control (LOOP) Instructions
• These instructions are used to execute a series of instructions some
number of times.
• The number is specified in CX register.
• CX register automatically decremented by one, each time after the
execution of the loop instructions. Until CX=0, execution will jump
to a destination specified by a label in the instruction.
• If the condition is false , execution simply goes onto the next
instruction after the iteration control instruction.
I. FLAG MANIPULATION
II. MACHINE(PROCESSOR ) CONTROL INSTRUCTIONS
68
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
69
70
Processor Control Instructions
71
Processor Control Instructions
73
Flag manipulation and Processor Control Instructions
74
The End of Chapter 4
75
Quiz 5%
1. Explain various data addressing modes with the help of examples in
8086?
2. Explain the use of Push and POP instruction in 8086?
3. write the interpretations of the following instructions of 8086?
A. mov AX, CL
B. mov DS:[0004H], BX
4. write the difference between the following instruction
A. mov DX, 437AH and mov DX, [437AH]
5. Write at least 5 data movement instruction set types of 8086?
76