Chapter 2.1: 8086 Architecture

Microprocessors
The 8086 at a glanceSystem busesBIU and EU Prefetch queueFetch–decode–executeRegister set Segment registers and IPSegmented memoryPhysical address calculation
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

General features

Explain the 8086's data and address bus widths, how much memory it can address, and its place in the x86 family.

Internal structure

Describe the roles of the BIU and EU, the prefetch queue and the idea of pipelining.

Registers

Group the 14 registers; explain the roles of the segment registers and IP.

Address calculation

Compute a 20-bit physical address as segment × 10h + offset, by hand and step by step.

Overview

The 8086 at a Glance

  • A 16-bit microprocessor introduced by Intel in 1978; the first member of the x86 family.
  • Its registers, ALU and data bus are 16 bits; it processes 16 bits of data at a time.
  • 20-bit address bus → 220 = 1,048,576 bytes = 1 MB of memory.
  • 16-bit data bus: moves 2 bytes at a time between the processor, memory and I/O devices.
  • CISC (complex instruction set) architecture: variable-length (1–6 byte) instructions, instructions that operate on memory.
  • Its instruction set, the basis of the PC, survives through backward compatibility.
16-bitdata bus
20-bitaddress bus
1 MBmemory
40 pinsDIP package
5–10 MHzclock
~29,000transistors
+5 Vsingle supply
HMOStechnology

Unlike its sibling the 8088, which has an 8-bit external data bus, the 8086 is 16 bits throughout.

Overview

The 8086 in the x86 Family

ProcessorYearInternal / external data busAddress busMemoryNote
8086197816 / 16 bits20 bits1 MB6-byte prefetch queue
8088197916 / 8 bits20 bits1 MB4-byte queue; IBM PC (1981)
80186198216 / 16 bits20 bits1 MBintegrated peripherals
80286198216 / 16 bits24 bits16 MBprotected mode
80386198532 / 32 bits32 bits4 GB32-bit x86 (IA-32)
Every later x86 processor starts up in real mode and behaves like an 8086. The programs we write with emu8086 in this course also run in 8086 real mode.

Overview

Why a 20-Bit Address?

16-bit address2¹⁶ = 65,536 bytes = 64 KB (0000h–FFFFh)20-bit address2²⁰ = 1,048,576 bytes = 1 MB (00000h–FFFFFh)But 8086 registers are 16 bits → one register can reach at most 64 KB.Solution: two 16-bit values → segment × 16 + offset = 20 bits.
n-bit address2n distinct bytes. 1 KB = 210, 1 MB = 220 bytes.
Data bus ≠ address busThe data bus is 16 bits (how much data at once), the address bus 20 bits (how much memory).
Hex notation20 bits = 5 hex digits: the lowest address is 00000h, the highest FFFFFh.

Overview

Memory Organization: The CPU Is Not Alone

Microprocessor (8086)
  • Puts all the functions of a CPU on a single integrated circuit.
  • It has no RAM or ROM inside; only registers.
  • Intermediate and final results are kept in registers.
  • Reaches memory and I/O over the system bus.
  • Needs external memory, a clock generator and buffer/latch circuits to run.
Microcontroller (e.g. 8051, AVR)
  • CPU + program memory (ROM/Flash) + RAM + timers + I/O ports on one chip.
  • Can work as a stand-alone system.
  • Embedded uses: washing machines, remote controls, sensor nodes.
  • Less computing power, lower cost and power consumption.
8284Aclock generatorCLK8086registers onlyaddress latches (8282)transceivers (8286)system busRAM/ROMI/O
An 8086-based computer = 8086 + 8284A clock generator + address latches + data bus transceivers + memory + I/O units.

Overview

System Buses

8086CPUMemoryRAM / ROMI/O deviceskeyboard, display, portsAddress bus (20 bits) — one-way: CPU → outData bus (16 bits) — two-wayControl bus — RD', WR', M/IO', INTR, READY …The “width” of a bus = its number of parallel lines (bits)
BusWhat does it carry?On the 8086
Addressthe memory location or I/O port number to read or write20 bits → 1 MB of memory; 16 bits for I/O → 64 K ports
Datainstruction and data bytes between the processor and memory/I/O16 bits, one word (2 bytes) at a time
Controlread, write, interrupt requests, status informationRD', WR', M/IO', ALE, INTR, READY …

Overview · Step through the code

Code Trace: Memory and I/O over the Buses

org 100h
mov ax, 1000h
mov ds, ax
mov bx, 0200h
mov al, [bx]     ; memory read
out 50h, al      ; I/O write
in al, 51h       ; I/O read
mov [bx+1], al   ; memory write
ret
8086AL00hDS1000hMemory10200h: 5Ah10201h: ??I/Oport 50h: ??port 51h: 3Chaddressdatacontrolno data access

