Contents

How Functions Talk: Calling Conventions and Syscalls on x86, x64, and ARM

Have you ever wondered what actually happens when you write a simple function call like foo(a, b) in C?

To us, it feels like magic—we pass some arguments, the function runs, and we get a result. But underneath the hood, the CPU is running a strict, highly coordinated choreography. It has to decide:

  • Do we pass these arguments through registers or push them into memory (the stack)?
  • In what order do we pass them?
  • Who cleans up the memory when the function is done?
  • How does the CPU remember where to go back to after the function finishes?

These rules are known as calling conventions.

In this post, we’ll demystify how functions talk to each other across different CPU architectures (x86, x64, and ARM) using a simple C program as our roadmap:

#include <unistd.h>

int foo (int a, int b){
    return a + b;
}

void say_hello(){
    write(1, "Hello\n", 6);
}

int main (int argc, char **argv){
    int a = 10;
    int b = 20;
    int c = foo(a, b);
    say_hello();
    return c;
}

We will walk step-by-step through how foo(a, b) compiles on each architecture, and then look at say_hello() to see how user-space programs cross the border to talk to the operating system kernel via system calls (syscalls).


The Rules of the Game: Caller vs. Callee

Before diving into assembly, we need to understand the relationship between the caller (the function making the call, like main) and the callee (the function being called, like foo).

Because registers are scarce resources shared by all functions, calling conventions divide them into two categories:

  • Caller-Saved (Volatile): Think of these as public scratchpads. The caller must save their values before making a call if it wants to keep them, because the callee is free to overwrite them. On x86, these are EAX, ECX, and EDX.
  • Callee-Saved (Non-Volatile): Think of these as private drawers. The callee is allowed to use them, but it must save their original values (usually by pushing them to the stack) and restore them before returning. On x86, these include EBX, ESI, EDI, EBP, and ESP.

Part 1: x86 (32-bit) User-Space Calling Conventions

The 32-bit x86 architecture is register-starved—it only has 8 general-purpose registers, and most of them already have dedicated jobs. Because of this, x86 relies heavily on the stack (RAM) to pass arguments.

1. cdecl (The Linux Default)

In C/C++ on Linux, cdecl is the standard.

How cdecl works under the hood:
  1. Pushing Right-to-Left: The caller pushes the arguments onto the stack in reverse order. So, b (20) is pushed first, followed by a (10). Why reverse order? This ensures the first argument (a) is always at the very top of the stack, which is critical for variadic functions like printf to easily locate the format string.
  2. The Jump: The call instruction is executed. This pushes the return address (the next instruction in main) onto the stack and jumps to foo.
  3. Establishing a Frame: Inside foo, a stack frame is set up using the Base Pointer (EBP). This gives the function a stable “base” to look up its arguments and local variables.
  4. Return Value: The result is calculated and placed in the EAX register.
  5. Stack Cleanup: Once foo returns, the caller (main) is responsible for cleaning up the stack by moving the Stack Pointer (ESP) back.

Let’s look at the complete compiled assembly for main and foo:

Complete Assembly of main:

main:
    push ebp            ; Step 1: Save the caller's old frame pointer
    mov ebp, esp        ; Step 2: Make our stack pointer the new base pointer
    sub esp, 16         ; Step 3: Reserve 16 bytes on the stack for locals (a, b, c)
    
    mov dword [ebp - 4], 10  ; Local 'a' = 10
    mov dword [ebp - 8], 20  ; Local 'b' = 20
    
    ; --- Preparing the call to foo(a, b) ---
    push dword [ebp - 8]     ; Push 'b' (20) onto the stack first (Right-to-Left)
    push dword [ebp - 4]     ; Push 'a' (10) onto the stack second
    call foo                 ; Jump to foo (pushes return address to stack)
    add esp, 8               ; Stack Cleanup: Caller clears the 8 bytes we pushed
    mov dword [ebp - 12], eax ; Store the return value (from EAX) into 'c'
    
    ; --- Preparing the call to say_hello() ---
    call say_hello           ; call say_hello
    
    mov eax, dword [ebp - 12] ; Put 'c' into EAX (our return code)
    leave                    ; Restore stack/frame pointers (mov esp, ebp / pop ebp)
    ret                      ; Return to OS

