{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture6 - Machine-Level Programming III Procedures Topics...

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

View Full Document Right Arrow Icon
Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures Topics Topics ° IA32 stack discipline ° Register saving conventions ° Creating pointers to local variables
Image of page 1

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

View Full Document Right Arrow Icon
– 2 – IA32 Stack 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
– 3 – IA32 Stack Pushing IA32 Stack Pushing 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 icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
– 4 – IA32 Stack Popping IA32 Stack Popping 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
– 5 – Procedure Control Flow Procedure Control Flow ° Use stack to support procedure call and return Procedure call: Procedure call: call label Push return address on stack; Jump to label Return address value 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: Procedure return: ° ret Pop address from stack; Jump to address
Image of page 5

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

View Full Document Right Arrow Icon
– 6 – Stack-Based Languages Stack-Based Languages Languages that Support Recursion 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 Stack Discipline ° State for given procedure needed for limited time ± From when called to when return ° Callee returns before caller does Stack Allocated in Stack Allocated in Frames Frames ° state for single procedure instantiation
Image of page 6
– 7 – Call Chain Example Call Chain Example Code Structure Code Structure yoo(…) { who(); } who(…) { • • • amI(); • • • amI(); • • • } amI(…) { amI(); } yoo who amI amI amI Call Chain ° Procedure amI recursive amI
Image of page 7

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

View Full Document Right Arrow Icon
– 8 – Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack “Top” Stack Frames Stack Frames Contents Contents ° Local variables ° Return information ° Temporary space Management Management ° Space allocated when enter procedure ± “Set-up” code ° Deallocated when return ± “Finish” code Pointers Pointers ° Stack pointer %esp indicates stack top ° Frame pointer %ebp indicates start of current frame amI
Image of page 8
– 9 – Stack Pointer %esp yoo Frame Pointer %ebp Stack Operation Stack Operation yoo Call Chain yoo(…) { who(); }
Image of page 9

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

View Full Document Right Arrow Icon
– 10 – Stack Pointer
Image of page 10
Image of page 11
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