680 likes | 831 Views
Procedures/Stacks. Stacks Procedures Parameter passing. What is the stack for?. Why a stack?. Memory Layout. 0. Instructions. Literals. literals (e.g., “example”). Static Data. static variables (including global variables (C)). Dynamic Data (Heap). new ' ed variables. Stack.
E N D
Procedures/Stacks • Stacks • Procedures • Parameter passing
What is the stack for? • Why a stack?
Memory Layout 0 Instructions Literals literals (e.g., “example”) Static Data static variables (including global variables (C)) Dynamic Data(Heap) new'ed variables Stack local variables 2N-1
Memory Layout Instructions Execution lifetime; immutable Read-only; executable Literals Execution lifetime; immutable Read-only; not executable Static Data Execution lifetime; mutable writable; not executable Dynamic Data(Heap) Programmer controlled lifetime; mutable writable; not executable Stack “Automatic” lifetime; mutable writable; not executable
IA32 Stack • Region of memory managedwith a stack discipline • Grows toward lower addresses • Customarily shown “upside-down” • Register %espcontains lowest stack address= address of “top” element Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: %esp Stack “Top”
IA32 Stack: Push Stack “Bottom” • pushlSrc Increasing Addresses Stack Grows Down -4 Stack Pointer: %esp Stack “Top”
IA32 Stack: Push Stack “Bottom” • pushlSrc • Fetch operand at Src • Decrement %espby 4 • Write operand at address given by %esp Increasing Addresses Stack Grows Down -4 Stack Pointer: %esp Stack “Top”
IA32 Stack: Pop Stack “Bottom” • poplDest Increasing Addresses Stack Grows Down +4 Stack Pointer: %esp Stack “Top”
IA32 Stack: Pop Stack “Bottom” • poplDest • Read operand at address %esp • Increment %espby 4 • Write operand to Dest Increasing Addresses Stack Grows Down +4 Stack Pointer: %esp Stack “Top”
Procedure Control Flow • Use stack to support procedure call and return • Procedure call: call label • Push return address on stack • Jump to label
Procedure Control Flow • Use stack to support procedure call and return • Procedure call: call label • Push return address on stack • Jump to label • Return address: • Address of instruction beyond call • Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax • Return address = 0x8048553 • Procedure return: ret • Pop address from stack • Jump to address
Procedure Call Example • 804854e: e8 3d 06 00 00 call 8048b90 <main> • 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 %esp 0x108 %esp 0x108 %eip 0x804854e %eip 0x804854e %eip: program counter
Procedure Call Example • 804854e: e8 3d 06 00 00 call 8048b90 <main> • 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e %eip: program counter
Procedure Call Example • 804854e: e8 3d 06 00 00 call 8048b90 <main> • 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048553 %eip: program counter
Procedure Call Example • 804854e: e8 3d 06 00 00 call 8048b90 <main> • 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x8048553 + 0x000063d 0x8048b90 %eip: program counter
Procedure Return Example • 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 %eip 0x8048591 %eip 0x8048591 %eip: program counter
Procedure Return Example • 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter
Procedure Return Example • 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter
Stack-Based Languages • Languages that support recursion • e.g., C, Pascal, Java • Code must bere-entrant • Multiple simultaneous instantiations of single procedure • What would happen if code could not be reentrant? • Need some place to store state of each instantiation • Arguments • Local variables • Return pointer • Stack discipline • State for a given procedure needed for a limited time • Starting from when it is called to when it returns • Callee always returns before caller does • Stack allocated inframes • State for a single procedure instantiation
Call Chain Example Example Call Chain • yoo(…) • { • • • • • who(); • • • • • } yoo • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } who amI amI • amI(…) • { • • • • • amI(); • • • • • } amI amI • Procedure amIis recursive(calls itself)
Stack Frames Previous Frame • Contents • Local variables • Return information • Temporary space • Management • Space allocated when procedure is entered • “Set-up” code • Space deallocated upon return • “Finish” code Frame Pointer: %ebp Frame for proc Stack Pointer: %esp Stack “Top”
Stack Example • yoo(…) • { • • • • • who(); • • • • • } yoo %ebp yoo who %esp amI amI amI amI
Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI
Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI
Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI %ebp amI %esp
Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI amI %ebp amI %esp
Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI %ebp amI %esp
Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI
Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI
Stack Example • amI(…) • { • • • • • • • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI
Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI
Stack Example • yoo(…) • { • • • • • who(); • • • • • } yoo %ebp yoo who %esp amI amI amI amI
IA32/Linux Stack Frame • Current Stack Frame (“Top” to Bottom) • Old frame pointer • Local variablesIf can’t be just kept in registers • Saved register context When reusing registers • “Argument build area”Parameters for functionabout to be called • Caller Stack Frame • Return addressPushed by call instruction • Arguments for this call Caller Frame Arguments Frame pointer%ebp Return Addr Old %ebp Saved Registers + Local Variables Argument Build Stack pointer %esp
Revisiting swap int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
Revisiting swap Calling swap from call_swap int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } call_swap: • • • pushl $zip2 # Global Var pushl $zip1 # Global Var call swap • • • void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
Revisiting swap Calling swap from call_swap int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } call_swap: • • • pushl $zip2 # Global Var pushl $zip1 # Global Var call swap • • • • • • Resulting Stack void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtnadr %esp
Revisiting swap swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Finish
swap Setup #1 Entering Stack Resulting Stack? %ebp • • • &zip2 &zip1 Rtnadr %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
swap Setup #1 Entering Stack Resulting Stack %ebp %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
swap Setup #1 Entering Stack %ebp %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
swap Setup #1 Entering Stack Resulting Stack %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
swap Setup #1 Entering Stack %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx
swap Setup #1 Entering Stack Resulting Stack %ebp • • • • • • Offset relative to new %ebp &zip2 12 yp &zip1 8 xp Rtnadr 4 %esp Rtnadr Old %ebp %ebp Old %ebx %esp movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp . . .
swapFinish #1 swap’s Stack Resulting Stack • • • yp xp Rtnadr Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret
swapFinish #1 swap’s Stack Resulting Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp Old %ebx Old %ebx %esp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Observation: Saved and restored register %ebx
swapFinish #2 swap’s Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp Old %ebx Old %ebx %esp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret
swapFinish #2 swap’s Stack Resulting Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp %esp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret
swapFinish #2 swap’s Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp %esp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret
swapFinish #3 swap’s Stack Resulting Stack %ebp • • • • • • yp yp xp xp Rtnadr Rtnadr %esp Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret
swapFinish #4 swap’s Stack %ebp • • • • • • yp yp xp xp Rtnadr Rtnadr %esp Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret