Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

8086 instruction set

Concise notes on the 8086 instruction set: instruction classes, encoding (opcode + ModR/M + displacement + immediate), common opcodes, addressing interpretation and worked encodings for exam practice.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

The 8086 instruction set is the foundation for x86 machine code and teaches how a CPU fetches, decodes and executes instructions. Engineers use this knowledge when writing or debugging low-level code (boot loaders, OS kernels), designing assemblers, or learning modern processor compatibility and microarchitecture behaviour.

Key ideas

  • Operand: a source or destination value for the operation; it may be a register, immediate (constant in the instruction), or memory location.
  • Register: a small, fast storage inside the CPU. 8086 has general-purpose 16-bit registers AX, BX, CX, DX and index/base registers SI, DI, BP, SP; also 8-bit sub-registers AH/AL etc.
  • Immediate: a constant embedded in the instruction (e.g., MOV AX, 0x1234).
  • Effective address (EA): the computed memory address used by memory operands. For 8086 EA uses combinations of base/index registers and an optional displacement.
  • ModR/M byte (Mod-Reg-R/M): when an instruction uses a register and a memory or register operand, the second instruction byte encodes addressing mode: 2 bits Mod, 3 bits Reg, 3 bits R/M, in that order high-to-low. Exact meaning:
    • Bits 7-6 = Mod: 00, 01, 10, 11. 11 means both operands are registers (no memory). 00 means no displacement except when R/M=110, which means a direct 16-bit displacement follows (memory direct). 01 means an 8-bit signed displacement follows (sign-extended to 16 bits for EA calculation). 10 means a 16-bit displacement follows.
    • Bits 5-3 = Reg: usually the register operand or opcode-extension (0..7 codes: 000=AX, 001=CX, 010=DX, 011=BX, 100=SP, 101=BP, 110=SI, 111=DI for 16-bit registers).
    • Bits 2-0 = R/M: selects one of eight addressing forms when Mod != 11, or a register code when Mod=11. For Mod != 11 the R/M codes map to EA formulas:
      • 000 -> [BX + SI]
      • 001 -> [BX + DI]
      • 010 -> [BP + SI]
      • 011 -> [BP + DI]
      • 100 -> [SI]
      • 101 -> [DI]
      • 110 -> [direct disp16] (only when Mod=00; otherwise [BP])
      • 111 -> [BX]
  • Byte order (endianness): 8086 uses little-endian for multi-byte immediates and displacements; low-order byte stored first.
  • Instruction classes: Data transfer (MOV, PUSH, POP), Arithmetic (ADD, SUB, CMP, INC, DEC), Logical (AND, OR, XOR, TEST), Control transfer (JMP, CALL, RET, conditional Jcc), String (MOVS, CMPS, SCAS, LODS, STOS), Flag/processor control (CLC, STI, HLT), and I/O (IN, OUT).

Exact statements students must know (write these in exam answers):

  • MOV r16, r/m16 uses opcode 8B /r (binary: 10001011 followed by ModR/M). Assumption: operands are 16-bit unless W bit indicates 8-bit.
  • MOV r/m16, r16 uses opcode 89 /r (binary: 10001001).
  • MOV r16, imm16 uses opcode B8 + rw (B8..BF) followed by 16-bit immediate (low byte then high byte).
  • ADD r/m16, r16 uses opcode 01 /r; ADD r16, r/m16 uses opcode 03 /r; ADD AX, imm16 uses 05 iw; ADD AL, imm8 uses 04 ib.
  • Short relative JMP (8-bit displacement) is EB cb; near relative JMP (16-bit displacement) is E9 cw. Conditional jumps use 70h+cc with 8-bit displacement (8086 supports only short conditional jumps with 8-bit offset).

What to draw and label for ModR/M decoding (exam diagram): draw a byte with 8 bit positions labelled 7 down to 0. Group bits 7-6 as Mod, 5-3 as Reg, 2-0 as R/M. Next to it draw a small table mapping Reg codes to registers (000=AX ... 111=DI). Below, draw R/M mapping to EA formulas for Mod != 11 and note special case Mod=00 R/M=110 -> direct disp16. Also note default segment rule: EA using BP defaults to SS; others default to DS (unless overridden with a segment-override prefix).

Be explicit about assumptions: these encodings are for 8086 real-mode style 16-bit operands; 8-bit forms use different register code sets (AL,CL,DL,BL,AH,CH,DH,BH) where appropriate.

Formulas

