Procedure Calls and Calling Conventions

When C calls an assembly function, both sides need the same rules for exchanging data and control. A calling convention specifies where arguments and results go, which registers survive a call, and how the stack is managed. These rules let separately compiled functions cooperate even when their internal implementations differ.

The interface contract

The concrete target here is x86-64 Linux using the System V AMD64 ABI. Consider ordinary functions whose arguments and result are 64-bit C long values. Floating-point arguments, structures, and variable argument lists require additional rules beyond this scope. C itself does not prescribe these register assignments.


The first six long arguments arrive in RDI, RSI, RDX, RCX, R8, and R9, in that order. A long result returns in RAX. Additional long arguments occupy 8-byte stack slots: at function entry, the seventh is 8 bytes above RSP, immediately above the return address.


Both sides also need compatible C declarations. Correct register placement cannot repair a disagreement about an argument's type or width. The assembly entry point must be exposed under the symbol name expected by the C caller.


Example: The body of long add2(long a, long b) can be:


asm
mov rax, rdi
add rax, rsi
ret


This uses Intel syntax. It is a leaf function: it needs no saved registers or local stack allocation.

Registers and preservation

A function is a callee relative to the function that invoked it, and a caller relative to any helper it invokes. Register responsibilities therefore change with your viewpoint.


Register group

Examples

Responsibility

Caller-saved

RAX, RDI, RSI, RDX, RCX, R8–R11

The caller protects values it still needs after a call.

Callee-saved

RBX, RBP, R12–R15

A function that changes one restores its incoming value before returning.

Stack pointer

RSP

Stack changes are undone before executing the return.


A caller-saved register may change during a call, even if the helper currently happens to leave it untouched. Save only values that are still needed. You can put them in allocated stack storage or in a callee-saved register whose incoming value you preserve.



The runtime stack

A stack frame is the stack storage associated with one active function invocation. It can hold saved registers, local data, and arguments for another call. Its exact layout depends on what that invocation needs.


A normal 64-bit call subtracts 8 bytes from RSP, stores the return address there, and transfers control. A 64-bit register push also subtracts 8 bytes. Stack allocation therefore moves toward lower addresses.


For the ordinary integer calls considered here, RSP must be a multiple of 16 immediately before call. Consequently:


RSPbefore call≡0(mod16),RSPcallee entry≡8(mod16),RSPafter allocation=RSPentry−8s−L,\begin{aligned} RSP_{\text{before call}} &\equiv 0 \pmod{16},\\ RSP_{\text{callee entry}} &\equiv 8 \pmod{16},\\ RSP_{\text{after allocation}} &= RSP_{\text{entry}}-8s-L, \end{aligned}


where ss is the number of 8-byte register pushes and LL is the additional allocation in bytes. Count saved registers, local storage, and any padding when checking alignment.


A possible stack frame contains a return address, a saved register, local storage, and alignment padding.


Before ret, release local storage and restore saved registers in reverse order, bringing RSP back to its entry value. Then ret removes the 8-byte return address and resumes the caller. A dedicated frame pointer, often RBP, is optional.



What to remember

  • Match argument locations, result location, and C types.
  • Protect needed caller-saved values across calls.
  • Restore any callee-saved registers you change.
  • Check alignment before each call and balance the stack before returning.

Example: Preserving an offset across a C helper call

A sensor-processing program calls long calibrated(long x, long offset) on x86-64 Linux using the System V AMD64 convention. The wrapper calls long helper(long x), which returns 3x+23x+2, then adds offset. All values fit in a 64-bit signed long.


For this invocation, x is 11, offset is 7, and RSP is decimal byte address 32760 at wrapper entry. The helper follows the convention and makes no further calls. Its internal work is otherwise unspecified.


A live value is a value that will be needed later. Here, offset remains live across the helper call. The wrapper uses this function body in Intel syntax:


asm
push rbx
mov rbx, rsi
call helper
add rax, rbx
pop rbx
ret


a) Explain why keeping offset only in RSI would be unsafe, and why the shown use of RBX is valid.


At wrapper entry, RDI contains ________ and RSI contains ________. The helper's first argument therefore already occupies the required register, RDI.


RSI is caller-saved, so the helper may change it. The wrapper needs the offset after the call and cannot rely on RSI retaining that value.


First, push rbx saves the incoming RBX value belonging to the wrapper's caller. Then mov rbx, rsi copies the offset into RBX. Because RBX is callee-saved, the helper must preserve this copy across its call.


After using the offset, the wrapper executes pop rbx to restore its own caller's incoming value. Thus the wrapper preserves its caller's RBX while using the helper's preservation guarantee.


Answer: RSI alone is unsafe across the call; the saved-and-restored RBX provides valid storage for the live offset.


b) Find RSP at helper entry and verify the wrapper's stack restoration through its final return.


The wrapper enters at byte address 32760. Its register push consumes 8 bytes, so RSP after push rbx is ________.


RSPbefore helper call=32760−8=32752.RSP_{\text{before helper call}}=32760-8=32752.


This address is correctly aligned because 32752/16=204732752/16=2047, an integer. The helper call then pushes its own 8-byte return address:


RSPhelper entry=32752−8=32744.RSP_{\text{helper entry}}=32752-8=32744.


Three eight-byte stack slots hold the outer return address, saved RBX, and helper return address.


The helper returns with its stack balanced. Its ret removes its return address, bringing RSP back to 32752. The wrapper's pop rbx removes the saved register value and brings RSP back to its wrapper-entry value, 32760.


Finally, the wrapper's ret removes the outer return address:


RSPafter wrapper return=32760+8=32768.RSP_{\text{after wrapper return}}=32760+8=32768.


That is the original caller's stack pointer immediately before it called the wrapper. No extra alignment allocation is necessary because the single register push already aligned the helper call.


Answer: RSP is 32744 at helper entry and 32768 after the wrapper returns, measured as decimal byte addresses.


c) Calculate the long value returned by the wrapper and identify the return register.


The helper receives x equal to 11 in RDI. Its calculation produces ________:


helper(11)=3(11)+2=33+2=35.\text{helper}(11)=3(11)+2=33+2=35.


The helper places that value in RAX. RBX still holds the offset, 7, because the helper preserves RBX. The wrapper's addition therefore gives:


RAX=35+7=42.RAX=35+7=42.


Restoring RBX with pop rbx does not change RAX, and the final ret transfers control back to the C caller. These values are dimensionless integer sensor counts, so no physical-unit conversion is required.


Answer: The wrapper returns 42 in RAX.

An assembly function called from C on x86-64 Linux follows the System V AMD64 convention. It returns the correct long result in RAX and balances its stack, but uses RBX as a temporary and leaves it changed. Which revision fixes this remaining convention violation?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A C program declares extern long checksum(long x); and calls checksum(37). A separate GNU assembly source defines the function and uses .globl checksum. The two sources are built separately and then combined into an executable.


Match each term to its role in connecting the C caller with the assembly implementation.

Match each term
A.

A declaration specifying the return type and parameter types so the C compiler can check a call.

B.

A name made available for references from other compiled modules.

C.

A binary module containing machine code and metadata needed to combine it with other modules.

D.

A build tool that combines compiled modules and resolves references to named definitions across them.

Linker

Relocatable object file

Global symbol

Function prototype

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An x86-64 Linux function using the System V AMD64 convention enters with RSP congruent to 8 modulo 16. It pushes two 64-bit callee-saved registers, then executes sub rsp, L before calling a helper. It needs at least 48 bytes of local storage within this allocation. There are no stack-passed helper arguments or other stack changes. What is the smallest possible value of L, in bytes? Answer as an integer, to zero decimal places.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice