Stack & Memory Management in Processors
Understand the stack operation in processors, processes, hardware, and software interfacing, including stack frames and memory management. Learn about stack discipline, ABI standards, and frame pointers.
Stack & Memory Management in Processors
E N D
Presentation Transcript
Lecture 5: The Stack EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM)
COURSE PLAN FOR TODAY Midterm I The Stack and Memory
Midterm I • Thursday, February 13th! • Two weeks away • Covers the following Material: • Chapters 1 – 3 • Laboratory 1
A Call Chain Image by David Thomas
Procedure Calling • Place parameters for procedure in registers • Transfer control to procedure • Procedure acquires storage • Procedure performs function. • Procedure places return value in appropriate register • Return control
The Stack • Region of memory managed with stack discipline. • Accessed with pushand pop
Stack Frames • Stack frames “belong” to a procedure. • Store local variables here (they go out of scope automatically) • Can communicate with other procedures with a stack frame.
Procedures and Register Use • What if we want to use some registers in the procedure? • Caller could have data in it!
Stack Discipline and ABI • Stack Discipline is important • Define an Application Binary Interface • How should procedures communicate? How should the be called? • Consistency, following standards, is the key.
Conventions and Discipline Caller Callee Callee saves temporary values in its frame before using. Callee restores values in its frame after using. • Caller saves temporary values in its frame before the call. • Caller restores values in its frame after the call.
Stack Frames • Contents • Local variables • Return information • Temporary space • Management • Space is allocated when entering • Set-up code • Deallocated when returning • Finish code Frame Pointer Stack Pointer
Example frame ptr foo() { … bar() … } foo stack ptr foo()
Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr foo() bar()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr foo() bar() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr foo() bar() baz() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr foo() bar() baz() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr baz() foo() bar() baz() baz() baz()
Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr baz() foo() bar() baz() baz() baz()
Example frame ptr foo() { … bar() … } foo stack ptr baz() foo() bar() baz() baz() baz()
What is the frame pointer used for? • Given a caller and a callee, the callee will begin with: • fp = sp • A callee will use the stack to: • Save the current sp, lr, and fp before calling a function • Store the arguments for, and return value space before calling a function • Save used registers before calling a function • Store variables local to the procedure
foo Frame pointer stack ptr frame ptr
foo Frame pointer frame ptr a = 0x00 stack ptr PUSH
foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH stack ptr
foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH c = 0x87 stack ptr
foo Frame pointer frame ptr a = 0x00 b = 0xFC PREPARETO CALL c = 0x87 stack ptr
foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH c = 0x87 r0 lr fp a0 ret stack ptr
foo Frame pointer a = 0x00 b = 0xFC RETURNFROMCALL c = 0x87 r0 lr fp a0 ret stack ptr
foo Frame pointer frame ptr a = 0x00 b = 0xFC POP! c = 0x87 stack ptr
5 minute paper • Take 5 minutes to think about these tasks. Determine why a frame pointer is helpful
foo Frame pointer frame ptr a = 0x00 +4 b = 0xFC +8 c = 0x87 +12 stack ptr
foo Frame pointer frame ptr a = 0x00 • The frame pointer gives usa base address from which tooffset in our frame for localstorage that will automatically go out of scope. +4 b = 0xFC +8 c = 0x87 +12 stack ptr
Recursion foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()
Recursion • Stack frames give each function private storage • Saved registers • Local variables • Return portions • Recursion is handled without special consideration. • Following stack discipline and call/return pattern is critical
PUSH and POP • PUSH<cond> {reglist} • POP<cond> {reglist} • Really pseudo-instructions! • PUSH – STMDB • Store multiple registers, decrement address before access. • POP – LDMIA • Store multiple registers, increment address after access.
foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC stack ptr r2 = 0x00 sp = ??
foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 stack ptr sp = ??-4
foo STMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4
foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4
foo LDMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4
foo LDMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC stack ptr r2 = 0x00 r3=0x87 sp = ??
Multiple Ways to Handle the Stack • Stack Pointer can • Point to the lass occupied stack (Full stack) • (needs pre-decumenting) • Point to the next address to be occupied (Empty stack) • (needs post-decrementing) • Stack type can be given as a postfix to the instruction: • STMFD/LDMFD • STMFA/LDMFA • STMED/LDMED • STMEA/LDMEA
RISC Mentality • There are no special functions for stacks! • Let’s look at stack instructions: • STMDA/LDMDA