Ch3 Notes PDF
Ch3 Notes PDF
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Opcode w 1 1 REG R/M
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Opcode w MOD=00 REG R/M
1
First byte Second byte
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Opcode MOD REG R/M
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Lower byte Higher byte
displacement
MOD=01 for 8 bit displacement
MOD=10 for 16 bit displacement
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Opcode MOD=11 Opcode R/M
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Lower byte Higher byte
Immediate data
Displacement
(see next page)
2
Fifth byte Sixth byte
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
Lower byte Higher byte
Immediate data
REG Codes:
Register
Code w=0 w=1
000 AL AX
001 CL CX
010 DL DX
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
Segment Register Codes:
Code Segment register
00 ES
01 CS
10 SS
11 DS
3
R/M, MOD codes:
MOD 00 01 10 11
R/M No displacement 8 bit displacement 16 bit displacement w=0 w=1
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 0 1 0 d w 1 1 REG R/M
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 0 1 0 d=1 w=0 1 1 0 1 1 0 0 1
D7 D6 D5 D4 D3 D2 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 0 1 0 d=0 w=0 1 1 0 0 1 0 1 1
4
8 bit operand
Therefore, MOV BL, CL = 88CBH
2) ADD AX, BX
Opcode for ADD = 000000dw MOD REG R/M
• d will be 1 since destination is a register
• w will be 1 for 16 bit operands (AX and BX)
• REG gives destination, destination is AX, therefore REG = code of AX = 000
• R/M gives source, source is BX, therefore R/M = code of BX = 011
• Register to Register type of operation, therefore MOD = 11
• ADD AX, BX = 00000011 11000011 = 03C3H
Same instruction can be coded as follows:
• d will be 0 since source is a register
• w will be 1 for 16 bit operands (AX and BX)
• REG gives source, source is BX, therefore REG = code of BX = 011
• R/M gives destination, destination is AX, therefore R/M = code of AX = 000
• Register to Register type of operation, therefore MOD = 11
• ADD AX, BX = 00000001 11011000 = 01D8H
3) MOV [SI], DL
Opcode = 100010dw MOD REG R/M
d = 0, REG field gives source. Source is DL register and its code is 010, therefore, REG = 010
w = 0, 8 bit operands
MOD = 00, the instruction is not having any displacement
R/M gives destination. Destination is [SI] and its code is 100, therefore, R/M = 100
MOV [SI], DL = 10001000 00010100 = 8814H
5
to some another location e.g. CALL, RET, JMP instructions. The addressing modes for the sequential
and control transfer instructions are as follows:
1) Immediate:
In this type of addressing mode, data is available in the instruction itself e.g.
MOV AX, 5000H
ADD BX, 1020H
2) Direct:
In this addressing mode a 16 bit offset address is directly specified in the instruction e.g.
MOV AX, [5000H]
3) Register:
In this mode, data is stored in registers and it is referred using registers e.g.
MOV AX, BX
ADD AL, CL
4) Register Indirect:
In this mode, the operand is specified indirectly using some register. The contents of register point to
some memory location in Data Segment or Extra Segment. The registers used to specify memory
location are BX, SI, DI, BP e.g.
MOV AX, [BX]
In the above instruction, the contents of register BX will be used as offset in data segment. From that
offset two bytes will be transferred to AX.
INC BYTE PTR [SI]
In the above instruction, the contents of memory location pointed by SI in data segment will be
incremented by 1.
Suppose SI = 1000H
6
5) Indexed:
In this mode offset of operand is stored in either SI or DI register. This is a form of register indirect
addressing mode e.g.
MOV AX, [SI]
6) Register relative:
In this addressing mode, effective address of data is formed by adding 8 bit or 16 bit displacement
with the contents of BX, BP, SI, or DI registers e.g.
MOV AX, 50H [BX]
7) Based Indexed:
In this addressing mode, the effective address of data is formed by adding contents of base register
(BX or BP) to the contents of an index register (SI, DI) e.g.
MOV AX, [BX] [SI]
8) Relative Based Indexed:
The effective address of data, in this mode, is formed by adding an 8 bit / 16 bit displacement to the
sum of contents of any one base register(BX or BP) and any one index register (SI or DI) e.g.
MOV AX, 1000H [BX] [SI]
7
These instructions transfer execution control to specified address. Cal, jump, return and
interrupt instructions belong to this category.
4) Loop instructions:
These instructions are used to implement conditional or unconditional loops. The loop count is
stored in CX register e.g. LOOP, LOOPZ, LOOPNZ instructions.
5) Machine control instructions:
These instructions are used to control 8086 microprocessor itself e.g. NOP, HLT, WAIT and
LOCK instructions.
6) Flag manipulation instructions:
These instructions are used to set or reset flags of 8086 e.g. STC, CLC, CMC, STI, CLI, CLD,
STD instructions.
7) Shift and Rotate instructions:
These instructions are used to shift or rotate the bits of operand in either right or left direction.
CL register can be used to store the count of shift/rotate operation.
8) String instructions:
These instructions are used to perform string manipulation operations such as load, move,
store, scan, compare etc.
8
MOV AX, BX ; AX = BX after execution
MOV DS, AX ; DS = AX after execution
MOV AX, [SI]
MOV AX, 50H [BX]
2) PUSH: Push to stack
➢ General form: PUSH source
➢ This instruction stores the contents of source on to the stack.
➢ The source can be a general purpose register, segment register or memory.
➢ When this instruction is executed, the stack pointer, which points to the top of stack, is
decremented by 2 and contents of source are copied on to the stack. The higher byte of source
is stored at higher address and lower byte is stored at lower address.
➢ No flags are affected by this instruction
➢ Examples:
PUSH AX ; Let AX = 1122H, SS = 2000H, SP = FFFFH
PUSH DS
PUSH [2000H]
PUSH AL ; Not allowed since AL is 8 bit register, ALWAYS a WORD
9
3) POP: Pop from stack
➢ General form: POP destination
➢ This instruction copies a word from stack segment pointed by SP to destination. The destination
can be a general purpose register, a segment register or a memory location. After copying, SP is
automatically incremented by 2.
➢ No flags are affected by POP instruction
➢ Examples:
POP AX ; Let SS = 2000H and SP = FFFDH
POP DS
POP CS ; This is not allowed
POP [5000H]
4) XCHG: Exchange
➢ General form: XCHG destination, source
➢ This instruction exchanges contents of source and destination.
➢ Source and destination both cannot be memory locations.
➢ Source and destination must be of same size (i.e. both must be bytes or both must be words).
➢ Segment register cannot be used with this instruction.
➢ No flags are affected by this instruction.
➢ Examples:
XCHG AX, DX
XCHG BL, CL
XCHG [5000H], AX
10
➢ This instruction copies data from a port to AL or AX register.
➢ If an 8 bit port is read, the data will go into AL.
➢ If a 16 bit port is read, the data will go to AX.
➢ The port can be a fixed port (having 8 bit port address) or a variable port (having 16 bit port
address). For variable type port, the port address is loaded in DX register.
➢ No flags are affected.
➢ Example: Fixed port (8 bit port address)
IN AL, 80H
IN AX, 45H
➢ Examples: Variable port (16 bit port address)
1) MOV DX, 8000H ; move port address into DX
IN AL, DX
2) MOV DX, 8080H ; move port address into DX
IN AX, DX
11
➢ This instruction is used to translate a byte from one code to another code.
➢ It replaces a byte in AL register with a byte pointed by BX in a lookup table in memory.
➢ Before using this instruction lookup table must be present in memory. Starting address of table
is loaded in BX register. The byte to be translated is loaded in AL.
AL DS : [ BX + AL ]
➢ The value in AL is added to BX. This new value will be used as pointer in data segment.
➢ From the location, pointed by [BX+AL], data will be transferred to AL.
➢ No flags are affected by this instruction.
➢ Following example shows how to find ASCII value of a decimal digit (0 – 9) present in AL.
END MAIN
12
9) LDS: Load Register and DS with words from memory
10) LES: Load Register and ES with words from memory
➢ General form: LDS Register, Memory address of first word
➢ This instruction copies a word from memory into register specified. It then copies a word from
next memory locations into DS/ES.
➢ No flags are affected.
➢ Example:
LDS BX, [5000H]
13
12) SAHF: Store AH register to lower byte of flag register
➢ General form: LAHF
➢ This instruction copies contents of AH register to lower byte of flag register.
➢ Depending upon the bits of AH register, the flags in the lower byte of flag register will be set
or reset.
➢ Example:
SAHF
➢ Example:
PUSHF
14
Instruction Set of 8086 Microprocessor
➢ Example:
POPF
Arithmetic instructions:
1) ADD: Add
➢ General form: ADD destination, source
➢ This instruction adds a number from source to destination. The result is available in
destination.
➢ The source may be an immediate number, a register or a memory location.
➢ The destination may be a register or a memory location.
➢ Both source and destination cannot be memory locations.
➢ The size of source and destination must be same i.e. both must be bytes or both must be words.
➢ Segment registers cannot be used.
➢ Flags affected: All condition flags (A, C, O, P, S, Z).
➢ Examples:
ADD AL, 67H
ADD DX, BX
ADD AX, [SI]
ADD BX, [5000H]
ADD [BX], 79H
15
Instruction Set of 8086 Microprocessor
ADC AX, BX
ADC AH, 67H
ADC BX, [SI]
ADC CX, [5000H]
ADC [BX], 23H
3) SUB: Subtract
4) SBB: Subtract with Borrow
➢ General form: SUB destination, source
SBB destination, source
➢ These instructions subtract a number in source from number in destination.
➢ The source may be an immediate number, a register or a memory location.
➢ The destination can be a register or a memory location.
➢ Both source and destination can not be memory locations.
➢ The size of source and destination must be same i.e. both must be bytes or both must be words.
➢ In case of SBB, borrow flag (i.e. Carry flag) and source will be subtracted from destination and
result is placed in destination.
➢ In case of SUB, only source will be subtracted from destination and result is placed in
destination.
destination = destination - source
➢ All condition flags are affected by these instructions (AF, CF, OF, PF, SF, ZF)
➢ Examples:
SUB AX, BX
SUB AH, 67H
SUB BX, [SI]
SUB CX, [5000H]
SUB [BX], 23H
5) INC: Increment
➢ General form: INC destination
➢ This instruction increments the destination by 1.
➢ The destination may be a register or a memory location.
➢ Immediate operand is not allowed.
➢ Flags affected: A, O, P, S and Z. Carry flag is not affected by this instruction.
➢ Examples:
16
Microprocessor and Programming Instruction Set of 8086 Microprocessor
INC BL
INC CX
INC BYTE PTR [BX]
INC WORD PTR [BX]
6) DEC: Decrement
➢ General form: DEC destination
➢ This instruction subtracts 1 from destination.
➢ The destination may be a register or a memory location.
➢ Immediate operand cannot be used.
➢ Flags affected: A, O, P, S and Z. Carry flag is not affected by this instruction.
➢ Examples:
DEC CL
DEC BP
DEC BYTE PTR [SI]
DEC WORD PTR [BX]
DEC COUNT ; COUNT is a variable
7) CMP: Compare
18
Microprocessor and Programming Instruction Set of 8086 Microprocessor
19
Microprocessor and Programming Instruction Set of 8086 Microprocessor
DAA ;C>9
7C + 06 = 82
ii) Let AL = 73, CL = 29
ADD AL, CL ; AL 73 + 29 = 9C
DAA ;C>9
9C + 06 = A2
A>9
A2 + 60 = 02 in AL and CF = 1
20
Microprocessor and Programming Instruction Set of 8086 Microprocessor
➢ For finding 2’s complement, it subtracts the contents of destination from zero.
➢ The result is stored in destination.
➢ The destination may be a register or a memory location.
➢ If operation cannot be completed then OF=1.
➢ All conditional flags are affected.
21
Microprocessor and Programming Instruction Set of 8086 Microprocessor
➢ Examples:
IMUL BX
IMUL AX
IMUL WORD PTR [SI]
22
Microprocessor and Programming Instruction Set of 8086 Microprocessor
2) OR: Logical OR
23
Microprocessor and Programming Instruction Set of 8086 Microprocessor
24
Microprocessor and Programming Instruction Set of 8086 Microprocessor
25
Microprocessor and Programming Instruction Set of 8086 Microprocessor
AL after execution:
26
Microprocessor and Programming Instruction Set of 8086 Microprocessor
AL before execution:
AL after execution:
2) SHR DX, CL
27
Microprocessor and Programming Instruction Set of 8086 Microprocessor
28
Microprocessor and Programming Instruction Set of 8086 Microprocessor
29
Microprocessor and Programming Instruction Set of 8086 Microprocessor
➢ For multiple byte/multiple word moves, the number of elements to be moved is put in CX
register. It acts as counter.
➢ After a byte/word move, SI and DI are automatically adjusted to point to next source and
destination byte/word.
➢ If Direction Flag (DF) = 0, SI and DI will be automatically incremented by 1 for byte move
(MOVSB) and incremented by 2 for word move (MOVSW).
➢ If DF = 1, then SI and DI will be automatically decremented.
➢ No flags are affected.
DS : SI ES : DI
30
Microprocessor and Programming Instruction Set of 8086 Microprocessor
31
Microprocessor and Programming Instruction Set of 8086 Microprocessor
32
Microprocessor and Programming Instruction Set of 8086 Microprocessor
33
Microprocessor and Programming Instruction Set of 8086 Microprocessor
34
Microprocessor and Programming Instruction Set of 8086 Microprocessor
2) HLT: Halt the processor. To make it come out of halt, state reset it or interrupt it.
3) NOP: No operation, microprocessor will not perform any operation for 4 clock cycles. IP will be
incremented by 1. This instruction can be used in delay loops.
4) ESC: Escape to external device like 8087
5) LOCK: Lock the bus
➢ It is a prefix, when used with some instruction, the buses are locked till the instruction is
executed completely. No other bus master can gain the access of buses.
35