Skip to main content
Be Bitwise

x86 Registers

x86 Architecture Deep Dive · CPU Architectures track

5 min read

Part ofSystems Programming Companion, week 3

In this module

A Named Register Set

Where our teaching ISA gives you tidy R0-R7, x86 hands you a lineup of named registers carrying forty years of historical baggage. You'll meet them constantly when reading assembly, writing exploits, or chasing down a calling convention.

General-Purpose Registers

In 64-bit mode (x86-64), there are 16 general-purpose registers. The original eight have a naming convention rooted in their historical roles:

64-bit32-bit16-bitHigh 8Low 8Original Purpose
RAXEAXAXAHALAccumulator
RBXEBXBXBHBLBase
RCXECXCXCHCLCounter
RDXEDXDXDHDLData
RSIESISI-SILSource Index
RDIEDIDI-DILDestination Index
RSPESPSP-SPLStack Pointer
RBPEBPBP-BPLBase Pointer

x86-64 added eight more: R8 through R15 (with sub-registers R8D, R8W, R8B, etc.).

Register Naming Convention (AL/AH/AX/EAX/RAX)

Each of the original registers can be accessed at different widths. AL is the low byte (bits 0-7), AH is the next byte (bits 8-15), AX is the low 16 bits, EAX is the low 32 bits, and RAX is the full 64 bits. Writing to EAX zero-extends into RAX. Writing to AX or AL does not zero-extend - it only modifies the targeted bits.

The low bytes SPL, BPL, SIL and DIL (and R8B-R15B) only exist in 64-bit mode and need a REX prefix to encode. Using that prefix changes what the old byte-register codes mean, so a single instruction can't mix AH, BH, CH or DH with any of them.

Worked Example: Four Writes to One Register

The zero-extension rule sounds like trivia until it changes a value under your nose. Start with rax holding 0x1122334455667788 and apply each write in turn:

Instructionrax afterwardsWhat changed
mov al, 0xff0x11223344556677ffOnly the lowest byte
mov ah, 0xee0x112233445566eeffOnly bits 8-15
mov ax, 0x12340x1122334455661234Only the low 16 bits
mov eax, 10x0000000000000001The low 32 bits, and the top 32 are cleared

The first three writes leave everything above them untouched, which is how the 16-bit and 8-bit registers always behaved. The 32-bit write is different. AMD made it zero the upper half when they designed x86-64, so that 32-bit code wouldn't drag stale high bits around and wouldn't need to wait for the old value of the full register before writing.

That rule is why compilers love 32-bit forms. xor eax, eax (2 bytes, 31 c0) clears all of rax, and it's the standard way to zero a register. You'll see it everywhere, often before a call or a ret.

Predict: mov rax, -1 then mov ax, 0. What's in rax?

0xffffffffffff0000. mov rax, -1 fills all 64 bits with ones. The 16-bit write replaces only the bottom 16 bits with zeros and leaves the other 48 bits set. Had the second instruction been mov eax, 0, the answer would be plain 0.

Here's rax with the same starting value. Press the four mov buttons in order and watch the outlined bits: the first three writes stay inside their own lane, while mov eax, 1 wipes the top 32 bits as well. The table underneath reads each sub-register out of the same 64 bits. Try mov rax, -1 followed by mov ax, 0 to check your answer to the question above.

263
262
261
260
259
258
257
256
255
254
253
252
251
250
249
248
247
246
245
244
243
242
241
240
239
238
237
236
235
234
233
232
231
230
229
228
227
226
225
224
223
222
221
220
219
218
217
216
215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal1234605616436508552
Hex0x1122334455667788
Binary00010001 00100010 00110011 01000100 01010101 01100110 01110111 10001000
FieldBitsHexDecimal
EAX31-00x556677881432778632
AX15-00x778830600
AH15-80x77119
AL7-00x88136

Special-Purpose Registers

A handful of registers carry fixed architectural duties:

  • RSP (Stack Pointer) - points to the top of the current stack frame. PUSH and POP modify this automatically.
  • RBP (Base Pointer) - traditionally points to the base of the current stack frame, making local variables and function arguments reachable at fixed offsets.
  • RIP (Instruction Pointer) - holds the address of the next instruction to execute. You can't write to RIP directly with MOV, but jumps and calls modify it.
  • RFLAGS - status bits set by arithmetic and comparison instructions. The ones you'll actually care about are ZF (zero), SF (sign), CF (carry), and OF (overflow). Conditional jumps read these flags.

cmp is where the flags earn their keep. cmp rax, rbx subtracts rbx from rax, throws the answer away and keeps only the flags. A following je jumps if ZF is set (the values were equal), jl checks SF and OF for a signed less-than, and jb checks CF for an unsigned below. Same comparison, different jump, different meaning, which is exactly where signed and unsigned bugs hide.

Load the 0xff vs 1 preset below: in 8 bits that byte is 255 to ja and -1 to jl, and both jumps fire. Then try INT_MIN - 1 to watch OF rescue jl after the result wraps, and switch to the drill to predict ZF, SF, CF and OF yourself before they're revealed.

cmp, test and the flags

Presets

cmp al, bl ; a - b, result thrown away

a
0xff
unsigned 255 · signed -1
b
0x01
unsigned 1 · signed 1
a - b (wrapped)
0xfe
unsigned 254 · signed -2
ZF=0SF=1CF=0OF=0
  • ZF: the result is zero.
  • SF: the result's top bit (bit 7) is set.
  • CF: unsigned a is below unsigned b, so the subtraction borrowed.
  • OF: the signed answer doesn't fit in 8 bits, so the result's sign is wrong.
JumpAfter cmp a, bReadsTaken
Equality
je / jza == bZF=1Not taken
jne / jnza != bZF=0Taken
Signed
jl / jngea < bSF≠OFTaken
jge / jnla >= bSF=OFNot taken
jle / jnga <= bZF=1 or SF≠OFTaken
jg / jnlea > bZF=0 and SF=OFNot taken
Unsigned
jb / jca < bCF=1Not taken
jae / jnca >= bCF=0Taken
jbe / jnaa <= bCF=1 or ZF=1Not taken
ja / jnbea > bCF=0 and ZF=0Taken

Segment Registers

x86 still drags six segment registers around from the 16-bit era: CS, DS, SS, ES, FS, and GS. They were core machinery in the original 8086's segmented memory model. In 64-bit mode most are vestigial, though operating systems repurpose FS and GS for thread-local storage and kernel data structures. On Linux, fs:0x28 is where each thread keeps its stack canary, which you'll meet in Exploit Mitigations.

SIMD Registers

Modern x86 processors include additional register sets for SIMD (Single Instruction, Multiple Data) operations:

  • XMM0-XMM15 - 128-bit registers for SSE operations.
  • YMM0-YMM15 - 256-bit registers for AVX (the XMM registers are the lower half).
  • ZMM0-ZMM31 - 512-bit registers for AVX-512.

These are used for floating-point maths, vector processing, and cryptographic operations.

Comparing to Our Teaching ISA

Our teaching ISA gives you eight registers (R0-R7), all identical and freely interchangeable. x86 has 16 in 64-bit mode, wider at 64 bits, and many of them carry implicit roles you have to remember. The whole register set reads like an archaeological dig — each layer added by a different generation of extension.