Unformatted text preview:

CS 105 Tour of the Black Holes of Computing Machine Level Programming III Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables 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 CS 105 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 CS 105 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 CS 105 Stack Operation Examples pushl eax popl edx 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 CS 105 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 6 ret Pop address from stack jump to address CS 105 Procedure Call Example 804854e 8048553 e8 3d 06 00 00 50 call 0x110 0x110 0x10c 0x10c 0x108 123 0x108 call pushl 8048b90 main eax 8048b90 123 0x104 0x8048553 esp 0x108 esp eip 0x804854e 0x108 0x104 eip 0x8048553 0x804854e 0x8048b90 eip is program counter 7 CS 105 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 0x8048592 0x8048553 eip is program counter 8 CS 105 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 9 State for single procedure instantiation CS 105 Call Chain Example Code Structure yoo who 10 Call Chain yoo who amI amI Procedure amI recursive who amI amI amI amI amI amI CS 105 Stack Frames Contents Local variables Return information Temporary space yoo who Management Space allocated when entering procedure Set up code prologue Deallocated when returning Finish code epilogue Pointers Frame Pointer ebp Stack Pointer Stack pointer esp indicates stack top Frame pointer ebp indicates start of esp current frame 11 amI proc Stack Top CS 105 Stack Operation yoo who 12 Call Chain yoo Frame Pointer ebp Stack Pointer esp yoo CS 105 Stack Operation who amI amI 13 Call Chain yoo who Frame Pointer ebp Stack Pointer esp yoo who CS 105 Stack Operation amI amI 14 Call Chain yoo yoo who amI Frame Pointer ebp Stack Pointer esp who amI CS 105 Stack Operation amI amI Call Chain yoo yoo who who amI amI Frame Pointer ebp Stack Pointer esp 15 amI amI CS 105 Stack Operation amI amI Call Chain yoo yoo who amI amI Frame Pointer ebp Stack Pointer esp who amI amI 16 CS 105 Stack Operation who amI amI Call Chain yoo who amI Frame Pointer ebp Stack Pointer esp yoo who amI amI 17 CS 105 Stack Operation who amI amI Call Chain yoo who amI amI Frame Pointer ebp Stack Pointer esp yoo who amI amI 18 CS 105 Stack Operation yoo who Call Chain Frame Pointer ebp Stack Pointer esp yoo yoo who amI amI amI amI 19 CS 105 IA32 Linux Stack Frame Current Stack Frame Top Low Addrs to Bottom Parameters for function about to be called 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 20 Arguments for this call Return Addr Old ebp Stack Pointer esp Argument Build CS 105 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 21 call swap pushl zip2 pushl zip1 call swap Global Var Global Var Resulting Stack zip2 zip1 Rtn adr esp CS 105 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 22 Set Up Body Finish CS 105 swap Setup 1 Entering Stack ebp Resulting Stack zip2 yp zip1 xp Rtn adr esp ebp Rtn adr Old ebp esp swap pushl ebp movl esp ebp pushl ebx 23 CS 105 swap Setup 2 Entering Stack ebp Resulting Stack zip2 yp zip1 xp Rtn adr esp Rtn adr Old ebp ebp esp swap pushl ebp movl esp ebp pushl ebx 24 CS 105 swap Setup 3 Entering Stack ebp Resulting Stack zip2 yp zip1 xp Rtn adr esp Rtn adr Old ebp ebp Old ebx esp swap pushl ebp movl esp ebp pushl ebx 25 CS 105 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 26 0 Old ebp ebp Old ebx esp Body CS 105 swap Finish 1 swap s Stack Offset 27 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 movl 4 ebp ebx movl ebp esp popl ebp ret CS 105 swap Finish 2 swap s Stack Offset swap s Stack Offset 12 yp 12 yp 8 xp xp 4 Rtn adr 8 4 Rtn adr 0 Old ebp ebp 4 Old ebx esp 0 Old ebp ebp esp movl 4 ebp ebx movl ebp esp popl ebp ret 28 CS 105 swap Finish 3 swap s Stack Offset swap s Stack Offset 12 yp 8 yp 8 xp 4 xp 4 Rtn adr 0 Rtn adr 0 Old ebp ebp esp ebp esp Also known as leave 29 movl 4 ebp ebx movl ebp esp popl ebp ret CS 105 swap Finish 4 swap s Stack Offset ebp ebp 12 yp zip2 8 xp 4 Rtn adr zip1 Exiting Stack esp esp Observation 30 Saved restored register ebx Didn t do so for eax ecx or edx movl 4 ebp ebx movl ebp esp popl ebp ret CS 105 Register Saving Conventions When procedure yoo calls who …


View Full Document

Harvey Mudd CS 105 - Machine-Level Programming III: Procedures

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Loading Unlocking...
Login

Join to view Machine-Level Programming III: Procedures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Machine-Level Programming III: Procedures and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?