ABI Calling Convention

RISC-V ABI Calling Convention Online Reference

This is the RISC-V ABI calling convention online reference page. Quickly look up how arguments are passed via a0-a7, return values via a0/a1, special register roles of ra/sp/s0, caller-saved vs callee-saved responsibilities, and stack frame with 16-byte alignment rules. Ideal for reading disassembly, writing RISC-V assembly, debugging function calls, and understanding compiler-generated code.

In One Sentence
Arguments in a0-a7, results in a0; caller saves a*/t*/ra, callee restores s*/sp; the stack frame must close before return.
Register Role Assignment

Argument Registers

a0 – a7 (x10 – x17)

In the standard integer calling convention, the first 8 integer or pointer arguments go into a0-a7; extra arguments are passed on the stack. a0-a7 are not preserved across calls.

Return Value Registers

a0, a1 (x10, x11)

Integer scalar return values use a0, and a0/a1 may be used for two-register returns. ret only jumps through the return address; it does not create a return value.

Caller-Saved Registers

t0 – t6 (x5 – x7, x28 – x31), ra (x1), a0 – a7 (x10 – x17)

If the caller still needs these values after a call, it must save them before the call. The callee may freely overwrite them.

Callee-Saved Registers

s0 – s11 (x8 – x9, x18 – x27), sp (x2)

The caller can trust s* to stay unchanged across calls. If the callee modifies them, it must save on entry and restore before return. sp may move down to allocate a stack frame but must be restored on exit.

Special-Purpose Registers

zero (x0), ra (x1), sp (x2), gp (x3), tp (x4), fp/s0 (x8)

zero is hardwired to 0 and writes are ignored. ra holds the return address. sp is aligned to a 128-bit (16-byte) boundary in the standard ABI. gp and tp are unallocatable registers usually maintained by the platform, runtime, or TLS mechanism. fp/s0 may be used as a frame pointer.

Calling Convention Details

Argument Passing

The integer calling convention uses a0-a7 for the first 8 integer or pointer arguments. With a hardware floating-point ABI, floating-point arguments may use fa0-fa7; otherwise they follow the integer convention or the stack.

Return Values

Scalar results usually go in a0; two-register results use a0/a1. Aggregates that cannot be returned in registers use a caller-provided return object address; psABI flattening and return rules define the exact cases.

Preservation

Caller-saved (a*, t*, ra): the caller must save if needed later. Callee-saved (s*, sp): the callee must restore before return. Prefer s* or the stack for values that must survive calls.

Stack Frame

The stack grows downward. sp moves down on entry to allocate a frame and must be restored on exit. The standard ABI requires sp to be aligned to a 128-bit (16-byte) boundary on procedure entry and remain aligned throughout execution.

Additional Notes

System Calls

ecall is the ISA environment-call instruction; syscall numbers, argument registers, and error returns are execution-environment or OS ABI details. A common Linux RISC-V user-space ABI uses a7 for the syscall number, a0-a5 for arguments, and a0 for the result.

16-Byte Stack Alignment

The psABI specifies standard stack alignment as a 128-bit boundary, i.e. 16 bytes. Handwritten assembly that calls other functions must keep sp aligned after allocating local stack space.

Frame Pointer (Optional)

s0/fp is not required for every function. Compilers generate frame pointers when needed for debugging or dynamic stack allocations. Handwritten assembly may skip fp as long as sp is correctly maintained.

Official References

This page is organized with reference to the official RISC-V documents below for architecture, ABI, CSR, and pseudo-instruction notes; platform or OS ABI differences still need to be checked against their own specifications.