LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 11
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What is the relationship between %rax, %eax, %ax, %ah, and %al?

Answer

They are all overlapping views of the same physical 64-bit register at different widths — write the small one and you change part of the big one.

Nested bit-ranges of %rax showing %eax, %ax, %ah and %al as overlapping windows.

* The named sub-registers are overlapping windows into %rax; a 32-bit write to %eax zeros the upper 32 bits, but %ax/%al writes do not. *

x86-64 keeps decades of register history layered on top of each other. %rax is the full 64-bit register; %eax is its low 32 bits; %ax its low 16; and %ax itself splits into two bytes, %ah (the high byte) and %al (the low byte).

|63--------------------32|31-----------16|15--8|7--0|
|                        |     %eax              |
|         %rax                      |  %ax        |
|                                   | %ah | %al   |
Name Size Bits
%rax 64-bit 0–63
%eax 32-bit 0–31
%ax 16-bit 0–15
%ah 8-bit 8–15
%al 8-bit 0–7

The one behavior that surprises everyone: writing a 32-bit subregister zeros the upper 32 bits of the 64-bit register, but writing a 16- or 8-bit subregister leaves the upper bits untouched.

mov $0x123456789ABCDEF0, %rax   # %rax fully set
mov $0x1, %eax                  # %rax now 0x0000000000000001 — top half wiped!

This zero-extension rule is a constant source of bugs (and useful idioms) in x86-64.

Go deeper:

Registers available in the x86-64 instruction set
Registers available in the x86-64 instruction set
Immae · CC BY-SA 3.0 · Wikimedia Commons
or press any other key

Question

How many general-purpose registers does x86-64 have, and what are their conventional roles?

Answer

x86-64 has 16 general-purpose 64-bit registers — the original 8 inherited from IA-32 plus 8 new ones (%r8–%r15) — each with a conventional role under the System V calling convention.

Colour-coded grid of the 16 x86-64 general-purpose registers with their System V roles.

* The System V ABI fixes each register's role: %rdi-%r9 pass the first six args, %rax returns, %rbx/%rbp/%r12-%r15 are callee-saved, %r10/%r11 scratch, %rsp the stack. *

Hardware doesn't force most of these roles; they're conventions the calling convention agrees on so that separately compiled functions interoperate. The original eight keep names from the 8086 era (the letters once meant Accumulator, Base, Counter, Data, etc.).

Original 8 (from IA-32):

  • %rax — return value (also "accumulator")
  • %rbx — callee-saved, general purpose
  • %rcx — 4th argument
  • %rdx — 3rd argument
  • %rsi — 2nd argument
  • %rdi — 1st argument
  • %rbp — frame/base pointer (now general purpose)
  • %rsp — the stack pointer (reserved)

New 8 (added by x86-64): %r8 and %r9 carry the 5th and 6th arguments; %r10/%r11 are caller-saved temporaries; %r12–%r15 are callee-saved.

Why double them? Eight registers caused constant "register pressure" — values had to be spilled to memory. Sixteen registers let far more of a function's working set stay in fast registers, cutting memory traffic.

Go deeper:

or press any other key