Professional Documents
Culture Documents
UNIT – III
INSTRUCTION SET OF 8086
MICROPROCESSOR
(16 – Marks)
a) Label Field: -
1) The first field is the label field. A label is a symbol or group of symbols used to
represent an address which is not specifically known at the time the statement is
written.
2) Labels are usually followed by colons. They are used only if required
(Optional).
b) Opcode Field: -
1) The second field is the opcode field. It contains the mnemonic that represents
the instructions or the type of operation to be performed.
2) Mnemonics are hence called operation code or opcode. It is compulsory field.
For e.g.: - ADD mnemonic indicates addition operation.
c) Operand Field: -
1) The third field is the operand field. It contains data, the memory address, the
port address or the name of the register on which the operation is to be
performed.
2) Operand refers to the data items acted upon by an instruction.
3) Operands may be specified in an instruction implicitly or explicitly, and hence
they are called as implicit operand or explicit operand.
4) If there are two operands, then one operand is called as source operand (right
hand side operand), and the other is called as destination operand (left hand side
operand).
For e.g.: - ADD AL,BL
5) In above example, there are two operands AL and BL, which are the name of
registers. BL is source operand whereas AL is the destination operand.
6) The instruction in an example adds the contents of AL and BL registers, and by
the INTEL convention, the result is stored in the register or memory location
(address) specified before the comma (destination operand) in the operand field,
i.e. in above example, the result is stored in AL register.
d) Comment Field: -
1) The fourth field is the comment field. The comment field starts with a
semicolon and is the optional field.
There are six general formats of instructions in 8086 instruction set. The length
of an instruction may vary from one byte to six bytes. These instruction formats
are as follows -
1) One Byte Instruction.
2) Register to Register.
3) Register to / from Memory with no Displacement.
4) Register to / from Memory with Displacement.
5) Immediate Operand to Register.
6) Immediate Operand to Memory with 16 bit Displacement.
D7 D0
OPCODE
1) This format is only one byte long and may have the implied data or register
operands.
2) The least significant 3 bits of the opcode are used for specifying the register
operand, if any.
3) Otherwise, all the 8 bits form an opcode and the operands are implied.
2) Register to Register: -
D7 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
OPCODE W 1 1 REG R/M
3) The second byte of the code shows the register operands and R/M field, as
shown.
4) The register represented by the REG field is one of the operands.
5) The R/M field specifies another register or memory location, i.e. the other
operand.
D7 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
OPCODE W MOD REG R/M
1) This format is also 2 bytes long and similar to the register to register format
except for the MOD field as shown.
2) The MOD field shows the mode of addressing. The R/M, REG, and the W fields
are same as in above format.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0 D7 D0 D7 D0
Lower Byte Higher Byte
OPCODE MOD REG R/M
of Disp. of Disp.
1) This type of instruction format contains one or two additional bytes for
displacement along with 2 byte the format of the register to / from memory
without displacement. The format is as shown.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0 D7 D0 D7 D0
Lower Byte Higher Byte
OPCODE 11 REG R/M
of DATA of DATA
1) In this format, the first byte as well as 3 bits from the second byte, which are
used for REG field in case of register to register format are used for opcode.
2) In also contains one or two bytes of immediate data. The complete instruction
format is as shown.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0 D7 D0 D7 D0
Lower Byte Higher Byte
OPCODE MOD REG R/M
of Disp. of Disp.
D7 D0 D7 D0
Lower Byte Higher Byte
of DATA of DATA
1) This type of instruction format requires 5 or 6 bytes for coding. The first 2 bytes
contain the information regarding opcode, MOD, and R/M fields.
2) The remaining 4 bytes contains 2 bytes of displacement and 2 bytes of data as
shown.
3) The opcode usually appears in the first byte, but in a few instructions, a register
destination is in the first byte and few other instructions may have their 3 bits of
opcode in the second byte.
4) The opcodes have the single bit indicators. Their definitions as follows –
a) W – bit: -
i) This indicates whether the instruction is to operate over an 8 bit or 16 – bit
data / operands.
ii) If W bit is 0, the operand is of 8 bits and if W is 1, the operand is of 16 bits.
b) D – bit: -
i) This is valid in case of double operand instruction. One of the operands must
be a register specified by the REG field.
4) In the second instruction of above example, data 1235H (16 bit data) is directly
given in the instruction and transferred to the specified register AX
immediately.
3) In the above example, BX is the base register and SI is the index register. The
effective address is computed as, 10H*DS+[BX]+[SI].
Intersegment Intrasegment
D) Loop Instructions: -
1) If these instructions have REP prefix with CX used as count register, they can be
used to implement unconditional and conditional loops as well as these are
useful to implement different loop structures. The LOOP, LOOPNZ and
LOOPZ instructions belong to this category.
H) String Instructions: -
1) These instructions involve various string manipulation operations like load,
move, scan, compare, store, etc.
iv) Before PUSH, the stack pointer is decremented by 2, and then stores the two
byte contents of the operand onto the location in stack segment where the stack
pointer points.
v) The higher byte is pushed first and then the lower byte is pushed. Thus out of
two bytes, higher byte occupies the higher address and lower byte occupies the
lower address.
Examples: -
1. PUSH BX ; Decrement SP by 2, copy BX to stack
2. PUSH DS ; Decrement SP by 2, copy DS to stack
3. PUSH [5000H] ; Content of location 5000H and 5001H in DS are
; pushed onto the stack
4. PUSH AL ; Invalid, must push a word
4) XCHG: – Exchange
i) Syntax: - XCHG Destination, Source
ii) The XCHG instruction exchanges the contents of a specified source with the
contents of a specified destination. No flags are affected by this instruction.
iii) The source and destination both can be a registers (but not the segment
registers) or one of them is memory location.
iv) Both the operands cannot be a memory location. In other words, this instruction
cannot directly exchange the contents of two memory locations.
v) The source and destination must both be words, or must both be bytes.
Examples: -
1. XCHG AX, DX ; Exchange word in DX with word in AX
2. XCHG BL, CH ; Exchange byte in CH with byte in BL
3. XCHG [5000H], AX ; Exchange word in location [5000H] in data
; Segment with word in AX
4. XCHG BX ; Exchange word in BX with word in AX
7) XLAT: – Translate
i) Syntax: - XLAT
ii) This instruction is used to translate a byte from one code to another code. This
instruction does not affect any flag.
iii) The XLAT instruction replaces a byte in AL register with a byte pointed to by
BX in a lookup table in memory.
iv) Suppose, a hexadecimal key pad having 16 keys from 0 to F is interfaced with
8086 using 8255.
v) Whenever a key is pressed, a code of that key (0 to F) is returned in AL. For
displaying the number corresponding to the pressed key on the 7-segment
display device, it is required that the 7-segment code corresponding to the key
pressed is found out and sent to the display port.
vi) This translation from the code of the pressed key to the corresponding 7-
segment code is performed using XLAT instruction.
Example: -
MOV AX, SEG TABLE ; Address of the segment containing look up
MOV DS, AX ; table is transferred in DS
MOV AL, CODE ;Code to the pressed key is transferred in AL
MOV BX, OFFSET TABLE ; Offset of the code lookup table in BX
XLAT ;Find the equivalent code and store in AL
7 0 15 8 7 0
BX Y X
5000 XX Y X
5001 YY
5002 mm DS/ES nn mm
5003 nn
iii) The LAHF instruction may be used to observe the status of all the condition
code flags (except overflow) at a time.
iii) The carry flag bit may be set as a result of the previous calculations. All the
condition code flags are affected by this instruction.
Examples: - The examples with addressing modes are as follow –
1. ADC AX, 0100H ; Immediate Addressing Mode
2. ADC AX, [5000H] ; Direct Addressing Mode
3. ADC AX, BX ; Register Addressing Mode
4. ADC AX, [BX] ; Register Indirect Addressing Mode
5. ADC AX, [SI] ; Indexed Addressing Mode
6. ADC 0100H ; Immediate Mode (Destination AX (implicit))
3) INC: – Increment
i) Syntax: - INC Destination
ii) This instruction increments the content of the specified destination by 1. In
other words, this instruction adds 1 to the content of the destination specified in
an instruction.
iii) The destination may be a register or a memory location but it cannot be an
immediate data. All the condition code flags are affected except the carry flag
(CF) by this instruction.
Examples: - The examples with addressing modes are as follow –
1. INC [5000H] ; Direct Addressing Mode
2. INC AX ; Register Addressing Mode
3. INC [BX] ; Register Indirect Addressing Mode
4. INC [SI] ; Indexed Addressing Mode
4) DEC: – Decrement
i) Syntax: - DEC Destination
ii) This instruction decrements the content of the specified destination by 1. In
other words, this instruction subtracts 1 from the content of the destination
specified in an instruction.
5) SUB: – Subtract
i) Syntax: - SUB Destination, Source
ii) The SUB instruction subtracts the contents of a source operand from the content
of a destination operand and stores the result in the destination operand. All the
condition code flags are affected by this instruction, depending upon the result.
iii) The source may be an immediate data (number), a register, or a memory
location.
iv) The destination may be a register or a memory location but it cannot be an
immediate data.
v) The source and destination in an instruction cannot both be memory locations.
Also the content of the segment registers cannot be subtracted using this
instruction.
vi) The source and destination must be of the same type i.e. both are of type byte (8
bit) or of type word (16 bit).
vii) If we want to subtract a byte from a word, we must copy the byte to a word
location such as a 16 bit register and fill the upper byte of the word with 0’s.
Examples: - The examples with addressing modes are as follow –
1. SUB AX, 0100H ; Immediate Addressing Mode
2. SUB AX, [5000H] ; Direct Addressing Mode
3. SUB AX, BX ; Register Addressing Mode
7) CMP: – Compare
i) Syntax: - CMP Destination, Source
ii) The CMP instruction compares the content of source operand with the content
of destination operand. All the condition code flags are affected by this
instruction, depending upon the result.
iii) For comparison, this instruction subtracts the content of source operand from
the content of destination operand, but does not store the result anywhere.
iv) The source and destination are not changed, but the flags are set to indicate the
results of the comparison.
v) If both the operands are equal, zero flag is set i.e. ZF=1. If the source operand is
greater than the destination operand, carry flag and sign flag (since the result is
negative) both are set i.e. CF=1, SF=1. If the source operand is less than the
destination operand, carry flag and sign flag both are reset i.e. CF=0, SF=0.
vi) The source may be an immediate data (number), a register, or a memory
location.
vii) The destination may be a register or a memory location but it cannot be an
immediate data.
viii) The source and destination in an instruction cannot both be memory locations.
Examples: - The examples with addressing modes are as follow –
1. CMP BX, 0100H ; Immediate Addressing Mode
2. CMP 0100H ; Immediate Addressing Mode
3. CMP BX, [1234H] ; Direct Addressing Mode
4. CMP BX, AX ; Register Addressing Mode
5. CMP AX, [BX] ; Register Indirect Addressing Mode
6. CMP AX, [SI] ; Indexed Addressing Mode
iii) If the lower 4 bits of AL register are greater than 9 or if the AF flag is 1, the AL
is decremented by 6 and AH register is decremented by 1, the CF and AF are set
to 1.
iv) Otherwise, the CF and AF are set to 0, the result needs no correction. As a
result, the upper nibble of AL is 00 and the lower nibble may be any number
from 0 to 9.
v) The procedure is similar to the AAA instruction. AH is modified as difference
of the previous contents of AH and borrow for adjustment.
iii) This adjustment must be made before dividing the two unpacked BCD digits in
AX by an unpacked BCD byte. This means that AAD instruction is executed
before the DIV instruction.
Example: -
; AX = 0607H unpacked BCD for 67 decimal
; CH = 09H, now adjust to binary
AAD ; Result: AX = 0043 = 43H = 67 decimal
DIV CH ; Divide AX by unpacked BCD in CH
; Quotient: AL = 07 unpacked BCD
; Remainder: AH = 04 unpacked BCD
; Flags undefined after DIV
2. AL = 73
CL = 29
ADD AL, CL ; AL AL + CL
; AL 73 + 29
; AL 9C
DAA ; AL 02 and CF = 1
vi) Similarly, in case of 16 bit multiplication, the result can be as large as 32 bits.
So 32 bit destination is required to store the result and that destination is DX
and AX register respectively.
vii) In such a case, the most significant word of the result is stored in DX, while the
least significant word of the result is stored in AX.
Examples: -
1. MUL BH ; (AX) (AL) × (BH)
2. MUL CX ; (DX) (AX) (AX) × (CX)
3. MUL WORD PTR [SI] ; (DX) (AX) (AX) × ([SI])
iv) The CBW operation must be done before a signed byte in AL can be divided by
another signed byte.
iii) The dividend must be in AX for 16 bit operation and divisor may be specified
using any one of the addressing modes except immediate.
iv) The result will be in AL (quotient) while AH will contain the remainder. If the
result is too big to fit in AL, type 0 (divide by zero) interrupt is generated.
v) In case of double word dividend (32 bit), the higher word should be in DX
(Remainder) and the lower word should be in AX (Quotient).
Examples: -
1. IDIV BL ; Divide word in AX by byte in BL
2. IDIV CX ; Divide double word in DX and AX by word in CX
b) Logical Instructions: -
1) AND: – Logical AND
i) Syntax: - AND Destination, Source
ii) The AND instruction bit by bit ANDs the content of source operand with the
content of destination operand. The result is stored in the destination operand.
All the condition code flags are affected by this instruction.
iii) The source may be an immediate data, a register or a memory location.
iv) The destination may be a register or a memory location but it cannot be an
immediate data.
v) Both the operands can be a register but both cannot be a memory location.
Examples: -
1. AND AX, 0008H ; Immediate Addressing Mode
This instruction bit by bit ANDs 0008H immediate data with the content of
destination i.e. AX, the result is stored in AX. Suppose AX=3F0FH, then the
output will be as,
0011 1111 0000 1111 = 3F0FH [AX]
0000 0000 0000 1000 = 0008H
0000 0000 0000 1000 = 0008H [AX]
2. AND AX, [5000H] ; Direct Addressing Mode
3. AND AX, BX ; Register Addressing Mode
2) OR: – Logical OR
i) Syntax: - OR Destination, Source
ii) The OR instruction bit by bit ORs the content of source operand with the
content of destination operand. The result is stored in the destination operand.
All the condition code flags are affected by this instruction.
iii) The source may be an immediate data, a register or a memory location.
iv) The destination may be a register or a memory location but it cannot be an
immediate data.
v) Both the operands can be a register but both cannot be a memory location.
Examples: -
1. OR AX, 0098H ; Immediate Addressing Mode
This instruction bit by bit ORs 0098H immediate data with the content of
destination i.e. AX, the result is stored in AX. Suppose AX=3F0FH, then the
output will be as,
0011 1111 0000 1111 = 3F0FH [AX]
0000 0000 1001 1000 = 0098H
0011 1111 1001 1111 = 3F9FH [AX]
2. OR AX, [5000H] ; Direct Addressing Mode
3. OR AX, BX ; Register Addressing Mode
v) A near call is a call to a procedure which is in the same code segment that
contains a CALL instruction.
vi) A far call is a call to a procedure which is in a different code segment from the
one that contains the CALL instruction.
vii) When 8086 executes a near CALL instruction, it decrement the SP by 2 and
copies the offset of next instruction after the CALL on to the stack.
viii) This offset saved on the stack is referred to as the return address, because this is
the address that execution will return to after the procedure executes.
ix) A near CALL instruction will also load IP with the offset of the first instruction
in the procedure.
x) When 8086 executes a far CALL instruction, it decrement the SP by 2 and
copies the contents of CS register to the stack. It then decrements the SP by 2
again and copies the offset of the instruction after the CALL instruction to the
stack.
xi) Finally, it loads CS with the segment base of the segment which contains the
procedure, and loads IP with the offset of the first instruction of the procedure in
that segment.
Memory
Contents
D) Loop Instructions: -
1) LOOP: – Loop Unconditionally
i) Syntax: - LOOP Label
ii) This instruction executes the part of the program from the label or address
specified in the instruction up to the LOOP instruction, CX number of times.
iii) At each iteration, CX is decremented automatically. This instruction implements
DECREMENT COUNTER and JUMP IF NOT ZERO structure.
iv) No flags are affected by this instruction.
Example: -
MOV CX, 0005 ; Number of times in CX
MOV BX, 1234H ; Data to BX
LABEL: MOV AX, CODE1
OR BX, AX
AND DX,
AX LOOP
LABEL
In the above example, the part of a program from label LABEL up to the LOOP
instruction is executed repeatedly until the count in CX become zero.
3) NOP: – No Operation
i) Syntax: - NOP
ii) When NOP instruction is executed, the processor does not perform any
operation till 4 clock cycles, except incrementing the IP by one.
iii) It then continues with further execution after 4 clock cycles. No flags are
affected by this instruction.
4) ESC: – Escape
i) Syntax: - ESC
ii) This instruction is used to pass instruction to a coprocessor, such as the 8087
math coprocessor which shares the address and data bus with an 8086.
iii) When ESC instruction executes, 8086 frees the bus for an external master like
coprocessor or peripheral devices.
iii) If the carry flag is 0 before this instruction, it will be set to 1 after the instruction
executes.
iv) If the carry flag is 1 before this instruction, it will be reset to 0 after the
instruction executes.
b) Rotate Instructions: -
1) ROR: – Rotate Right without Carry
i) Syntax: - ROR Destination, Count
ii) The ROR instruction rotates the contents of the specified destination operand bit
by bit to the right either by one or by the count specified in CL, excluding carry.
iii) The LSB is pushed into the carry flag and simultaneously it is transferred into
the MSB position at each operation. The remaining bits are shifted right by the
specified count positions.
iv) The destination operand can be a register (except a segment register) or a
memory location but it cannot be an immediate data.
v) Only CF and OF are affected by this instruction. The remaining bits are left
unchanged by the rotate operation.
Example: -
MOV CL, 02 ; Move 02 count in CL register
ROR AX, CL ; Rotate content of AX by 2 bit position
Suppose AX=1234H, then after execution, the contents are as follows
Bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CF
Operand 0 001
0 0 0 0 01 00 10 01 00 00 10 11 01 10 01 00 ×
Result 1st
0
Result 2nd 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1
0
ii) The ROL instruction rotates the contents of the specified destination operand bit
by bit to the left either by one or by the count specified in CL, excluding carry.
iii) The MSB is pushed into the carry flag and simultaneously it is transferred into
the LSB position at each operation. The remaining bits are shifted left by the
specified count positions.
iv) The destination operand can be a register (except a segment register) or a
memory location but it cannot be an immediate data.
v) Only CF and OF are affected by this instruction. The remaining bits are left
unchanged by the rotate operation.
Example: -
MOV CL, 02 ; Move 02 count in CL register
ROL AX, CL ; Rotate content of AX by 2 bit position
Suppose AX=1234H, then after execution, the contents are as follows
Bits C 15 14 13 12 11 10 9 8 7 6 5 4 321 0
F
Operand 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0
Result 1st 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0
Result 2nd 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 0
Example: -
MOV CL, 02 ; Move 02 count in CL register
RCR AX, CL ; Rotate content of AX by 2 bit position
Suppose AX=1235H, then after execution, the contents are as follows
Bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CF
Operand 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 1 0
Result 1st 0 0 0 0 1 0 0 1 0 0 0 1 101 0 1
Result 2nd 1 0 0 0 0 1 0 0 1 0 0 0 110 1 0
H) String Instructions: -
1) REP/PEPE/REPZ/REPNE/REPNZ: – Repeat Instruction Prefix
i) Syntax: - REP Instruction
ii) REP is a prefix which is written before one of the string instructions.
iii) The instruction, to which the REP prefix is provided, is executed until the CX
register becomes zero. Flags are affected by this instruction.
iv) At each iteration CX is automatically decremented by one. When CX becomes
zero, the execution proceeds to the next instruction in sequence.
v) REPE and REPZ stand for Repeat if Equal and Repeat if Zero, often used with
the Compare String instruction or with the Scan String instruction.
vi) REPE or REPZ will cause the string instruction to be repeated as long as the
compared bytes or words are equal (ZF=1) and CX is not zero.
vii) REPNE and REPNZ stand for Repeat if Not Equal and Repeat if Not Zero,
often used with the Scan String instruction.
viii) REPNE or REPNZ will cause the string instruction to be repeated until the
compared bytes or words are equal (ZF=1) or until CX=0 (end of string).
Examples: -
1. REP MOVSB
This instruction continues to copy string bytes until CX becomes zero.
2. REPE CMPSB
This instruction compares string bytes until end of string or until string bytes
not equal.
3. REPNE SCASW
This instruction scan a string of words until a word in the string matches the
word in AX or until all of the string has been scanned.
iii) The address of the source string is pointed by DS:SI pair while the address of
the destination string is pointed by ES:DI pair.
iv) The length of the byte string or word string must be stored in CX register. The
REP instruction prefix is used with MOVS instruction to repeat it by a value
given in counter CX.
v) After MOVS instruction is executed once, the index registers are automatically
incremented and CX is decremented. The incrementing or decrementing of the
pointers SI and DI depend upon the direction flag.
vi) If DF=0, the index registers are incremented, otherwise, they are decremented,
in case of all the string manipulation instructions.
Example: -
LEA SI, STR1 ; Load offset of source string in DS into SI
LEA DI, STR2 ; Load offset of destination string in ES into DI
CLD ; Clear direction flag to increment SI and DI
MOV CX, 04H ; Set counter CX by loading length of string
REP MOVSB ; Decrement CX and copy string bytes until CX=0
vi) This byte by byte or word by word comparison continues till a mismatch is
found or a CX becomes zero. When a mismatch is found, the carry and zero
flags are modified appropriately and the execution proceeds further. Example: -
LEA SI, STR1 ; Load offset of source string in DS into SI
LEA DI, STR2 ; Load offset of destination string in ES into DI
CLD ; Clear direction flag to increment SI and DI
MOV CX, 04H ; Set counter CX by loading length of string
REP CMPSB ; Repeat comparison of string bytes until end of
; String or until compared bytes are not equal
References: The contents and diagrams for notes preparation are taken from
1) Douglas V. Hall, Microprocessors and Interfacing, Second Edition, Tata McGraw Hill publications.
2) A. K. Ray, K. M. Bhurchandi, Advanced Microprocessors and Peripherals, Second Edition, Tata McGraw
Hill Publications.