Calling C functions

EXTERN and linking

EXTERN declares a symbol defined in another object, like a libc function. Link against libc with gcc, which adds the startup code and the dynamic linker flags:

extern printf section .data fmt db "printf says %ld", 10, 0 section .text global main main: push rbp ; keep RSP 16-byte aligned mov rdi, fmt mov rsi, 42 xor eax, eax call printf wrt ..plt pop rbp xor eax, eax ret

Assemble the file with nasm and link it with gcc: main returns to the C startup code, and the pushed RBP keeps RSP 16-byte aligned for the call.

nasm -f elf64 -o print.o print.asm
gcc -no-pie -o print print.o
./print
echo $?

printf says 42
0

The System V ABI

Integer arguments go in RDI, RSI, RDX, RCX, R8, R9; floating-point arguments in XMM0-XMM7; the return value in RAX (or XMM0). AL must hold the number of vector registers used when calling a variadic function like printf:

mov rdi, fmt mov rsi, rcx xor eax, eax ; zero vector regs for printf call printf wrt ..plt

A printf example

The wrt ..plt suffix routes the call through the procedure linkage table, which the dynamic linker fills in. For a program that only uses syscalls, plain ld works; with libc, link with gcc instead.

Questions

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

  1. What does extern printf mean?

    Answer

    The symbol is defined in another object

    printf lives in the C library, so the assembly file only declares its name.

  2. Why link a libc program with gcc instead of plain ld?

    Answer

    gcc adds the C startup code and the dynamic linker setup

    The libc program expects the C runtime to call main and to have libc loaded.

  3. What does wrt ..plt do?

    Answer

    It routes the call through the procedure linkage table

    The PLT entry is filled by the dynamic linker with the real address.

  4. Which register must hold the number of vector registers used for a variadic call?

    Answer

    AL

    The low byte of RAX says how many XMM registers carry arguments.

  5. Where does a variadic function such as printf find the integer arguments?

    Answer

    RDI, RSI, RDX, RCX, R8, R9

    The System V ABI passes integer arguments in the six integer registers.

  6. Why does a program push RBP once before it calls printf from main?

    Answer

    To keep RSP 16-byte aligned at the call

    At the entry to main RSP is 8 mod 16; one push makes it 16-byte aligned.

  7. What does xor eax, eax do before the printf call?

    Answer

    It says no vector registers are used

    AL counts the vector registers for a variadic call, and this call uses none.

  8. What does the C startup code expect main to return?

    Answer

    The exit status in RAX

    main returns its int in EAX, and the startup code passes it to exit.

  9. How does an assembly program print the number 42 with printf?

    Answer

    printf with the format and 42 in RSI

    printf formats the %ld with the value from RSI.

  10. In the System V ABI, where does a floating-point argument go?

    Answer

    XMM0 to XMM7

    The eight XMM registers carry the first floating-point arguments.

The code on this page was assembled with NASM 3.01 and linked with gcc 15.2.0, and the program was run on Alpine Linux 3.24.3 (x86_64) in a Docker container. It needs libc, so it is linked with gcc rather than with ld alone.

Article author: Arthur Isaev