(Here we list instruction encoding patterns / syntax to remember.) MOV r16, r/m16 -> 8B /r (8B followed by ModR/M; /r = ModR/M where Reg=destination) MOV r/m16, r16 -> 89 /r (89 followed by ModR/M; Reg=source) MOV r16, imm16 -> B8 + rw iw (opcode B8..BF where low 3 bits pick register; then immediate 16-bit little-endian) ADD r/m16, r16 -> 01 /r (01 + ModR/M) ADD r16, r/m16 -> 03 /r (03 + ModR/M) ADD AX, imm16 -> 05 iw (05 followed by imm16 little-endian) ADD AL, imm8 -> 04 ib (04 followed by imm8) ADD r/m16, imm16 -> 81 /0 iw (81 with /0 in Reg field, then imm16) ADD r/m16, imm8 -> 83 /0 ib (83 with /0, imm8 sign-extended) JMP short -> EB cb (8-bit relative offset) JMP near -> E9 cw (16-bit relative offset) JMP far -> EA cd (far absolute ptr16:16; offset word then segment word, little-endian) CALL near -> E8 cw (16-bit relative offset) CALL far -> 9A cd (far absolute ptr16:16; offset word then segment word, little-endian) MOV AX, moffs16 -> A1 iw (AX <- [moffs16] using DS by default; no ModR/M) MOV moffs16, AX -> A3 iw ([moffs16] <- AX using DS by default; no ModR/M)

Symbols: Reg codes 000..111 map to AX,CX,DX,BX,SP,BP,SI,DI (for 16-bit). Little-endian means immediate low byte first then high. cb = 8-bit signed relative displacement; cw = 16-bit signed relative displacement; cd = far pointer 16:16 (offset word then segment word). moffs16 = absolute 16-bit memory offset (DS by default unless segment override).

Worked examples

Example 1 — register-to-register move Question: Encode MOV BX, AX. Step 1: Operation is MOV r16, r/m16 (reg <- r/m) so opcode = 8B /r. Step 2: Destination Reg = BX -> reg code 011. Source is register AX -> when both are registers Mod=11 and R/M = AX code 000. Step 3: Build ModR/M byte: Mod=11 (binary 11), Reg=011, R/M=000 -> bits 11 011 000 = 11011000 = 0xD8. Step 4: Final bytes: opcode 8B then ModR/M D8 -> 8B D8.

Example 2 — register to direct memory Question: Encode MOV [0x1234], AX (store AX into memory at address 0x1234). Step 1: Use MOV r/m16, r16 -> opcode 89 /r (destination memory, source reg). Step 2: Reg field = AX -> 000. For direct memory address we use Mod=00 and R/M=110 which signals a 16-bit displacement follows. Step 3: ModR/M byte = 00 000 110 = 00000110 = 0x06. Step 4: Append 16-bit displacement little-endian 0x1234 -> bytes 34 12. Step 5: Final bytes: 89 06 34 12.

Example 3 — immediate to register Question: Encode MOV CX, 0x3456. Step 1: MOV r16, imm16 uses B8 + rw. Register CX code rw=001 so opcode B8 + 1 = B9. Step 2: Append imm16 little-endian: 56 34. Step 3: Final bytes: B9 56 34.

Example 4 — short jump Question: Jump forward 5 bytes from next instruction. Encoding: JMP short uses EB cb where cb is signed 8-bit relative displacement. If displacement is +5 -> cb = 0x05. Final bytes: EB 05.

(Each worked example is step-by-step; check bytes match standard 8086 encodings.)

Common mistakes

  • Using Mod=00 with R/M=110 meaning [disp16] — some assume it means [BP].
  • Confusing opcode for MOV r16, r/m16 (8B) vs MOV r/m16, r16 (89) — destination/source order matters.
  • For ADD immediate, using 05 for non-AX registers (05 is only ADD AX, imm16; use 81/83 encodings for general r/m).
  • Conditional jumps on 8086 are short (8-bit) — expecting 16-bit displacement opcode is wrong.

For your exam

Typical question types: definitions (define ModR/M, opcode, immediate), short notes (classify instruction types), encoding/decoding problems (give bytes decode to instruction; or given instruction produce machine bytes), derivations (show ModR/M fields for given operands), and diagram drawing (show ModR/M bit layout and register maps). What full-mark answers include:

  • Correct opcode byte(s) chosen with justification of which of the forms (e.g., 8B vs 89) you used.
  • Correct ModR/M composition (explicit Mod, Reg, R/M bits and their binary -> hex value).
  • Correct displacement/immediate values shown in little-endian order and appended.
  • For decode tasks show each step: split opcode, ModR/M bits, interpret R/M to EA, state if displacement bytes follow and their value, and final readable instruction.

Quick check

  1. What Mod bits value indicates register-to-register addressing? 2) Opcode B8..BF encodes which instruction form? 3) In ModR/M when Mod=00 and R/M=110 what follows? Answers: 1) Mod=11. 2) MOV r16, imm16 (B8 + register code). 3) A 16-bit direct displacement (disp16) follows, addressing [disp16].

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?