</>EmbeddedInterview

What is a stack frame in ARM64?

MediumARM Architecture

Short answer: A function may save its frame pointer X29 and link register X30 on the stack. Leaf functions may not require a frame.

SP beforeCallerStack frameSaved X29 / X30SP afterRestore
STP X29, X30, [SP, #-16]!
MOV X29, SP
// function body
LDP X29, X30, [SP], #16
RET

1. Entry: STP with pre-index addressing subtracts 16 from SP and stores X29 and X30. MOV sets the frame pointer to the new SP.

2. Body: A function may reserve more aligned stack space for local variables, saved registers or outgoing arguments.

3. Exit: LDP restores the saved registers and increments SP by 16. RET branches to the saved return address.

Before frame After allocation
SP points to caller stack top SP points to saved X29
LR contains return address Saved X30 is at SP + 8

4. Optimization: Leaf functions may omit stack frames and some platforms permit frame-pointer omission.

Interview takeaway: The stack frame preserves call state and local storage; the stack grows toward lower addresses in the usual AArch64 ABI.

Numerical stack-frame trace

Assume SP starts at 0x8000 and the function executes the following prologue:

STP X29, X30, [SP, #-16]!
MOV X29, SP
SUB SP, SP, #32

SP first becomes 0x7FF0. Saved X29 is at 0x7FF0 and saved X30 at 0x7FF8. X29 points to the frame record. After reserving 32 bytes, SP becomes 0x7FD0, leaving aligned local storage below the saved registers.

Higher addresses
0x8000  caller's original SP
0x7FF8  saved X30 (return address)
0x7FF0  saved X29  <- frame pointer
        local storage (32 bytes)
0x7FD0  current SP
Lower addresses

Before returning, reverse the local allocation and restore the saved pair with LDP. The compiler may instead omit a frame pointer, combine allocations, or use different spill offsets; the ABI and unwind metadata govern the valid layout. A stack frame is not necessarily the same size as a function's C local variables, since alignment, register spills and outgoing arguments also matter.