Calling C, and being called from C
printf from assembly, an assembly function from C, and what GCC's own call site reveals about the calling convention.
- Call a C library function from assembly, with arguments in the right order
- Write an assembly function that C code can call
- Read GCC's call site for caller cleanup and stack alignment
The calling convention from the last lesson is a contract, and C compilers keep it. That means assembly can call C, and C can call assembly.
printf from assembly
global main
extern printf
section .data
msg db "Testing %i...", 0x0a, 0x00
section .text
main:
push ebp
mov ebp, esp
push 123 ; second argument
push msg ; first argument: the format string
call printf
mov eax, 0 ; return 0 from main
mov esp, ebp
pop ebp
ret
Three things change once the C library is involved. The entry point is main, not _start, because the C runtime’s own _start sets things up and then calls main. extern printf tells the assembler the symbol comes from elsewhere. And the format string ends in 0x00, because C strings — unlike sys_write buffers — are terminated by a zero byte.
Arguments are pushed right to left, so the first argument ends up at the lowest address, [esp] at the moment of the call — exactly where printf expects its format string. mov esp, ebp removes both arguments, so no separate add esp, 8 is needed. Built with
nasm -f elf32 10_call_c_fun.asm -o call_c.o
gcc -m32 call_c.o -o call_c
./call_c # Testing 123...
Assembly called from C
add42.asm implements int add42(int x):
global add42
add42:
push ebp
mov ebp, esp
mov eax, [ebp+8]
add eax, 42
mov esp, ebp
pop ebp
ret
and main.c calls it like any other function, printing Result: 72. The repository keeps the 32-bit binary it built, add42, and disassembling it shows the C side of the contract — GCC’s code for add42(30):
sub esp, 0xc
push 0x1e ; 30
call add42
add esp, 0x10 ; caller removes the argument (and the padding)
mov DWORD PTR [ebp-0xc], eax ; the result
The argument goes on the stack, the result comes back in eax, and the caller cleans up — the same three rules as lesson 6. The extra sub esp, 0xc is padding: GCC keeps esp a multiple of 16 at every call, as the modern 32-bit Linux ABI requires (main even starts with and esp, 0xfffffff0 to force it). 12 bytes of padding plus the 4-byte argument make 16.
10_call_c_fun.asm does not align anything itself. If its caller called main with an aligned stack, main starts 4 bytes below a multiple of 16 (the return address), and its three pushes — ebp, 123, msg — add 12 more, so esp happens to be aligned again exactly at call printf. Push one argument more or fewer and it would not be. GCC’s habit of realigning inside main is a hint that relying on the entry alignment is fragile; and esp, 0xfffffff0 after the prologue makes the assembly version robust too.
What was verified here
The programs in lessons 1–6 were assembled and run as 32-bit binaries for this course. The two programs in this lesson need a 32-bit C library to link, which was not available, so Testing 123... and Result: 72 are what the code computes rather than captured output; the disassembly above comes from the committed add42 binary itself.
Try it yourself. Make 10_call_c_fun.asm print two numbers with "%i and %i\n". In which order do the three pushes go? Then build main.c twice, once with add42.asm and once with add42.c (gcc -m32 -O0 -S add42.c shows the assembly GCC writes): how close is it to the hand-written version?