Phase 0 · Module 1 of 4
x86-64 registers, and reading what your compiler wrote
Registers and operating modes · 12 min read
x86-64 gives you sixteen 64-bit general-purpose registers. Their names carry 45 years of history, but the rules for using them today are simple, and once you know them you can read the assembly your compiler produces.
The registers
- RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, and R8–R15: 64 bits each.
- Smaller views of the same register: EAX is the low 32 bits of RAX, AX the low 16, AL the low 8. Writing to EAX zeroes the top half of RAX.
- RSP is the stack pointer; RBP is often a frame pointer.
- RIP, the instruction pointer, and RFLAGS, the status flags.
On Linux and macOS, the System V ABI says the first six integer arguments arrive in RDI, RSI, RDX, RCX, R8 and R9, and the result goes back in RAX. (Windows uses a different convention, which the course also covers.)
Read your compiler's output
// add.c
long add(long a, long b) {
return a + b;
}$ gcc -O2 -S -masm=intel -o - add.c
add:
lea rax, [rdi+rsi]
reta arrives in RDI and b in RSI. The compiler uses LEA, an instruction designed to compute addresses, as a fast way to add two registers and store the result in RAX without touching the flags. Then RET pops the return address and jumps back. Depending on your compiler, you may also see endbr64 at the top: a marker for Intel's control-flow protection.
Modes, briefly
An x86-64 CPU still boots in 16-bit real mode, then firmware or a bootloader moves it to 32-bit protected mode and finally to 64-bit long mode, with paging turned on. Writing that transition yourself is part of the capstone, where you boot your own small kernel under QEMU.
Try it
- Compile long mul(long a, long b) and long square(long a) with -O2. Which instructions appear?
- Write a function with seven long arguments and return the seventh. Where does it come from, if not a register?
In the full course you go on to the System V ABI and stack frames, paging and the TLB, interrupts and the syscall path, SIMD with AVX and the hardware support that makes virtualization possible.