Data segment DS = 1000h (base 10000h). Memory holds 5Ah at 10200h and I/O port 51h holds 3Ch (sample values).

8086AL00hDS1000hMemory10200h: 5Ah10201h: ??I/Oport 50h: ??port 51h: 3Chaddressdatacontrolno data access

BX = 0200h. Instruction fetch only; no bus cycle for data.

8086AL5AhDS1000hMemory10200h: 5Ah10201h: ??I/Oport 50h: ??port 51h: 3Chaddressdatacontrol10200h5Ah ← from memoryM/IO'=1 RD'=0 WR'=1

Memory read: the address bus carries the 20-bit 10200h, M/IO' = 1, RD' = 0. Memory puts 5Ah on the data bus → AL = 5Ah.

8086AL5AhDS1000hMemory10200h: 5Ah10201h: ??I/Oport 50h: 5Ahport 51h: 3Chaddressdatacontrol0050h (I/O)5Ah → outM/IO'=0 RD'=1 WR'=0

I/O write: the address bus carries only the port number (0050h, 16 bits), M/IO' = 0, WR' = 0; AL = 5Ah is on the data bus. Memory ignores this cycle.

8086AL3ChDS1000hMemory10200h: 5Ah10201h: ??I/Oport 50h: 5Ahport 51h: 3Chaddressdatacontrol0051h (I/O)3Ch ← from deviceM/IO'=0 RD'=0 WR'=1

I/O read: port 0051h, M/IO' = 0, RD' = 0. The device puts 3Ch on the data bus → AL = 3Ch.

8086AL3ChDS1000hMemory10200h: 5Ah10201h: 3ChI/Oport 50h: 5Ahport 51h: 3Chaddressdatacontrol10201h3Ch → outM/IO'=1 RD'=1 WR'=0

Memory write: EA = BX + 1 = 0201h → PA = 10201h, M/IO' = 1, WR' = 0, data 3Ch. Memory and I/O share the same address and data buses; M/IO' tells them apart.

Internal Structure

Two Main Units: BIU and EU

Bus Interface Unit (BIU)
  • Handles all communication with the outside world: fetches instructions from memory.
  • Reads operands from memory and writes results to memory or I/O.
  • Forms the 20-bit physical address from the segment registers and IP.
  • Fills the 6-byte prefetch queue.
Execution Unit (EU)
  • Takes the next instruction byte from the queue, decodes it and executes it.
  • Performs arithmetic and logic operations in the ALU.
  • Updates the general-purpose registers and flags.
  • Asks the BIU when it needs an operand in memory; it is not connected to the external buses.
system busBIUforms addresses · fetchesqueueEUdecodes · ALU · registers
The two units work in parallel: while the EU executes one instruction, the BIU fetches the next instruction bytes. This is the 8086's simple form of pipelining.

Internal Structure

8086 Block Diagram

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)
Left: EUgeneral registers, ALU, flags, control system
Right: BIUsegment registers, IP, address adder, queue, bus control
Bridgethe EU takes instruction bytes from the queue over the 8-bit Q bus

Internal Structure

How the 8086 Works

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

The big picture. Let us follow one instruction step by step: ADD AX, BX.

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

1. The BIU computes the physical address of the instruction to fetch: CS × 10h + IP (address adder).

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

2. The 20-bit address goes onto the external address bus; the bus control logic starts a memory read cycle.

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

3. The incoming instruction bytes enter the 6-byte FIFO queue; IP advances by the number of bytes fetched.

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

4. When its turn comes, the EU takes the opcode over the Q bus; the control system decodes the instruction.

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

5. Control signals move AX and BX over the ALU data bus into the temporary registers, then into the ALU; the add takes place.

Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

6. The result is written to AX and the flags (CF, ZF, SF, OF, PF, AF) are updated. Meanwhile the BIU keeps fetching the next bytes.

Internal Structure

Bus Interface Unit (BIU)

  • Interfaces with external memory and I/O devices over the system bus.
  • Forms the 20-bit physical address from a segment register and an offset (address adder).
  • Fetches instructions from memory and writes them to the queue.
  • Reads operands the EU requests from memory/I/O and writes results.
  • Manages the 6-byte prefetch queue → makes pipelining possible.
Inside the BIU
  • Segment registers: CS, DS, SS, ES
  • Instruction pointer: IP
  • Address adder (Σ)
  • 6-byte instruction queue
  • Bus control logic

Internal Structure

