The Stack Stack An Abstract Data Type An
The Stack
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.
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
A Physical Stack • Coin rest in the arm of an automobile Initial State 1995 1996 1998 1982 1995 After One Push After Three More Pushes After One Pop • First quarter out is the last quarter in.
A Hardware Implementation • Data items move between registers Empty: Yes ////// TOP Empty: No #18 TOP Empty: No #12 TOP No #31 ////// #5 #18 ////// #31 ////// #18 ////// ////// Initial State After One Push After Three More Pushes After Two Pops TOP
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 ////// #5 #5 ////// #31 #18 ////// x 100 Initial State TOP SP TOP #18 x 0 FF After One Push SP SP = stack pointer PUSH instruction does a post-decrement POP instruction does a pre-increment x 0 FC After Three More Pushes SP ////// x 0 FE After Two Pops TOP SP
• Stack is located in same memory as data – On atmega 328 the user can place the stack wherever, but 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 atmega 328 reference, page 12
• The SP (stack pointer) on the atmega 328 is actually two 8 -bit registers – SPH (stack pointer high byte) at I/O addr 0 x 3 E – SPL (stack pointer low byte) at I/O addr 0 x 3 D • 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 – How to detect? – Both of these are highly undesirable
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 atmega 328 P reference
• 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; … }
The stack is the Achilles’ heel of any general-purpose processor
- Slides: 11