Registers
Registers are small storage slots inside the CPU. They are the fastest storage there is, and almost every instruction reads or writes at least one of them. x86-64 has sixteen 64-bit general-purpose registers, plus a few special ones.
The general-purpose registers
| 64-bit | Traditional role (name origin) | Common use in Linux programs |
|---|---|---|
rax |
accumulator | return values, syscall number |
rbx |
base | general, preserved across calls |
rcx |
counter | loop counters, 4th argument |
rdx |
data | mul/div high half, 3rd argument |
rsi |
source index | string source, 2nd argument |
rdi |
destination index | string destination, 1st argument |
rbp |
base pointer | stack frame base (optional), preserved |
rsp |
stack pointer | top of the stack — don't use it for data! |
r8 – r15 |
new in 64-bit mode | general; r8, r9 = 5th/6th argument |
The names come from the 16-bit 8086 (1978), where each register had a fixed job. Today almost all of them are interchangeable for ordinary instructions; the "roles" are mostly conventions you will learn in the Calling Convention lesson. A few instructions still use specific registers implicitly: mul/div use rax and rdx, loop uses rcx, string instructions use rsi/rdi/rcx, and shifts by a variable amount use cl.
Sub-registers
Each 64-bit register can also be accessed in smaller pieces. For rax:
63 31 15 7 0
+--------------------------------+----------------+--------+-------+
| rax (64 bits) |
+--------------------------------+----------------------------------+
| eax (32 bits) |
+----------------+-----------------+
| ax (16) |
+--------+--------+
| ah | al |
+--------+--------+| 64-bit | 32-bit | 16-bit | low 8-bit | high 8-bit |
|---|---|---|---|---|
rax rbx rcx rdx |
eax ebx ecx edx |
ax bx cx dx |
al bl cl dl |
ah bh ch dh |
rsi rdi rbp rsp |
esi edi ebp esp |
si di bp sp |
sil dil bpl spl |
— |
r8 … r15 |
r8d … r15d |
r8w … r15w |
r8b … r15b |
— |
They are not separate registers: writing al changes the lowest byte of rax.
section .text
global _start
_start:
mov rax, 0x1122334455667788
mov al, 0xFF ; replace only the lowest byte
mov rdi, rax
call print_hex ; 0x11223344556677FF
mov rax, 0x1122334455667788
mov ah, 0x00 ; bits 8-15
mov rdi, rax
call print_hex ; 0x1122334455660088
mov rax, 0x1122334455667788
mov ax, 0xBEEF ; the lowest 16 bits
mov rdi, rax
call print_hex
mov eax, 60
xor edi, edi
syscall
; print_hex: print rdi as 16 hex digits with a 0x prefix and a newline
print_hex:
sub rsp, 32
mov word [rsp], '0x' ; the prefix (NASM stores '0' first, then 'x')
mov ecx, 16 ; 16 hex digits
.digit:
rol rdi, 4 ; bring the top 4 bits to the bottom
mov eax, edi
and eax, 0xF ; isolate one hex digit (0-15)
cmp eax, 10
jb .decimal
add eax, 'A' - '0' - 10 ; 10-15 -> 'A'-'F'
.decimal:
add eax, '0'
mov rdx, 18
sub rdx, rcx ; position 2..17 in the buffer
mov [rsp + rdx], al
dec ecx
jnz .digit
mov byte [rsp + 18], 10 ; newline
mov eax, 1 ; write(1, rsp, 19)
mov edi, 1
mov rsi, rsp
mov edx, 19
syscall
add rsp, 32
ret0x11223344556677FF
0x1122334455660088
0x112233445566BEEF
This example uses a hexadecimal printer, print_hex, because hex shows the individual bytes clearly. You will understand every instruction in it by the end of the Shifts & Rotates lesson.
The 32-bit rule: writes zero the upper half
There is one important asymmetry. Writing an 8- or 16-bit sub-register leaves the other bits alone, but writing a 32-bit register (eax, edi, r9d, …) clears bits 32–63 of the full register:
section .text
global _start
_start:
mov rax, -1 ; all 64 bits set
mov ax, 5 ; 16-bit write: upper 48 bits untouched
mov rdi, rax
call print_uint
mov rax, -1
mov eax, 5 ; 32-bit write: upper 32 bits cleared!
mov rdi, rax
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret18446744073709486085
5
That is why you will see mov eax, 1 and xor edi, edi instead of mov rax, 1 and xor rdi, rdi in good code: the 32-bit forms have the same effect on the full register and are shorter to encode.
Special registers
| Register | Purpose |
|---|---|
rip |
instruction pointer: address of the next instruction. You can't mov into it; jumps, calls and returns change it. |
rflags |
status flags set by arithmetic and comparisons (zero, carry, sign, overflow, …), used by conditional jumps. |
xmm0 – xmm15 |
128-bit vector registers for floating point and SIMD (later lessons). |
r8 to r15
The eight extra registers that came with 64-bit mode work exactly like the others, with a regular naming scheme for their parts:
section .text
global _start
_start:
mov r12, 1000
mov r12b, 0 ; low byte of r12: 1000 = 0x3E8 -> 0x300
mov rdi, r12
call print_uint ; 768
mov r9d, 70000 ; 32-bit write clears the top of r9
mov r9w, 0 ; 70000 = 0x11170 -> 0x10000
mov rdi, r9
call print_uint ; 65536
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret768
65536
Tip: Never use
rspfor your own data. It points to the top of the stack, whichcall,ret,pushandpop(and the kernel when delivering signals) rely on.
Test yourself
After mov rax, -1 followed by mov eax, 7, what is in rax?
- 0xFFFFFFFF00000007
- 7
- -7
- 0xFFFFFFFFFFFFFF07
Which name refers to the lowest 8 bits of rdi?
didldildh
Which register should never be used for ordinary data?
rbxr15rsprsi