The Prefetch Queue

  • A 6-byte FIFO (first in, first out) queue: bytes are used in the order fetched.
  • While the EU executes, the BIU prefetches the next instruction bytes → pipelining.
  • Once 2 bytes are free in the queue, the BIU fetches the next word.
  • A branch (JMP, CALL, RET, conditional jump, interrupt) flushes the queue; the prefetched bytes are on the wrong path.
  • 8086 instructions are 1–6 bytes (without prefixes); the queue holds the longest in full.
BIUfetches from memorywriteB81342123014D8566-byte FIFO (first in, first out)takeEUdecodes and executestailhead
GainThe EU mostly takes instructions from the queue without waiting; memory latency is hidden.
LossA branch empties the queue; the EU waits until it refills.
The 80888-bit external bus → a 4-byte queue; fetches once 1 byte is free.

Internal Structure

Pipelining

F = fetch (BIU), E = decode + execute (EU). Each box is assumed to take 2 time units.

time →BIUEUF1E1F2E2F3E3Sequential (no pipeline): 3 instructions = 12 time units

Without a pipeline: fetch (F) first, then execute (E); the BIU and EU take turns sitting idle.

time →BIUEUF1F2F3F4F5E1E2E3E4E5Pipeline: 3 instructions = 8 units; each further one adds only 2

8086: while the EU executes E1, the BIU fetches F2. Both units work at once; throughput roughly doubles.

time →BIUEUF1F2F3F4F5E1E2E3F4, F5 discardedF targetE3 = JMP → queue flushedThe EU waits for new bytes from the target address

Branching: if E3 is a JMP, the prefetched F4 and F5 are invalid. The queue is emptied and refilled from the target address.

Internal Structure

The Queue Step by Step

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the leftEUEU now:— (waiting)IP (next instruction) = 0100hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

Start (or after a branch): the queue is empty, the EU waits. CS:IP = CS:0100h.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the leftB8341201D841EUEU now:decoding MOV AX,1234hIP (next instruction) = 0100hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The BIU fetches word by word (B8 34, 12 01, D8 41); the queue fills. The EU starts decoding as soon as the first byte arrives.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the left01D841EUEU now:MOV AX,1234hIP (next instruction) = 0103hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The EU took the 3-byte MOV AX,1234h from the queue. The queue has 3 free slots (≥ 2).

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the left01D841EB05EUEU now:MOV AX,1234hIP (next instruction) = 0103hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

While the EU executes, the BIU fetches the next word (EB 05). No waiting.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the left41EB059090EUEU now:ADD AX,BXIP (next instruction) = 0105hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The EU took 01 D8 (ADD AX,BX); the BIU fetched 90 90.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the leftEB0590909090EUEU now:INC CXIP (next instruction) = 0106hB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The EU took 41 (INC CX); the BIU fetched two more NOP bytes, and the queue is full.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the leftEUEU now:JMP SHORT 010DhIP (next instruction) = 010DhB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The EU executes EB 05: IP = 0108h + 5 = 010Dh. The 90s in the queue are invalid → the queue is flushed.

Memory (CS:address → byte)B80100340101120102010103D80104410105EB010605010790010890010990010A90010B90010CB0010DQueue (6 bytes, FIFO) — output on the leftB0EUEU now:— (waiting)IP (next instruction) = 010DhB8 34 12 = MOV AX,1234h · 01 D8 = ADD AX,BX · 41 = INC CX · EB 05 = JMP SHORT 010Dh · 90 = NOP · B0 … = MOV AL,…

The BIU starts fetching again at 010Dh (an odd address, so 1 byte first). The EU waits for the byte.

Internal Structure

Prefetch and Decode Functions

The prefetch function
  • Fetches instructions from memory and keeps them in a buffer (the queue).
  • A program counter holds the address of the next byte to fetch (on the 8086, IP and a counter inside the BIU do this).
  • The EU takes instructions from the buffer instead of memory → memory access time is hidden.
  • More instructions execute per unit of time.
The decode function
  • Turns machine code into control signals/micro-operations.
  • Runs in parallel with prefetching.
  • Decodes complex instructions such as conditional jumps, CALL and RET.
  • Steers register-to-register and memory-to-register transfers.
On the 8086 these are not separate units: prefetching happens in the BIU, decoding in the EU's control system (with a microcode ROM). Separate prefetch and decode units appear with the 80286.

Internal Structure

Execution Unit (EU)

Main parts of the EU:

  • General-purpose registers
  • Arithmetic logic unit (ALU)
  • Special-purpose registers (SP, BP, SI, DI)
  • Instruction register and decoder
  • FLAGS (status) register
  • Control system
Execution Unit (EU)Bus Interface Unit (BIU)AHALAXBHBLBXCHCLCXDHDLDXSPBPSIDIGeneralregistersPointer /indexALU data bus (16 bits)Temporary registersALUFlags registerEU controlsystemAddress adderΣCSDSSSESIPInternal comm. registersAddress bus (20 bits)Buscontrollogic16external buses ⇅123456Instruction queue (6 bytes)Q bus (8 bits)

