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 .text
global main
main:
; ...
call printf wrt ..plt
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.
Next: Debugging with GDB
Article author: Arthur Isaev