Stack

Function Call

Function call convention determines how this works, in 2106:

  1. Caller: pass parameters using registers or stack
  2. Caller: save return PC on stack
  3. Jump to callee (transfer control from caller to callee)
  4. Callee: Save current value of the registers it intends to use on stack (spill the registers)
  5. Callee: Save old frame pointer, stack pointer on stack
  6. Callee: Allocate space for local variables on stack
  7. Callee: Adjust FP to correct location, adjust SP to point to new stack top

Returning from Function

  1. Callee: Place return result in register (if applicable)
  2. Callee: Restore spilled registers
  3. Callee: Restore saved FP & SP
  4. Jump to return address (transfer control back to caller using saved PC)
  5. Caller: Utilise return result in register (if applicable)
  6. 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, cpp new etc.)
  • Allocated at runtime: Cannot place in data region
  • Not deallocated with function: cannot place on stack
  • Allocated in heap. Heap grows downward