Rutgers University Santosh Nagarakatte 89 Procedure Control Flow n Use stack to

Rutgers university santosh nagarakatte 89 procedure

This preview shows page 89 - 105 out of 126 pages.

Rutgers University Santosh Nagarakatte 89 Procedure Control Flow n Use stack to support procedure call and return Procedure call: call label Push return address on stack; Jump to label Return address value n Address of instruction beyond call n Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax l Return address = 0x8048553 Procedure return: n ret Pop address from stack; Jump to address
Image of page 89
%esp %eip %esp %eip 0x804854e 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
Image of page 90
%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x108 0x10c 0x110 0x8048553 123 Procedure Return Example 0x108 0x10c 0x110 123 ret 8048591: c3 ret 0x108 %eip is program counter 0x8048553 0x8048553
Image of page 91
Rutgers University Santosh Nagarakatte 92 Stack-Based Languages Languages that Support Recursion n e.g., C, Pascal, Java n Code must be “ Reentrant l Multiple simultaneous instantiations of single procedure n Need some place to store state of each instantiation l Arguments, local variables, return pointer Stack Discipline n State for given procedure needed for limited time l From when called to when return n Callee returns before caller does Stack Allocated in Frames (Activation records) n state for single procedure instantiation
Image of page 92
Rutgers University Santosh Nagarakatte 93 Call Chain Example Code Structure yoo(…) { who(); } who(…) { • • • amI(); • • • amI(); • • • } amI(…) { amI(); } yoo who amI amI amI Call Chain n Procedure amI recursive amI
Image of page 93
Rutgers University Santosh Nagarakatte 94 Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack “Top” Stack Frames Contents n Local variables, return value n Temporary space Management n Space allocated when enter procedure l “Set-up” code n Deallocated when return l “Finish” code Pointers n Stack pointer %esp : stack top n Frame pointer %ebp : start of current frame amI
Image of page 94
Rutgers University Santosh Nagarakatte 95 Stack Pointer %esp yoo Frame Pointer %ebp Stack Operation yoo Call Chain yoo(…) { who(); }
Image of page 95
Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { • • • amI(); • • • amI(); • • • }
Image of page 96
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); }
Image of page 97
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI amI
Image of page 98
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI amI amI amI
Image of page 99
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI amI amI
Image of page 100
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI amI
Image of page 101
Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { • • • amI(); • • • amI(); • • • } amI amI amI
Image of page 102
Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who Call Chain amI(…) { } amI amI amI amI
Image of page 103
Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { • • • amI(); • • • amI(); • • • } amI amI amI amI
Image of page 104
Image of page 105

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture