When a method is called, the arguments are typically pushed onto the call stack (or passed in registers, depending on the calling convention) before control transfers to the method. The exact mechanism—stack, registers, or a mix—is defined by the platform's calling convention and the language's runtime. The method then accesses these arguments via fixed offsets from a frame pointer or stack pointer, or directly from registers.
Background: The Call Stack and Calling Conventions
To understand argument passing, you need to know about the call stack. The call stack is a region of memory that grows and shrinks as functions are called and return. Each function invocation gets a stack frame (also called an activation record) that holds its local variables, saved registers, return address, and often the arguments passed to it. The order and location of argument pushing are dictated by the calling convention—a low-level agreement between caller and callee about how arguments are passed, who cleans up the stack, and which registers are preserved.
Different architectures and operating systems use different calling conventions. For example, on x86-64, the System V AMD64 ABI (used by Linux and macOS) passes the first six integer or pointer arguments in registers RDI, RSI, RDX, RCX, R8, and R9, and the first eight floating-point arguments in XMM0–XMM7. Additional arguments are pushed onto the stack in reverse order (right-to-left). On Windows x64, the first four arguments go in RCX, RDX, R8, and R9, with stack space reserved for them (the shadow space), and additional arguments are pushed right-to-left. In contrast, 32-bit x86 conventions like cdecl push all arguments onto the stack right-to-left, while stdcall also pushes right-to-left but requires the callee to clean up the stack.
How Arguments Are Pushed: Step by Step
The exact sequence depends on the calling convention, but a typical stack-based push follows these steps:
- The caller evaluates the arguments and places them in the appropriate locations (registers or stack). For stack arguments, it pushes them in reverse order so that the first argument ends up at the lowest address (closest to the return address).
- The caller executes a call instruction, which pushes the return address onto the stack and jumps to the method's entry point.
- The callee (the method) sets up its stack frame, often by pushing the old frame pointer (e.g., EBP) and then moving the stack pointer to the frame pointer. This creates a stable reference point for accessing arguments and locals.
- Arguments are accessed via positive offsets from the frame pointer (for stack arguments) or from saved registers.
For example, in cdecl on x86, a call like foo(a, b, c) might compile to:
push c
push b
push a
call foo
add esp, 12 ; caller cleans up stackInside foo, after setting up the frame, a is at [ebp+8], b at [ebp+12], and c at [ebp+16] (assuming a 32-bit system with 4-byte arguments). The return address is at [ebp+4], and the saved ebp is at [ebp].
Register-Based Argument Passing
Modern 64-bit conventions favor registers for the first few arguments to improve performance. On x86-64 System V, integer arguments go in RDI, RSI, RDX, RCX, R8, R9; floating-point arguments in XMM0–XMM7. If a function has more arguments than registers available, the extras are pushed onto the stack. The caller must also ensure the stack is 16-byte aligned before the call instruction. The callee can access register arguments directly, but if it needs to use those registers for other purposes, it must save them (often to the stack) first.
Other architectures have their own conventions. ARM 32-bit uses R0–R3 for the first four arguments, with additional arguments on the stack. ARM64 uses X0–X7 for integer arguments and V0–V7 for floating-point. RISC-V uses a0–a7 for arguments. In all cases, the calling convention specifies the details.
Stack Layout and Frame Pointers
The stack grows downward on most architectures (toward lower addresses). When arguments are pushed, the stack pointer decreases. The layout of a typical stack frame (for a cdecl function) looks like this, from higher to lower addresses:
- Arguments (pushed by caller, in reverse order)
- Return address (pushed by call instruction)
- Saved frame pointer (pushed by callee)
- Local variables (allocated by callee)
- Saved registers (if any)
The frame pointer (EBP/RBP) points to the saved frame pointer, so arguments are at positive offsets (e.g., [ebp+8] for the first argument). Some compilers omit the frame pointer for optimization, using the stack pointer directly, which complicates debugging but saves a register.
Common Problems and Misconceptions
A common misconception is that arguments are always pushed onto the stack. In reality, many are passed in registers, and the stack is used only for overflow or when the convention requires it. Another issue is stack alignment: some conventions (like System V AMD64) require the stack to be 16-byte aligned before a call, and misalignment can cause crashes, especially with SSE instructions. Also, the order of argument evaluation is unspecified in many languages (e.g., C and C++), so side effects in argument expressions can lead to unexpected results. Finally, buffer overflows can occur if a function writes beyond the space allocated for arguments or locals, corrupting the return address.
How to Choose or Work with Calling Conventions
In most high-level languages, you don't choose the calling convention; the compiler and platform dictate it. However, when writing assembly, interfacing with foreign functions, or developing compilers, you must follow the ABI. Factors to consider include:
- Target platform and architecture (x86, ARM, etc.)
- Operating system ABI (System V, Windows, etc.)
- Language requirements (e.g., C++ name mangling, variadic functions)
- Performance trade-offs (register vs. stack passing)
For variadic functions like printf, special conventions apply: on x86-64 System V, the number of vector registers used must be in AL, and arguments are passed in registers and stack as usual. On Windows x64, variadic arguments are passed on the stack after the first four.
Frequently Asked Questions
Are arguments always pushed onto the stack?
No. Many calling conventions pass the first several arguments in registers to improve speed. Stack pushing is used for additional arguments or when the convention requires it.
In what order are arguments pushed?
For stack-based passing, arguments are typically pushed right-to-left (reverse order) so that the first argument is at the lowest address. This is common in cdecl and stdcall.
Who cleans up the stack after a call?
It depends on the calling convention. In cdecl, the caller cleans up; in stdcall, the callee cleans up. In register-based conventions, the stack is usually cleaned by the caller if needed.
What is a shadow space?
On Windows x64, the caller must allocate 32 bytes of shadow space on the stack before calling a function, even if the function uses fewer than four register arguments. This space can be used by the callee to spill register arguments.
Can arguments be passed on the heap?
Generally, no. Arguments are passed on the stack or in registers. However, arguments that are pointers or references can point to heap-allocated data. The argument itself is still passed by value (the pointer).
How does the method access arguments?
Via fixed offsets from the frame pointer or stack pointer, or directly from registers. The compiler generates code to load them as needed.