Microprocessors · Lecture Notes
Chapter 2.1: 8086 Architecture
Every line written in assembly language drives a concrete piece of hardware inside the processor: it loads a value into a register, makes the ALU perform an addition, or puts a 20-bit address on the address bus. That is why you need to know the inside of the 8086 before programming it. This chapter starts with the general features of the Intel 8086 and explains how the processor is connected to memory and to I/O devices over buses. It then examines two internal units, the bus interface unit (BIU) and the execution unit (EU), together with the prefetch queue and the pipeline. It shows the ALU’s operations and the byte lengths of instructions in memory through code traces. The last sections cover the register set and the 8086’s most distinctive feature, segmented memory: how a 20-bit physical address is produced from two 16-bit values, why the same byte can be named in many ways, and which access uses which segment.
Learning outcomes
- Explain the general features
- State the data bus and address bus widths of the 8086, the amount of memory it can address, its place in the x86 family, and which chips make up an 8086 system.
- Follow the buses
- Separate the roles of the address, data, and control buses; show in code which values are on the buses during a memory or I/O read and write.
- Describe the internal structure
- Explain the roles of the BIU and EU, how the prefetch queue works, the gain from the pipeline, and the effect of a branch on the queue; trace the fetch and execution of an instruction on a block diagram.
- Read machine code
- Separate the opcode, ModR/M byte, displacement, and immediate fields of an instruction; find the value of IP at each step from the instruction lengths.
- Group the registers
- Divide the 14 registers into groups; explain the usage rules of the segment registers and the role of IP.
- Compute physical addresses
- Find the 20-bit physical address with
segment × 10h + offset; normalize an address; show the wraparound at the 1 MB limit and the default segment rules with code traces.
2.1.1Overview of the 8086
The Intel 8086 is a 16-bit microprocessor that came to market in 1978. It is the first member of the x86 processor family that runs in desktop and laptop computers today. Over more than forty years the processors of this family grew to 32 and 64 bits and their clock rates rose thousands of times. Even so, every new member kept running instructions written for the 8086. Learning the 8086’s registers, address computation, and instruction format therefore means learning the lowest layer of today’s processors.
This section first gives the 8086’s basic numbers: how many bits of data it works with, how much memory it can address, and what technology it is made in. It then describes the 8086’s place in the family and why the 20-bit address bus was chosen. Finally it shows that a microprocessor is not a computer by itself and what chips it needs around it.
2.1.1.1Basic features
Calling a processor “16-bit” describes the width of the data it can work on at once. The 8086’s general-purpose registers are 16 bits. The arithmetic logic unit (ALU) adds two 16-bit numbers in a single operation. The external data bus is also 16 lines wide, so the processor exchanges 2 bytes with memory at a time. The equality of these three widths makes the 8086 a “fully 16-bit” processor.
The address bus, on the other hand, has 20 lines. Since each line carries one bit, the processor can produce 220 different addresses. 220 = 1,048,576 bytes, that is, 1 MB. Addresses are written in hexadecimal with five digits: the lowest address is 00000h and the highest is FFFFFh.
| Feature | Value | Meaning |
|---|---|---|
| Data bus | 16 bit | at most 2 bytes are carried in one bus cycle |
| Address bus | 20 bits | 220 bytes = 1 MB of memory is addressed |
| Registers, ALU | 16 bit | the word size is 16 bits |
| Package | 40-pin DIP | dual in-line plastic or ceramic package |
| Clock | 5–10 MHz | 8086: 5 MHz, 8086-2: 8 MHz, 8086-1: 10 MHz |
| Transistors | about 29,000 | HMOS (high-density n-channel MOS) technology |
| Supply | +5 V | a single supply voltage |
| Architecture | CISC | instructions 1–6 bytes long, instructions that operate on memory |
Table 2.1.1 collects these numbers. The clock speed is the frequency of the square wave that synchronizes all the operations inside the processor. One period of a 5 MHz clock lasts 1 / 5,000,000 s = 200 ns. This time is called a clock cycle. On the 8086 the simplest instructions finish in a few clock cycles; complex instructions such as multiply and divide can take hundreds of cycles.
In a CISC (complex instruction set computer) design the instructions have different lengths, and an instruction can work directly with an operand in memory. For example, the ADD AX, [BX] instruction reads a word from memory and adds it to AX. This design aimed to keep programs short when memory was expensive: frequently used jobs were fitted into a single instruction.
The 8086 also has a sibling: the 8088. The 8088’s registers and ALU are 16 bits, but its external data bus is 8 lines. To read a word from memory it makes two separate bus cycles. IBM chose the 8088 for its first personal computer in 1981; the 8-bit bus combined easily with the cheap 8-bit peripheral chips of those years. From the programmer’s point of view the two processors run the same instructions.
The idea that “a 16-bit processor addresses 64 KB of memory” is wrong. The width of the data bus determines how much data is carried at a time, and the width of the address bus determines how much memory can be selected. The two are independent of each other. The 8086’s data is 16 bits and its address 20 bits; so it addresses 1 MB, not 64 KB.
2.1.1.2Its place in the x86 family
The processors that came after the 8086 were designed so that they could run programs written for their predecessors unchanged. This principle is called backward compatibility. Table 2.1.2 compares the first five members of the family.
| Processor | Year | Internal / external data bus | Address bus | Memory | Notable feature |
|---|---|---|---|---|---|
| 8086 | 1978 | 16 / 16 bits | 20 bits | 1 MB | 6-byte prefetch queue |
| 8088 | 1979 | 16 / 8 bits | 20 bits | 1 MB | 4-byte queue; IBM PC (1981) |
| 80186 | 1982 | 16 / 16 bits | 20 bits | 1 MB | integrated peripherals |
| 80286 | 1982 | 16 / 16 bits | 24 bits | 16 MB | protected mode |
| 80386 | 1985 | 32 / 32 bits | 32 bits | 4 GB | 32-bit x86 (IA-32) |
The memory column of the table comes directly from the address bus: 24 bits gives 224 = 16,777,216 bytes (16 MB), and 32 bits gives 232 = 4,294,967,296 bytes (4 GB). The 80186 adds peripherals such as a clock generator, timers, and an interrupt controller to the 8086 core, packing more work into a single chip for embedded systems. The 80286 brings protected mode: in this mode programs cannot access each other’s memory, and all 16 MB can be used.
Real mode is the operating mode in which x86 processors behave like an 8086: addresses are computed as 20-bit physical addresses from a 16-bit segment and offset pair, there is no memory protection, and the directly accessible space is 1 MB. Since the 80286, every x86 processor starts in real mode when powered on.
All programs written with emu8086 in this course run in 8086 real mode. On a computer today, too, the processor is in real mode in the first moments of boot; the operating system later switches it to protected or 64-bit mode.
2.1.1.3Why a 20-bit address?
Memory is an ordered sequence of cells, each holding one byte. Each cell has a number, that is, an address. To access a cell, the processor puts its address on the address bus. An n-line address bus can carry 2n different bit patterns, so it can select 2n different bytes.
Let us compare the capacity of 16-bit and 20-bit address buses.
- 16 bits: 216 = 65,536 bytes. Since 1 KB = 210 = 1024 bytes, 65,536 / 1024 = 64 KB. Addresses
0000h–FFFFh. - 20 bits: 220 = 1,048,576 bytes. Since 1 MB = 220 bytes, exactly 1 MB. Addresses
00000h–FFFFFh. - Ratio: 220 / 216 = 24 = 16. Four extra lines multiply the addressable memory by 16.
Each hexadecimal digit is four bits; 20 bits is exactly five digits. That is why physical addresses are always written with five digits.
The designers of the 8086 faced a contradiction here. In 1978, 64 KB of memory had become too small; a larger address space was needed. But the processor’s registers were 16 bits, and a single register could point to only a 64 KB range. Widening the registers to 20 bits would have enlarged the chip and broken compatibility with software from 8-bit processors. The solution was to produce the address from two 16-bit values: one gives the start of a region of memory, that is, the segment, and the other gives the distance within that region, that is, the offset. The processor multiplies the segment value by 16 and adds the offset. This computation yields a 20-bit result. Figure 2.1.1 summarizes the idea.
2.1.1.4Microprocessor and microcontroller
The 8086 is a microprocessor: it puts all the functions of the central processing unit (CPU) in a single integrated circuit. But it has no memory inside to store programs or data. Inside the chip there are only registers; intermediate and final results are kept in these registers. Program code, variables, and the stack are in external RAM and ROM chips. The processor reaches this memory and the I/O (input/output) devices over the system bus.
A microprocessor is an integrated circuit that puts the CPU’s fetch, decode, and execute functions on a single chip but leaves memory and I/O units outside. A microcontroller (MCU), on the other hand, carries program memory (ROM or Flash), RAM, timers, and I/O ports on the same chip together with the CPU.
The two approaches have different uses. A microcontroller (8051, AVR, PIC) can work like a complete system by itself: once power and a crystal are connected, it reads its program from its own memory, lights lamps from its ports, and reads sensors. In embedded applications such as washing machines, remote controls, and sensor nodes, low cost and low power consumption stand out. Its processing power, however, is generally limited. A microprocessor is flexible: the amount of memory and the peripherals are chosen to suit the application, but the system consists of many chips.
Figure 2.1.2 shows the parts of the smallest 8086 system. The job of each chip is as follows:
- The 8284A clock generator gives the 8086 its clock signal (CLK). It divides the frequency of the external crystal by three: a 15 MHz crystal produces a 5 MHz clock. It also synchronizes the RESET and READY signals with the clock.
- Address latches (8282 or 74LS373) capture the address and hold it for the length of the bus cycle. The 8086’s 40 pins are not enough to give every signal its own pin. For this reason the low 16 bits of the address and the data come out on the same pins (AD0–AD15) at different times. At the start of the cycle the pins carry the address; the processor produces an ALE (address latch enable) pulse and the latches capture the address. Then the same pins switch to data, but the memory keeps seeing the address in the latch. Three 8-bit latches are needed for 20 bits.
- Data bus transceivers (8286) amplify the data lines and set their direction: from memory to processor on a read, from processor to memory on a write. Two are used for 16 bits.
- Memory (RAM and ROM) holds the program and data; the I/O devices connect to the outside world, such as the keyboard, display, and ports.
These extra chips are the price of a microprocessor. In return the system’s memory can be expanded up to 1 MB and any kind of peripheral can be added. Personal computers chose this path.
2.1.2System buses
The processor, memory, and I/O devices are connected by parallel bundles of wires. Each bundle is called a bus. The width of a bus is the number of lines in it; each line carries one bit at a time. There are three buses in the system, and each has its own job.
2.1.2.1Address, data, and control buses
The system bus is the whole of the three buses that connect the CPU, memory, and I/O devices. The address bus selects the memory location or I/O port to be accessed and works only from the processor outward. The data bus carries instruction and data bytes in both directions. The control bus consists of signals that tell the type of the operation (read, write, memory or I/O) and its timing.
| Bus | What it carries | Direction | On the 8086 |
|---|---|---|---|
| Address | the memory location to be read or written, the I/O port number | CPU → outside | 20 bits → 1 MB of memory; for I/O 16 bits → 64 K ports |
| Data | instruction and data bytes between the processor and memory or I/O | bidirectional | 16 bits, one word (2 bytes) at a time |
| Control | read, write, memory/I/O select, interrupt requests, status information | depends on the signal | RD', WR', M/IO', ALE, INTR, READY … |
Only the processor produces the address; a memory or I/O device puts nothing on the address bus. That is why the address bus is unidirectional. Data, on the other hand, flows from outside to the processor on a read and from the processor outward on a write; the data bus is bidirectional. Some of the signals on the control bus are produced by the processor (RD', WR', M/IO', ALE), and some by external devices (INTR, the interrupt request, and READY, the ready signal).
The prime mark (') at the end of a signal name shows that the signal is active low: the signal does its job when it is at logic level 0. RD' = 0 means “read now,” and WR' = 0 means “write now.” The M/IO' signal selects the target of the access: memory when 1, I/O when 0. Memory chips respond only when M/IO' = 1, and I/O devices respond only when M/IO' = 0. In this way the same address and data lines serve two separate address spaces.
The I/O address space is smaller than memory. In an I/O access only the low 16 bits of the address (A15–A0) are used, and the A19–A16 lines are 0. There are 216 = 65,536 port numbers. The IN and OUT instructions take the port number either as an 8-bit constant inside the instruction (0–FFh) or in the DX register (0–FFFFh).
2.1.2.2The bus cycle
Each time the processor reads once or writes once to memory or I/O, it takes one bus cycle. On the 8086 a bus cycle consists of at least four clock cycles. These cycles are called T states and are named T1, T2, T3, and T4.
A bus cycle is the time period over which a single read or write operation with memory or I/O runs from start to finish. On the 8086 it consists of four T states (T1–T4); a slow device can have wait states (Tw) inserted between T3 and T4 by holding the READY signal at 0.
A memory read cycle proceeds in this order:
- T1: The processor puts the 20-bit address on the address lines, announces the target with M/IO', and issues the ALE pulse. The latches capture the address.
- T2: The AD0–AD15 pins are released from the address and become free for data. The processor makes the RD' signal 0: “read.”
- T3: The memory puts the byte or word at the address on the data bus. The processor samples READY; if the device is ready it takes the data.
- T4: RD' becomes 1 again and the memory releases the data bus. The cycle ends.
In a write cycle the order is the same; the difference is that the processor puts the data on the data bus starting at T2 and makes the WR' signal 0 instead of RD'. While WR' is active, the memory writes the value on the data bus into its cell.
A 5 MHz 8086 reads 100 words from memory with no wait states. How long does it take at least?
- One clock cycle: 1 / 5 MHz = 200 ns.
- One bus cycle: 4 × 200 ns = 800 ns.
- If the words are at even addresses, each word arrives in a single cycle: 100 × 800 ns = 80,000 ns = 80 µs.
This shows that the bus’s maximum transfer rate is 2 bytes / 800 ns = 2.5 MB/s. If the words were at odd addresses, each would take two cycles and the time would be 160 µs. The 8086’s memory is divided into two byte banks, even-addressed and odd-addressed. The bytes of a word starting at an odd address fall in different rows and cannot be read together in a single cycle.
Comparing these times with registers makes one thing clear: memory is slow. A register-to-register MOV AX, BX takes 2 clock cycles on the 8086. MOV AX, [BX], which reads the same value from memory, takes 13 cycles including the address calculation and the bus cycle. That is why frequently used values are kept in registers and why the 8086’s internal structure is arranged to hide memory waiting.
2.1.2.3Code trace: memory and I/O on the buses
The following program makes four accesses: it reads from memory, writes to an I/O port, reads from an I/O port, and writes to memory. Following which values are on the buses in each access makes the roles of the address, data, and control buses concrete. At the start of the program it is assumed that memory at address 10200h holds 5Ah and the I/O port numbered 51h holds 3Ch.
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
retorg 100h is not an instruction but a directive given to the assembler. It declares that the program, in .COM form, starts at offset 100h of the segment. The first two instructions load 1000h into the DS register. The value is moved first to AX and then from AX to DS, because on the 8086 a constant cannot be loaded directly into a segment register. With DS = 1000h the data segment starts at physical address 10000h. mov bx, 0200h puts the offset into BX. These three instructions change only registers; no bus is used for data. The buses work only to fetch the instruction bytes.
mov al, [bx] is the first data access. The square brackets mean “the memory location BX points to.” The processor computes the physical address as DS × 10h + BX = 10000h + 0200h = 10200h and starts a memory read cycle: 10200h on the address bus, M/IO' = 1, RD' = 0. The memory puts 5Ah on the data bus and the processor writes it into AL.
out 50h, al sends the value in AL to port number 50h. This is an I/O write cycle. Only the port number is on the address bus: 0050h. Because M/IO' = 0, the memory chips ignore this cycle; WR' = 0 and the data bus holds 5Ah. The device attached to port 50h takes the value.
in al, 51h is an I/O read cycle: address 0051h, M/IO' = 0, RD' = 0. The device puts 3Ch on the data bus and AL = 3Ch. The last data instruction, mov [bx+1], al, writes to memory. The effective address is BX + 1 = 0201h, and the physical address is 10000h + 0201h = 10201h. In the cycle M/IO' = 1, WR' = 0, and the data bus holds 3Ch. ret ends the program and returns control to the operating system.
| Instruction | AL | Address bus | Data bus | M/IO' | RD' | WR' |
|---|---|---|---|---|---|---|
mov ax,1000h / mov ds,ax | 00h | — | — | — | — | — |
mov bx,0200h | 00h | — | — | — | — | — |
mov al,[bx] | 5Ah | 10200h | 5Ah (from memory) | 1 | 0 | 1 |
out 50h,al | 5Ah | 0050h | 5Ah (out) | 0 | 1 | 0 |
in al,51h | 3Ch | 0051h | 3Ch (from device) | 0 | 0 | 1 |
mov [bx+1],al | 3Ch | 10201h | 3Ch (out) | 1 | 1 | 0 |
mov al,[bx]): address 10200h, M/IO' = 1, RD' = 0. On the right an I/O write (out 50h,al): address 0050h, M/IO' = 0, WR' = 0.in al,51h): the device puts 3Ch on the data bus. On the right a memory write (mov [bx+1],al): 3Ch is written to address 10201h.Table 2.1.4, Figure 2.1.4, and Figure 2.1.5 show what the four accesses have in common: they all use the same address and data lines. M/IO' separates memory from I/O, and RD' and WR' separate reads from writes. In a memory access the address bus holds the 20-bit physical address, and in an I/O access the 16-bit port number. When the program is run in emu8086, the final value of AL is 3Ch, the byte at 10201h is 3Ch, and the value written to port 50h is 5Ah.
It is wrong to think that out 50h, al writes to the memory cell at address 50h. A port number and a memory address are in different address spaces. Because M/IO' = 0, the memory chips do not respond to this cycle at all; the memory byte at 00050h does not change. On the 8088 this signal has the opposite name and polarity (IO/M'), but the principle of the distinction is the same.
2.1.3Internal structure: BIU and EU
A simple processor does the same three steps in turn for every instruction: it fetches the instruction from memory, decodes what to do, and executes it. While it fetches, the processor’s computation units wait idle; while it executes, the bus sits idle. To close this gap, the 8086 splits its inside into two independent units. One does all the bus-related work and the other executes instructions. Because the two units can work at the same time, the bytes of the next instruction can be fetched while one instruction is being executed.
2.1.3.1Two basic units
The bus interface unit (BIU) is the unit that carries out all of the 8086’s communication with the outside world: it fetches instructions from memory, computes the 20-bit physical address, reads operands, and writes results. The execution unit (EU) is the unit that decodes and executes instructions: it contains the ALU, the general-purpose registers, and the flags, and is not connected directly to the external buses.
The bridge between the two units is the prefetch queue. The BIU appends the instruction bytes it fetches from memory to the end of the queue; the EU takes the bytes of the next instruction from the front of the queue. If the EU needs an operand in memory, for example while executing ADD AX, [BX], it computes the offset of the address and asks the BIU to do the read. The BIU runs the bus cycle and hands the data to the EU. The EU never puts anything on the address bus itself.
Thanks to this division of labor the two units work in parallel. While the EU does an addition, the BIU fetches the bytes of the next instruction. The overlap of fetching and execution in time is called a pipeline. The 8086’s pipeline has only two stages, but it gave a significant speed gain for 1978.
2.1.3.2Block diagram
Figure 2.1.7 divides the inside of the processor into two halves. In the EU on the left half, the four general-purpose registers sit at the top: AX, BX, CX, DX. Each can be split into two 8-bit halves (AH-AL, BH-BL, CH-CL, DH-DL). Below them are the pointer and index registers: SP, BP, SI, DI. All these registers are connected to the 16-bit ALU data bus. The ALU takes its operands from temporary registers; the programmer cannot see these temporary registers. The ALU’s output returns to the data bus and carries the result to a register. At the same time the flags register is updated according to the result of the operation. The EU’s control system decodes the opcode coming from the queue and produces the signals that run all these parts in sequence.
The most prominent part of the BIU on the right half is the address adder (Σ). It multiplies the value in a segment register by 16 and adds an offset; the result is the 20-bit physical address and goes out on the address bus. Below the adder are the four segment registers (CS, DS, SS, ES) and the instruction pointer IP. The internal communication registers temporarily hold data moving between the EU and the BIU. The bus control logic connects to the external buses and runs the bus cycles. Instruction bytes coming from memory are written into the 6-byte instruction queue; the EU takes these bytes over the 8-bit Q bus.
Which unit a register sits in also says something about its role. The segment registers and IP are in the BIU, because both are for producing addresses. The registers that operate on data and the flags are in the EU, because they are part of execution.
2.1.3.3The journey of an instruction
To see how the two units work together, let us follow a single instruction, ADD AX, BX, from its fetch from memory to the writing of the result.
- The BIU computes the physical address of the instruction to be fetched in the address adder: CS × 10h + IP.
- The 20-bit address is put on the external address bus; the bus control logic starts a memory read cycle.
- The incoming instruction bytes are written into the 6-byte queue. IP advances by the number of bytes fetched.
- When this instruction’s turn comes, the EU takes the opcode over the Q bus. The control system decodes the instruction: the operation is addition, the operands are AX and BX.
- Control signals carry AX and BX over the ALU data bus to the temporary registers and from there to the ALU. The ALU adds.
- The result is written to AX; the flags (CF, ZF, SF, OF, PF, AF) are updated according to the result. Meanwhile the BIU keeps fetching the next bytes.
The first three of the steps take place in the BIU and the last three in the EU (Figure 2.1.8, Figure 2.1.9). This boundary is the basis of the pipeline. The moment the EU takes an instruction from the queue in the fourth step, room opens up in the queue. The BIU starts a new fetch cycle without waiting for the EU’s fifth and sixth steps. Because ADD AX, BX uses no memory operand, the EU needs the bus not at all during execution, and the bus is given entirely to prefetching.
2.1.3.4The bus interface unit (BIU)
Listing the BIU’s duties together explains why it is a separate unit:
- It interfaces with memory and I/O devices over the system bus; it runs all the bus cycles.
- It produces the 20-bit physical address from a segment register and an offset in the address adder.
- It fetches instructions from memory and writes them into the queue.
- It reads from memory or I/O the operands the EU asks for and writes the results.
- It manages the 6-byte prefetch queue; this queue is what makes the pipeline possible.
Inside the BIU are the segment registers (CS, DS, SS, ES), the instruction pointer (IP), the address adder, the 6-byte instruction queue, and the bus control logic. When the EU asks for a memory operand, the BIU finishes the fetch cycle in progress and then carries out the EU’s request. The EU’s request takes priority over prefetching, because the EU cannot proceed until that data arrives, while the queue can wait a little.
2.1.4The prefetch queue and the pipeline
The BIU and EU can work in parallel only because there is a buffer between them that accumulates bytes. Without this buffer the BIU would have to hold the byte it fetched until the EU took it. The queue separates the two units: the BIU fills the queue, the EU empties it, and each works at its own pace.
2.1.4.1How the queue works
Prefetching is taking instruction bytes from memory and holding them before they are executed. The 8086’s prefetch queue is a 6-byte FIFO (first in, first out) buffer that holds the instruction bytes the BIU fetches: bytes are consumed in the order they were fetched.
Four rules determine the queue’s behavior:
- The fetch rule. When at least 2 bytes of room open up in the queue and the EU does not want the bus, the BIU fetches the next word (2 bytes). Because the 16-bit data bus carries two bytes in one cycle, the unit of fetching is the word.
- The consume rule. The EU takes the bytes of the next instruction from the head of the queue. If the queue is empty, the EU waits until the first byte arrives.
- The full queue. If the queue is full and the EU does not want a memory operand, the BIU stays idle. No bus cycle is made in this case.
- The branch rule. When a branch (JMP, CALL, RET, conditional jump, interrupt) executes, the queue is flushed. The prefetched bytes came from the following addresses, while the program now continues from another address. The BIU starts fetching from the new address.
The queue being 6 bytes is not arbitrary. 8086 instructions without prefixes are between 1 and 6 bytes. A 6-byte queue can hold even the longest instruction completely. Because the 8088’s external data bus is 8 bits, only one byte arrives per cycle; its queue is 4 bytes and it starts fetching when 1 byte is freed.
The gain from the queue is that the EU usually finds an instruction without waiting. The memory access time passes in the background while the EU executes and is invisible to the programmer. The loss appears at a branch: the queue is flushed and the EU waits until it fills again. In programs full of loops and conditional jumps this loss is a significant share.
2.1.4.2The pipeline
A pipeline is an arrangement in which different stages of consecutive instructions are processed at the same time. The 8086 has two stages: fetch (F) in the BIU and decode and execute (E) in the EU. While the EU executes one instruction, the BIU fetches the next.
To see the gain of the pipeline, let us use a simple model: let each fetch take 2 time units, and each decode and execute also 2 time units. In reality instruction times differ, but the model shows the idea clearly.
In the sequential arrangement (Figure 2.1.11) each instruction takes 2 + 2 = 4 units and n instructions take 4n units. In the pipeline (Figure 2.1.12) the first fetch takes 2 units; after that one instruction finishes every 2 units. n instructions take 2 + 2n units. As the number of instructions grows, the ratio 4n / (2n + 2) approaches 2: the throughput roughly doubles.
Let us compute the time of 5 instructions in three cases with the same model.
- No pipeline: 5 × (2 + 2) = 20 units.
- With the pipeline: 2 + 5 × 2 = 12 units. F1 0–2, E1 2–4 (F2 at the same time), E2 4–6, E3 6–8, E4 8–10, E5 10–12.
- The third instruction is a JMP: during E3 (6–8) the prefetched fourth instruction is on the wrong path and is discarded. The target can be fetched only when E3 ends: F4 8–10, E4 10–12, E5 12–14. Total 14 units.
The branch takes back 2 units of the pipeline’s gain. In a program of 100 instructions the pipeline reduces 400 units to 202 units; each jump adds 2 units to this figure again.
Figure 2.1.13 shows this loss on a timeline. Modern processors ease the same problem with branch prediction: they guess the outcome of a jump in advance and start fetching from the target early. The 8086 has no such mechanism; every taken branch resets the queue.
2.1.4.3The queue step by step
Following the rules of the queue on a program shows what happens at the byte level. The program starts at CS:0100h and holds these bytes in memory:
0100: B8 34 12 mov ax, 1234h
0103: 01 D8 add ax, bx
0105: 41 inc cx
0106: EB 05 jmp short 010Dh
0108: 90 90 90 90 90 ; five NOPs
010D: B0 ... mov al, ...The number at the start of each line is the offset of the instruction in the code segment. B8 is the opcode “load a 16-bit constant into AX,” and the constant follows with the low byte first (34 12). 01 D8 is the machine code of ADD AX, BX. 41 is the one-byte INC CX. EB 05 is the short jump: 5 is added to the address of the instruction after the jump. 90 is the NOP instruction, which does nothing.
| Step | Queue | EU | IP of the next instruction | Event |
|---|---|---|---|---|
| 1 | (empty) | waiting | 0100h | start or after a branch |
| 2 | B8 34 12 01 D8 41 | MOV AX,1234h being decoded | 0100h | BIU fetched three words (B8 34, 12 01, D8 41) |
| 3 | 01 D8 41 | MOV AX,1234h | 0103h | EU pulled 3 bytes; 3 free slots |
| 4 | 01 D8 41 EB 05 | MOV AX,1234h | 0103h | BIU fetched EB 05; EU did not wait |
| 5 | 41 EB 05 90 90 | ADD AX,BX | 0105h | EU took 01 D8; BIU fetched 90 90 |
| 6 | EB 05 90 90 90 90 | INC CX | 0106h | EU took 41; BIU fetched two more NOPs, queue full |
| 7 | (flushed) | JMP SHORT 010Dh | 010Dh | IP = 0108h + 5 = 010Dh; four 90s discarded |
| 8 | B0 | waiting | 010Dh | BIU fetches from 010Dh; because it is an odd address, 1 byte first |
Table 2.1.5 reveals a few details. In the second step the EU starts working before the queue is completely full; decoding can begin once the first byte arrives. In the third step, when the EU pulls the 3-byte instruction from the queue, 3 free slots open up. Because the rule is “at least 2 free slots,” the BIU immediately fetches a word. From then on the BIU stays ahead of the EU: in the fourth and sixth steps the EU never waits.
In the seventh step the jump is executed (Figure 2.1.15). The displacement of the short jump is computed relative to the address of the instruction after the jump. Since the JMP starts at 0106h and takes two bytes, the next address is 0108h; 0108h + 5 = 010Dh. The four 90 bytes waiting in the queue came from addresses 0108h–010Bh and will no longer be executed. The queue is flushed. In the eighth step the BIU starts fetching from 010Dh. 010Dh is an odd address; because the 8086 can read a word in a single cycle only from an even address, it first fetches just one byte (B0), and the later fetches again proceed word by word from even addresses. The EU waits during this time; this wait is the price of the branch.
Note that ADD AX, BX is encoded here as 01 D8. On the 8086 an operation between two registers can be encoded in two ways. The opcode 01 means “add the register to the r/m field,” and the opcode 03 means “add the r/m field to the register.” When both destination and source are registers, the two do the same job: 01 D8 and 03 C3 are equivalent. Most assemblers, emu8086 included, produce the second form.
2.1.4.4Prefetch and decode functions
Prefetching and decoding are treated as two separate functions in processor design. The prefetch function fetches instructions from memory and keeps them in a buffer. A program counter holds the address of the next byte to be fetched. On the 8086 this is done with IP and a counter inside the BIU: IP is the “next instruction” address the programmer sees, while the BIU’s counter points to the next byte to enter the queue. When the queue is full this counter is a few bytes ahead of IP. Because the EU takes instructions from the buffer instead of memory, the memory access time is hidden and more instructions are executed per unit time.
The decode function turns machine code into control signals and micro-operations. It determines which operation, which operands, and which addressing mode an opcode asks for. It directs transfers from register to register and from memory to register step by step, as well as complex instructions such as conditional jumps, CALL, and RET. Decoding also works in parallel with prefetching.
On the 8086 these functions are not separate units. Prefetching is done in the BIU and decoding in the EU’s control system. The control system is microcode-based: each machine instruction corresponds to a short sequence of micro-operations stored in a ROM inside the chip. A complex instruction, such as divide, runs a long sequence of micro-operations; that is why it takes hundreds of clock cycles. Separate prefetch and decode units appeared with the 80286; in the 80286 address computation and instruction decoding moved to their own units and the pipeline grew to four stages.
2.1.5The execution unit
The EU is where instructions are really “done.” It decodes the bytes coming from the queue, takes the operands from registers, has the ALU do the operation, and writes the result back. This section covers the parts of the EU, the operations the ALU performs, and the control unit that directs the EU.
2.1.5.1Components of the EU
The main components of the EU are these:
- General-purpose registers (AX, BX, CX, DX): they store data and take part in arithmetic and logic operations.
- Arithmetic logic unit (ALU): it does 8-bit and 16-bit computations.
- Pointer and index registers (SP, BP, SI, DI): they hold offsets in stack and memory addressing.
- Instruction register and instruction decoder: they hold and decode the opcode taken from the queue.
- Flags register (FLAGS): it holds the status bits that summarize the result of the last operation and the control bits that determine the processor’s behavior.
- Control system: it produces, in sequence, the signals that run all these parts.
2.1.5.2The arithmetic logic unit
The arithmetic logic unit (ALU) is the unit that performs an arithmetic or logic operation on two operands and produces the result. The 8086’s ALU works with 8-bit and 16-bit operands and updates the status flags according to the result after every operation.
The ALU’s work falls into two groups. The arithmetic operations are addition, subtraction, multiplication, division, increment, decrement, and comparison. The logic operations are AND, OR, XOR, and NOT, and shifting and rotating bits left or right. Comparison (CMP) is really a subtraction; its result is stored nowhere, only the flags are set.
| Instruction | Operation | Size |
|---|---|---|
ADD AX, BX | AX ← AX + BX | 16 bit |
SUB AL, 5 | AL ← AL − 5 | 8 bits |
MUL BL | AX ← AL × BL | 8 → 16 bits |
DIV CX | AX ← DX:AX ÷ CX, DX ← remainder | 32 → 16 bits |
AND AL, 0Fh | the upper 4 bits of AL are cleared | 8 bits |
XOR AX, AX | AX ← 0 (ZF = 1) | 16 bit |
The size column for multiplication and division needs attention. The product of two 8-bit numbers can grow to 16 bits: 255 × 255 = 65,025. This is why MUL BL writes its result not to AL but to the whole of AX. In division the reverse holds: a 16-bit division starts with a 32-bit dividend. The upper half of the dividend is in DX and the lower half in AX, and this pair is written DX:AX. The quotient goes to AX and the remainder to DX.
Flags are the trace the ALU leaves after each operation. There are six status flags:
- CF (carry flag): 1 if a carry or borrow came out of the top bit in an unsigned operation.
- ZF (zero flag): 1 if the result is zero.
- SF (sign flag): the most significant bit of the result; 1 if negative in the signed interpretation.
- OF (overflow flag): 1 if the result of a signed operation did not fit in the destination.
- PF (parity flag): 1 if the low byte of the result has an even number of 1s.
- AF (auxiliary carry flag): 1 if there was a carry or borrow from bit 3 to bit 4.
In addition there are three control flags (TF, IF, DF) set by the programmer; they are not affected by the ALU’s result. Not every instruction changes all the flags: MOV touches none, INC and DEC preserve CF, and after MUL and DIV some flags are undefined. An undefined flag can be 0 or 1 in the processor, but its value cannot be relied on; a program must not make decisions by looking at it.
2.1.5.3Code trace: what does the ALU do?
The following program runs the operations in Table 2.1.6 in turn. At each step we follow which register changes and what the flags are.
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
retThe first two MOVs load the operands into registers: AX = 0010h, BX = 0030h. MOV does not use the ALU; the value is just copied from one place to another and the flags do not change. add ax, bx is a 16-bit addition: 0010h + 0030h = 0040h. In decimal that is 16 + 48 = 64. No carry comes out of the top bit (CF = 0), the result is not zero (ZF = 0), and the top bit is 0 (SF = 0).
sub al, 5 is an 8-bit subtraction and affects only AL. AL = 40h; 40h − 05h = 3Bh (64 − 5 = 59). AH does not change, so AX = 003Bh. Since the result fits as unsigned, no borrow is needed and CF stays 0. In the low nibble, 0 − 5 borrows from bit 4, so AF = 1.
mov bl, 4 changes only the low half of BX: since BH is already 00h, BX = 0004h. mul bl multiplies AL by BL: 3Bh × 04h = ECh, that is, 59 × 4 = 236. The result is written to AX as 16 bits: AX = 00ECh. Because the upper half of the result (AH) is zero, CF = OF = 0; these two flags carry the information “the result fit in AL.” SF, ZF, AF, and PF, on the other hand, are undefined after MUL.
Preparing for the division takes two lines. mov dx, 0 clears the upper half of the dividend; so DX:AX = 0000 00ECh = 236. mov cx, 3 loads the divisor. div cx divides 236 by 3: the quotient 78 = 004Eh is written to AX and the remainder 2 = 0002h to DX. Check: 78 × 3 + 2 = 236. After DIV all six status flags are undefined.
and al, 0Fh is a masking operation. 4Eh = 0100 1110 and 0Fh = 0000 1111; AND multiplies the bits one by one: 0000 1110 = 0Eh. The upper four bits where the mask is 0 are cleared, and the lower four bits where it is 1 are kept. AND always makes CF = OF = 0. The last instruction, xor ax, ax, XORs every bit with itself. Since the XOR of two equal bits is 0, AX = 0000h and ZF = 1. This is a shorter way to clear a register than mov ax, 0: 33 C0 is two bytes and B8 00 00 is three. There is a difference as well: XOR changes the flags, MOV does not.
| Row | AX | BX | CX | DX | CF | ZF | SF | OF |
|---|---|---|---|---|---|---|---|---|
2–3 mov ax / mov bx | 0010h | 0030h | 0000h | 0000h | 0 | 0 | 0 | 0 |
4 add ax, bx | 0040h | 0030h | 0000h | 0000h | 0 | 0 | 0 | 0 |
5 sub al, 5 | 003Bh | 0030h | 0000h | 0000h | 0 | 0 | 0 | 0 |
6–7 mov bl / mul bl | 00ECh | 0004h | 0000h | 0000h | 0 | ? | ? | 0 |
8–9 mov dx / mov cx | 00ECh | 0004h | 0003h | 0000h | 0 | ? | ? | 0 |
10 div cx | 004Eh | 0004h | 0003h | 0002h | ? | ? | ? | ? |
11 and al, 0Fh | 000Eh | 0004h | 0003h | 0002h | 0 | 0 | 0 | 0 |
12 xor ax, ax | 0000h | 0004h | 0003h | 0002h | 0 | 1 | 0 | 0 |
add ax, bx: 16-bit addition, AX = 0040h. On the right mul bl: 16-bit product of 8-bit operands, AX = 00ECh; ZF and SF undefined.div cx: DX:AX = 236, quotient AX = 004Eh, remainder DX = 0002h; all status flags undefined. On the right xor ax, ax: AX = 0000h, ZF = 1.Forgetting to clear DX before div cx is common. A 16-bit DIV always divides the DX:AX pair. If DX has been left with, say, 0005h, the dividend becomes 5 × 65,536 + 236 = 327,916. The quotient of dividing this number by 3 is 109,305, which does not fit in 16 bits. In this case the processor raises a divide-error interrupt (INT 0) and the program stops.
The times of these instructions are also very different. On the 8086 ADD AX, BX takes 3 clock cycles. MUL BL takes 70–77 cycles and DIV CX takes 144–162 cycles, because the microcode does multiplication and division by shifting and adding bit by bit. At 5 MHz a single 16-bit division comes to about 30 µs. When the divisor is a power of 2, the shift instructions are much faster than division.
2.1.5.4The instruction register, decoder, and control unit
Inside the EU there are three parts the programmer never sees but that work on every instruction.
- The instruction register: the EU takes the instruction to be executed from the queue; while the opcode is decoded it is held here temporarily. The programmer cannot access this register.
- The instruction decoder: it decodes the opcode. It determines which operation is to be done, where the operands are, and which addressing mode is used, and sends this information to the control circuit.
- The control unit: it manages the flow of instructions and data inside the processor. It sequences the fetch–decode–execute cycle and determines what the other components do and when.
The control unit’s duties can be summarized as a cycle: fetching the instruction through the BIU, decoding it, executing it, updating the processor’s state (registers, flags), and, at the end of each instruction, checking for interrupt requests. A request can come on two lines: a maskable one from INTR (accepted only when IF = 1) and a non-maskable one from NMI. If there is a request, the processor branches to the interrupt service routine before moving on to the next instruction. Some error conditions also produce interrupts; division by zero or a quotient that does not fit triggers INT 0.
The 8086’s control unit is microcode-based. Features such as power management are absent from the 8086; they came with much later processors, for example the 386SL. The 8086 either runs or stops with the HLT instruction until an interrupt arrives.
2.1.6Instruction lengths and machine code
A program sitting in memory is not assembly lines but a sequence of bytes. Each instruction is translated into one or more bytes that the processor decodes. On the 8086 this byte count varies from instruction to instruction. Knowing the length is the key to understanding the behavior of the queue and of IP: IP advances after each instruction by exactly the length of that instruction.
2.1.6.1Instruction format
An 8086 machine instruction consists of these fields: the opcode (1 byte; it specifies which operation to perform), a ModR/M byte if present (it specifies the operands and the addressing mode), an 8- or 16-bit displacement if present (a constant added to the memory address), and an 8- or 16-bit immediate if present (a constant operand inside the instruction). An instruction without prefixes is between 1 and 6 bytes.
| Instruction | Machine code | Bytes | Structure |
|---|---|---|---|
INC CX | 41 | 1 | opcode only |
MOV AX, BX | 8B C3 | 2 | opcode + ModR/M |
ADD AL, 7Ch | 04 7C | 2 | short AL-specific code + 8-bit immediate |
MOV BX, 4050h | BB 50 40 | 3 | opcode + 16-bit immediate |
MOV WORD PTR [BX+1234h], 5678h | C7 87 34 12 78 56 | 6 | opcode + ModR/M + 16-bit displacement + 16-bit immediate |
The shortest instruction in Table 2.1.8 is INC CX. On the 8086 the opcodes of the instructions that increment a 16-bit register by one start at 40h, and the register number is added to the opcode: 40 for AX, 41 for CX, 42 for DX, 43 for BX. Because the register is inside the opcode, no separate byte is needed. In the same way, in MOV BX, 4050h the opcode BB means “load a 16-bit constant into BX” (B8 + 3, the number of BX). The constant follows.
Immediate values are written to memory in little-endian order: low byte first, then the high byte. 4050h sits in memory in the order 50 40. The same rule holds for displacements: the displacement 1234h is written 34 12 and the immediate 5678h is written 78 56.
ADD AL, 7Ch uses one of the short codes the 8086 reserves for AL and AX. The general form would take three bytes, 80 C0 7C; the AL-specific opcode 04 needs no ModR/M byte, and the instruction drops to two bytes. This privilege of the accumulator (AX/AL) is meant to shorten frequent operations.
When one of the operands is a general register or memory, the ModR/M byte comes into play. This byte is split into three fields:
ModR/M = mod (2 bits) · reg (3 bits) · r/m (3 bits)
- mod says how to interpret the r/m field: if 11, r/m is a register; if 00, 01, or 10, it is a memory operand, and there is no displacement, an 8-bit displacement, or a 16-bit displacement, respectively.
- reg gives the register of one of the operands: 000 AX, 001 CX, 010 DX, 011 BX, 100 SP, 101 BP, 110 SI, 111 DI (in 8-bit instructions AL, CL, DL, BL, AH, CH, DH, BH).
- r/m gives the second register if mod = 11, otherwise the address form: 000 [BX+SI], 001 [BX+DI], 010 [BP+SI], 011 [BP+DI], 100 [SI], 101 [DI], 110 [BP] (a direct address when mod = 00), 111 [BX].
Let us decode two ModR/M bytes.
8B C3: the opcode 8B means “move r/m16 to reg16” (MOV r16, r/m16). C3h = 1100 0011 = 11 000 011. mod = 11 (register), reg = 000 (AX), r/m = 011 (BX). The instruction isMOV AX, BX.C7 87 …: the opcode C7 means “write a 16-bit constant to r/m16”; the reg field here is not a register but a subtype of the operation and must be 000. 87h = 1000 0111 = 10 000 111. mod = 10 (16-bit displacement), r/m = 111 ([BX]). The displacement34 12= 1234h and the immediate78 56= 5678h follow. The instruction isMOV WORD PTR [BX+1234h], 5678h.
If 89 D8 is decoded the same way: 89 means “move reg16 to r/m16,” D8h = 11 011 000, reg = BX, r/m = AX. This is also MOV AX, BX. A register-to-register MOV has two equivalent encodings.
2.1.6.2Code trace: bytes and IP
The following program runs the instructions in Table 2.1.8 in turn. The purpose of the trace is to see how many bytes each instruction takes in memory and how IP advances accordingly.
org 100h
inc cx
mov bx, 4050h
mov ax, bx
add al, 7Ch
mov word ptr [bx+1234h], 5678h
retThe program starts at 0100h and occupies the range 0100h–010Eh of the code segment, 15 bytes in total: 41 BB 50 40 8B C3 04 7C C7 87 34 12 78 56 C3. inc cx consists of the opcode alone; CX = 1 and IP advances to 0101h. mov bx, 4050h is three bytes (BB 50 40); IP = 0104h. mov ax, bx is two bytes (8B C3); AX = 4050h, IP = 0106h.
add al, 7Ch is the AL-specific short code (04 7C). AL = 50h + 7Ch = CCh; in decimal 80 + 124 = 204. Since AH has not changed, AX = 40CCh. IP = 0108h. The six-byte mov word ptr [bx+1234h], 5678h computes the effective address BX + 1234h = 4050h + 1234h = 5284h and writes 5678h there. In memory DS:5284h holds 78 and DS:5285h holds 56. IP = 010Eh. The last instruction, ret, is a single byte (C3).
| Instruction | Bytes | Length | IP | CX | BX | AX | Memory |
|---|---|---|---|---|---|---|---|
inc cx | 41 | 1 | 0101h | 0001h | 0000h | 0000h | — |
mov bx, 4050h | BB 50 40 | 3 | 0104h | 0001h | 4050h | 0000h | — |
mov ax, bx | 8B C3 | 2 | 0106h | 0001h | 4050h | 4050h | — |
add al, 7Ch | 04 7C | 2 | 0108h | 0001h | 4050h | 40CCh | — |
mov word ptr [bx+1234h], 5678h | C7 87 34 12 78 56 | 6 | 010Eh | 0001h | 4050h | 40CCh | DS:5284h = 78, DS:5285h = 56 |
ret | C3 | 1 | 0000h | 0001h | 4050h | 40CCh | — |
mov bx, 4050h: 3 bytes, IP from 0101h to 0104h. On the right the six-byte mov word ptr [bx+1234h], 5678h: IP from 0108h to 010Eh; 5678h is written to DS:5284h in little-endian order.The IP column of Table 2.1.9 is obtained by adding the length of each instruction to the previous IP: 0100h + 1 = 0101h, + 3 = 0104h, + 2 = 0106h, + 2 = 0108h, + 6 = 010Eh. In the last row IP becomes 0000h. RET is a branch: into IP it loads not a sequential value but the word at the top of the stack. When a .COM program starts, DOS puts 0000h on the stack; at offset 0000h of the segment, in front of the program, is the 256-byte PSP header, whose first instruction terminates the program. So RET returns control to the operating system.
Six instructions took 15 bytes: the lengths varied between 1 and 6 bytes. This is why a 6-byte queue is enough. The machine-code listing and trace emu8086 produces give the bytes and values in the table exactly.
When reading machine code, interpreting BB 50 40 as “load 5040h into BX” is wrong. The 8086 is little-endian: the first byte is the low byte. The correct reading is 4050h. The same mistake is made with memory dumps: the bytes 78 56 starting at DS:5284h are the word 5678h.
2.1.7The register set
The most important tool an assembly programmer has is the registers. Every instruction either reads from a register, writes to a register, or accesses the memory location a register points to. This section groups the 8086’s registers and explains the meaning of word size. The two groups that play a role in address generation, the segment registers and IP, are treated in detail.
2.1.7.1The 14 registers
A register is a very fast, small memory cell inside the processor. All the registers a processor offers the programmer are called the register set. The 8086’s register set has 14 registers of 16 bits.
Figure 2.1.20 divides the registers into four groups: 4 + 4 + 4 + 2 = 14.
- The general-purpose registers (AX, BX, CX, DX) store data and take part in arithmetic and logic operations. Each can be split into two 8-bit halves: the high half of AX is AH and the low half is AL. AX is called the accumulator; multiply, divide, and I/O instructions use it. BX is the base register and can hold an offset in memory addressing. CX is the counter; loop and repeat instructions decrement it. DX is the data register; in multiply and divide it becomes the upper half of AX, and in I/O it holds the port number.
- The pointer and index registers (SP, BP, SI, DI) point to locations in memory. SP is the stack pointer; it holds the offset of the top of the stack. BP is the base pointer; it accesses parameters and local variables on the stack. SI is the source index and DI the destination index; they are used in array and string operations. These registers cannot be split in two.
- The segment registers (CS, DS, SS, ES) point to the starts of the four active segments in memory.
- The special registers: IP points to the next instruction, and FLAGS holds the result of the last operation and the processor’s control bits.
That the general-purpose registers can be split in two is an important detail. A value written to AL changes the low byte of AX and does not change AH. This behavior was seen in the earlier code traces: sub al, 5 changed only AL, and mov bl, 4 changed only BL. The register names describe this structure too: H points to the high byte and L to the low byte.
2.1.7.2Word size
The 8086 is a 16-bit integer processor. Each instruction works on a single data element; there are no vector instructions that process several data at once. Floating-point arithmetic is not in hardware either; a separate coprocessor, the 8087, can be added to the system for this job. Without an 8087, floating-point operations are done in software with long subroutines built from integer instructions.
The width of the registers inside the chip determines the amount of data the processor can work on at once, that is, the word size. On the 8086 the word is 16 bits. This width also determines the value ranges:
| Width | Example | Unsigned range | Signed range |
|---|---|---|---|
| 8 bits | AL, BH | 0 … 255 (FFh) | −128 … +127 |
| 16 bit | AX, SI | 0 … 65,535 (FFFFh) | −32,768 … +32,767 |
| 32 bits | DX:AX | 0 … 4,294,967,295 | −2,147,483,648 … +2,147,483,647 |
A 32-bit value does not fit in a single register; it is held in a two-register pair such as DX:AX. Multiply and divide use this pair automatically. In addition and subtraction it is done in two steps: first the low words are added with ADD, then the high words are added with carry using ADC.
Registers are the fastest storage inside the processor. Accessing a register needs no bus cycle; the value is immediately ready on the internal data bus. Accessing memory, however, takes at least one bus cycle, that is, at least four clock cycles. This difference is the reason a good assembly program keeps frequently used values in registers.
2.1.7.3The segment registers
A segment is a block of 8086 memory that starts on a 16-byte boundary and is at most 64 KB long. A segment register holds the starting address of a segment divided by 16; the physical start of the segment, that is, the segment base, is the value in the register multiplied by 10h.
Segments are in memory; segment registers are inside the processor. This distinction matters: a segment register “points to” a region of memory and does not contain it. When the register is changed, the region it points to shifts, but the bytes in memory stay where they are. Each of the four segment registers has a specific role:
| Register | Name | Segment it points to | Offset used with it |
|---|---|---|---|
| CS | code segment | the program’s instructions | IP (CS:IP is the next instruction) |
| DS | data segment | variables and data | BX, SI, DI, direct address |
| SS | stack segment | stack | SP, BP (SS:SP, SS:BP) |
| ES | extra segment | extra data; string destination | DI (ES:DI) |
There are three rules for using the segment registers. These rules come from the segment registers being tied to the BIU and not the ALU, and from the narrow room reserved for them in instruction encoding.
- An immediate value cannot be loaded.
MOV DS, 2000his invalid; the 8086 instruction set has no opcode that loads a constant into a segment register. The value goes through a general register first:MOV AX, 2000h, thenMOV DS, AX. The value can also be taken from memory:MOV DS, [BX]is valid. - CS is not changed with MOV. When CS changes, the next instruction is taken from somewhere else; that is a branch. CS changes only with far JMP, far CALL, RETF, INT, and IRET; these instructions set IP at the same time. The assembler does not accept the line
MOV CS, AX. - There is no direct MOV from segment to segment.
MOV ES, DSis invalid. The value goes either through a general register (MOV AX, DS/MOV ES, AX) or through the stack (PUSH DS/POP ES).
Writing MOV DS, 1000h to set the data segment does not assemble. The assembler gives an error on this line. The correct notation is two lines: MOV AX, 1000h and MOV DS, AX. This pattern appears in every segment setup and is the reason that in all the code traces the value goes to the segment registers through AX.
2.1.7.4The instruction pointer (IP)
The instruction pointer (IP) is the 16-bit register that holds the offset of the next instruction to be executed in the code segment. The physical address of the next instruction is CS × 10h + IP.
IP advances as each instruction is fetched; by the time an instruction executes, IP has already grown by the length of that instruction. This behavior was seen in the instruction-length trace: the one-byte INC CX advanced IP by 1, and the six-byte MOV by 6. Branch instructions (JMP, conditional jumps, CALL, RET, LOOP, INT), on the other hand, load IP with a new, non-sequential value. The flow of the program changes with this value.
The programmer cannot read or write IP directly; there is no instruction like MOV IP, …. The only way to change IP is a branch. To find out the value of IP, a small trick is used: the CALL instruction pushes the return address, that is, the IP of the next instruction, onto the stack; right afterward this value can be taken into a register with POP.
If CS = 4321h and IP = 1000h, at which physical address is the next instruction?
- CS × 10h: multiplying a hexadecimal number by 10h is appending a 0 on its right. 4321h × 10h = 43210h.
- Add IP, right-aligned: 43210h + 01000h.
- The digits are added from the right: 0 + 0 = 0, 1 + 0 = 1, 2 + 0 = 2, 3 + 1 = 4, 4 + 0 = 4. The result is 44210h; no carry occurred.
The next instruction is at physical address 44210h. This address is 20 bits and goes straight onto the address bus.
2.1.8Segmented memory
What sets the 8086 apart from many other processors is that it addresses memory through segments. A programmer never writes a 20-bit address directly; every memory access arises from the combination of a segment register and an offset. This section explains how this combination is made, why the same byte can be named in many ways, what happens at the 1 MB limit, and which access uses which segment.
2.1.8.1Logical and physical addresses
The 8086 addresses memory in a segmented way. Every memory address is given by two 16-bit parts: the segment and the offset. In writing, the two parts are separated by a colon: 2000h:0010h. The segment register points to the start of the segment; the offset is the distance of the wanted byte from the start of the segment and ranges from 0000h to FFFFh. The two parts are combined in the address adder in the BIU to give the 20-bit physical address.
A logical address is the segment:offset pair the program uses. The physical address (PA) is the real 20-bit address that goes onto the address bus and is computed as:
PA = segment × 10h + offset
If the sum exceeds 20 bits, the 8086 uses only the low 20 bits.
Multiplying by 10h equals shifting a binary number left 4 bits; no multiplier is needed in hardware. The bits of the segment value are connected to the adder’s input shifted four positions, and four 0s are appended on the right. In this way the 16-bit segment value becomes a 20-bit base. The offset is added unshifted, right-aligned. Figure 2.1.21 shows this arrangement.
As a result of the shift, the last hexadecimal digit of a segment base is always 0. Segment bases are therefore multiples of 16. These 16-byte boundaries in memory are called paragraphs. Each of the 65,536 values a segment register can take points to a paragraph boundary: 1,048,576 / 16 = 65,536.
2.1.8.2The physical address computation step by step
The computation is always done in three steps: a 0 is appended to the right of the segment value, the offset is written below it right-aligned, and the two numbers are added in hexadecimal.
Let the segment register be 1000h and the offset F000h.
- Segment base: 1000h × 10h = 10000h.
- The offset F000h, that is, 0F000h.
- Sum: 10000h + 0F000h = 1F000h.
Since the offset can be at most FFFFh, this segment covers the range 10000h–1FFFFh. The length of the range is 10000h bytes, that is, 64 KB.
If CS = F000h and IP = FFF0h, what is the physical address of the instruction?
- F000h × 10h = F0000h.
- IP = FFF0h is right-aligned: 0FFF0h.
- F0000h + 0FFF0h = FFFF0h.
FFFF0h is the start of the last 16 bytes of the 1 MB. When the 8086 is reset it loads CS = FFFFh and IP = 0000h: FFFF0h + 0000h = FFFF0h. Two different pairs point to the same byte; the processor fetches its first instruction from here in every case. In computers this address is in the BIOS ROM, and there is a far jump to the BIOS startup code.
Two mistakes are common. The first is to forget to multiply the segment value by 10h and get 4321h + 1000h = 5321h; this is a 16-bit number and cannot be a physical address. The second is to apply the shift to the wrong part: shifting the offset and getting 4321h + 10000h = 14321h. The rule is single: the segment is shifted, and the offset is added as it is.
2.1.8.3The physical address calculator
The interactive calculator in the slides produces the same three rows for any segment and offset entered: the segment, segment × 10h, and the sum. It also draws the 64 KB range of the segment and the position of the physical address on a 1 MB memory map and gives the normalized form of the address. For 4321h:1000h, for example, the segment range is 43210h–5320Fh, the physical address is 44210h, and the normalized form is 4421h:0000h. If the sum exceeds 1 MB, the calculator drops the 21st bit and shows that the result wraps around.
2.1.8.4Memory segmentation
The 20-bit address bus addresses 1 MB; a segment is at most 64 KB, because the offset is 16 bits. At any moment four segments are active: those CS, DS, SS, and ES point to. Without changing the segment registers, at most 4 × 64 KB = 256 KB can be accessed directly. To access another region of memory, a new value is loaded into a segment register.
Segments do not divide memory into 16 fixed parts. 1 MB / 64 KB = 16 is only arithmetic; on the 8086 segment starts are not fixed. A segment can start at every paragraph boundary; there are 65,536 possible starts. So segments can be disjoint, and they can also overlap. In Figure 2.1.24 the segment ES = 2800h starts at 28000h and ends at 37FFFh. The first half of this range is the same bytes as the second half of the DS segment (28000h–2FFFFh), and the second half is the same bytes as the first half of the SS segment (30000h–37FFFh). The same byte can be accessed through both DS and ES with different offsets: physical 2A000h is read with DS:A000h and also with ES:2000h.
Let us find the range of the segment ES = 2800h and see whether address 30010h can be accessed with ES.
- Segment base: 2800h × 10h = 28000h.
- Last byte: 28000h + FFFFh = 37FFFh.
- 30010h is in this range. The offset: 30010h − 28000h = 8010h. Since it is less than FFFFh, it is valid:
ES:8010h.
The same byte is also written as SS:0010h with SS = 3000h. With DS = 2000h it cannot be accessed: 30010h − 20000h = 10010h, which does not fit in 16 bits.
2.1.8.5Why segmentation?
Segmentation was a cheap way to address 1 MB in a 16-bit processor. But alongside the conveniences it brought, it has burdens.
| Pros | Cons |
|---|---|
| 20-bit (1 MB) memory is addressed with 16-bit registers. | A single array or structure cannot exceed 64 KB; if it does, the segment has to be changed. |
| Code, data, and stack are separated from one another; the program layout becomes clear. | The same physical address has many segment:offset notations; pointer comparison becomes harder. |
| Relocatability: a program can be loaded elsewhere in memory by changing only the segment registers. | There is no protection between segments; a faulty write can corrupt another segment. |
| Instructions stay short: most addresses are written with a 16-bit offset. | Address computation adds an extra mental load for the programmer. |
Relocatability is especially valuable. A .COM program uses all the addresses inside it as offsets from the start of the segment. Whichever paragraph the operating system loads the program into, it is enough to set CS, DS, ES, and SS to that paragraph; no byte of the program needs to change. emu8086 loads .COM programs at segment 0700h: CS = DS = ES = SS = 0700h and the program starts at 0700h:0100h.
The cons became heavier with time. For data structures larger than 64 KB, programmers had to change segments by hand; compilers defined “near” and “far” pointer types. The lack of protection made multitasking operating systems hard. The 80286’s protected mode and the 80386’s 32-bit flat address space came to solve these problems.
2.1.8.6The same address, different notations
Because segments can overlap, a physical address is not indicated by a single logical address. If the segment is increased by one paragraph the base moves 16 bytes forward; if the offset is decreased by 16 the sum does not change. Table 2.1.13 shows this with numbers.
| Logical address | Calculation | Physical address |
|---|---|---|
1234h:0005h | 12340h + 0005h | 12345h |
1000h:2345h | 10000h + 2345h | 12345h |
1200h:0345h | 12000h + 0345h | 12345h |
0234h:FFF5h | 02340h + FFF5h | 12335h (not 12345h) |
FFFFh:0010h | FFFF0h + 0010h = 100000h | 00000h (wraps to 20 bits on the 8086) |
The first three rows point to the same byte. The fourth row is misleading: the segment 0234h is 1000h smaller than 1234h, that is, the base is 10000h bytes behind. To reach the same byte the offset would have to be 10005h, but that does not fit in 16 bits. With FFF5h one falls 16 bytes behind. In the last row the sum exceeds 1 MB and wraps to 20 bits.
A normalized address is a logical address whose offset is between 0h and Fh. The first four digits of the five-digit physical address give the segment and the last digit gives the offset: 12345h → 1234h:0005h.
A physical address has at most 4096 different notations. While the offset is decreased by 16 each time, the segment is increased by one; as long as the offset stays in the range 0000h–FFFFh, every step gives a valid notation. Because the last digit of the offset stays fixed, the number of values that can be chosen is 65,536 / 16 = 4096. For 12345h the notations extend from 0235h:FFF5h to 1234h:0005h: 1234h − 0235h + 1 = 1000h = 4096. For addresses near the very start of memory the number of notations is smaller, because the segment cannot go below 0.
Let us normalize the physical address 3A2C5h and find the required offset if DS = 3000h.
- The first four digits are the segment, the last digit the offset:
3A2Ch:0005h. Check: 3A2C0h + 5h = 3A2C5h. - If DS = 3000h the segment base is 30000h. Offset = 3A2C5h − 30000h = A2C5h.
- A2C5h ≤ FFFFh, so it is valid:
3000h:A2C5h.
The same question can be asked in reverse for CS = 2F00h and IP = 01A4h: PA = 2F000h + 01A4h = 2F1A4h; the normalized notation is 2F1Ah:0004h. In emu8086 a byte written to address 2F00h:01A4h is read back exactly with ES:[0004h] when ES = 2F1Ah.
2.1.8.7Code trace: the address wraps around at 1 MB
The sum segment × 10h + offset can exceed FFFFFh: the largest case is FFFF0h + FFFFh = 10FFEFh, which takes 21 bits. The 8086 has only 20 address lines (A19–A0). The 21st bit does not go onto the address bus, that is, it is dropped. As a result the address wraps around to the start of memory. The following program shows this behavior.
org 100h
mov ax, 0
mov ds, ax
mov byte ptr [0500h], 77h
mov ax, 0FFFFh
mov ds, ax
mov bl, [0510h] ; FFFF:0510
retThe first two instructions make DS = 0000h; the segment is at the very start of memory. mov byte ptr [0500h], 77h writes 77h to address 0000h:0500h, that is, physical 00500h. The byte ptr specifier announces that the data to be written is one byte; the constant 77h by itself does not determine this. Then DS = FFFFh is set. The base of this segment is FFFF0h, the last 16 bytes of the 1 MB. mov bl, [0510h] computes the physical address as FFFF0h + 0510h = 100500h. This number is 21 bits. The 8086’s 20 address lines carry only 00500h, and the processor reads the very same byte to which 77h was written in the first step: BL = 77h.
| Row | DS | BL | Calculation | byte at 00500h |
|---|---|---|---|---|
2–3 mov ax,0 / mov ds,ax | 0000h | 00h | — | 00h |
4 mov byte ptr [0500h], 77h | 0000h | 00h | 00000h + 0500h = 00500h | 77h |
5–6 mov ax,0FFFFh / mov ds,ax | FFFFh | 00h | base FFFF0h | 77h |
7 mov bl, [0510h] | FFFFh | 77h | FFFF0h + 0510h = 100500h → 00500h | 77h |
mov bl, [0510h]: FFFF0h + 0510h = 100500h. The 21st bit is dropped, the address wraps to 00500h, and BL = 77h.This rule means that in the segment FFFFh, all addresses from FFFFh:0010h to FFFFh:FFFFh fall in the region 00000h–0FFEFh at the start of memory. Wraparound on the 8086 is not a bug but the natural consequence of the 20-bit address bus. Some old programs were written relying on this behavior.
The situation changed with the 80286. The 80286 has 24 address lines, and even in real mode the 21st line (A20) can be enabled. In that case FFFFh:0510h really reaches 100500h, just above 1 MB. To keep compatibility with old programs, a circuit that turns the A20 line off and on was added to IBM PC/AT computers. With A20 enabled, the 65,520 bytes above 1 MB (FFFF0h + FFFFh − FFFFFh = FFF0h) can be accessed in real mode through the segment FFFFh.
Writing 100500h as the physical address for FFFFh:0510h is wrong on the 8086; this number does not fit in 20 bits. If the sum exceeds 1 MB the top bit is dropped and 100000h is subtracted: 100500h → 00500h. The same check must be made at the end of every physical address computation.
2.1.8.8Default segments and the segment prefix
When an instruction writes only an offset, for example mov al, [bx], the processor itself decides which segment register to use. This decision depends on the type of access and on the register used in the address computation.
The default segment is the segment the processor uses in a memory access when no segment is specified. The segment override prefix is a one-byte prefix added to the start of an instruction that selects another segment register instead of the default: 26h for ES, 2Eh for CS, 36h for SS, 3Eh for DS.
| Access type | Segment | Offset source | Can it be overridden with a prefix? |
|---|---|---|---|
| Instruction fetch | CS | IP | no |
| Stack operations (PUSH, POP, CALL, RET) | SS | SP | no |
Addressing with BP [BP+…] | SS | effective address | yes: DS:[BP] |
General data [BX], [SI], [DI], [1234h] | DS | effective address | yes: ES:[BX] |
| String source (MOVSB, LODSB) | DS | SI | yes |
| String destination (MOVSB, STOSB) | ES | DI | no |
The logic of the rules in Table 2.1.15 comes from the roles of the registers. Instructions are always in the code segment. The stack is always in the stack segment. Because BP is designed to reach parameters and local variables on the stack, every address that contains BP uses SS by default: [BP], [BP+SI], [BP+DI+4]. All the remaining data accesses use DS. String instructions read the source from DS:SI and write the destination through ES:DI; so a block can be copied between two different segments.
To change the default, a segment prefix is written: MOV AL, ES:[BX] computes the physical address as ES × 10h + BX. The prefix adds a byte to the instruction (26h here) and lengthens execution by two clock cycles. Instruction fetch, stack operations, and the string destination cannot be changed with a prefix; in these accesses the processor behaves as if there were no prefix.
With SS = 2000h, DS = 3000h, BP = 0100h, and SI = 0020h, to which address does mov byte ptr [bp+si+4], 0AAh write? What changes if the same instruction is written with the ds: prefix?
- Effective address: BP + SI + 4 = 0100h + 0020h + 0004h = 0124h.
- The address contains BP; the default segment is SS. PA = 20000h + 0124h = 20124h.
- With the
ds:prefix, PA = 30000h + 0124h = 30124h. The machine code grows fromC6 42 04 AAto3E C6 42 04 AA.
emu8086 shows that the two instructions write to addresses 20124h and 30124h and that the instruction with the prefix takes 5 bytes.
Thinking that [BX] and [BP] use the same segment is a common mistake. Even if BX and BP hold the same value, [BX] is computed relative to DS and [BP] relative to SS, and the two instructions access different bytes. In a .COM program the difference is invisible because DS = SS; the mistake shows up when the segments are set separately.
2.1.8.9Code trace: the same offset, three segments
The following program uses the same offset, 0010h, with three different segments and accesses three different bytes. Because the program starts as a .COM program, initially CS = DS = ES = SS = 0700h.
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
retThe first four instructions set DS = 2000h and ES = 3000h; the values pass through AX. SS stays at 0700h, untouched. This creates three different segment bases: 07000h (SS), 20000h (DS), 30000h (ES). BX and BP hold the same offset, 0010h. Which byte is accessed will be determined not by the offset but by the segment used.
mov byte ptr [bx], 11h uses DS by default: it writes 11h to address 20000h + 0010h = 20010h. mov byte ptr [bp], 22h uses SS because it contains BP: it writes 22h to address 07000h + 0010h = 07010h. The es: prefix in mov byte ptr es:[bx], 33h selects ES instead of DS: it writes 33h to address 30000h + 0010h = 30010h; the machine code has the 26h prefix at its start. The last two instructions read: mov al, [bx] again reads DS:0010h and AL = 11h, not the 33h at ES:0010h. mov ah, [bp] reads SS:0010h and AH = 22h. The result is AX = 2211h.
| Row | Segment | Physical address | Operation | AX |
|---|---|---|---|---|
| 2–5 | — | — | DS = 2000h, ES = 3000h, SS = 0700h | 3000h |
| 6–7 | — | — | BX = BP = 0010h | 3000h |
8 [bx] ← 11h | DS | 20010h | write 11h | 3000h |
9 [bp] ← 22h | SS | 07010h | write 22h | 3000h |
10 es:[bx] ← 33h | ES | 30010h | write 33h | 3000h |
11 al ← [bx] | DS | 20010h | read 11h → AL | 3011h |
12 ah ← [bp] | SS | 07010h | read 22h → AH | 2211h |
[bp]: SS × 10h + BP = 22h is written to 07010h. On the right es:[bx]: the prefix selects ES, and 33h is written to 30010h. The same offset corresponds to three different bytes.After row 11, AX = 3011h: AL took the new value, while AH kept the 30h left over from the earlier mov ax, 3000h. Row 12 makes AH 22h. This detail shows once more that writing to an 8-bit register does not touch the other half of the 16-bit register.
2.1.8.10Code trace: access with segment:offset
The last trace shows that the same byte can be read with two different logical addresses and where the last byte of a segment is.
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 byteWith DS = 2000h the data segment is the range 20000h–2FFFFh. mov byte ptr [bx], 41h writes 41h to 20000h + 0010h = 20010h; 41h is the ASCII code of the character 'A'. Then DS = 2001h is set. The segment value grew by 1, but the base moved forward 16 bytes, that is, one paragraph: the new segment is the range 20010h–3000Fh. With BX = 0000h, mov al, [bx] reads 20010h + 0000h = 20010h and AL = 41h. 2000h:0010h and 2001h:0000h are the same byte. The last two instructions use the largest offset of a segment with SI = FFFFh: 20010h + FFFFh = 3000Fh. This is the last byte of the segment DS = 2001h, and AH takes the value there.
| Row | AX | DS | BX | SI | FA | [20010h] |
|---|---|---|---|---|---|---|
1 mov ax, 2000h | 2000h | ? | 0000h | 0000h | — | ? |
2 mov ds, ax | 2000h | 2000h | 0000h | 0000h | — | ? |
3 mov bx, 0010h | 2000h | 2000h | 0010h | 0000h | — | ? |
4 mov byte ptr [bx], 41h | 2000h | 2000h | 0010h | 0000h | 20010h | 41h |
5 mov ax, 2001h | 2001h | 2000h | 0010h | 0000h | — | 41h |
6 mov ds, ax | 2001h | 2001h | 0010h | 0000h | — | 41h |
7 mov bx, 0000h | 2001h | 2001h | 0000h | 0000h | — | 41h |
8 mov al, [bx] | 2041h | 2001h | 0000h | 0000h | 20010h | 41h |
9 mov si, 0FFFFh | 2041h | 2001h | 0000h | FFFFh | — | 41h |
10 mov ah, [si] | ??41h | 2001h | 0000h | FFFFh | 3000Fh | 41h |
In the checker, when 99h is placed in advance at 3000Fh, the program ends with AX = 9941h; AH really takes the byte at 3000Fh. That AX is 2041h in the eighth row shows the same principle: AL became 41h and AH kept the 20h left over from the fifth row.
2.1.9Summary
- The 8086 is a 16-bit microprocessor that came out in 1978 and is the first member of the x86 family. Its registers, ALU, and data bus are 16 bits and its address bus is 20 bits: it addresses 220 bytes = 1 MB of memory. It is a CISC design; its instructions are 1–6 bytes long. All later x86 processors start in real mode at power-on, behaving like an 8086.
- There is no memory inside the microprocessor; to work it needs the 8284A clock generator, address latches (8282), data bus transceivers (8286), memory, and I/O units. A microcontroller puts these on a single chip.
- The system bus consists of three buses: the unidirectional 20-bit address bus, the bidirectional 16-bit data bus, and the control bus (RD', WR', M/IO', ALE, INTR, READY). M/IO' separates memory from I/O; I/O ports are in a separate 16-bit address space.
- A bus cycle takes at least four clock cycles (T1–T4); 800 ns at 5 MHz. Memory is much slower than registers.
- The 8086 consists of two units. The BIU generates addresses, fetches instructions, and runs all the bus cycles; the segment registers, IP, the address adder, and the queue are in it. The EU decodes and executes instructions; the general registers, ALU, flags, and control system are in it.
- The 6-byte prefetch queue is a FIFO. When at least 2 bytes are freed, the BIU fetches a word. The overlap of fetching and execution is a two-stage pipeline and roughly doubles throughput. A branch flushes the queue and takes back part of this gain.
- The ALU does 8- and 16-bit arithmetic and logic operations and updates the status flags (CF, ZF, SF, OF, PF, AF).
MULwidens the result (AL × BL → AX),DIVdivides DX:AX;MOVdoes not change the flags. - A machine instruction consists of an opcode, a ModR/M byte (mod · reg · r/m), a displacement, and an immediate. Multibyte constants are written in little-endian order. IP advances after each instruction by the length of that instruction; branches load IP with a new value.
- The 8086 has 14 registers: AX, BX, CX, DX · SP, BP, SI, DI · CS, DS, SS, ES · IP, FLAGS. An immediate value cannot be loaded into a segment register, CS cannot be changed with MOV, and there is no direct MOV from segment to segment.
- Physical address = segment × 10h + offset. A segment is at most 64 KB, starts on a paragraph (16-byte) boundary, and segments can overlap. The same address has at most 4096 notations; in the normalized notation the offset is between 0h and Fh.
- If the sum exceeds 1 MB, on the 8086 the 21st bit is dropped and the address wraps around (FFFFh:0510h → 00500h). On an 80286 with the A20 line enabled, the same address reaches above 1 MB.
- Default segments: instruction fetch CS:IP, stack SS:SP, BP-based addresses SS, other data accesses DS, string destination ES:DI. A segment prefix (26h, 2Eh, 36h, 3Eh) changes the default in data accesses.
2.1.10Key terms
- microprocessor
- microcontroller (MCU)
- CISC (complex instruction set computer)
- real mode
- clock cycle
- clock generator
- latch
- buffer (bus transceiver)
- bus
- system bus
- address bus
- data bus
- control bus
- bus cycle
- bus interface unit (BIU)
- execution unit (EU)
- prefetch
- prefetch queue
- pipeline
- arithmetic logic unit (ALU)
- flag
- status flag
- instruction register
- instruction decoder
- control unit
- opcode
- ModR/M byte
- displacement
- immediate value
- effective address (EA)
- little-endian
- register
- register set
- general-purpose register
- word
- segment
- segment register
- code / data / stack / extra segment
- offset
- segment:offset
- physical address
- paragraph
- segmented memory
- segmentation
- instruction pointer (IP)
- default segment
- segment override prefix
2.1.11Test Yourself
The 80286’s address bus is 24 bits. How many bytes does it address, and how is its highest address written in hexadecimal? How many times the 8086’s address space is it?
Answer
224 = 16,777,216 bytes = 16 MB. The highest address is FFFFFFh, with six hexadecimal digits. 224 / 220 = 24 = 16 times.
In how many nanoseconds does an 8 MHz 8086-2 complete a bus cycle with no wait states? At this speed, how long does it take at least to read 50 words that start at odd addresses?
Answer
The clock cycle is 1 / 8 MHz = 125 ns; the bus cycle is 4 × 125 = 500 ns. A word starting at an odd address needs two cycles: 50 × 2 × 500 ns = 50,000 ns = 50 µs. Had the words been at even addresses the time would have been 25 µs.
In the model where fetch takes 2 time units and decode-and-execute takes 2, 8 instructions run and the fourth instruction is a JMP. What are the total times without and with the pipeline?
Answer
Without the pipeline, 8 × 4 = 32 units. With the pipeline and no branch it would have been 2 + 8 × 2 = 18 units. The JMP invalidates the fifth instruction fetched during E4 (8–10); the target is fetched at 10–12, and E5 is 12–14, E6 14–16, E7 16–18, E8 18–20. Total 20 units.
The machine code of the instruction
ADD CX, [BX+SI+10h]is03 48 10. Split the ModR/M byte into its fields and give the meaning of each field.Answer
48h = 0100 1000 = 01 001 000. mod = 01: a memory operand, with an 8-bit displacement (10h). reg = 001: CX. r/m = 000: [BX+SI]. Since the opcode 03 means “add r/m16 to reg16,” the instruction is CX ← CX + [BX+SI+10h]; since BP is not in the address, the segment is DS. The instruction is 3 bytes.
PUSH AXexecutes with SS = 9000h and SP = FFFEh (AX = 1234h). What is the new value of SP, and to which physical addresses are the bytes 34h and 12h written?Answer
PUSH first decrements SP by 2: SP = FFFCh. The word is written to SS:FFFCh in little-endian order. Since the base is 90000h, 34h → 9FFFCh and 12h → 9FFFDh.
Find the physical address of the logical address
0ABCDh:1234hand normalize it.Answer
ABCD0h + 01234h = ACF04h. The sum does not exceed FFFFFh. Normalized notation: the first four digits are the segment and the last digit is the offset →
ACF0h:0004h.To which physical address does the address
F800h:9000hcorrespond on the 8086? What changes on an 80286 with the A20 line enabled?Answer
F8000h + 09000h = 101000h; 21 bits. On the 8086 the 21st bit is dropped and the address wraps to 01000h. On an 80286 with A20 enabled, the address really is 101000h, that is, 4 KB above 1 MB.
SS = 2000h, DS = 3000h, BP = 0100h, SI = 0020h. Which physical address does
MOV AL, [BP+SI+4]read? How is the instruction written to read the same data from the DS segment, and how many bytes longer does it become?Answer
The effective address is 0124h; since it contains BP, the default segment is SS: 20000h + 0124h = 20124h. To read from DS, a segment prefix is written:
MOV AL, DS:[BP+SI+4]→ 30124h. The prefix (3Eh) adds one byte to the instruction.We want to swap the values of DS and ES without disturbing the general-purpose registers. Why can’t
MOV ES, DSbe used? Write a solution that works with the stack.Answer
On the 8086 there is no direct MOV from segment to segment. With the stack:
PUSH DS,PUSH ES,POP DS,POP ES. Because the stack is LIFO, first ES’s value comes off into DS, then DS’s old value into ES. Starting with DS = 1111h and ES = 2222h, the result is DS = 2222h and ES = 1111h.
2.1.12Further reading
- Intel, The 8086 Family User's Manual (1979), Chapter 2: the BIU and EU, the queue, segmentation, and instruction encoding are presented in the designer’s own words, with timing tables.
- B. B. Brey, The Intel Microprocessors (8th edition), Chapters 2 and 3: real-mode memory addressing, default segments, and addressing modes are treated with plenty of examples.
- M. A. Mazidi, J. G. Mazidi, and D. Causey, The x86 PC: Assembly Language, Design, and Interfacing (5th edition), Chapter 1: the 8086’s internal structure, the pipeline, and the segment:offset computation are explained with DEBUG examples.
2.1.13References
- Intel, 8086 16-Bit HMOS Microprocessor, datasheet (231455).
- Intel, The 8086 Family User's Manual, 1979.
- Barry B. Brey, The Intel Microprocessors, 8th edition, Pearson, 2009.
- Muhammad Ali Mazidi, Janice Gillispie Mazidi, Danny Causey, The x86 PC: Assembly Language, Design, and Interfacing, 5th edition, Pearson, 2010.
- emu8086 documentation and instruction set reference.
Related pages: Chapter 2.1 slides · © Sercan Külcü. All rights reserved.