Internal Structure

Arithmetic Logic Unit (ALU)

  • Performs 8- and 16-bit arithmetic and logic operations.
  • Arithmetic: add, subtract, multiply, divide, increment/decrement, compare.
  • Logic: AND, OR, XOR, NOT, shifts and rotates.
  • Updates the flags according to each result.
InstructionOperationSize
ADD AX, BXAX ← AX + BX16 bits
SUB AL, 5AL ← AL − 58 bits
MUL BLAX ← AL × BL8 → 16
DIV CXAX ← DX:AX ÷ CX, DX ← remainder32 → 16
AND AL, 0Fhclears the upper 4 bits of AL8 bits
XOR AX, AXAX ← 0 (ZF = 1)16 bits

Internal Structure · Step through the code

Code Trace: What Does the ALU Do?

org 100h
mov ax, 0010h
mov bx, 0030h
add ax, bx      ; AX ← AX + BX
sub al, 5       ; AL ← AL − 5
mov bl, 4
mul bl          ; AX ← AL × BL
mov dx, 0
mov cx, 3
div cx          ; AX ← DX:AX ÷ CX
and al, 0Fh     ; clear upper 4 bits
xor ax, ax      ; AX ← 0
ret
AX0010hBX0030hALU(unused)flagsCF0ZF0SF0OF0AX = 0010hDX = 0000hBX = 0030hCX = 0000h

The operands are loaded into registers: AX = 0010h, BX = 0030h. MOV does not use the ALU; the flags do not change.

AX0010hBX0030hALU+AX = 0040hflagsCF0ZF0SF0OF0AX = 0040hDX = 0000hBX = 0030hCX = 0000h

add ax, bx: the ALU does a 16-bit add → AX = 0040h. No carry (CF = 0), result not zero (ZF = 0).

AL40himmediate05hALU−AL = 3BhflagsCF0ZF0SF0OF0AX = 003BhDX = 0000hBX = 0030hCX = 0000h

sub al, 5: an 8-bit subtract; only AL changes → AL = 3Bh (AX = 003Bh). No borrow: CF = 0.

AL3BhBL04hALU×AX = 00EChflagsCF0ZF?SF?OF0AX = 00EChDX = 0000hBX = 0004hCX = 0000h

BL = 4. mul bl: the product of 8-bit operands is 16 bits: 3Bh × 04h = 00ECh → AX. AH = 0, so CF = OF = 0; SF and ZF are undefined (?).

DX:AX000000EChCX0003hALU÷flagsCF0ZF?SF?OF0AX = 00EChDX = 0000hBX = 0004hCX = 0003h

Setting up the divide: the dividend is the 32-bit DX:AX pair, so DX = 0; the divisor is CX = 3.

DX:AX000000EChCX0003hALU÷AX=004Eh · DX=0002hflagsCF?ZF?SF?OF?AX = 004EhDX = 0002hBX = 0004hCX = 0003h

div cx: 236 ÷ 3 = 78 remainder 2 → quotient AX = 004Eh, remainder DX = 0002h. Flags are undefined after DIV.

AL4Ehmask0FhALUANDAL = 0EhflagsCF0ZF0SF0OF0AX = 000EhDX = 0002hBX = 0004hCX = 0003h

and al, 0Fh: 0100 1110 AND 0000 1111 = 0000 1110 → AL = 0Eh. The upper 4 bits are cleared; AND sets CF = OF = 0.

AX000EhAX000EhALUXORAX = 0000hflagsCF0ZF1SF0OF0AX = 0000hDX = 0002hBX = 0004hCX = 0003h

xor ax, ax: any bit XORed with itself is 0 → AX = 0000h, ZF = 1. A shorter (2-byte) way to clear than mov ax, 0.

Internal Structure

Instruction Register, Decoder and Control

Instruction register

The EU takes the next instruction from the queue; its opcode is held here while it is decoded. Programs cannot access this register.

Instruction decoder

Decodes the opcode: which operation, which operands, which addressing mode? Passes that information to the control circuitry.

Control unit

Directs the flow of instructions and data inside the processor; sequences the fetch–decode–execute cycle and coordinates the other parts.

The control unit fetches instructions (through the BIU), decodes and executes them, updates the processor state (registers, flags), and checks for interrupt requests (INTR, NMI) at the end of each instruction. Error conditions (e.g. divide by zero) are handled by special interrupts.

The 8086 has no power management; those features came much later (e.g. the 386SL). The 8086's control unit is microcoded.

Internal Structure

Instruction Lengths

