1 / 58

Lecture 5: The Stack

Lecture 5: The Stack. EEN 312: Processors: Hardware, Software, and Interfacing. Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM). COURSE PLAN FOR TODAY Midterm I The Stack and Memory. MIDTERM I. Midterm I. Thursday, February 13 th ! Two weeks away

saburo
Download Presentation

Lecture 5: The Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5: The Stack EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM)

  2. COURSE PLAN FOR TODAY Midterm I The Stack and Memory

  3. MIDTERM I

  4. Midterm I • Thursday, February 13th! • Two weeks away • Covers the following Material: • Chapters 1 – 3 • Laboratory 1

  5. THE STACK AND MEMORY

  6. A Call Chain Image by David Thomas

  7. Procedure Calling • Place parameters for procedure in registers • Transfer control to procedure • Procedure acquires storage • Procedure performs function. • Procedure places return value in appropriate register • Return control

  8. The Stack • Region of memory managed with stack discipline. • Accessed with pushand pop

  9. The Stack

  10. Stack Frames • Stack frames “belong” to a procedure. • Store local variables here (they go out of scope automatically) • Can communicate with other procedures with a stack frame.

  11. Procedures and Register Use • What if we want to use some registers in the procedure? • Caller could have data in it!

  12. Stack Discipline and ABI • Stack Discipline is important • Define an Application Binary Interface • How should procedures communicate? How should the be called? • Consistency, following standards, is the key.

  13. Conventions and Discipline Caller Callee Callee saves temporary values in its frame before using. Callee restores values in its frame after using. • Caller saves temporary values in its frame before the call. • Caller restores values in its frame after the call.

  14. Stack Frames • Contents • Local variables • Return information • Temporary space • Management • Space is allocated when entering • Set-up code • Deallocated when returning • Finish code Frame Pointer Stack Pointer

  15. Example frame ptr foo() { … bar() … } foo stack ptr foo()

  16. Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr foo() bar()

  17. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr foo() bar() baz()

  18. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz()

  19. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()

  20. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()

  21. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr foo() bar() baz() baz() baz()

  22. Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr foo() bar() baz() baz() baz()

  23. Example foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar frame ptr baz stack ptr baz() foo() bar() baz() baz() baz()

  24. Example foo() { … bar() … } foo bar() { … baz … baz() … } frame ptr bar stack ptr baz() foo() bar() baz() baz() baz()

  25. Example frame ptr foo() { … bar() … } foo stack ptr baz() foo() bar() baz() baz() baz()

  26. What is the frame pointer used for? • Given a caller and a callee, the callee will begin with: • fp = sp • A callee will use the stack to: • Save the current sp, lr, and fp before calling a function • Store the arguments for, and return value space before calling a function • Save used registers before calling a function • Store variables local to the procedure

  27. foo Frame pointer stack ptr frame ptr

  28. foo Frame pointer frame ptr a = 0x00 stack ptr PUSH

  29. foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH stack ptr

  30. foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH c = 0x87 stack ptr

  31. foo Frame pointer frame ptr a = 0x00 b = 0xFC PREPARETO CALL c = 0x87 stack ptr

  32. foo Frame pointer frame ptr a = 0x00 b = 0xFC PUSH c = 0x87 r0 lr fp a0 ret stack ptr

  33. foo Frame pointer a = 0x00 b = 0xFC RETURNFROMCALL c = 0x87 r0 lr fp a0 ret stack ptr

  34. foo Frame pointer frame ptr a = 0x00 b = 0xFC POP! c = 0x87 stack ptr

  35. 5 minute paper • Take 5 minutes to think about these tasks. Determine why a frame pointer is helpful

  36. foo Frame pointer frame ptr a = 0x00 +4 b = 0xFC +8 c = 0x87 +12 stack ptr

  37. foo Frame pointer frame ptr a = 0x00 • The frame pointer gives usa base address from which tooffset in our frame for localstorage that will automatically go out of scope. +4 b = 0xFC +8 c = 0x87 +12 stack ptr

  38. Recursion foo() { … bar() … } foo bar() { … baz … baz() … } baz() { … baz() … } bar baz() { … baz() … } baz baz() { … baz() … } baz frame ptr baz stack ptr foo() bar() baz() baz() baz()

  39. Recursion • Stack frames give each function private storage • Saved registers • Local variables • Return portions • Recursion is handled without special consideration. • Following stack discipline and call/return pattern is critical

  40. PUSH and POP • PUSH<cond> {reglist} • POP<cond> {reglist} • Really pseudo-instructions! • PUSH – STMDB • Store multiple registers, decrement address before access. • POP – LDMIA • Store multiple registers, increment address after access.

  41. foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC stack ptr r2 = 0x00 sp = ??

  42. foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 stack ptr sp = ??-4

  43. foo STMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4

  44. foo STMDB r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4

  45. foo LDMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC r2 = 0x00 r3=0x87 stack ptr sp = ??-4

  46. foo LDMIA r0 = 0x87 r0 = 0x00 r1 = 0x00 r2 = 0xFC stack ptr r2 = 0x00 r3=0x87 sp = ??

  47. Multiple Ways to Handle the Stack • Stack Pointer can • Point to the lass occupied stack (Full stack) • (needs pre-decumenting) • Point to the next address to be occupied (Empty stack) • (needs post-decrementing) • Stack type can be given as a postfix to the instruction: • STMFD/LDMFD • STMFA/LDMFA • STMED/LDMED • STMEA/LDMEA

  48. RISC Mentality • There are no special functions for stacks! • Let’s look at stack instructions: • STMDA/LDMDA

More Related