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