procedures and call stacks
play

Procedures and Call Stacks How do I pass arguments to - PDF document

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?


  1. 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 ¡ques@ons, ¡we ¡need ¡a ¡ call ¡stack ¡… ¡ Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 1 ¡ University ¡of ¡Washington ¡ Memory ¡Layout ¡ 2 N -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 ¡

  2. University ¡of ¡Washington ¡ Memory ¡Layout ¡ Stack “Automatic” lifetime; writable; not executable mutable Dynamic Data Programmer controlled lifetime; writable; not executable mutable (Heap) Static Data writable; not executable Execution lifetime; mutable Literals Read-only; not executable Execution lifetime; immutable Instructions Execution lifetime; immutable Read-only; executable Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 3 ¡ University ¡of ¡Washington ¡ IA32 ¡Stack ¡ Stack ¡“BoTom” ¡ ¢ Region ¡of ¡memory ¡managed ¡ ¡ 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 ¡

  3. 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 ¡ Increasing ¡ § Decrement ¡ %esp ¡ by ¡4 ¡ Addresses ¡ § Write ¡operand ¡at ¡address ¡ ¡ given ¡by ¡ %esp Stack ¡Grows ¡ Down ¡ -­‑4 ¡ Stack ¡Pointer: ¡ %esp Stack ¡“Top” ¡ Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 6 ¡

  4. 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 ¡ Increasing ¡ § Increment ¡ %esp ¡ by ¡4 ¡ Addresses ¡ § Write ¡operand ¡to ¡ Dest ¡ Stack ¡Grows ¡ Down ¡ +4 ¡ Stack ¡Pointer: ¡ %esp Stack ¡“Top” ¡ Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 8 ¡

  5. University ¡of ¡Washington ¡ Procedure ¡Call ¡Overview ¡ Caller ¡ ¡ ¡ ¡ ¡… ¡ Callee ¡ <set ¡up ¡args> ¡ 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 ¡conven@on ¡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 ¡

  6. 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 ¡

  7. 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 ¡

  8. 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 0x8048553 %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 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048553 %eip: program ¡counter Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 16 ¡

  9. 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 0x108 0x104 %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 ¡

  10. 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 0x8048591 0x8048553 %eip: program ¡counter Autumn ¡2012 ¡ Procedures ¡and ¡Stacks ¡ 20 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend