x86-64 Cheat Sheet#
It is a good idea to have this cheat sheet with you, e.g., by printing it, when writing x86-64 code.
Online emulator: https://cs.au.dk/~timany/x64emu
Registers:
Register |
Description |
Preserved Across Calls |
---|---|---|
|
Instruction pointer; cannot be manipulated directly |
Irrelevant but no! |
|
General purpose; stores return value |
No |
|
General purpose; sometimes also used as the base pointer |
Yes |
|
General purpose; used for 3rd and 4th arguments |
No |
|
Stack pointer |
Yes (automatically) |
|
Can be used as base pointer |
Yes |
|
General purpose; used for 1st and 2nd arguments |
No |
|
General purpose; used for 5th and 6th arguments |
No |
|
General purpose |
No |
|
General purpose |
Yes |
Stack: Stack grows from higher address to lower addresses (push decrements
rsp
; pop incrementsrsp
)Endianness: x86 is little-endian: the value
0xFFA02B1C
is stored in memory as follows:1C2BA0FF
Flags: SF (sign; 1 if negative), ZF (zero), OF (overflow), CF (carry), PF (parity; 1 if number of 1’s is even)
Instructions:
Instruction |
Description |
Flags |
Src/Dest |
---|---|---|---|
|
Move 64-bit value |
- |
Reg/Mem→Reg/Mem |
|
Load effective address |
- |
Mem→Reg |
|
Arithmetic/Logic |
all above |
Reg/Mem→Reg/Mem |
|
Arithmetic/Logic |
all above |
Reg/Mem→Reg/Mem |
|
sets |
- |
→ |
|
divides |
undefined |
Reg/Mem→q: |
|
Shift dest |
all above |
Imm/ |
|
manipulate stack |
- |
Reg/mem↔stack |
|
function calls |
- |
|
|
unconditional jump |
- |
Mem |
|
conditional jump |
- |
Mem |
|
conditionally set byte |
- |
Mem |
Note: the
idivq
instruction throws exception on div by zero or overflow; usecqto
to avoid overflow
Conditions determined by flags for instructions
j(e/ne/g/ge/l/le)
andset(e/ne/g/ge/l/le)
:
Condition |
Flags |
Condition |
Flags |
Condition |
Flags |
---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Operands (we use AT&T assembly syntax with src operand first:
instr src, dest
):
Description |
Syntax |
---|---|
Register |
|
Immediate |
|
|
|
indirect addressing for data |
|
indirect addressing for jump |
|
indirect relative addressing for data |
|
indirect relative addressing for jump |
|
indirect (relative) addressing for conditional jump |
N/A |
Assembly (
.s
) files:
Description |
Syntax |
Description |
Syntax |
---|---|---|---|
Data section |
|
Constant 64-bit number |
|
Text (code) section |
|
Constant string |
|
Labels (code or data) |
|
Declare label global |
|
Calling convention (System V ABI):
The first 6 arguments passed in registers:
%rdi
,%rsi
,%rdx
,%rcx
,%r8
,%r9
(also in the registers’ table)Remaining arguments pushed on the stack from right to left, before call
Respect callee versus caller saved registers; callee saved registers are marked as “Preserved Across Calls” in the registers’ table
Stack must be 16-byte aligned before
callq
instruction, otherwise, your program may crash on calls!Function prologue example:
pushq %rbp ; save the caller's base pointer on the stack movq %rsp, %rbp ; set our base pointer to the current stack pointer ; this is useful to be able to restore it and ; as an anchor for referring to variables on the stack subq 112, %rsp ; reserve memory on stack for function's local ; variables (112 = 14 * 8)
Function epilogue example:
movq %rbp, %rsp; restore the rsp to where it was right after pushing %rbp ; of the caller popq %rbp ; restore the rbp to caller's value ; the stack pointer is now exactly where it was before entering ; the function, i.e., right at the return address retq ; return to the caller