TExcelTAD
Home › x86-64 Assembly › System V Calling Convention

System V Calling Convention

When your code calls a function written by somebody else — a C library, the compiler's output, a colleague's routine — both sides must agree on where arguments go, where the result comes back and which registers survive. On Linux, macOS and the BSDs that agreement is the System V AMD64 ABI. Windows uses a different one (the Microsoft x64 convention).

The rules at a glance

What Where
Integer/pointer arguments 1–6 rdi, rsi, rdx, rcx, r8, r9
More arguments on the stack, pushed right to left (argument 7 at the lowest address)
Floating-point arguments 1–8 xmm0 – xmm7
Integer return value rax (128-bit: rdx:rax)
Floating-point return value xmm0
Stack alignment rsp is a multiple of 16 at the call instruction
Red zone the 128 bytes below rsp may be used by a leaf function without moving rsp

A memory aid for the argument order: "Diane's silk dress costs $89" — DI, SI, DX, CX, R8, R9.

Who saves which registers

Category Registers Meaning
caller-saved (volatile, "scratch") rax, rcx, rdx, rsi, rdi, r8–r11, all xmm a called function may overwrite them; save them yourself if you need them after the call
callee-saved (non-volatile) rbx, rbp, r12–r15, rsp a function must restore them before returning if it changes them

This is the rule every example in this course has relied on: values kept in rbx or r12–r15 survive call print_uint.

A function with three arguments

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 4                  ; length
    mov esi, 5                  ; width
    mov edx, 6                  ; height
    call box_volume
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; box_volume(length, width, height) -> length * width * height
box_volume:
    mov rax, rdi
    imul rax, rsi
    imul rax, rdx
    ret

; ---- 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
    ret
120

More than six arguments

Arguments 7 and up go on the stack. The caller pushes them in reverse order, so argument 7 ends up closest to the top. Inside the function, [rsp] is the return address, so argument 7 is at [rsp + 8], argument 8 at [rsp + 16]. After the call, the caller removes them by adding to rsp:

nasm · try it yourself
section .text
global _start
_start:
    push 8                      ; argument 8 (pushed first)
    push 7                      ; argument 7
    mov edi, 1
    mov esi, 2
    mov edx, 3
    mov ecx, 4
    mov r8d, 5
    mov r9d, 6
    call sum8                   ; rsp was 16-byte aligned: 2 pushes keep it so
    add rsp, 16                 ; caller cleans up the stack arguments
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; sum8(a1, ..., a8) -> the sum of all eight arguments
sum8:
    lea rax, [rdi + rsi]
    add rax, rdx
    add rax, rcx
    add rax, r8
    add rax, r9
    add rax, [rsp + 8]          ; argument 7
    add rax, [rsp + 16]         ; argument 8
    ret

; ---- 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
    ret
36

Prologue, epilogue and the frame pointer

Larger functions usually start with a prologue that sets up a stack frame, and end with a matching epilogue. With rbp as a frame pointer, locals sit at fixed negative offsets from rbp, even if rsp moves during the function:

nasm
my_function:
    push rbp                ; save the caller's frame pointer
    mov rbp, rsp            ; rbp = base of our frame
    sub rsp, 32             ; 32 bytes for local variables
    ; ... locals at [rbp - 8], [rbp - 16], ...
    mov rsp, rbp            ; free the locals   (or: leave)
    pop rbp                 ; restore the caller's frame pointer
    ret

leave is a one-instruction epilogue equal to mov rsp, rbp + pop rbp. Optimizing compilers often omit the frame pointer (-fomit-frame-pointer) and address locals relative to rsp instead, which frees rbp as an extra register.

Here a function keeps three local counters in its frame while counting vowels, digits and other characters of a string:

nasm · try it yourself
section .data
sample:     db "R2-D2 and C-3PO"
sample_len  equ $ - sample

section .text
global _start
_start:
    mov rdi, sample
    mov esi, sample_len
    call classify

    mov eax, 60
    xor edi, edi
    syscall

; classify(ptr, len): print the number of vowels, digits and other characters
classify:
    push rbp
    mov rbp, rsp
    sub rsp, 32                 ; three 8-byte locals (+ padding)
    push rbx                    ; callee-saved registers we use
    push r12
    mov qword [rbp - 8], 0      ; vowels
    mov qword [rbp - 16], 0     ; digits
    mov qword [rbp - 24], 0     ; others
    mov rbx, rdi                ; pointer
    mov r12, rsi                ; remaining length
.next:
    test r12, r12
    jz .report
    movzx eax, byte [rbx]
    or al, 0x20                 ; lowercase for the vowel test
    cmp al, 'a'
    je .vowel
    cmp al, 'e'
    je .vowel
    cmp al, 'i'
    je .vowel
    cmp al, 'o'
    je .vowel
    cmp al, 'u'
    je .vowel
    movzx eax, byte [rbx]
    sub eax, '0'
    cmp eax, 9
    jbe .digit                  ; unsigned range check: '0'..'9'
    inc qword [rbp - 24]
    jmp .advance
.vowel:
    inc qword [rbp - 8]
    jmp .advance
.digit:
    inc qword [rbp - 16]
.advance:
    inc rbx
    dec r12
    jmp .next
.report:
    mov rdi, [rbp - 8]
    call print_uint
    mov rdi, [rbp - 16]
    call print_uint
    mov rdi, [rbp - 24]
    call print_uint
    pop r12
    pop rbx
    leave                       ; mov rsp, rbp + pop rbp
    ret

; ---- 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
    ret
2
3
10

The string R2-D2 and C-3PO has 15 characters: two vowels (a, O), three digits and ten others (letters, dashes and spaces). Notice how the frame works: the three counters live at [rbp - 8], [rbp - 16] and [rbp - 24], and those addresses stay valid even though push rbx, push r12 and the calls to print_uint move rsp around. The frame layout looks like this:

text
[rbp + 8]    return address (pushed by call)
[rbp]        the caller's rbp (pushed by the prologue)
[rbp - 8]    vowels
[rbp - 16]   digits
[rbp - 24]   others
[rbp - 32]   padding (keeps rsp 16-byte aligned)
[rbp - 40]   saved rbx
[rbp - 48]   saved r12   <- rsp

Tip: The convention's alignment rule holds here too: at entry rsp is 16n + 8 (the return address), push rbp makes it 16n, and sub rsp, 32 plus two pushes keep it a multiple of 16 for the calls inside.

Go further: Calling Conventions: System V and Microsoft x64 compares both conventions on the same C and shows how to recognise them in an unknown binary.

Test yourself

In which register is the 4th integer argument passed?

  1. rdx
  2. rcx
  3. r8
  4. r10

Which of these registers must a function restore before returning if it modifies it?

  1. rax
  2. rdx
  3. r12
  4. r11

Inside a function, right after the call, where is the 7th integer argument?

  1. [rsp]
  2. [rsp + 8]
  3. [rbp - 8]
  4. r10