8086 instructions have variable length: opcode + (optional) addressing byte + displacement + immediate data.

InstructionMachine code (hex)BytesDescription
INC CX411opcode only
MOV AX, BX8B C32opcode + ModR/M byte
ADD AL, 7Ch04 7C2short AL form + 8-bit immediate
MOV BX, 4050hBB 50 403opcode + 16-bit immediate (little-endian)
MOV WORD PTR [BX+1234h], 5678hC7 87 34 12 78 566ModR/M + 16-bit displacement + 16-bit immediate
Immediate values are stored little-endian (low byte first): 4050h → 50 first, then 40.

A register-to-register MOV is 2 bytes (8B C3 or the equivalent 89 D8).

Internal Structure · Step through the code

Code Trace: Bytes and IP

org 100h
inc cx
mov bx, 4050h
mov ax, bx
add al, 7Ch
mov word ptr [bx+1234h], 5678h
ret
code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPINC CX1 byte · opcodeIP: 0100h → 0101h

The program starts at 0100h. inc cx is just an opcode: 41 (1 byte) → IP = 0101h. CX = 1.

code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPMOV BX,4050h3 bytes · opcode + 16-bit immediateIP: 0101h → 0104h

BB 50 40: BB = “16-bit constant into BX”; the constant is little-endian (50 first, then 40). 3 bytes → IP = 0104h.

code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPMOV AX,BX2 bytes · opcode + ModR/MIP: 0104h → 0106h

8B C3: 8B = MOV r16, r/m16; ModR/M C3 = 11 000 011 → mod 11 (register), reg AX, r/m BX. 2 bytes → IP = 0106h.

code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPADD AL,7Ch2 bytes · short AL form + 8-bit immediateIP: 0106h → 0108h

04 7C: the short AL form + an 8-bit constant. AL = 50h + 7Ch = CCh, AX = 40CCh. IP = 0108h.

code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPMOV WORD PTR [BX+1234h],5678h6 bytes · opcode + ModR/M + disp + immIP: 0108h → 010EhDS:5284h7856data: 5678h

6 bytes: C7 + ModR/M 87 (mod 10: 16-bit displacement, r/m BX) + 34 12 + 78 56. EA = 4050h + 1234h = 5284h; 5678h is written there. IP = 010Eh.

code segment (CS:0100h–010Eh)41BB50408BC3047CC78734127856C3IPRET1 byte · opcodeIP: 010Eh → 0000hDS:5284h7856data: 5678h

C3 = RET (1 byte); IP gets 0000h from the stack (the COM program returns to DOS). Six instructions took 15 bytes: lengths range from 1 to 6 bytes.

Registers

The Register Set

General-purpose (data) registersAXAHALaccumulatorBXBHBLbaseCXCHCLcounterDXDHDLdata15 8 7 0Pointer and indexSPstack pointerBPbase pointerSIsource indexDIdestination indexSegmentCScodeDSdataSSstackESextraSpecialIPinstruction pointerFLAGSflags4 + 4 + 4 + 2 = 14 registers of 16 bits
  • General-purpose: hold data and take part in arithmetic/logic; each splits into two 8-bit halves.
  • Segment registers: point to the bases of the four active segments in memory. Special: IP points to the next instruction, FLAGS reflects the last result.

Registers

Internal Registers and Word Size

  • The 8086 is a 16-bit integer (scalar, non-vector) processor; floating point needs a separate coprocessor (the 8087).
  • It comes in a 40-pin dual in-line package (DIP).
  • The size of the on-chip registers sets how much data the processor handles at once (the word size) → 16 bits.
  • It gives the programmer 14 registers of 16 bits.
  • Registers are the fastest storage in the processor: a memory access needs a bus cycle (≥ 4 clocks), a register access does not.
8 bits0 … 255 (FFh)
16 bits0 … 65,535 (FFFFh)
32 bits (DX:AX)0 … 4,294,967,295

Registers

Segment Registers

  • Segments live in memory; segment registers live inside the processor.
  • Each segment register holds a segment's start address / 16 (segment base = register × 10h).
CS Code SegmentProgram code; CS:IP is the next instruction
DS Data SegmentVariables and data
SS Stack SegmentThe stack; SS:SP, SS:BP
ES Extra SegmentExtra data; string destination ES:DI
Rules
  • A segment register cannot be loaded with an immediate: MOV DS, 2000h is invalid; use MOV AX, 2000h, then MOV DS, AX.
  • CS cannot be changed with MOV; JMP FAR, CALL FAR, RET FAR, INT and IRET change it.
  • There is no direct segment-to-segment MOV (MOV ES, DS is invalid).

Registers

