Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Frame Linkage.

Similar presentations


Presentation on theme: "Stack Frame Linkage."— Presentation transcript:

1 Stack Frame Linkage

2 Memory Memory: Text Segment : Code
Data Segment : Global variables / constants Stack Segment : Temporary storage

3 Memory Up Close Variables with global duration in .data:

4 Static Storage Static : Allocated at start of program
Assembly static storage for "function":

5 Static Storage Static Pros: Simple

6 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

7 Local Variables Alternative: Stack based temporary storage as needed for functions

8 Local Variables Stack Frame / Activation Record: memory for one function call

9 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

10 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

11 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

12 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

13 What Goes In Registers we may mess up Parameters Local variables r4+
FP LR (if needed) Parameters Local variables

14 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

15 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

16 Sample Sample Function: Has two local variables Each is 4 bytes

17 Prolog Frame Setup:

18 Prolog Frame Setup: Push callers fp, your lr

19 Prolog Frame Setup: Push callers fp, your lr
Push r4+ if you will use This function doesn't

20 Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use
Set fp to start of this frame

21 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

22 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

23 Prolog Params / Locals accessed as fp + offset
Ordering set by compiler

24 Using the Frame Every variable read/write must take place on stack
Registers only temporary

25 Using the Frame Get value Do math Store result

26 Using the Frame Get value Do math Store result

27 Using the Frame Get value Do math Store result

28 Using the Frame Get value Do math Store result

29 Using the Frame Get value Do math Store result

30 Using the Frame Get value Do math Store result

31 Epilog Cleanup Put return in r0+

32 Epilog Cleanup Put return in r0+
Remove locals/params By moving stack pointer

33 Epilog Cleanup Put return in r0+ Remove locals/params
Restore old fp and our lr Wipes out our fp

34 Epilog Cleanup Put return in r0+ Remove locals/params
Restore old fp and our lr Return

35 Done to here

36 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

37 Call Stack closeEnough calls abs:

38 closeEnough closeEnough is called

39 closeEnough Builds frame, stores params

40 closeEnough Does work…

41 closeEnough Calls abs

42 abs closeEnough abs stores old fp with current lr

43 abs abs closeEnough closeEnough Now safe to update fp

44 abs abs makes space for x and loads param into it abs closeEnough

45 abs abs closeEnough closeEnough … after work is done, put x in r0

46 abs abs closeEnough closeEnough … pop locals/params

47 abs closeEnough closeEnough … popping fp retores pointer in old frame

48 Notes All variables allocated up front: Even if all can't be used!!!

49 Notes Arrays / struct / objects on stack allocated as block:

50 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

51 Recursion Recursive procedure Calls itself

52 Recursion Each call has own stack frame: fact(2) fact(3) fact(4)


Download ppt "Stack Frame Linkage."

Similar presentations


Ads by Google