IA32 Stack 15 213 n Region of memory managed with stack discipline n Grows toward lower addresses Register esp indicates lowest stack address The course that gives CMU its Zip Machine Level Programming III Procedures Sept 17 2002 n Stack Bottom Increasing Addresses l address of top element Topics n IA32 stack discipline n Register saving conventions n Creating pointers to local variables Stack Grows Down Stack Pointer esp Stack Top IA32 Stack Pushing Pushing IA32 Stack Popping Popping Stack Bottom Pushing n pushl Src n Fetch operand at Src Decrement esp by 4 n n 15 213 F 02 2 class07 ppt Increasing Addresses Write operand at address given by esp Stack Pointer esp Stack Bottom Popping n popl Dest n Read operand at address given by esp n Increment esp by 4 n Write to Dest Increasing Addresses Stack Pointer esp Stack Grows Down Stack Grows Down 4 4 Stack Top Stack Top 3 15 213 F 02 4 15 213 F 02 Procedure Control Flow Flow Stack Operation Examples Examples pushl eax n popl edx Use stack to support procedure call and return Procedure call call label 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 Return address value 0x108 123 0x108 123 n 0x104 213 0x104 213 n eax 213 eax 213 eax 213 edx 555 edx 555 edx 555 213 esp 0x108 esp 0x108 0x104 esp 0x104 0x108 15 213 F 02 5 e8 3d 06 00 00 50 call call pushl Address of instruction beyond call Example from disassembly 804854e e8 3d 06 00 00 call 8048b90 main 8048553 50 pushl eax l Return address 0x8048553 Procedure return n ret Pop address from stack Jump to address 15 213 F 02 6 Procedure Call Example 804854e 8048553 Push return address on stack Jump to label Procedure Return Example 8048b90 main eax 8048591 c3 ret 8048b90 ret 0x110 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x10c 0x108 0x108 123 0x108 123 0x104 0x8048553 esp esp 0x108 eip 0x804854e 0x108 0x104 0x8048553 esp 0x108 0x104 eip 0x8048b90 0x804854e 0x104 eip 0x8048591 eip is program counter 7 123 123 0x8048553 esp 0x104 0x108 eip 0x8048553 0x8048591 eip is program counter 15 213 F 02 8 15 213 F 02 Stack Based Languages Languages Call Chain Example Languages that Support Recursion n e g C Pascal Java n Code must be Reentrant Code Structure yoo who l Multiple simultaneous instantiations of single procedure n Need some place to store state of each instantiation l Arguments l Local variables l Return pointer Stack Discipline n State for given procedure needed for limited time Call Chain yoo who amI amI who amI amI amI l From when called to when return n Callee returns before caller does n Stack Allocated in Frames n Procedure amI recursive state for single procedure instantiation 15 213 F 02 9 Stack Frames Local variables n Return information n Temporary space amI Management n Space allocated when enter procedure l Set up code n Deallocated when return l Finish code Pointers n n 11 Stack pointer esp indicates stack top Frame pointer ebp indicates start of current frame yoo who who Frame Pointer ebp Stack Pointer esp amI 15 213 F 02 Stack Operation yoo amI 10 Contents n amI Call Chain yoo Frame Pointer ebp Stack Pointer esp yoo proc Stack Top 15 213 F 02 12 15 213 F 02 Stack Operation who amI amI Call Chain yoo who Frame Pointer ebp Stack Pointer esp yoo 15 213 F 02 Stack Operation amI amI amI amI who 13 Call Chain who amI Frame Pointer ebp yoo yoo who amI Frame Pointer ebp Stack Pointer esp amI who amI 15 213 F 02 14 amI amI yoo amI Call Chain Stack Operation yoo who Stack Operation Call Chain yoo yoo who who amI amI amI amI Stack Pointer esp amI Frame Pointer ebp amI amI Stack Pointer esp 15 15 213 F 02 16 15 213 F 02 Stack Operation amI amI Call Chain yoo amI amI amI who Frame Pointer ebp amI Stack Pointer esp 19 Call Chain yoo who amI Frame Pointer ebp Stack Pointer esp yoo yoo Frame Pointer ebp who amI Stack Pointer esp who amI amI 15 213 F 02 Stack Operation Call Chain amI amI 17 who amI amI amI amI yoo who Stack Operation 15 213 F 02 18 Stack Operation yoo amI who Call Chain yoo amI amI amI amI 20 Frame Pointer ebp who amI 15 213 F 02 yoo amI Stack Pointer esp who amI 15 213 F 02 Stack Operation who amI amI Call Chain Frame Pointer ebp yoo who amI amI Stack Pointer esp Stack Operation yoo yoo who who amI n n Arguments Frame Pointer ebp Return Addr Old ebp Saved Registers Local Variables Old frame pointer Return address l Pushed by call instruction n 23 void call swap swap zip1 zip2 Saved register context Caller Stack Frame n int zip1 15213 int zip2 91125 Caller Frame l Argument build l If can t keep in registers Arguments for this call 15 213 F 02 22 Calling swap from call swap Current Stack Frame Top to Bottom Local variables amI Revisiting swap IA32 Linux Stack Frame Frame n amI amI Parameters for function about to call yoo who amI 15 213 F 02 Frame Pointer ebp Stack Pointer esp yoo amI 21 n Call Chain Stack Pointer esp Argument Build 15 213 F 02 void swap int xp int yp int t0 xp int t1 yp xp t1 yp t0 24 call swap pushl zip2 pushl zip1 call swap Global Var Global Var Resulting Stack zip2 zip1 Rtn adr esp 15 213 F 02 Revisiting swap void swap int xp int yp int t0 xp int t1 yp xp t1 yp t0 swap Setup 1 swap pushl ebp movl esp ebp pushl ebx movl movl movl movl movl movl Set Up 12 ebp ecx 8 ebp edx ecx eax edx ebx eax edx ebx ecx ebp Body zip2 yp zip1 xp 15 213 F 02 26 swap Setup 3 Resulting Stack Resulting Stack Entering Stack ebp zip2 yp zip2 yp zip1 xp zip1 xp Rtn adr esp esp swap pushl ebp movl esp ebp pushl ebx ebp Rtn adr Old ebp Rtn adr esp ebp esp swap pushl ebp movl esp ebp pushl ebx 27 Rtn adr Finish swap Setup 2 Entering Stack esp Old ebp 15 213 F 02 25 ebp Rtn adr movl 4 ebp ebx movl ebp esp popl ebp ret Resulting Stack Entering Stack Rtn adr Old ebp ebp Old ebx esp swap pushl ebp movl esp ebp pushl ebx 15 213 F 02 28 15 213 F 02 swap Finish 1 Effect of swap Setup Entering Stack Resulting Stack ebp swap s Stack Offset Offset relative to ebp Offset 12 yp 12 yp 8 xp 8 xp Rtn adr 4 Rtn adr zip2 12 yp 4 zip1 8 xp 0 Old ebp ebp 0 Old ebp ebp 4 Rtn adr 4 Old ebx esp 4 Old ebx esp esp Rtn adr movl 12 ebp ecx get yp movl 8 ebp edx get xp 0 Old ebp ebp Old …
View Full Document