1 / 11

The Stack

The Stack. Keeping Track of Function Calls. The Stack Pointer. The CPU usually contains a special register, called the Stack Pointer (SP) It is used by the CPU whenever it needs to push something onto, or pop something off, the stack. Push and Pop. push Decrement SP

ona
Télécharger la présentation

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. The Stack Keeping Track of Function Calls

  2. The Stack Pointer • The CPU usually contains a special register, called the Stack Pointer (SP) • It is used by the CPU whenever it needs to push something onto, or pop something off, the stack

  3. Push and Pop push • Decrement SP • Write value to new memory location that SP points to pop • Read memory location pointed to by SP • Increment SP

  4. Or in reverse… Some CPUs reverse steps 1 and 2 so that: • push  write then decrement • pop  increment then read

  5. On LC-3 • PUSH ADD R6, R6, #-1 STR R0, R6, #0 POP LDR R0, R6, #0 ADD R6, R6, #+1

  6. Setting up the Stack • The CPU assumes that the SP is pointing to an area of memory that has been reserved for stack use • It is therefore necessary for the programmer to choose where to put the stack, and to initialise the SP to the appropriate address before using it! • For a decrement-before-write CPU, the appropriate address is one higher than the highest address in the reserved area

  7. Stack Setup Example Chosen stack area SP

  8. Stack Operation pop (returns ee) Empty stack push dd push ee SP SP SP SP

  9. Stack Overflow • If you push more data onto the stack than will fit into the area you’ve allocated to it, then you have created a stack overflow • This is potentially disastrous, since you may overwrite (and thus destroy) important data, or instructions, outside the stack area

  10. Stack Overflow • Advanced computers have special software to detect stack overflow, but simple ones do not • On a simple computer, you have to design the program to avoid it • Now you know what the ‘stack overflow’ error message you may have seen means • What might be a common cause?

  11. Test Yourself! • What is meant by Stack Overflow? • What is the Stack Pointer (SP) used for? • Explain push and pop with regards to the stack

More Related