Instruction Pointer (IP)

  • A 16-bit register.
  • Holds the offset of the next instruction in the code segment.
  • Advances by each instruction's length as its bytes are fetched.
  • Branch instructions (JMP, Jcc, CALL, RET, LOOP, INT) load IP with a new value.
  • Programs cannot read or write IP directly (there is no MOV IP, …).
  • Physical address of the next instruction: CS × 10h + IP.
Example

CS = 4321h, IP = 1000h

  43210h   ; CS × 10h
+ 01000h   ; IP
--------
  44210h

The next instruction is at physical address 44210h.

Segmented Memory

Segmented Memory Architecture

  • The 8086 addresses memory in segments.
  • Every memory address is given as two 16-bit parts: segment : offset, e.g. 2000h:0010h.
  • The segment register points to the start of the segment.
  • The offset is the distance of the byte from the start of the segment (0000h–FFFFh).
  • The address adder in the BIU combines the two into a 20-bit physical address.
Logical vs. physical address

Logical address: the segment:offset pair the program uses.

Physical address: the actual 20-bit address placed on the address bus.

PA = segment × 10h + offset

segment (16 bits)2000h× 10hsegment base20000hΣoffset 0010hphysical address (20 bits)20010hA19–A0

Segmented Memory

Segment and Offset

1 MB of memoryFFFFFh00000h10000h1FFFFhSegment register1000h

Memory is 1 MB: 00000h–FFFFFh. We will build a 20-bit address from a segment register and an offset.

1 MB of memoryFFFFFh00000h10000h1FFFFhSegment register1000h

The segment register holds 1000h (16 bits).

1 MB of memoryFFFFFh00000h10000h1FFFFh64 KB segmentSegment register1000h× 10h (<< 4)10000hbase

The segment value is shifted left 4 bits (× 10h): 1000h → 10000h. A segment base is always a multiple of 16 (a paragraph boundary).

1 MB of memoryFFFFFh00000h10000h1FFFFh64 KB segmentSegment register1000h× 10h (<< 4)10000hbaseOffsetF000h

The offset is F000h. The largest offset is FFFFh, so the segment spans 10000h–1FFFFh (64 KB).

1 MB of memoryFFFFFh00000h10000h1FFFFh64 KB segmentSegment register1000h× 10h (<< 4)10000hbaseOffsetF000h+1F000h10000h + F000h = 1F000h

The adder: 10000h + F000h = 1F000h. This 20-bit value goes onto the address bus.

Segmented Memory

Step by Step: CS:IP → Physical Address

Example 1: CS = 4321h, IP = 1000h4321CS (16 bits)

CS = 4321h (4 hex digits = 16 bits).

Example 1: CS = 4321h, IP = 1000h43210CS × 10h← a 0 appended on the right

× 10h = append a 0 on the right in hex (shift left 4 bits): 43210h.

Example 1: CS = 4321h, IP = 1000h43210CS × 10h1000+ IP

IP = 1000h is written below, right-aligned (the leftmost digit is blank = 0).

Example 1: CS = 4321h, IP = 1000h43210CS × 10h1000+ IP44210= physical43210h + 1000h = 44210h

Add the digits from the right: 0+0, 1+0, 2+0, 3+1, 4+0 → 44210h. No carry.

Example 2: CS = F000h, IP = FFF0hF000CS (16 bits)

CS = F000h, IP = FFF0h: the address of the first instruction in the BIOS after reset (the 8086 loads CS = FFFFh, IP = 0000h on reset; both give the same byte).

Example 2: CS = F000h, IP = FFF0hF0000CS × 10h← a 0 appended on the right

F000h × 10h = F0000h.

Example 2: CS = F000h, IP = FFF0hF0000CS × 10hFFF0+ IP

IP = FFF0h is right-aligned.

Example 2: CS = F000h, IP = FFF0hF0000CS × 10hFFF0+ IPFFFF0= physicalF0000h + FFF0h = FFFF0h

F0000h + FFF0h = FFFF0h: the last 16 bytes of the 1 MB. After reset the processor takes its first instruction from here (the same address as FFFFh:0000h).

Segmented Memory

Demo: Physical Address Calculator

:

  
  

The map shows the segment base and the 64 KB segment range. If the sum passes 1 MB, the 8086 truncates the address to 20 bits (wraparound).

Segmented Memory

Memory Segmentation

00000h40000h80000hC0000hFFFFFh1 MB = 16 × 64 KBCS=1000h10000h–1FFFFhDS=2000h20000h–2FFFFhES=2800h28000h–37FFFhSS=3000h30000h–3FFFFhThe ES segment overlaps DS and SS: segments may overlap.
  • The 20-bit address bus reaches 1 MB; a segment is at most 64 KB (16-bit offset).
  • Only 4 segments are active at any time (CS, DS, SS, ES) → at most 256 KB is directly reachable at once. To reach another area, change a segment register.
  • Segments are not 16 fixed pieces: one can start at any paragraph (16-byte) boundary, and segments may overlap or be disjoint.

