The Stack.

Slides:



Advertisements
Similar presentations
Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Advertisements

Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
Eng. Mohammed Timraz Electronics & Communication Engineer University of Palestine Faculty of Engineering and Urban planning Software Engineering Department.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
Chapter 10 The Stack l Stack data structure l Interrupt I/O l Arithmetic using a stack.
Embedded Systems 7763B Mt Druitt College of TAFE
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
© 2010 Kettering University, All rights reserved..
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Stacks & Subroutines Content. Stacks A stack is a Last In First Out (LIFO) buffer containing a number of data items usually implemented as a block of.
CE-2810 Dr. Mark L. Hornick 1 Mixing C and assembly Safety goggles on!
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Interrupt I/O (again!) Arithmetic using a stack.
PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter.
1 Stack Advanced Programming. 2 The Stack It is a special area of memory used as temporary storage A stack is a LIFO data structure Putting data into.
Multiple data transfer instructions ARM also supports multiple loads and stores: When the data to be copied to the stack is known to be a multiple of 4.
Chapter 10 And, Finally... The Stack
Stack Operations Dr. Hadi AL Saadi.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Overview of Instruction Set Architectures
ECE 3430 – Intro to Microcomputer Systems
Protection of System Resources
Introduction of microprocessor
Subroutines and the Stack
Chapter 10 And, Finally... The Stack
Overview Introduction General Register Organization Stack Organization
Computer Organization and Design
In this lecture Global variables Local variables System stack
Chapter 10 The Stack.
Arithmetic using a stack
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Subroutines and the Stack
The University of Adelaide, School of Computer Science
Chapter 10 And, Finally... The Stack
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
Program and memory layout
Program and memory layout
Subroutines and the Stack
COMP3221: Microprocessors and Embedded Systems
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Computer Organization and Assembly Language
Computer Operation 6/22/2019.
Presentation transcript:

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 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

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

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

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

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

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

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