Registers

General purpose

x86-64 has sixteen 64-bit general-purpose registers: rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, and r8 through r15. Any of them can hold any integer value in most instructions.

mov rax, 100 ; any register works mov r8, 200 ; r8-r15 are 64-bit too mov r15b, 1 ; the lowest 8 bits of r15

Traditional roles

Some registers have traditional roles: rax holds return values, rcx and rdx hold multiplication and division results, rsi and rdi index string operations, rbp points at the stack frame, and rsp tracks the top of the stack.

mov rax, 60 ; sys_exit number goes in rax mov rdi, 0 ; exit status goes in rdi

Register widths

Each 64-bit register has narrower views: the low 32 bits (eax), low 16 (ax), and low 8 (al). The four classic registers also have a high byte (ah, bh, ch, dh), which r8 and the others do not. Writing a 32-bit register zero-extends into the 64-bit one:

mov eax, 1 ; rax becomes 0x0000000000000001 mov ax, 0xFFFF ; only the low 16 bits change

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How many general-purpose 64-bit registers does x86-64 have?

    Answer

    16

    The sixteen are rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp and r8 through r15.

  2. Which register holds the first argument of a Linux system call?

    Answer

    rdi

    The call number is in rax, and the arguments start at rdi.

  3. Which register traditionally holds a function return value?

    Answer

    rax

    The System V ABI returns an integer in rax, and Linux returns a syscall result there too.

  4. What is the 32-bit view of r15 called?

    Answer

    r15d

    The 32-bit names are eax, ebx and so on, and r8d through r15d.

  5. What does writing eax do to the upper 32 bits of rax?

    Answer

    It clears them

    A write to a 32-bit register zero-extends into the 64-bit register.

  6. Which of these is not a general-purpose register?

    Answer

    rip

    rip is the instruction pointer; the other three name a general-purpose register or part of one.

  7. What exit status does this program return? section .text global _start _start: mov edi, 7 mov rax, 60 syscall

    Answer

    7

    rdi holds 7 when sys_exit runs, so rdi becomes the status.

  8. What does this program print? section .data msg db "Z", 10 section .text global _start _start: mov r8, 1 mov r9, 1 mov r10, msg mov r11, 2 mov rax, r8 mov rdi, r9 mov rsi, r10 mov rdx, r11 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    Z

    Any general-purpose register can hold a system call argument; here r8-r11 carry the write arguments.

  9. Which register names the top of the stack?

    Answer

    rsp

    rsp points at the top of the stack, and rbp conventionally points at the frame base.

  10. Which part of rax is al?

    Answer

    The low 8 bits

    al is the lowest byte; ax is the low word and eax the low doubleword.

The snippets on this page are fragments, not whole programs: they show the registers and their widths. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev