cs33-machine_programming_procedures

cs33-machine_programming_procedures - Machine-Level...

Info icon This preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Some notes adopted from Bryant and O’Hallaron Machine-Level Programming: Procedures Chapter 3 of B&O
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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”
Image of page 2
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
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
%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
Image of page 6
%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
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Image of page 8
Call Chain Example Code Structure yoo(…) { who(); } who(…) { • • • amI(); • • • amI(); • • • } amI(…) { amI(); } yoo who amI amI amI Call Chain – Procedure amI recursive amI
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Image of page 10
Stack Pointer %esp yoo Frame Pointer %ebp yoo Call Chain yoo(…) { who(); }
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stack Pointer %esp yoo who Frame Pointer %ebp yoo who Call Chain who(…) { • • • amI(); • • • amI(); • • • }
Image of page 12
Stack Pointer %esp yoo who amI Frame Pointer %ebp yoo who amI Call Chain amI(…) { amI(); }
Image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Stack Pointer %esp yoo who amI Frame Pointer %ebp yoo who amI Call Chain amI(…) { amI(); } amI amI
Image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern