
University of Washington Procedures and Call Stacks ¢ How do I pass arguments to a procedure? ¢ How do I get a return value from a procedure? ¢ Where do I put local variables? ¢ When a func@on returns, how does it know where to return to? ¢ To answer these quesons, we need a call stack … Autumn 2012 Procedures and Stacks 1 University of Washington Memory Layout 2N-1 Stack local variables Dynamic Data new'ed variables (Heap) static variables Static Data (including global variables (C)) Literals literals (e.g., “example”) Instructions 0 Autumn 2012 Procedures and Stacks 2 University of Washington Memory Layout Stack “Automatic” lifetime; writable; not executable mutable Dynamic Data Programmer controlled lifetime; writable; not executable mutable (Heap) writable; not executable Static Data Execution lifetime; mutable Read-only; not executable Literals Execution lifetime; immutable Read-only; executable Instructions Execution lifetime; immutable Autumn 2012 Procedures and Stacks 3 University of Washington IA32 Stack ¢ Region of memory managed Stack “BoTom” with a stack discipline ¢ Grows toward lower addresses Increasing ¢ Customarily shown “upside-down” Addresses ¢ Register %esp contains lowest stack address = address of “top” element Stack Grows Down Stack Pointer: %esp Stack “Top” Autumn 2012 Procedures and Stacks 4 University of Washington IA32 Stack: Push Stack “BoTom” ¢ pushl Src Increasing Addresses Stack Grows Down Stack Pointer: %esp Stack “Top” Autumn 2012 Procedures and Stacks 5 University of Washington IA32 Stack: Push Stack “BoTom” ¢ pushl Src § Fetch operand at Src § Decrement %esp by 4 Increasing Addresses § Write operand at address given by %esp Stack Grows Down -4 Stack Pointer: %esp Stack “Top” Autumn 2012 Procedures and Stacks 6 University of Washington IA32 Stack: Pop Stack “BoTom” ¢ popl Dest Increasing Addresses Stack Grows Down Stack Pointer: %esp Stack “Top” Autumn 2012 Procedures and Stacks 7 University of Washington IA32 Stack: Pop Stack “BoTom” ¢ popl Dest § Read operand at address %esp § Increment %esp by 4 Increasing Addresses § Write operand to Dest Stack Grows Down +4 Stack Pointer: %esp Stack “Top” Autumn 2012 Procedures and Stacks 8 University of Washington Procedure Call Overview Caller … <set up args> Callee call <create local vars> <clean up args> … <find return val> <set up return val> … <destroy local vars> return ¢ Callee must know where to find args ¢ Callee must know where to find “return address” ¢ Caller must know where to find return val ¢ Caller and Callee run on same cpu → use the same registers § Might need to save registers used by Callee Autumn 2012 Procedures and Stacks 9 University of Washington Procedure Call Overview Caller … <save regs> Callee <set up args> call <save regs> <clean up args> <create local vars> <restore regs> … <find return val> <set up return val> … <destroy local vars> <restore regs> return ¢ The convenon of where to leave/find things is called the procedure call linkage § Details vary between systems § We will see the convenBon for IA32/Linux in detail Autumn 2012 Procedures and Stacks 10 University of Washington Procedure Control Flow ¢ Use stack to support procedure call and return ¢ Procedure call: call label § Push return address on stack § Jump to label Autumn 2012 Procedures and Stacks 11 University of Washington 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 instrucBon 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 Autumn 2012 Procedures and Stacks 12 University of Washington Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x10c 0x108 123 %esp 0x108 %eip 0x804854e %eip: program counter Autumn 2012 Procedures and Stacks 13 University of Washington 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 Autumn 2012 Procedures and Stacks 14 University of Washington 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 0x804854e0x8048553 %eip: program counter Autumn 2012 Procedures and Stacks 15 University of Washington 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 0x1080x104 %eip 0x804854e %eip 0x804854e0x8048553 %eip: program counter Autumn 2012 Procedures and Stacks 16 University of Washington 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 0x1080x104 %eip 0x804854e %eip 0x8048553 + 0x000063d 0x8048b90 %eip: program counter Autumn 2012 Procedures and Stacks 17 University of Washington Procedure Return Example 8048591: c3 ret ret 0x110 0x10c 0x108 123 0x104 0x8048553 %esp 0x104 %eip 0x8048591 %eip: program counter Autumn 2012 Procedures and Stacks 18 University of Washington 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 Autumn 2012 Procedures and Stacks 19 University of Washington 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 0x80485910x8048553 %eip: program counter Autumn 2012 Procedures and Stacks 20 University of Washington Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x1040x108 %eip 0x8048591 %eip 0x80485910x8048553 %eip: program counter Autumn 2012 Procedures and Stacks 21 University of Washington Stack-Based Languages ¢ Languages that support recursion § e.g., C, Pascal, Java § Code must be re-entrant § MulBple simultaneous instanBaons of single procedure – What would happen if code could not be reentrant? § Need some place to store state of each instanBaon § Arguments § Local variables § Return pointer ¢ Stack discipline § State for a given procedure needed for a limited Bme § StarBng from when it is called to when it returns § Callee always returns before caller does ¢ Stack allocated in frames § State for a single procedure instanBaon Autumn 2012 Procedures and Stacks 22 University of Washington Call Chain Example Example Call Chain yoo(…) { • yoo • who(…) who(); { who • • • • • amI(); } amI(…) amI amI • • • { amI(); • amI • • • • } amI(); • amI • } Procedure amI is recursive (calls itself) Autumn 2012 Procedures and Stacks 23 University of Washington Stack Frames Previous ¢ Contents Frame § Local variables § Return informaon Frame Pointer: %ebp § Temporary space Frame for proc Stack Pointer: %esp ¢ Management? Stack “Top” Autumn 2012 Procedures and Stacks 24 University of Washington Stack Frames Previous ¢ Contents Frame § Local variables § Return informaon Frame Pointer: %ebp § Temporary space Frame for proc Stack Pointer: %esp ¢ Management § Space allocated when procedure is entered Stack “Top” § “Set-up” code § Space deallocated upon return § “Finish” code Autumn 2012 Procedures and Stacks 25 University of Washington Stack Example yoo(…) yoo %ebp { • yoo who • %esp who(); • amI amI • } amI amI Autumn 2012 Procedures and Stacks 26 University of Washington Stack Example who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI amI Autumn 2012 Procedures and Stacks 27 University of Washington Stack Example amI(…) yoo { • yoo who • amI(); • amI amI who • } amI %ebp amI %esp amI Autumn 2012 Procedures and Stacks 28 University of Washington Stack Example amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI amI %ebp amI %esp Autumn 2012 Procedures and Stacks 29 University of Washington Stack Example amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI amI amI %ebp amI %esp Autumn 2012 Procedures and Stacks 30 University of Washington Stack Example amI(…) yoo { • yoo who • amI(); • amI amI who • } amI amI amI %ebp amI %esp Autumn 2012 Procedures and Stacks 31 University of Washington Stack Example amI(…) yoo { • yoo who • amI(); • amI amI who • } amI %ebp amI %esp amI Autumn 2012 Procedures and Stacks 32 University of Washington Stack Example who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI amI Autumn 2012 Procedures and Stacks 33 University of Washington Stack Example amI(…) yoo { • yoo who • • • amI amI who • } amI %ebp amI %esp amI Autumn 2012 Procedures and Stacks 34 University of Washington Stack Example who(…) yoo { • • • yoo who amI(); • • • %ebp amI(); amI amI who • • • %esp } amI amI Autumn 2012 Procedures and Stacks 35 University of Washington Stack Example yoo(…) yoo %ebp { • yoo who • %esp who(); • amI amI • } amI amI Autumn 2012 Procedures and Stacks 36 University of Washington IA32/Linux Stack Frame ¢ Current Stack Frame (“Top” to BoTom) § Old frame pointer § Local variables Caller If can’t be just kept in registers Frame § Saved register context Arguments When reusing registers § “Argument build area” Frame pointer Return Addr
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages38 Page
-
File Size-