Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Stack.

Similar presentations


Presentation on theme: "The Stack."— Presentation transcript:

1 The Stack

2 Stack: An Abstract Data Type
An important abstraction that you will encounter in many applications. Has many uses: Storing Context for Function Calls and Interrupts Temporary variables not in registers (local variables) Evaluating arithmetic expressions Store intermediate results on stack instead of in registers Etc.

3 Stacks A LIFO (last-in first-out) storage structure.
The first thing you put in is the last thing you take out. The last thing you put in is the first thing you take out. This means of access is what defines a stack, not the specific implementation. Two main operations: PUSH: add an item to the stack POP: remove an item from the stack

4 A Physical Stack Coin rest in the arm of an automobile
First quarter out is the last quarter in. 1995 1996 1998 1998 1982 1982 1995 1995 Initial State After One Push After Three More Pushes After One Pop

5 A Hardware Implementation
Data items move between registers Empty: Yes Empty: No Empty: No Empty: No / / / / / / TOP #18 TOP #12 TOP #31 TOP / / / / / / / / / / / / #5 #18 / / / / / / / / / / / / #31 / / / / / / / / / / / / / / / / / / #18 / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / Initial State After One Push After Three More Pushes After Two Pops

6 A Software Implementation
Uses memory instead of registers Data items don't move in memory, just our idea about where the TOP of the stack is (SP). / / / / / / / / / / / / / / / / / / TOP / / / / / / / / / / / / / / / / / / #12 #12 / / / / / / / / / / / / #5 #5 TOP / / / / / / / / / / / / TOP #31 #31 / / / / / / TOP #18 #18 #18 x100 SP x0FF SP x0FC SP x0FE SP Initial State After One Push After Three More Pushes After Two Pops SP = stack pointer PUSH instruction does a post-decrement POP instruction does a pre-increment

7 Stack is located in same memory as data
On atmega328 the user can place the stack wherever, but out of RESET it is placed at the highest address in DMEM Stack “grows” downward (decreasing addresses) “During interrupts and subroutine calls, the return address Program Counter (PC) is stored on the stack. The Stack is effectively allocated in the general data SRAM, and consequently the Stack size is only limited by the total SRAM size and the usage of the SRAM. All user programs must initialize the SP in the Reset routine (before sub-routines or interrupts are executed). The Stack Pointer (SP) is read/write accessible in the I/O space.” From atmega328 reference, page 12

8 The SP (stack pointer) on the atmega328 is actually two 8-bit registers
SPH (stack pointer high byte) at I/O addr 0x3E SPL (stack pointer low byte) at I/O addr 0x3D Aberrant Conditions Stack Overflow We push too many things onto the stack How to detect? Stack Underflow We pop too many things off of the stack Both of these are highly undesirable

9 Stack Instructions PUSH: post-decrement of SP by 1
POP: pre-increment of SP by 1 CALL/RCALL: decrements SP by 2 RET/RET: increments SP by 2 See Table 7-1 on p. 12 of atmega328P reference

10 Compilers typically generate code to store “local” variables in memory (on the stack)
int main() { int i, j, k; unsigned width; } Void frob_widget(widget* p, float frob_smoothing) { float impulse_magnitude;

11 The stack is the Achilles’ heel of any general-purpose processor


Download ppt "The Stack."

Similar presentations


Ads by Google