Callee Side (foo):

foo:
    push ebp            ; Set up stack frame
    mov ebp, esp        
    mov eax, [ebp + 8]  ; Look up 'a' (at EBP + 8 bytes)
    add eax, [ebp + 12] ; Add 'b' (at EBP + 12 bytes) to EAX
    pop ebp             ; Clean up stack frame
    ret                 ; Return (result is in EAX)

2. stdcall (The Windows Choice)

If you’ve ever written code for the Windows API, you’ve seen stdcall.

  • The Difference: The parameters are still pushed right-to-left, but the callee (the function being called) cleans up the stack instead of the caller.
  • How: The callee uses the ret N instruction, which pops the return address and adds N bytes to ESP at the same time.

Caller Side (Inside main):

; (Inside main: Prologue, stack allocation, and variable initialization are identical to cdecl)
push 20         ; Push 'b' (20)
push 10         ; Push 'a' (10)
call foo        ; Call foo. Callee will clean up!
; Note: No 'add esp, 8' here. The caller does nothing.
; (Rest of main: call say_hello and Epilogue are identical to cdecl)

Callee Side (foo):

foo:
    push ebp
    mov ebp, esp
    mov eax, [ebp + 8]
    add eax, [ebp + 12]
    pop ebp
    ret 8               ; Returns AND cleans up 8 bytes of stack arguments!

3. fastcall

As the name suggests, fastcall optimizes speed by avoiding memory writes to the stack.

  • The Rule: The first two arguments are placed directly into registers ECX and EDX. If there are more than two arguments, the rest go on the stack.
  • Stack Cleanup: The callee cleans up the stack (similar to stdcall).

Caller Side (Inside main):

; (Inside main: Prologue, stack allocation, and variable initialization are identical to cdecl)
mov edx, 20     ; 2nd argument 'b' goes into EDX register
mov ecx, 10     ; 1st argument 'a' goes into ECX register
call foo        ; Call foo
; (Rest of main: call say_hello and Epilogue are identical to cdecl)

Callee Side (foo):

foo:
    mov eax, ecx    ; Move 'a' (from ECX) to return register EAX
    add eax, edx    ; Add 'b' (from EDX)
    ret             ; Return

Part 2: x86-64 (64-bit) User-Space Calling Conventions

In the transition to 64-bit, the general-purpose register file doubled in size (adding registers R8 through R15, and expanding all registers to 64 bits). With so many registers available, passing arguments via memory became obsolete. Both Windows and Unix systems shifted to register-based passing.

1. System V AMD64 ABI (Linux, macOS, BSD)

In the Unix world, we pass up to 6 integer/pointer arguments via registers. The register pipeline is strictly ordered:

  1. RDI (First argument)
  2. RSI (Second argument)
  3. RDX (Third argument)
  4. RCX (Fourth argument)
  5. R8 (Fifth argument)
  6. R9 (Sixth argument)

If you have a 7th argument, only then does it spill onto the stack.

Complete Assembly of main:

main:
    push rbp
    mov rbp, rsp
    sub rsp, 16         ; Allocate local storage
    
    mov dword [rbp - 4], 10  ; local 'a' = 10
    mov dword [rbp - 8], 20  ; local 'b' = 20
    
    ; --- Preparing the call to foo(a, b) ---
    mov esi, dword [rbp - 8] ; Load 2nd arg 'b' (20) into ESI register
    mov edi, dword [rbp - 4] ; Load 1st arg 'a' (10) into EDI register
    call foo                 ; Call foo
    mov dword [rbp - 12], eax ; Store return value in 'c'
    
    ; --- Preparing the call to say_hello() ---
    call say_hello           ; call say_hello
    
    mov eax, dword [rbp - 12] ; Put 'c' in EAX
    leave
    ret

Callee Side (foo):

foo:
    mov eax, edi    ; EAX = 'a' (retrieved from EDI)
    add eax, esi    ; EAX = EAX + 'b' (retrieved from ESI)
    ret             ; Return

2. Microsoft x64 Calling Convention (Windows)