1 MB / 64 KB = 16 is only arithmetic; 8086 segment starts are not fixed (65,536 possible starts).

Segmented Memory

Why Segmentation?

Pros

  • 16-bit registers can address 20 bits (1 MB) of memory.
  • Code, data and stack are kept apart; the program layout is clear.
  • Relocatability: a program can be loaded elsewhere in memory just by changing the segment registers.
  • Instructions stay short: most addresses are written as 16-bit offsets.

Cons

  • A single array or structure cannot exceed 64 KB (otherwise you must switch segments).
  • One physical address has many segment:offset forms → comparing pointers gets harder.
  • There is no protection between segments: a bad write can corrupt another segment.
  • Address arithmetic burdens the programmer.

Segmented Memory

Same Address, Different Segment:Offset

Logical addressCalculationPhysical address
1234h:0005h12340h + 0005h12345h
1000h:2345h10000h + 2345h12345h
1200h:0345h12000h + 0345h12345h
0234h:FFF5h02340h + FFF5h12335h (not 12345h)
FFFFh:0010hFFFF0h + 0010h = 100000h00000h (wraps to 20 bits on the 8086)
Normalization: to bring the offset into 0–Fh, take segment = the first 4 digits of the PA and offset = the last digit. 12345h → 1234h:0005h. A physical address has at most 4096 different forms.

Segmented Memory · Step through the code

Code Trace: The Address Wraps Around at 1 MB

org 100h
mov ax, 0
mov ds, ax
mov byte ptr [0500h], 77h
mov ax, 0FFFFh
mov ds, ax
mov bl, [0510h]    ; FFFF:0510
ret
DS0000hBL00hphysical 00500h00h

DS = 0000h: the segment is at the very start of memory. A segment register cannot take a constant, so the value goes through AX.

DS0000hBL00hphysical 00500h77h0000h × 10h + 0500h= 00500h

mov byte ptr [0500h], 77h: 0000:0500 → 77h is written to physical 00500h.

DSFFFFhBL00hphysical 00500h77h

DS = FFFFh: the segment starts at FFFF0h, the last 16 bytes of the 1 MB.

DSFFFFhBL77hphysical 00500h77hFFFF0h + 0510h = 100500hbit 21 dropped → 00500hwrapped

mov bl, [0510h]: the sum needs 21 bits; the 8086's 20 address lines carry only 00500h. The same byte is read: BL = 77h.

DSFFFFhBL77hphysical 00500h77h

RET. Addresses from FFFF:0010 to FFFF:FFFF fall into 00000h–0FFEFh at the start of memory.

On the 80286 and later, if the 21st address line (A20) is enabled, FFFF:0510 really reaches 100500h; it can be disabled because some old 8086 programs rely on the wraparound.

Segmented Memory

Default Segments

Access typeSegmentOffset sourceCan it be overridden?
Instruction fetchCSIPno
Stack operations (PUSH, POP, CALL, RET)SSSPno
BP addressing [BP+…]SSeffective addressyes: DS:[BP]
General data [BX], [SI], [DI], [1234h]DSeffective addressyes: ES:[BX]
String source (MOVSB, LODSB)DSSIyes
String destination (MOVSB, STOSB)ESDIno
A segment override prefix changes the default segment: MOV AL, ES:[BX] → physical address = ES × 10h + BX. The prefix adds 1 byte (26h) to the instruction.

Segmented Memory · Step through the code

Code Trace: One Offset, Three Segments

org 100h
mov ax, 2000h
mov ds, ax
mov ax, 3000h
mov es, ax
mov bx, 0010h
mov bp, 0010h
mov byte ptr [bx], 11h    ; DS
mov byte ptr [bp], 22h    ; SS
mov byte ptr es:[bx], 33h ; ES
mov al, [bx]              ; DS
mov ah, [bp]              ; SS
ret
segment registersDS = 2000hES = 0700hSS = 0700hBX = 0000h · BP = 0000hES segment (not yet)30010h??DS segment20010h??SS segment07010h??

A segment register cannot take a constant; DS = 2000h goes through AX. In a COM program SS = CS (0700h here).

segment registersDS = 2000hES = 3000hSS = 0700hBX = 0000h · BP = 0000hES segment30010h??DS segment20010h??SS segment07010h??

Likewise ES = 3000h. Now there are three segment bases: 07000h (SS), 20000h (DS), 30000h (ES).

segment registersDS = 2000hES = 3000hSS = 0700hBX = 0010h · BP = 0010hES segment30010h??DS segment20010h??SS segment07010h??

