This preview has intentionally blurred parts. Sign up to view the full document

View Full Document

Unformatted Document Excerpt

Some notes adopted from Bryant and OHallaron Machine-Level Programming: Procedures Chapter 3 of B&O IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Register %esp indicates lowest stack address address of top element Stack Pointer %esp Stack Grows Down Increasing Addresses Stack Top Stack Bottom IA32 Stack Pushing Pushing pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack Top Stack Bottom Stack Pointer %esp-4 IA32 Stack Popping Popping popl Dest Read operand at address given by %esp Increment %esp by 4 Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack Top Stack Bottom +4 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: ret Pop address from stack; Jump to address %esp %eip %esp %eip 0x804854e 0x108 0x108 0x10c 0x110 0x104 0x804854e 0x8048553 123 Procedure Call Example 0x108 0x10c 0x110 123 0x108 call 8048b90 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x8048b90 0x104 %eip is program counter %esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x104 0x108 0x10c 0x110 0x8048553 123 Procedure Return Example 0x108 0x10c 0x110 123 ret 8048591: c3 ret 0x108 %eip is program counter 0x8048553 0x8048553 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 Call Chain Example Code Structure yoo() { who(); } who() { amI(); amI(); } amI() { amI(); } yoo who amI amI amI Call Chain Procedure amI recursive amI Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack Top Stack Frames Contents Local variables Return information Temporary space Management Space allocated when enter procedure Set up code Deallocated when return Finish code Pointers Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame amI Previous Frame Stack Pointer %esp yoo Frame Pointer %ebp yoo Call Chain yoo() { who(); } Stack Pointer %esp yoo who ... View Full Document

End of Preview

Sign up now to access the rest of the document