
Stack
Function Call
Function call convention determines how this works, in 2106:
- Caller: pass parameters using registers or stack
- Caller: save return PC on stack
- Jump to callee (transfer control from caller to callee)
- Callee: Save current value of the registers it intends to use on stack (spill the registers)
- Callee: Save old frame pointer, stack pointer on stack
- Callee: Allocate space for local variables on stack
- Callee: Adjust FP to correct location, adjust SP to point to new stack top
Returning from Function
- Callee: Place return result in register (if applicable)
- Callee: Restore spilled registers
- Callee: Restore saved FP & SP
- Jump to return address (transfer control back to caller using saved PC)
- Caller: Utilise return result in register (if applicable)
- Caller: Continue execution
Frame Pointer
- To facilitate access of items in the stack frame
- SP can be difficult to use because it can change (when allocating more stuff etc)
- FP points to fixed location in stack frame, so we can easily access items relative to FP
- Usage of FP is platform dependent

Heap
- For dynamically allocated memory (c
malloc, cppnewetc.) - Allocated at runtime: Cannot place in data region
- Not deallocated with function: cannot place on stack
- Allocated in heap. Heap grows downward