Email or username:

Password:

Forgot your password?
Ken Shirriff

The Intel 8086 microprocessor (1978) led to the x86 architecture that your computer probably runs today. The 8086 provided a complicated set of memory access modes to get values from memory. Let's take a close look at how microcode and hardware work together to implement them. 🧵

15 comments
Ken Shirriff

If you've done x86 programming, you probably know the ModR/M byte. This byte follows the instruction opcode and describes the memory access mode. Based on octal, this byte's mod, reg, and r/m fields specify the mode, register to use, and a register or memory mode.

Ken Shirriff

First, the 8086 uses the "Group Decode ROM" to analyze the instruction. It determines if it uses a ModR/M byte, a word bit, ALU, and various other features. The mod and r/m fields are copied into the N and M registers, internal 8086 registers that are invisible to the programmer.

Ken Shirriff

Now microcode takes over, running the internal steps for the instruction. Each micro-instruction is 21 bits long, with 5 different types of micro-instruction. Each moves an internal source register to a destination. It also does a particular task such as an ALU or memory op.

Ken Shirriff

Suppose you want to add the AX register to the BX register: "ADD BX,AX". The microcode moves the register specified by N (i.e. BX) to the ALU's temporary A register. BX, specified by N, moves to tmpB. The sum Σ is moved to BX (N). The ALU op (XI) comes from the instruction.

Ken Shirriff

Thus, a simple add takes 3 micro-instructions (and 3 clock cycles). The microcode is very generic: it doesn't know the particular registers, the ALU operation, or the operand size. It just specifies the steps and the hardware figures out the details. This keeps microcode small.

Ken Shirriff

Adding to memory "ADD [SI],AX" uses the same microcode. A microcode subroutine gets the SI address and reads (R) from memory. Now M represents the memory value. The microcode adds, then falls through to write (W) the result to memory due to the writeback (WB) condition.

Ken Shirriff

Each of the 8 addressing formulas has a short microcode subroutine to compute the effective address. The 8086's Translation ROM determines the appropriate 13-bit microcode address based on the ModR/M byte.

Ken Shirriff

For an addressing mode with a displacement "ADD AX,[SI+1234]", a few more lines of microcode fetch the displacement bytes (e.g. 1234) from the instruction prefetch queue (Q), put them in tmpB, and add them to the SI value.

Ken Shirriff

The 8086 chip squeezed a lot of functionality into just 512 words of microcode, making hardware take care of details. The die photo shows these functional blocks on the die. Microcode takes a large area but even registers (like M and N) use a substantial area of the silicon.

Ken Shirriff

Microcode is very low level and pretty tricky, but hopefully this brief Mastodon explanation makes some sense. For a more detailed look at microcode, see my blog post: righto.com/2023/02/8086-modrm-

Ken Shirriff replied to Ken
thinkberg

@kenshirriff Do you only decode HW? I still have 255 byte .COM that does stuff you could identify :)

Ken Shirriff

@thinkberg I haven't been doing 8086 software reverse engineering, but there are many other people who can do that.

Neko May

@kenshirriff Very much loving these microcode deep dives!

j2i.net

Thinking about this, I'm thinking about the movie "Inside Out." Only instead of people controlling a person it's a program controlling programs.

@kenshirriff

Go Up