The register contract isn’t checked by the compiler: if an assembly function breaks argument order or stack alignment, the bug blows up inside libc, not at the call site.
Until now, the bridge between C99 and assembly relied on symbol names. Yet symbol names say nothing about where the seventh argument lives or who is responsible for aligning rsp. Those rules make up the ABI. As our testbed demonstrates, violating a single clause crashes printf long before control ever returns to _start.
Mental Model: Registers, Stack, and Alignment
| What it looks like in C99 | What happens in hardware |
|---|---|
| Function arguments are an ordered list | The first six integer arguments go in rdi, rsi, rdx, rcx, r8, r9 |
| The seventh argument goes in a register too | It is pushed onto the stack at [rsp + 8] after call |
double passes just like an integer |
It travels in xmm0–xmm7, in a separate register queue |
printf figures out its arguments on its own |
al reports how many vector registers were passed |
| The callee aligns the stack | The caller must guarantee rsp % 16 == 0 before call |
The integer/pointer register queue (rdi, rsi, rdx, rcx, r8, r9) and the floating-point/vector queue (xmm0–xmm7) are allocated independently. A double argument never consumes an integer register—it only claims the next slot in the vector queue.
So placing a double between two integers does not shift the integer queue: the next int64_t still takes the next free integer register. That rule will matter directly in the asm_mix lab where argument types alternate.
Integer/pointer queue: [ rdi | rsi | rdx | rcx | r8 | r9 ] ──► overflow to stack [rsp+8], [rsp+16]
Float/double queue: [ xmm0 | xmm1 | xmm2 | ... | xmm7 ] ──► overflow to stackEight Arguments: Six in Registers, Two on the Stack
sum8.asm sums eight int64_t values, reading the final two straight off the stack:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; sum8.asm — sum of eight int64_t arguments: six in registers, two on the stack.
default rel
section .text
global asm_sum8
; ------------------------------------------------------------------------------
; int64_t asm_sum8(int64_t a, int64_t b, int64_t c, int64_t d,
; int64_t e, int64_t f, int64_t g, int64_t h)
; Input: rdi, rsi, rdx, rcx, r8, r9, then [rsp + 8] = g and [rsp + 16] = h
; Output: rax = a + b + c + d + e + f + g + h
; Clobbers: rax, flags (caller-saved)
; ------------------------------------------------------------------------------
asm_sum8:
lea rax, [rdi + rsi]
add rax, rdx
add rax, rcx
add rax, r8
add rax, r9
add rax, [rsp + 8]
add rax, [rsp + 16]
ret
section .note.GNU-stack noalloc noexec nowrite progbitsOn the caller side, the C99 compiler lays out the arguments according to this exact contract. Here is the disassembly of main calling asm_sum8(1, 2, 3, 4, 5, 6, 7, 8):
$ objdump -d -M intel build/sum8_test | grep -B12 'call.*<asm_sum8>'
1273: 6a 08 push 0x8
1275: 6a 07 push 0x7
1277: 41 b9 06 00 00 00 mov r9d,0x6
127d: 41 b8 05 00 00 00 mov r8d,0x5
1283: b9 04 00 00 00 mov ecx,0x4
1288: ba 03 00 00 00 mov edx,0x3
128d: be 02 00 00 00 mov esi,0x2
1292: bf 01 00 00 00 mov edi,0x1
1297: e8 d4 01 00 00 call 1470 <asm_sum8>push 0x8 and push 0x7 place the seventh and eighth arguments onto the stack (two qwords equal 16 bytes, preserving rsp % 16 == 0 right before call; with only one stack argument, the compiler would push 8 bytes of padding first). Pushes happen right-to-left: the last argument goes first, leaving 7 on top at the moment of call. The first six arguments fill the integer registers in fixed order.
Once call pushes the return address to [rsp], the seventh argument sits at [rsp + 8] and the eighth at [rsp + 16]. GDB shows this on the very first instruction:
$ gdb -q --nx -batch -ex 'break asm_sum8' -ex run \
-ex 'info registers rdi rsi rdx rcx r8 r9' -ex 'x/2gx $rsp+8' build/sum8_test
Breakpoint 1, asm_sum8 () at sum8.asm:18
rdi 0x1 1
rsi 0x2 2
rdx 0x3 3
rcx 0x4 4
r8 0x5 5
r9 0x6 6
0x7fffffffd6b0: 0x0000000000000007 0x0000000000000008x/2gx prints two 8-byte words: the seventh and eighth arguments. The assembled asm_sum8 function itself is just:
$ objdump -d -M intel build/sum8.o
0000000000000000 <asm_sum8>:
0: 48 8d 04 37 lea rax,[rdi+rsi*1]
4: 48 01 d0 add rax,rdx
7: 48 01 c8 add rax,rcx
a: 4c 01 c0 add rax,r8
d: 4c 01 c8 add rax,r9
10: 48 03 44 24 08 add rax,QWORD PTR [rsp+0x8]
15: 48 03 44 24 10 add rax,QWORD PTR [rsp+0x10]
1a: c3 retasm_sum8 needs no prologue or frame: it touches no callee-saved registers and makes no calls, so rsp points directly to the return address on entry.
variadic Calls and the al Register
Variadic calls have an extra clause in the contract. bad_printf.asm puts 1.5 in xmm0 but zeroes al:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; bad_printf.asm — double passed in xmm0, but al = 0 hides it from printf.
default rel
section .rodata
fmt: db "value = %.2f", 10, 0
value: dq 1.5
section .text
extern printf
extern exit
global _start
_start:
and rsp, -16
movsd xmm0, [value]
lea rdi, [fmt]
xor eax, eax
call printf wrt ..plt
xor edi, edi
call exit wrt ..plt
section .note.GNU-stack noalloc noexec nowrite progbitsThe %.2f format specifier expects a floating-point value, yet al = 0 tells printf that zero vector registers were passed. printf skips spilling xmm0 into its va_arg register-save area and reads uninitialized stack memory instead; on this clean stack it happens to contain zero (on a dirty stack it would print garbage or NaN), yielding 0.00:
$ ./build/bad_printf
value = 0.00The fix is to set al to the number of vector registers used: mov eax, 1 for a single double, or xor eax, eax when passing none.
Stack Alignment and movaps
Before every call, rsp must be a multiple of 16. Inside the callee after call, it is offset by 8 because the return address occupies one qword. abi_misalign.asm deliberately leaves rsp % 16 == 8 right before calling printf:
Caller: rsp % 16 == 0 ──► call ──► pushes ret addr (8 B)
Callee: on entry rsp % 16 == 8 ──► needs push or sub rsp, 8 (else movaps -> SIGSEGV); SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; abi_misalign.asm — calling printf with rsp % 16 == 8 violates System V ABI.
default rel
section .rodata
fmt: db "misaligned %.2f", 10, 0
value: dq 1.5
section .text
extern printf
extern exit
global _start
_start:
and rsp, -16
sub rsp, 8
lea rdi, [fmt]
movsd xmm0, [value]
mov eax, 1
call printf wrt ..plt
xor edi, edi
call exit wrt ..plt
section .note.GNU-stack noalloc noexec nowrite progbitsand rsp, -16 aligns the stack, and sub rsp, 8 immediately breaks it. Inside printf, the prologue saves xmm registers with movaps, which requires a 16-byte-aligned address:
$ gdb -q --nx -batch -ex 'set debuginfod enabled off' \
-ex 'set pagination off' -ex 'set disassembly-flavor intel' \
-ex run -ex 'x/i $rip' -ex 'bt 3' build/abi_misalign
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7c5b8e9 in printf () from /usr/lib/libc.so.6
=> 0x7ffff7c5b8e9 <printf+57>: movaps XMMWORD PTR [rbp-0x80],xmm0
#0 0x00007ffff7c5b8e9 in printf () from /usr/lib/libc.so.6
#1 0x0000555555555051 in _start () at abi_misalign.asm:24SIGSEGVfires insideprintf, not_start: the violation triggers on the very first aligned spill instruction.movapsrequires 16-byte alignment; a misalignedrspshifts the entire register save area off alignment.bt 3(backtrace, lesson 05) prints the top three stack frames, pointing straight back to_startline24(call printf).
Verification in the Terminal
The C99 test harness exercises asm_sum8 with several argument sets and prints the results:
/* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 */
/* sum8_test.c — C99 test of register and stack arguments for asm_sum8. */
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
int64_t asm_sum8(int64_t a, int64_t b, int64_t c, int64_t d, int64_t e, int64_t f, int64_t g,
int64_t h);
static bool check_case(const char *name, int64_t got, int64_t want)
{
char line[64] = {0};
int written = 0;
written =
snprintf(line, sizeof line, "%s: got %" PRId64 ", want %" PRId64 "\n", name, got, want);
if (written < 0 || (size_t)written >= sizeof line) {
return false;
}
fputs(line, stdout);
return got == want;
}
int main(void)
{
bool ok = true;
ok = check_case("1..8", asm_sum8(1, 2, 3, 4, 5, 6, 7, 8), 36) && ok;
ok = check_case("zeros", asm_sum8(0, 0, 0, 0, 0, 0, 0, 0), 0) && ok;
ok = check_case("negatives", asm_sum8(-1, -2, -3, -4, -5, -6, -7, -8), -36) && ok;
ok = check_case("stack-heavy", asm_sum8(0, 0, 0, 0, 0, 0, INT64_C(1) << 40, INT64_C(1) << 40),
INT64_C(1) << 41) &&
ok;
if (!ok) {
fprintf(stderr, "asm_sum8: mismatch\n");
return 1;
}
printf("asm_sum8: ok\n");
return 0;
}The build enables two hardening flags: -fPIE produces a position-independent executable, and -fstack-protector-strong inserts a stack canary into functions containing local buffers. Checking the compiled binary:
$ readelf -W -h build/sum8_test | grep Type:
Type: DYN (Position-Independent Executable file)
$ objdump -d -M intel build/sum8_test | grep -E 'fs:0x28|__stack_chk_fail'
118d: 64 48 8b 04 25 28 00 mov rax,QWORD PTR fs:0x28
1255: 64 48 2b 14 25 28 00 sub rdx,QWORD PTR fs:0x28
1260: e8 db fd ff ff call 1040 <__stack_chk_fail@plt>DYNinstead ofEXECindicates PIE: the loader picks the base address at runtime instead of hardcoding it.fs:0x28reads the stack canary from TLS (more onTLSandfs:in lesson 23): the prologue saves it on the frame, and the epilogue checks it and calls__stack_chk_failon mismatch.- The canary appears specifically in
check_casebecause it allocateschar line[64]on the stack;asm_sum8has no local buffers.
[ low addresses ] char line[64] -(overflow)-> canary fs:0x28
|
+--> saved rbp --> ret addr [ high addresses ]Final test run:
$ ./build/sum8_test
1..8: got 36, want 36
zeros: got 0, want 0
negatives: got -36, want -36
stack-heavy: got 2199023255552, want 2199023255552
asm_sum8: okCommon Pitfalls
-
"
printfcrashes inside libc."
Fact:text0x00007ffff7c5b8e9 <printf+57>: movaps XMMWORD PTR [rbp-0x80],xmm0 Program received signal SIGSEGV, Segmentation fault.Cause:
rspwasn’t 16-byte aligned beforecall;printf’s prologue executedmovapsagainst an unaligned stack slot.
Fix: ensurersp % 16 == 0before everycall, accounting for everypushandsub rsp. -
"
printfprints zeros instead of mydouble."
Fact: withxmm0 = 1.5andal = 0, output isvalue = 0.00.
Cause:va_argchecksaland skips saving vector registers if the count is zero.
Fix: setalto the number of vector registers passed beforecall. -
“The stack canary didn’t show up.”
Fact:objdump -d -M intel ... | grep -c 'fs:0x28'prints0under-fno-stack-protectorand2under-fstack-protector-strong.
Cause: without the flag, the compiler omits stack-integrity checks.
Fix: keep-fstack-protector-strongin your build flags and verify__stack_chk_failinobjdump.
Lab
Goal. Write asm_mix, accepting interleaved integers and doubles and returning the sum of all arguments as a double.
Contract and requirements.
- Prototype:
double asm_mix(int64_t i0, double d0, int64_t i1, double d1, int64_t i2, double d2, int64_t i3, double d3, int64_t i4, int64_t i5, int64_t i6); - Integer arguments fill
rdi,rsi,rdx,rcx,r8,r9, with the seventh pushed onto the stack;doubles fillxmm0–xmm3. - Return in
xmm0, converting integers todoublewithcvtsi2sd. - Compile the C99 harness with
-std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Werror -pedantic.
Verification criteria.
- Calling
asm_mix(1, 0.5, 2, 1.5, 3, 2.5, 4, 3.5, 5, 6, 7)returns36.00. - In GDB on entry to
asm_mix,rdi–r9andxmm0–xmm3hold the expected values, andx/gx $rsp+8prints7. objdump -d -M intelshows no reads from[rsp]where[rsp + 8]belongs.- Running under
-fsanitize=address,undefinedprints zero errors.
Hints, expected output, and walkthrough
Register assignment depends on argument type, not parameter position. Each int64_t claims the next free integer register, and each double claims the next xmm register; thus i4 lands in r8, i5 in r9, and i6 on the stack.
Accumulate the total in xmm0: convert integers via cvtsi2sd and add with addsd. Read the seventh integer from [rsp + 8], since [rsp] holds the return address.
Expected values:
- integer sum:
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28; - floating-point sum:
0.5 + 1.5 + 2.5 + 3.5 = 8.0; - total:
36.00; you do not need to setalwhen returning adouble, as that rule applies only when calling variadic functions.
Testbed sources, Makefile, and automated tests for this lesson: lessons/18-system-v-abi.
Summary
- The System V AMD64 ABI governs register allocation, stack arguments, alignment, and return values.
- The first six integer arguments go in
rdi,rsi,rdx,rcx,r8,r9; additional ones go on the stack. - The seventh integer argument sits at
[rsp + 8]because[rsp]holds the return address. - The caller must guarantee
rsp % 16 == 0beforecall;printfrelies on this formovaps. - For variadic calls,
alspecifies how many vector registers were passed. -fPIEproduces ELF typeDYN, while-fstack-protector-strongadds thefs:0x28stack canary and__stack_chk_fail.
gcc -O0 vs. -O2, and where inline assembly stops making sense.