You are on page 1of 8

Encoding of 8086 Instructions

byte

8086 Instructions are represented as binary numbers Instructions require between
1 and 6 bytes.

7 6 5 4 3 2 1 0

1

opcode
mod

2

d w

reg

Opcode byte

r/m

Addressing mode byte

3

[optional]

low disp

4

[optional]

high disp

5

[optional]

low data

6

[optional]

high data

This is the general instruction format used by the majority of 2-operand instructions
but some instructions have short form e.g. pop (special cases has no mod byte and
reg. is with opcode byte) some instruction can have more than form due to short
form bec. it is used so much and accumulator operand.

Note that bytes 1 and 2 are divided up into 6 fields:
opcode
d

direction (or s = sign extension)

w

word/byte

mod

mode

reg

register

r/m

register/memory

Details on Fields

Opcode Byte
Opcode field specifies the operation performed (mov, xchg, etc).

d (direction) field specifies the direction of data movement:
d=1

data moves from operand specified by R/M

d=0

field to operand specified by REG field
data moves from operand specified by REG
field to operand specified by R/M field

• d position MAY be replaced by "s" bit s=1 one byte of immediate data is present which must be signextended to produce a 16-bit operand s=0 • two bytes of immediate are present w (word/byte) specifies operand size W=1 data is word W=0 data is byte Addressing Mode Byte (Byte 2) • • Contains three fields Mod Bits 6-7 (mode. determines how R/M field is interpreted Reg Bits 3-5 (register) or SREG (Seg register) R/M Bits 0-2 (register/memory) Byte 2 Specifies details about operands R/M .

R/M 110 (used for direct addressing) • This actually assembles as MOV DL.MOD • • At second line this is direct addressing mod = 00 & r/m = 110 and it must be followed by two address bytes. Reg: • At reg = 011 & w =1 register is BX not BP SREG (Not exist in manual) 000 ES 001 CS 010 SS [BP] Doesn’t this cause a problem? – What about MOV DL. At mod = 01 disp is 8 bit signed (max –ve is -128). Mode 01 – [BP] addressing mode. [BP]? – No displacement. Mode 00 – [BP] addressing mode. [BP+0] – 8-bit displacement. R/M 110 011 DS . at mod = 10 disp 16 bit unsigned.

.• Note that this makes [BP] move instructions at least three bytes long.g.g.e. DIV instruction r/m indicates register/mem word or byte) • For two-operand instructions (except those with an immediate operand) one is a register determined by REG (SREG) field and the other may be register or memory and is determined by R/M field. no registers and memory locations only disp or nothing e. JMP) • For one-operand instructions the R/M field indicates where the operand is to be found (e. • Direction bit has meaning only in two-operand instructions Indicates whether "destination" is specified by REG or by R/M Note that this allows many instructions to be encoded in two different ways. Addressing Mode Byte • In general is not present if instruction has no operands (i.

 SREG 000 ES 001 CS 010 SS 011 DS .

MOV reg/mem.d = 0 because we need R/M Table to encode [BX+10h] therefore first byte is (1000 .CL .w = 0 because we are dealing with a byte .MOD = 11 because it is register-register .w = 1 because we are dealing with a byte .Examples • MOV instruction has seven possible formats. we can use MOD=01 REG=001(CL) and R/M=111(BX+Disp).if d = 1 then REG = dest (AX) 000 and R/M = source (BX) 011 = 1000 1011 1100 0011 (8B C3) EX: 2 • MOV [BX+10h].if d = 0 then REG = source (BX) 011 and R/M = dest (AX) 000 = 1000 1001 1101 1000 (89 D8) .CX Result = 89 4F 10 .Since 10H can be encoded as an 8-bit displacement.d = 0 because we need R/M Table to encode [BX+10h] therefore first byte is (1000 1000) = 88H . We will not discuss them all.BX . so the second byte= 0100 1111 = 4FH and the last byte is 10H Result = 88 4F 10 Note: MOV [BX+10H].reg/mem • This instruction has the structure: 100010dw MOD REG R/M Disp1 Disp2 where displacements are optional depending on the MOD bits EX: 1 • MOV AX.w = 1 because we are dealing with words .

byte 5 immediate data Result = C6 06 00 01 10 MOV mem to accumulator • This instruction has the structure: 1010 000w disp1 disp2 EX: 4 MOV AX.MOD = 00 and R/M = 110 (Displacement) For Direct addressing so second byte 0000 0110 = 06 . Many other instructions have a short form for AX register.bytes 3 and 4 are address.10H . so the second byte= 0100 1111 = 4FH and the last byte is 10H MOV immediate to reg/mem • This instruction has the structure: 1100 011w MOD 000 R/M disp1 disp2 Where displacement bytes optional depending on value of MOD EX: 3 • MOV BYTE PTR [0100H]. • • Could also be assembled as: move memory to register 100010dw MOD REG R/M Disp1 Disp2 .1001) = 89H . we can use MOD=01 REG=001(CX) and R/M=111 (BX+Disp).[0100h] .w = 0 because we have byte operand so first byte 1100 0110 = C6 .Since 10H can be encoded as an 8-bit displacement.w = 1 because we have word operand So first byte 1010 0001 Result = A1 00 01 • Note special form for accumulator.

d = 1 because we need Reg to be dest so first byte 1000 1011 = 8B . it will be found in table D-2 in manual. For Decoding: use table D-3 in manual for instruction decoding For clock cycles taken for each instruction.MOD = 00 and R/M = 110 (Displacement) For Direct addressing. . REG = 000 (AX) so second byte 0000 0110 = 06 ..byte 3 and byte 4 are address 1000 1011 0000 0110 0000 0000 0000 0001 Result = 8B 06 00 01 For Encoding you will use table D-2 in manual for different instruction format for each instruction.w = 1 because we have word operand .