Stack Frame Linkage
Memory Memory: Text Segment : Code Data Segment : Global variables / constants Stack Segment : Temporary storage
Memory Up Close Variables with global duration in .data:
Static Storage Static : Allocated at start of program Assembly static storage for "function":
Static Storage Static Pros: Simple
Static Storage Static Pros: Static Cons: Simple Allocated if we need or not Allocated for entire run of the program Only one copy of memory for function Non-reenterant
Local Variables Alternative: Stack based temporary storage as needed for functions
Local Variables Stack Frame / Activation Record: memory for one function call
Detail View Stack Frame Context for function Book keeping Parameters Local variables Book keeping Stored registers & Return address Start of previous frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a
Local Variables Components located relative to bottom of frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame
Local Variables Stack Pointer can move during function… not a fixed reference -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame
Local Variables Frame Pointer Fixed reference point to bottom of current stack frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame
What Goes In Registers we may mess up Parameters Local variables r4+ FP LR (if needed) Parameters Local variables
Updated Call Convention Caller Save r0-r3 if you care about them Put arguments in r0-r3, BL Prolog Save r4+ if you will use Push callers fp Push your lr Set frame pointer Move sp to make space on stack for locals/params Store params from registers to frame
Updated Call Convention Epilog Put return values in r0-r3 Reset stack pointer to remove locals Pop all saved registers Including fp and possibly lr Return (MOV PC, LR) Regain Control Get return values from r0 Pop any r0-r3 registers you saved
Sample Sample Function: Has two local variables Each is 4 bytes
Prolog Frame Setup:
Prolog Frame Setup: Push callers fp, your lr
Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use This function doesn't
Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame
Prolog Stack Frame Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame Allocate params/locals This needs 8 bytes
Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame Allocate params/locals Copy params to stack frame
Prolog Params / Locals accessed as fp + offset Ordering set by compiler
Using the Frame Every variable read/write must take place on stack Registers only temporary
Using the Frame Get value Do math Store result
Using the Frame Get value Do math Store result
Using the Frame Get value Do math Store result
Using the Frame Get value Do math Store result
Using the Frame Get value Do math Store result
Using the Frame Get value Do math Store result
Epilog Cleanup Put return in r0+
Epilog Cleanup Put return in r0+ Remove locals/params By moving stack pointer
Epilog Cleanup Put return in r0+ Remove locals/params Restore old fp and our lr Wipes out our fp
Epilog Cleanup Put return in r0+ Remove locals/params Restore old fp and our lr Return
Done to here
Function as Caller & Callee Functions call each other Lifecycle: Prolog Set up stack frame Do work Call others? Set up call Regain control Do Work Epilog Clean up stack frame Return
Call Stack closeEnough calls abs:
closeEnough closeEnough is called
closeEnough Builds frame, stores params
closeEnough Does work…
closeEnough Calls abs
abs closeEnough abs stores old fp with current lr
abs abs closeEnough closeEnough Now safe to update fp
abs abs makes space for x and loads param into it abs closeEnough
abs abs closeEnough closeEnough … after work is done, put x in r0
abs abs closeEnough closeEnough … pop locals/params
abs closeEnough closeEnough … popping fp retores pointer in old frame
Notes All variables allocated up front: Even if all can't be used!!!
Notes Arrays / struct / objects on stack allocated as block:
Real World Compilers may each build frames differently Compilers may omit steps if optimizing Skip saving link register if do not call other functions Skip saving frame pointer Skip loading/storing from stack Extra/large parameters placed on stack by caller
Recursion Recursive procedure Calls itself
Recursion Each call has own stack frame: fact(2) fact(3) fact(4)