Windows took a slightly different path. It only uses 4 registers: RCX, RDX, R8, and R9.

  • Shadow Space (Home Space): This is a unique Windows requirement. The caller must reserve 32 bytes of stack space (enough to hold four 8-byte registers) right before making the call. Even if the function takes no arguments, this space must be allocated! The callee can use this area to dump (or “spill”) the register arguments back to the stack if it needs to perform register-heavy tasks.

Caller Side (Inside main):

; (Inside main: Prologue, stack allocation, and variable initialization are identical to System V ABI)
sub rsp, 32     ; Allocate 32 bytes of "Shadow Space" on the stack
mov edx, 20     ; 2nd argument 'b' in EDX
mov ecx, 10     ; 1st argument 'a' in ECX
call foo        ; Call foo
add rsp, 32     ; Deallocate the Shadow Space
; (Rest of main: call say_hello and Epilogue are identical to System V ABI)

Callee Side (foo):

foo:
    mov eax, ecx    ; EAX = 'a' (from ECX)
    add eax, edx    ; EAX = EAX + 'b' (from EDX)
    ret

Part 3: ARM Calling Conventions (32-bit and 64-bit)

ARM is a RISC (Reduced Instruction Set Computer) architecture, designed from the ground up to have a large number of registers.

One crucial difference between x86 and ARM is the Link Register (LR). On x86, the call instruction writes the return address directly to the stack (memory). Because memory is slow, ARM avoids this write by storing the return address in a dedicated CPU register called the LR.

If a function does not call any other functions (known as a leaf function, like foo), it never has to touch the stack. It can read arguments from registers, do the work, and jump straight back using the LR.

1. ARM 32-bit (AAPCS)

  • Register Pipeline: First 4 arguments go in registers R0 through R3.
  • Return Value: Placed in R0.
  • Return Address: Placed in Link Register LR (R14).

Complete Assembly of main:

main:
    push {fp, lr}       @ Step 1: Save the frame pointer and Link Register (so we can return later)
    add fp, sp, #4      @ Step 2: Establish the frame pointer
    sub sp, sp, #12     @ Step 3: Reserve 12 bytes on stack for locals (a, b, c)
    
    mov r3, #10
    str r3, [fp, #-8]   @ store local 'a' = 10
    mov r3, #20
    str r3, [fp, #-12]  @ store local 'b' = 20
    
    @ --- Preparing the call to foo(a, b) ---
    ldr r1, [fp, #-12]  @ Load 'b' (20) into R1 (2nd argument register)
    ldr r0, [fp, #-8]   @ Load 'a' (10) into R0 (1st argument register)
    bl foo              @ Branch with Link: calls foo, sets LR to return address
    str r0, [fp, #-16]  @ Store return value (from R0) in 'c'
    
    @ --- Preparing the call to say_hello() ---
    bl say_hello        @ Branch with Link to say_hello
    
    ldr r0, [fp, #-16]  @ Load return value 'c' into R0 (our return code)
    sub sp, fp, #4      @ Restore stack pointer
    pop {fp, pc}        @ Pop saved FP back, and pop saved LR directly into Program Counter (PC) to return!

Callee Side (foo):

foo:
    add r0, r0, r1  @ R0 = a (from R0) + b (from R1)
    bx lr           @ Branch Exchange: Jump straight back using the Link Register (no stack touched!)

2. ARM 64-bit (AAPCS64)

  • Register Pipeline: The first 8 integer/pointer arguments go in registers X0 through X7 (using the 32-bit sub-registers W0 through W7 for 32-bit integers).
  • Return Value: Placed in X0 (or W0).
  • Return Address: Placed in Link Register LR (X30).

Complete Assembly of main:

main:
    stp x29, x30, [sp, #-32]!  // Save Frame Record: Frame Pointer (x29) and Link Register (x30)
    mov x29, sp                // Set up new frame pointer
    
    mov w0, #10
    str w0, [x29, #16]         // store local 'a' = 10
    mov w0, #20
    str w0, [x29, #20]         // store local 'b' = 20
    
    // --- Preparing the call to foo(a, b) ---
    ldr w1, [x29, #20]         // Load 'b' into W1 (2nd argument)
    ldr w0, [x29, #16]         // Load 'a' into W0 (1st argument)
    bl foo                     // Branch with Link to foo
    str w0, [x29, #24]         // Store return value (from W0) in 'c'
    
    // --- Preparing the call to say_hello() ---
    bl say_hello               // Branch with Link to say_hello
    
    ldr w0, [x29, #24]         // Load 'c' into W0 (our return code)
    ldp x29, x30, [sp], #32    // Restore frame pointer and LR, clean up stack
    ret                        // Return (jumps to address in x30)

Callee Side (foo):

foo:
    add w0, w0, w1  // W0 = a (from W0) + b (from W1)
    ret             // Return (alias for 'br x30')

Part 4: System Calls (Syscalls) vs. User-space

So far, we have only discussed functions calling other functions within the same application (user-space / Ring 3). But what happens when we need to talk to the hardware—for example, writing “Hello” to the screen?

User-space programs are restricted. If they want to perform I/O, allocate physical memory, or communicate over the network, they must ask the operating system kernel (kernel-space / Ring 0) to do it on their behalf.

This request is called a System Call (Syscall).

To bridge this gap, we cannot use the standard user-space calling conventions. The kernel runs with higher privileges and maintains a completely separate stack for security. If the kernel read arguments from the user-space stack, a malicious program could modify those arguments mid-syscall (a vulnerability known as a Time-of-Check to Time-of-Use or TOCTOU bug).

Therefore, system calls pass arguments exclusively through registers.

Let’s look at how our say_hello() function calls the library function write(1, "Hello\n", 6). Under the hood, the C library wrapper loads the system call details into registers and triggers a transition instruction to alert the CPU to hand control over to the kernel.

1. x86 32-bit Linux Syscalls (int 0x80)

  • Transition Trigger: Software interrupt int 0x80.
  • The Convention:
    • The syscall number goes in EAX (sys_write is number 4).
    • Arguments go in: EBX, ECX, EDX, ESI, EDI, EBP (in order).
    • Return value goes in EAX.

How say_hello looks under the hood:

section .data
    msg db "Hello", 0xA ; "Hello\n"
    len equ $ - msg

section .text
say_hello:
    mov eax, 4      ; Syscall number 4: sys_write
    mov ebx, 1      ; 1st Arg (EBX): File descriptor (1 = stdout)
    mov ecx, msg    ; 2nd Arg (ECX): Pointer to message buffer
    mov edx, len    ; 3rd Arg (EDX): Message length (6 bytes)
    int 0x80        ; Software Interrupt: CPU switches to kernel mode
    ret

2. x86-64 Linux Syscalls (syscall)

  • Transition Trigger: The fast-entry instruction syscall.
  • The Convention:
    • Syscall number goes in RAX (sys_write is number 1).
    • Arguments go in: RDI, RSI, RDX, R10, R8, R9.
    • Return value goes in RAX.
    • Note: Notice that the 4th argument goes in R10 instead of the user-space RCX. This is because the CPU’s syscall instruction automatically overwrites RCX with the saved program counter (RIP) to know where to return to user-space.

How say_hello looks under the hood:

section .data
    msg db "Hello", 0xA
    len equ $ - msg

section .text
say_hello:
    mov rax, 1      ; Syscall number 1: sys_write
    mov rdi, 1      ; 1st Arg (RDI): File descriptor (1 = stdout)
    mov rsi, msg    ; 2nd Arg (RSI): Pointer to message buffer
    mov rdx, len    ; 3rd Arg (RDX): Message length (6 bytes)
    syscall         ; Trigger fast system call transition
    ret

3. ARM 32-bit Linux Syscalls (svc #0)

  • Transition Trigger: Supervisor Call instruction svc 0 (previously swi).
  • The Convention:
    • Syscall number goes in R7 (sys_write is number 4).
    • Arguments go in: R0 through R5.
    • Return value goes in R0.

How say_hello looks under the hood:

.section .data
msg:
    .ascii "Hello\n"
len = . - msg

.section .text
say_hello:
    mov r7, #4      @ Syscall number 4: sys_write
    mov r0, #1      @ 1st Arg (R0): File descriptor (1 = stdout)
    ldr r1, =msg    @ 2nd Arg (R1): Pointer to message buffer
    mov r2, #6      @ 3rd Arg (R2): Message length (6 bytes)
    svc #0          @ Supervisor Call: switch to kernel
    bx lr           @ Return

4. ARM 64-bit Linux Syscalls (svc #0)

  • Transition Trigger: Supervisor Call instruction svc #0.
  • The Convention:
    • Syscall number goes in X8 (sys_write is number 64).
    • Arguments go in: X0 through X5.
    • Return value goes in X0.

How say_hello looks under the hood:

.section .data
msg:
    .ascii "Hello\n"
len = . - msg

.section .text
say_hello:
    mov x8, #64     // Syscall number 64: sys_write
    mov x0, #1      // 1st Arg (X0): File descriptor (1 = stdout)
    adr x1, msg     // 2nd Arg (X1): Pointer to message buffer
    mov x2, #6      // 3rd Arg (X2): Message length (6 bytes)
    svc #0          // Supervisor Call: switch to kernel
    ret             // Return

The Grand Summary

To tie everything together, let’s break down the comparative conventions into clear, architecture-specific tables. This prevents horizontal scrolling and helps highlight how each platform solves parameter passing.

1. x86 (32-bit) Conventions

Primarily memory/stack-based due to the limited number of registers.

ConventionContextArgument PassingStack CleanupReturn / Syscall ID
cdeclUser-space (Linux C)Stack (Right-to-Left)Caller (add esp, N)Result in EAX
stdcallUser-space (Windows API)Stack (Right-to-Left)Callee (ret N)Result in EAX
fastcallUser-space (Windows)ECX, EDX $\rightarrow$ StackCallee (ret N)Result in EAX
int 0x80Kernel (Linux Syscall)EBX, ECX, EDX, ESI, EDI, EBPN/A (Register-only)Syscall ID in EAX

2. x86-64 (64-bit) Conventions

Register-based, leveraging the expanded general-purpose registers.

ConventionContextArgument Registers (In Order)Stack OverflowStack CleanupReturn / Syscall ID
System V ABIUser-space (Linux/macOS)RDI, RSI, RDX, RCX, R8, R9StackCallerResult in RAX
Microsoft x64User-space (Windows)RCX, RDX, R8, R9 (Requires 32B Shadow Space)StackCallerResult in RAX
syscallKernel (Linux Syscall)RDI, RSI, RDX, R10, R8, R9N/A (Register-only)N/ASyscall ID in RAX

3. ARM (32-bit & 64-bit) Conventions

RISC-based conventions utilizing registers and a Link Register (LR) to store return addresses instead of memory.

ConventionArchitectureContextArgument RegistersOverflowReturn AddressReturn / Syscall ID
AAPCSARM 32-bitUser-spaceR0, R1, R2, R3StackLink Register (LR / R14)Result in R0
ARM32 SyscallARM 32-bitKernelR0, R1, R2, R3, R4, R5N/ALink Register (LR)Syscall ID in R7
AAPCS64ARM 64-bitUser-spaceX0 through X7StackLink Register (LR / X30)Result in X0
ARM64 SyscallARM 64-bitKernelX0 through X5N/ALink Register (LR)Syscall ID in X8

Key Takeaways

When reading or writing assembly across platforms, keep these core rules of thumb in mind:

  1. Stack vs. Registers: 32-bit platforms pass arguments in memory (stack) because registers are scarce. 64-bit platforms pass arguments in registers for raw execution speed.
  2. The Security Boundary: User-space stack pointer references are never used by the kernel during a system call. Arguments are loaded into registers before transitioning to Ring 0 to prevent race conditions.
  3. The Link Register (LR) Power: Unlike x86 which performs a memory write (push) to store the return address on every function call, ARM keeps the return address in a CPU register (LR). This means “leaf functions” (functions that don’t call other functions) can execute and return without ever touching RAM.
  4. The RCX vs. R10 Trap: In x86-64 Linux system calls, the syscall instruction automatically uses the RCX register to store the user-space return address. Because of this hardware behavior, the kernel receives its 4th argument in R10 instead of RCX.

The next time you write write(1, "Hello\n", 6) or foo(a, b) in C, you can visualize exactly how your compiler rearranges your local stack frames and loads registers to keep the low-level machine humming!