BX and BP hold the same offset: 0010h. The segment used, not the offset, decides which byte is accessed.

DS × 10h + BX20000h+ 0010h20010hwrite 11hES segment30010h??DS segment20010h11hSS segment07010h??

[bx] uses DS by default: 2000h × 10h + 0010h = 20010h ← 11h.

SS × 10h + BP07000h+ 0010h07010hwrite 22hES segment30010h??DS segment20010h11hSS segment07010h22h

[bp] uses SS by default: 0700h × 10h + 0010h = 07010h ← 22h. Same offset, different byte.

ES × 10h + BX30000h+ 0010h30010hwrite 33hES segment30010h33hDS segment20010h11hSS segment07010h22h

The es: prefix selects ES instead of DS (+1 byte, 26h): 30000h + 0010h = 30010h ← 33h.

DS × 10h + BX20000h+ 0010h20010hread → ALES segment30010h33hDS segment20010h11hSS segment07010h22h

mov al, [bx] reads DS:0010h again → AL = 11h (not the 33h at ES:0010h).

SS × 10h + BP07000h+ 0010h07010hread → AHES segment30010h33hDS segment20010h11hSS segment07010h22h

mov ah, [bp] reads SS:0010h → AH = 22h. Result AX = 2211h: three accesses, one offset, three physical addresses.

Segmented Memory · Step through the code

Code Trace: Access by Segment:Offset

AX ← 2000h. A segment register cannot take an immediate, so the value goes into AX first.

DS ← AX = 2000h. The data segment is now 20000h–2FFFFh.

BX ← 0010h (offset).

PA = 2000h×10h + 0010h = 20010h. 41h ('A') is written there.

AX ← 2001h.

DS ← 2001h: the segment base moved 16 bytes (one paragraph) → 20010h–3000Fh.

BX ← 0000h.

PA = 20010h + 0000h = 20010h → AL = 41h. 2000h:0010h and 2001h:0000h are the same byte!

SI ← FFFFh: the largest offset.

PA = 20010h + FFFFh = 3000Fh: the last byte of this segment. AH gets the value in that byte.

mov ax, 2000h
mov ds, ax          ; DS = 2000h
mov bx, 0010h
mov byte ptr [bx], 41h
mov ax, 2001h
mov ds, ax          ; +16 bytes
mov bx, 0000h
mov al, [bx]        ; same byte
mov si, 0FFFFh
mov ah, [si]        ; last byte

Summary

Summary

Overview

16-bit data bus and registers, 20-bit address bus → 1 MB. CISC, variable-length instructions.

BIU + EU

The BIU fetches and forms addresses; the EU decodes and executes. A 6-byte FIFO queue lets them work in parallel.

14 registers

AX, BX, CX, DX · SP, BP, SI, DI · CS, DS, SS, ES · IP, FLAGS.

Segment registers

CS, DS, SS and ES point to the bases of the four active segments; a segment register cannot take an immediate.

Address calculation

PA = segment × 10h + offset. A segment is ≤ 64 KB, starts on a paragraph boundary, and segments may overlap.

Branches

Load IP with a new value and flush the prefetch queue.

Summary

Exercises

  1. If CS = 2F00h and IP = 01A4h, what is the physical address of the next instruction?
  2. Write the physical address 3A2C5h in normalized segment:offset form; with DS = 3000h, what must the offset be?
  3. Why does MOV DS, 1000h not assemble? Give the correct form.
  4. If the EU executes a JMP while the queue holds 6 bytes, what happens to the queue? Why?
  5. Can the 8086 execute one instruction while fetching another? Thanks to which units?
Hints / answers
  1. 2F000h + 01A4h = 2F1A4h.
  2. 3A2Ch:0005h; with DS = 3000h the offset = 3A2C5h − 30000h = A2C5h.
  3. A segment register cannot take an immediate: MOV AX, 1000h + MOV DS, AX.
  4. The queue is flushed; the prefetched bytes came from the following addresses, not from the jump target.
  5. Yes: the BIU fetches while the EU executes (pipelining).

Summary

References

  • Intel, 8086 16-Bit HMOS Microprocessor data sheet (231455).
  • Intel, The 8086 Family User's Manual, 1979.
  • B. B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009.
  • M. A. Mazidi, J. G. Mazidi, D. Causey, The x86 PC: Assembly Language, Design, and Interfacing, 5th ed., Pearson, 2010.
  • emu8086 documentation and instruction set reference.

The End

Chapter 2.1: 8086 Architecture
Previous: Chapter 1.2: Number Systems · Next: Chapter 2.2: 8086 Registers
1 / 1 Sercan KÜLCÜ. All rights reserved.