15 213 Machine Level Programming III Procedures Sept 17 2007 IA32 x86 64 stack discipline Register saving conventions Creating pointers to local variables class06 ppt Argument passing in registers Minimizing stack usage Using stack pointer as only reference 15 213 F 07 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack Bottom Increasing Addresses Register esp indicates lowest stack address address of top element Stack Pointer esp Stack Grows Down Stack Top 2 15 213 Intro to Computer Systems Fall 2007 IA32 Stack Pushing Pushing pushl Src Fetch operand at Src Decrement esp by 4 Write operand at address given by esp Stack Bottom Increasing Addresses Stack Pointer esp Stack Grows Down 4 Stack Top 3 15 213 Intro to Computer Systems Fall 2007 IA32 Stack Popping Popping popl Dest Read operand at address given by esp Increment esp by 4 Write to Dest Stack Bottom Increasing Addresses Stack Pointer esp Stack Grows Down 4 Stack Top 4 15 213 Intro to Computer Systems Fall 2007 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 call 8048b90 main 8048553 50 pushl eax Return address 0x8048553 Procedure return 5 ret Pop address from stack Jump to address 15 213 Intro to Computer Systems Fall 2007 Procedure Call Example 804854e 8048553 e8 3d 06 00 00 50 call 0x110 0x110 0x10c 0x10c 0x108 123 0x108 call pushl 8048b90 123 0x104 0x8048553 esp 0x108 esp eip 0x804854e 0x108 0x104 eip 0x8048b90 0x804854e eip is program counter 6 15 213 Intro to Computer Systems Fall 2007 8048b90 main eax Procedure Return Example 8048591 c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 0x104 0x8048553 esp 0x104 eip 0x8048591 eip is program counter 7 15 213 Intro to Computer Systems Fall 2007 123 0x8048553 esp 0x104 0x108 eip 0x8048553 0x8048591 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 8 state for single procedure instantiation 15 213 Intro to Computer Systems Fall 2007 Call Chain Example Code Structure yoo who 9 Call Chain yoo who amI amI Procedure amI recursive who amI amI amI 15 213 Intro to Computer Systems Fall 2007 amI amI amI Stack Frames Contents Local variables Return information Temporary space yoo who Management amI Space allocated when enter procedure Set up code Deallocated when return Finish code Pointers Frame Pointer ebp Stack Stack pointer esp indicates stack Pointer top Frame pointer ebp indicates start of esp current frame 10 15 213 Intro to Computer Systems Fall 2007 proc Stack Top Stack Operation yoo who 11 Call Chain yoo Frame Pointer ebp Stack Pointer esp 15 213 Intro to Computer Systems Fall 2007 yoo Stack Operation who amI amI 12 Call Chain yoo who Frame Pointer ebp Stack Pointer esp 15 213 Intro to Computer Systems Fall 2007 yoo who Stack Operation amI amI 13 Call Chain yoo yoo who amI Frame Pointer ebp Stack Pointer esp 15 213 Intro to Computer Systems Fall 2007 who amI Stack Operation amI amI Call Chain yoo yoo who who amI amI Frame Pointer ebp Stack Pointer esp 14 15 213 Intro to Computer Systems Fall 2007 amI amI Stack Operation amI amI Call Chain yoo yoo who who amI amI amI amI Frame Pointer ebp Stack Pointer esp 15 15 213 Intro to Computer Systems Fall 2007 amI amI Stack Operation amI amI Call Chain yoo yoo who who amI amI amI 16 Frame Pointer ebp Stack Pointer esp 15 213 Intro to Computer Systems Fall 2007 amI amI Stack Operation amI amI Call Chain yoo yoo who amI amI Frame Pointer ebp Stack Pointer esp amI 17 15 213 Intro to Computer Systems Fall 2007 who amI Stack Operation who amI amI Call Chain yoo who amI Frame Pointer ebp Stack Pointer esp amI amI 18 15 213 Intro to Computer Systems Fall 2007 yoo who Stack Operation amI Call Chain yoo yoo Frame Pointer ebp who amI amI amI Stack Pointer esp amI 19 15 213 Intro to Computer Systems Fall 2007 who amI Stack Operation who amI amI Call Chain yoo who amI amI Frame Pointer ebp Stack Pointer esp amI amI 20 15 213 Intro to Computer Systems Fall 2007 yoo who Stack Operation yoo who Call Chain Frame Pointer ebp Stack Pointer esp yoo who amI amI amI amI 21 15 213 Intro to Computer Systems Fall 2007 yoo 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 Saved register context Old frame pointer Arguments Frame Pointer ebp Saved Registers Local Variables Caller Stack Frame Return address Pushed by call instruction 22 Arguments for this call Return Addr Old ebp Stack Pointer esp 15 213 Intro to Computer Systems Fall 2007 Argument Build Revisiting swap Calling swap from call swap int zip1 15213 int zip2 91125 call swap pushl zip2 pushl zip1 call swap void call swap swap zip1 zip2 void swap int xp int yp int t0 xp int t1 yp xp t1 yp t0 23 15 213 Intro to Computer Systems Fall 2007 Global Var Global Var Resulting Stack zip2 zip1 Rtn adr esp 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 12 ebp ecx 8 ebp edx ecx eax edx ebx eax edx ebx ecx movl 4 ebp ebx movl ebp esp popl ebp ret 24 15 213 Intro to Computer Systems Fall 2007 Set Up Body Finish swap Setup 1 Entering Stack ebp Resulting Stack zip2 yp zip1 xp Rtn adr Rtn adr esp Old ebp swap pushl ebp movl esp ebp pushl ebx 25 ebp 15 213 Intro to Computer Systems Fall 2007 esp swap Setup 2 Entering Stack ebp Resulting Stack zip2 yp zip1 xp Rtn adr Rtn adr esp Old ebp ebp esp swap pushl ebp movl esp ebp pushl ebx 26 15 213 Intro to Computer Systems Fall 2007 swap Setup 3 Entering Stack ebp zip2 yp zip1 xp Rtn adr Rtn adr esp swap pushl ebp movl esp ebp pushl ebx 27 Resulting Stack 15 213 Intro to Computer Systems Fall 2007 Old ebp ebp Old ebx esp 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 28 0 Old ebp ebp Old ebx esp Body 15 213 Intro to Computer Systems Fall 2007 swap Finish 1 swap s Stack
View Full Document