$npx -y skills add mohitmishra786/low-level-dev-skills --skill abi-and-calling-conventionsABI and calling conventions skill for cross-language boundaries. Use when explaining System V AMD64, ARM AAPCS, RISC-V psABI, stack frames, variadic calls, or FFI register rules. Activates on queries about calling convention, ABI, System V AMD64, AAPCS, stack frame, variadic func
| 1 | # ABI and Calling Conventions |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Document application binary interface (ABI) rules: register roles, stack alignment, argument passing, return values, and variadic conventions across System V AMD64, ARM AAPCS, and RISC-V — essential for assembly, FFI, and debugging. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Writing assembly thunks or inline asm clobbers |
| 10 | - Debugging corrupted stack in mixed C/asm |
| 11 | - FFI between Rust/C/Zig (`skills/rust/rust-ffi`, `skills/zig/zig-cinterop`) |
| 12 | - Reading disassembly from `skills/debuggers/gdb` |
| 13 | |
| 14 | ## Workflow |
| 15 | |
| 16 | ### 1. System V AMD64 (Linux/macOS) |
| 17 | |
| 18 | | Item | Rule | |
| 19 | |------|------| |
| 20 | | Integer args | `rdi, rsi, rdx, rcx, r8, r9` | |
| 21 | | XMM args | `xmm0`–`xmm7` (float) | |
| 22 | | Return int/ptr | `rax` (+ `rdx` for 128-bit) | |
| 23 | | Stack alignment | 16-byte before `call` | |
| 24 | | Red zone | 128 bytes below `rsp` (Linux) | |
| 25 | | Callee-saved | `rbx, rbp, r12–r15` | |
| 26 | |
| 27 | ```c |
| 28 | /* void foo(int a, int b, int c, int d, int e, int f, int g); */ |
| 29 | /* a–f in regs, g on stack */ |
| 30 | ``` |
| 31 | |
| 32 | Windows x64 differs — see `skills/compilers/msvc-cl`. |
| 33 | |
| 34 | ### 2. ARM AAPCS (AArch32/AArch64) |
| 35 | |
| 36 | **AArch64:** |
| 37 | |
| 38 | | Item | Rule | |
| 39 | |------|------| |
| 40 | | Integer args | `x0`–`x7` | |
| 41 | | Float args | `v0`–`v7` | |
| 42 | | Return | `x0`/`x1` or `v0` | |
| 43 | | Stack align | 16-byte | |
| 44 | | Callee-saved | `x19`–`x28`, `fp` (`x29`), `lr` (`x30`) | |
| 45 | |
| 46 | Thumb interworking: LSB of function pointer set for Thumb code. |
| 47 | |
| 48 | See `skills/low-level-programming/assembly-arm`. |
| 49 | |
| 50 | ### 3. RISC-V psABI (RV64) |
| 51 | |
| 52 | | Item | Rule | |
| 53 | |------|------| |
| 54 | | Integer args | `a0`–`a7` | |
| 55 | | Callee-saved | `s0`–`s11`, `sp` | |
| 56 | | Return | `a0`, `a1` | |
| 57 | | Stack align | 16-byte | |
| 58 | |
| 59 | See `skills/low-level-programming/assembly-riscv`. |
| 60 | |
| 61 | ### 4. Stack frame layout (conceptual) |
| 62 | |
| 63 | ``` |
| 64 | high addresses |
| 65 | ├── return address |
| 66 | ├── saved frame pointer |
| 67 | ├── local variables |
| 68 | ├── spill slots / alignment padding |
| 69 | └── outgoing args (if any) |
| 70 | low addresses (rsp) |
| 71 | ``` |
| 72 | |
| 73 | ### 5. Variadic functions |
| 74 | |
| 75 | System V: `al` holds number of vector args used; `register_save_area` on stack for `va_start`. Prefer typed wrappers over raw `va_arg` in portable FFI. |
| 76 | |
| 77 | ### 6. Verify in compiler output |
| 78 | |
| 79 | ```bash |
| 80 | gcc -O2 -S -o - foo.c # study .cfi_* and mov args |
| 81 | objdump -d -M intel ./a.out |
| 82 | ``` |
| 83 | |
| 84 | ### 7. Agent usage |
| 85 | |
| 86 | ``` |
| 87 | /abi-and-calling-conventions Which registers hold args 5–8 in AArch64 for my C function? |
| 88 | ``` |
| 89 | |
| 90 | ## Common Problems |
| 91 | |
| 92 | | Symptom | Cause | Fix | |
| 93 | |---------|-------|-----| |
| 94 | | Crash on `call` | Stack misaligned | Align `rsp` mod 16 | |
| 95 | | Wrong float arg | XMM vs GPR mismatch | Match prototype | |
| 96 | | Corrupt callee-saved | Asm clobber missing | List in clobber or save | |
| 97 | | FFI garbage | Windows vs SysV mix | Match toolchain ABI | |
| 98 | | Variadic UB | Wrong type to `va_arg` | Cast to promoted type | |
| 99 | |
| 100 | ## Related Skills |
| 101 | |
| 102 | - `skills/low-level-programming/assembly-x86` — x86-64 asm |
| 103 | - `skills/low-level-programming/assembly-arm` — AAPCS examples |
| 104 | - `skills/low-level-programming/assembly-riscv` — RISC-V asm |
| 105 | - `skills/rust/rust-ffi` — `extern "C"` guarantees |
| 106 | - `skills/binaries/elf-inspection` — symbol and relocation views |