Quiz Entry - updated: 2026.12.01
What is each of the ESP32's registers a0 to a15 and pc used for when one function calls another?
a0 holds the return address, a1 is the stack pointer, a2 to a7 carry the arguments (and a2 to a5 the results), a8 to a15 are free for the function's own work, and pc points at the next instruction.
The roles are a convention, the calling convention or ABI, that compiled code agrees on so that functions built separately can call each other.
| Register | Role |
|---|---|
a0 |
Return address: where execution continues when this function returns. |
a1 (sp) |
Stack pointer: the bottom of this function's area of the stack. |
a2 – a5 |
The 1st to 4th argument on entry, and the 1st to 4th word of the result on return. |
a6 – a7 |
The 5th and 6th argument. |
a7 (fp) |
Optionally the frame pointer, if the function needs one (for example when it allocates a variable-size block on the stack). |
a8 – a15 |
General purpose: local variables, temporaries, and arguments being prepared for a function this one calls. |
pc |
Program counter: the address of the instruction being executed. Not one of the 16. |
a0 and a1 must always hold exactly those values, because debuggers and exception handlers read them to unwind the stack and print a backtrace.
Notice that a2 is both the first argument and the result. A function like int square(int x) finds x in a2 and leaves x*x in a2.
Go deeper:
Xtensa ISA Reference Manual, §8.1 "The Windowed Register and CALL0 ABIs" (PDF) — the register-usage table and stack layout.
Wikipedia — Calling convention — the general idea, with x86 and ARM for comparison.