What is Calling convention?
The agreement on how arguments and return values are passed in registers or on the stack, and who saves which registers, when calling a function. It is a key part of the ABI.
Japanese name: 呼び出し規約
The agreement on how arguments and return values are passed in registers or on the stack, and who saves which registers, when calling a function. It is a key part of the ABI.
Japanese name: 呼び出し規約
A calling convention fixes the low-level details of a function call: which registers carry the arguments, where the return value is placed, how the return address is saved, which registers a called function may overwrite, and how the stack frame is laid out and aligned. The caller and the callee are often compiled separately, perhaps by different compilers or hand-written in assembly, so neither can see how the other was built. As long as both obey the same convention, a C function can call a library routine or an assembly function and the values arrive where expected.
RISC-V has 32 integer registers, x0 to x31, and the convention gives them role names. Up to eight integer arguments travel in a0 to a7 (x10 to x17); a ninth or tenth argument goes on the stack at a fixed offset from sp. The result returns in a0, or in a0 and a1 for a 64-bit value on RV32. In ABIs that pass arguments in floating-point registers, floating-point arguments use fa0 to fa7. A call is jal ra, func, which writes the return address into ra (x1). The stack pointer must be a multiple of 16 bytes on entry, so frames are allocated in 16-byte steps.
Registers are split into two groups. Caller-saved temporaries, t0 to t6 in RISC-V along with the argument registers, may be overwritten freely by any callee, so a caller that still needs such a value after the call must save it first. Callee-saved registers, s0 to s11, must look unchanged when the call returns, so a function that wants to use one saves it in its prologue and restores it in its epilogue. The split lets short leaf functions run on temporaries with no saving at all, while long-lived values such as loop counters live safely in s registers.
The calling convention is one part of the application binary interface (ABI), which also fixes the sizes and alignment of data types, struct layout and how system calls are made. An API, by contrast, describes how functions are used at the source level. Two builds can share an API yet be incompatible at the binary level because their ABIs differ, so a library must then be recompiled. Different platforms define different conventions for the same ISA family: on x86-64, Linux passes the first six integer arguments in registers, while Windows uses four.
Answer: B. So that separately compiled functions and libraries can pass arguments and return values correctly
A calling convention specifies which registers hold arguments, where return values go, which registers the callee may clobber and how the stack is used. Because of this agreement, libraries built by different compilers, or assembly functions combined with C functions, work together correctly. The calling convention is a key part of the ABI (application binary interface).
Answer: B. Arguments in a0-a7; return value in a0 (and a1)
In the standard RISC-V calling convention, up to eight integer arguments are passed in a0-a7 (x10-x17), and the return value comes back in a0, or in a0 and a1 for values such as 64-bit integers on RV32. Passing through registers, which are faster than memory, keeps call overhead low. Arguments beyond eight go on the stack. In ABIs that pass arguments in floating-point registers, floating-point arguments use fa0-fa7.
It is the agreement on how a function call passes arguments and return values, saves the return address and divides register-saving duties between caller and callee. It lets separately compiled code, including libraries and assembly routines, call each other correctly.
A caller-saved register may be overwritten by the called function, so the caller must save it if it needs the value afterward. A callee-saved register must be preserved by the called function, which saves and restores it if it uses it.
The first eight integer arguments go in registers a0 to a7 and any further arguments go on the stack. The return value comes back in a0, or a0 and a1 for a value twice the register width.
Calling convention appears in 9 practice questions across 1 topic, including ISA & Assembly.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.