A cheatsheet isn’t for rote memorization—it’s a lookup reference: every table below is verified against real
objdumpor GDB output rather than memory.
Over the past twenty-four lessons, we have unpacked registers, memory, the stack, function calls, syscalls, the ABI, vectors, and atomics one piece at a time. Here those facts are condensed into compact tables, backed by a small testbed that probes four of them directly: argument placement, SIB addressing, RFLAGS bits, and callee-saved register preservation.
Mental Model: Reference vs. Rote Memory
| Intuition | What the testbed confirms |
|---|---|
| There are too many registers to keep straight | 16 general-purpose registers, each with four bit-widths and a defined ABI role |
| Function arguments always go on the stack | The first six integer arguments travel in rdi, rsi, rdx, rcx, r8, r9 |
| Flags are separate variables | There is one RFLAGS register, and jcc tests its individual bits |
| Any function can clobber any register | Half the GPRs are callee-saved and half are caller-saved |
syscall works just like call |
The fourth argument goes in r10, and rcx and r11 are clobbered |
Registers and Bit Widths
Register width is selected by register name:
rax— full 64 bits;eax— low 32 bits;ax— low 16 bits;al— low 8 bits.
| 64 | 32 | 16 | 8 | System V ABI Role |
|---|---|---|---|---|
rax |
eax |
ax |
al |
return value, caller-saved |
rbx |
ebx |
bx |
bl |
callee-saved |
rcx |
ecx |
cx |
cl |
4th integer argument, caller-saved |
rdx |
edx |
dx |
dl |
3rd argument and upper half of RDX:RAX |
rsi |
esi |
si |
sil |
2nd argument, caller-saved |
rdi |
edi |
di |
dil |
1st argument, caller-saved |
rbp |
ebp |
bp |
bpl |
frame pointer, callee-saved |
rsp |
esp |
sp |
spl |
stack pointer, must be restored before ret |
r8 |
r8d |
r8w |
r8b |
5th argument, caller-saved |
r9 |
r9d |
r9w |
r9b |
6th argument, caller-saved |
r10 |
r10d |
r10w |
r10b |
4th syscall argument, caller-saved |
r11 |
r11d |
r11w |
r11b |
clobbered by syscall, caller-saved |
r12 |
r12d |
r12w |
r12b |
callee-saved |
r13 |
r13d |
r13w |
r13b |
callee-saved |
r14 |
r14d |
r14w |
r14b |
callee-saved |
r15 |
r15d |
r15w |
r15b |
callee-saved |
Writing to a 32-bit register name zero-extends into the upper 32 bits of the 64-bit register, whereas writing to an 8- or 16-bit name leaves the upper bits untouched. That is why mov eax, 1 clears all of rax, while mov al, 1 modifies only the low byte.
Beyond the general-purpose registers, execution state includes rip (next instruction pointer), rflags (status flags), xmm0–xmm15 (128-bit SSE), st0–st7 (x87 FPU stack), k0–k7 (AVX-512 opmask registers), and ymm/zmm for wider vector extensions.
System V ABI at a Glance
| Item | Location / Rule |
|---|---|
| 1st–6th integer/pointer arguments | rdi, rsi, rdx, rcx, r8, r9 |
| 7th and subsequent arguments | on the stack, starting at [rsp + 8] inside the callee |
| Integer return value | rax |
| 128-bit return value | rdx:rax |
| Floating-point arguments | xmm0–xmm7 |
| Floating-point return value | xmm0 |
| Callee-saved | rbx, rbp, r12–r15 |
| Caller-saved | rax, rcx, rdx, rsi, rdi, r8–r11 |
| Stack alignment | rsp % 16 == 0 immediately before call |
| Red zone | 128 bytes below rsp that a leaf function (calling no other functions) may use without adjusting rsp |
| Variadic calls | al holds the count of xmm registers passed |
Upon entering a function, rsp is always misaligned by 8 bytes relative to 16 because call has just pushed the 8-byte return address. Consequently, a prologue with a single push re-aligns rsp to 16 bytes, whereas a prologue with two pushes leaves it misaligned by 8.
RFLAGS and the jcc Matrix
RFLAGS is not a set of independent variables—it is a single 64-bit register. Six bits matter most for branching and system state:
| Bit | Name | Meaning | Modified by |
|---|---|---|---|
| 0 | CF |
unsigned carry / borrow | add, sub, cmp, shl |
| 2 | PF |
parity of the low byte of the result | most arithmetic instructions |
| 6 | ZF |
result is zero | test, cmp, sub |
| 7 | SF |
sign bit (MSB) of the result | arithmetic and logic instructions |
| 9 | IF |
interrupt enable flag (kernel system flag, not writable from ring 3 without privileges) | sti, cli |
| 11 | OF |
signed arithmetic overflow | add, sub, imul |
Conditional jumps fall into two families: signed jumps inspect SF and OF, while unsigned jumps inspect CF and ZF.
| Instruction | Condition | Family |
|---|---|---|
je / jz |
ZF = 1 |
both |
jne / jnz |
ZF = 0 |
both |
jg / jnle |
ZF = 0 and SF = OF |
signed |
jge / jnl |
SF = OF |
signed |
jl / jnge |
SF != OF |
signed |
jle / jng |
ZF = 1 or SF != OF |
signed |
ja / jnbe |
CF = 0 and ZF = 0 |
unsigned |
jae / jnb |
CF = 0 |
unsigned |
jb / jnae |
CF = 1 |
unsigned |
jbe / jna |
CF = 1 or ZF = 1 |
unsigned |
js / jns |
SF = 1 / SF = 0 |
signed |
jo / jno |
OF = 1 / OF = 0 |
signed |
The exact same pair of inputs can trigger opposite outcomes depending on the jump family:
jatestsCFandZF;jgtestsSFandOF.
Use ja/jb exclusively for unsigned comparisons and jg/jl exclusively for signed comparisons.
SIB Addressing
An effective address follows the formula [base + index * scale + disp]. Only four scale factors exist in hardware:
| Field | Valid Values | Constraint |
|---|---|---|
base |
any 64-bit GPR | rip uses a separate RIP-relative encoding |
index |
any 64-bit GPR except rsp |
encoding rsp in the index field means “no index” |
scale |
1, 2, 4, 8 | no other scale factors can be encoded |
disp |
8-bit or 32-bit | sign-extended to 64 bits in 64-bit mode |
Addressing examples:
[rdi]— base register only;[rdi + 8]— base plus displacement;[rdi + rsi * 4]—int32_tarray indexing;[rdi + rsi * 8 + 16]— struct field inside an array of 64-bit elements or pointers.
With lea, the CPU evaluates the same effective-address formula without dereferencing memory.
Syscalls and Data Directives
A Linux syscall differs from a C function call:
- syscall number goes in
rax; - arguments go in
rdi,rsi,rdx,r10,r8,r9; - return value comes back in
rax.
The syscall instruction clobbers rcx and r11 in hardware.
| Syscall | rax |
Arguments | Return Value |
|---|---|---|---|
read |
0 | rdi = fd, rsi = buf, rdx = count |
byte count or -errno |
write |
1 | rdi = fd, rsi = buf, rdx = count |
byte count or -errno |
open |
2 | rdi = path, rsi = flags, rdx = mode |
fd or -errno |
close |
3 | rdi = fd |
0 or -errno |
mmap |
9 | rdi, rsi, rdx, r10, r8, r9 |
address or -errno |
munmap |
11 | rdi = addr, rsi = len |
0 or -errno |
exit |
60 | rdi = code |
does not return |
Data directives specify the size of each emitted element, not the total byte length of the line.
| Directive | Size | Example |
|---|---|---|
db |
1 byte | db "ok", 10 |
dw |
2 bytes | dw 0x1234 |
dd |
4 bytes | dd 1.0 |
dq |
8 bytes | dq 0x1122334455667788 |
resb |
1 byte, uninitialized | resb 64 |
resw |
2 bytes, uninitialized | resw 32 |
resd |
4 bytes, uninitialized | resd 16 |
resq |
8 bytes, uninitialized | resq 8 |
equ defines an assembly-time constant, times repeats a directive, and align pads up to a boundary. String length should still be computed by the assembler via $ - label, never counted by hand.
Vectors, BMI, and Memory Barriers
| Extension | Width | Example | Key Additions |
|---|---|---|---|
x87 |
80-bit | fld, faddp |
eight stack registers st0–st7 |
SSE2 |
128-bit | movaps, addsd |
xmm0–xmm15, scalar double math |
AVX2 |
256-bit | vpaddd, vpmovzxbd |
ymm registers, three-operand VEX encoding |
AVX-512 |
512-bit | vaddps zmm0, zmm1, zmm2 |
zmm registers, EVEX encoding, opmasks k0–k7 |
BMI1 |
64-bit | andn, bextr, blsi |
single-instruction bit manipulation on GPRs |
BMI2 |
64-bit | mulx, shrx, pdep, pext |
flagless multiply outside rdx:rax, shifts without cl |
Memory fences do not flush caches or “clear” RAM—they constrain the ordering in which ordinary loads and stores become globally visible.
| Instruction | What it orders |
|---|---|
lfence |
loads before the fence complete before loads after it |
sfence |
stores before the fence become visible before stores after it |
mfence |
both loads and stores across the fence |
lock prefix |
a single read-modify-write instruction atomically as a full barrier |
pause |
hints to an SMT sibling thread inside a spin-wait loop |
Testbed: Probing Four Facts from the Tables
lessons/24-cheatsheet/abi_probe.asm verifies argument placement, SIB addressing, RFLAGS bits, and callee-saved register preservation directly on hardware.
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; abi_probe.asm — four cheatsheet facts: arguments, SIB, RFLAGS, and callee-saved registers.
default rel
%define FLAGS_MASK 0x8C1
section .text
global asm_sum6
global asm_pick_sib
global asm_flags_probe
global asm_callee_saved
; ------------------------------------------------------------------------------
; int64_t asm_sum6(int64_t a, int64_t b, int64_t c, int64_t d, int64_t e, int64_t f)
; Input: rdi, rsi, rdx, rcx, r8, r9
; Output: rax = a + b + c + d + e + f
; Clobbers: rax, flags (caller-saved)
; ------------------------------------------------------------------------------
asm_sum6:
lea rax, [rdi + rsi]
add rax, rdx
add rax, rcx
add rax, r8
add rax, r9
ret
; ------------------------------------------------------------------------------
; int32_t asm_pick_sib(const int32_t *arr, size_t index)
; Input: rdi = arr, rsi = index
; Output: eax = arr[index]
; Clobbers: rax (flags untouched)
; ------------------------------------------------------------------------------
asm_pick_sib:
mov eax, dword [rdi + rsi * 4]
ret
; ------------------------------------------------------------------------------
; uint64_t asm_flags_probe(int64_t a, int64_t b)
; Input: rdi = a, rsi = b
; Output: rax = CF, ZF, SF, and OF from a - b in bits 0, 6, 7, and 11
; Clobbers: rax, flags
; ------------------------------------------------------------------------------
asm_flags_probe:
mov rax, rdi
sub rax, rsi
pushfq
pop rax
and eax, FLAGS_MASK
ret
; ------------------------------------------------------------------------------
; uint64_t asm_callee_saved(uint64_t x)
; Input: rdi = x
; Output: rax = x + 0x1111 + 0x2222
; Clobbers: rax, flags (caller-saved)
; Preserves: rbx, r12 (callee-saved; push/pop in prologue and epilogue)
; ------------------------------------------------------------------------------
asm_callee_saved:
push rbx
push r12
mov rbx, 0x1111
mov r12, 0x2222
lea rax, [rdi + rbx]
add rax, r12
pop r12
pop rbx
ret
section .note.GNU-stack noalloc noexec nowrite progbitsasm_sum6 receives its six arguments in the exact registers listed in the ABI table. Breaking on the first instruction in GDB confirms all six values:
$ gdb -q --nx -batch -ex 'break asm_sum6' -ex run \
-ex 'info registers rdi rsi rdx rcx r8 r9' ./build/abi_probe_test
Breakpoint 1, asm_sum6 () at abi_probe.asm:22
22 lea rax, [rdi + rsi]
rdi 0x1 1
rsi 0x2 2
rdx 0x3 3
rcx 0x4 4
r8 0x5 5
r9 0x6 6asm_pick_sib demonstrates that scaling by 4 inside a SIB byte isn’t a separate “multiply by four” instruction—it’s encoded directly into the addressing mode. objdump prints it inside the memory operand:
$ objdump -d -M intel build/abi_probe.o | sed -n '/<asm_pick_sib>:/,+2p'
0000000000000011 <asm_pick_sib>:
11: 8b 04 b7 mov eax,DWORD PTR [rdi+rsi*4]
14: c3 retasm_flags_probe subtracts b from a and immediately captures RFLAGS. Masking with 0x8C1 isolates CF, ZF, SF, and OF:
$ ./build/abi_probe_test
sum6: 21
sib: 30
flags_gt: 0
flags_less: 129
flags_zero: 64
flags_overflow: 2048
callee_saved: 17203
abi: ok0—flags_gtfor5 - 3(a > b): none of the four tracked flags are set;129(0x81) —CFandSFfrom3 - 5: no unsigned borrow occurs whenever , whereas setsCF;64(0x40) —ZFfrom5 - 5, since the result is zero;2048(0x800) —OFalone fromINT64_MIN - 1. Unsigned0x8000000000000000 - 1needs no borrow soCF = 0, while signed subtraction wraps around soOF = 1;17203(0x4333) —0x1000 + 0x1111 + 0x2222;rbxandr12are restored to the caller’s values.
Common Failure Modes
-
Thinking callee-saved means “do not touch this register.”
Symptom:asm_callee_savedmodifiesrbxandr12, yet restores them withpopbeforeret.
Cause: The ABI only requires the register’s value to match upon return—it doesn’t forbid using it inside the function.
Fix:pushin the prologue andpopin reverse order on every exit path. -
Assuming flags stay valid arbitrarily long after
sub.
Symptom:and eax, FLAGS_MASKoverwritesZF,SF, andOF, which is whypushfqprecedes it.
Cause: Nearly every arithmetic and logical instruction updates some subset ofRFLAGS.
Fix: Capture flags immediately after the instruction of interest usingsetccorpushfq. -
Expecting
shl eax, 32to shift a 64-bit value.
Symptom: On a 32-bit operand, the shift count is masked modulo 32, turningshl eax, 32intoshl eax, 0.
Cause: Operand width governs both the result width and the shift-count mask.
Fix: Use a 64-bit destination register when shifting by 32 or more:shl rax, 32. -
Trying to encode
[rax + rsp * 2]as a standard SIB operand.
Symptom:[rsp + rax * 2]assembles cleanly (rspis base,raxis index), whereas[rax + rsp * 2]fails to encode.
Cause: Binary100in the SIB index field is reserved to mean “no index register,” sorspcan serve as a base but never as an index.
Fix: Move the index to another register such asrcx([rax + rcx * 2]), or makerspthe base ([rsp + rax * 2]).
Lab
Goal. Add asm_sum7 to the testbed to accept seven integer arguments and return their sum.
Contract.
- C99 prototype:
int64_t asm_sum7(int64_t a, int64_t b, int64_t c, int64_t d, int64_t e, int64_t f, int64_t g). - The first six arguments arrive in registers; the seventh arrives on the stack.
- Do not clobber any callee-saved registers.
- The C99 test checks
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28and verifies thatrbxandr12remain unchanged after the call.
Acceptance criteria.
make testpasses.objdump -d -M intel build/abi_probe.oshows reading the seventh argument from[rsp + 8].make leaksreports no ASan errors.
Hints, expected output, and walkthrough
Inside the callee, [rsp] holds the return address pushed by call, so the seventh argument sits at [rsp + 8]. Reading [rsp] instead adds the return address into the sum, producing a large pseudo-random number instead of 28.
Expected testbed output:
sum7: 28
sum7: okIf the sum comes back as 21, the seventh argument was skipped. If it’s off by a fixed or huge offset, you read the wrong stack slot. If rbx or r12 changed across the call, your prologue/epilogue missed a push/pop.
Testbed sources, Makefile, and automated tests for this lesson: lessons/24-cheatsheet.
Summary
- The sixteen general-purpose registers each expose four bit-widths, and writing to a 32-bit name zeroes the upper 32 bits.
- The first six integer arguments pass in
rdi,rsi,rdx,rcx,r8,r9; any additional ones spill onto the stack. RFLAGSis a single register whoseCF,ZF,SF, andOFbits drive conditional jumps (jcc).- SIB addressing supports scales 1, 2, 4, and 8, and
rspcannot be used as an index register. syscalltakes its fourth argument inr10and clobbersrcxandr11.- Data directives
db–dqemit initialized elements of fixed width, whileresb–resqreserve space without emitting file bytes.
CPUID, XCR0, and the feature matrix for SSE2, AVX, AVX2, AVX-512, and BMI.