Chapter 10 The Stack. 10-2 Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:

Slides:



Advertisements
Similar presentations
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stack: An Abstract.
Advertisements

Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
CPU Review and Programming Models CT101 – Computing Systems.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
1/1/ / faculty of Electrical Engineering eindhoven university of technology Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir.
A look at interrupts What are interrupts and why are they needed.
CSCI 4717/5717 Computer Architecture
A look at interrupts What are interrupts and why are they needed in an embedded system? Equally as important – how are these ideas handled on the Blackfin.
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.
1 Computer System Overview OS-1 Course AA
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Chapter 7 Interupts DMA Channels Context Switching.
Chapter 6 Programming in Machine Language The LC-3 Simulator
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Computer System Organization S H Srinivasan
A look at interrupts What are interrupts and why are they needed.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Midterm Tuesday October 23 Covers Chapters 3 through 6 - Buses, Clocks, Timing, Edge Triggering, Level Triggering - Cache Memory Systems - Internal Memory.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
Overview von Neumann Model Components of a Computer Some Computer Organization Models The Computer Bus An Example Organization: The LC-3.
Midterm Wednesday 11/19 Overview: 25% First Midterm material - Number/character representation and conversion, number arithmetic - DeMorgan’s Law, Combinational.
COMPUTER SYSTEMS An Integrated Approach to Architecture and Operating Systems Chapter 4 Processor Implementation ©Copyright 2008 Umakishore Ramachandran.
Chapter 10 The Stack l Stack data structure l Interrupt I/O l Arithmetic using a stack.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
Embedded Systems 7763B Mt Druitt College of TAFE
3/11/2002CSE Input/Output Input/Output Control Datapath Memory Processor Input Output Memory Input Output Network Control Datapath Processor.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 And, Finally... The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
8.3 Stack Organization Stack: A storage device that stores information in such a manner that the item stored last is the first item retrieved. Also called.
Chapter 8 I/O. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-2 I/O: Connecting to Outside World So far,
The Stack This is a special data structure: –The first item to be placed into the stack will be the last item taken out. Two basic operations: –Push: Places.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
1 Memory and Data Structures Patt and Patel Ch. 10 & 16.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Computer Organization 1 Instruction Fetch and Execute.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
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.
Structure and Role of a Processor
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
The Stack An Hong 2015 Fall School of Computer Science and Technology Lecture on Introduction to Computing Systems.
1 Chapter 1 Basic Structures Of Computers. Computer : Introduction A computer is an electronic machine,devised for performing calculations and controlling.
Chapter 10 And, Finally... The Stack
Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir. A.C. Verschueren Eindhoven University of Technology Section of Digital.
Interrupts and exceptions
ECE 3430 – Intro to Microcomputer Systems
Timer and Interrupts.
The Stack.
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Chapter 10 And, Finally... The Stack
Overview Introduction General Register Organization Stack Organization
Chapter 10 The Stack.
Arithmetic using a stack
HKN ECE 220: Spring 2018 Midterm 1
Chapter 8 Central Processing Unit
BIC 10503: COMPUTER ARCHITECTURE
Chapter 10 And, Finally... The Stack
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Interrupts and exceptions
Presentation transcript:

Chapter 10 The Stack

10-2 Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses: Interrupt-Driven I/O The rest of the story… Evaluating arithmetic expressions Store intermediate results on stack instead of in registers

10-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. Many different implementations satisfy the above definition. Two main operations: PUSH: add an item to the stack POP: remove an item from the stack

10-4 A Physical Stack Coin rest in the arm of an automobile First quarter out is the last quarter in Initial StateAfter One Push After Three More Pushes After One Pop

10-5 A Hardware Implementation / / / Yes Empty: TOP #18 / / / No Empty: TOP #12 #5 #31 #18 / / / No Empty: TOP #31 #18 / / / No Empty: TOP Initial StateAfter One Push After Three More Pushes After Two Pops

10-6 An LC-3 Software Implementation Data items don't move in memory, just our idea about where the TOP of the stack is. / / / TOP / / / #18 / / / TOP #12 #5 #31 #18 / / / TOP #12 #5 #31 #18 / / / TOP Initial StateAfter One Push After Three More Pushes After Two Pops x3000x2FFFx2FFCx2FFE R6 R6 is used to hold the Top of Stack (TOS) pointer.

10-7 Basic Push and Pop Code For our implementation, stack grows downward (when item added, TOS moves closer to 0) Push ADD R6, R6, #-1 ; decrement stack ptr STR R0, R6, #0 ; store data (R0) Pop LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; increment stack ptr

10-8 Pop with Underflow Detection If we try to pop too many items off the stack, an underflow condition occurs. Check for underflow by checking TOS before removing data. Return status code in R5 (0 for success, 1 for underflow) POP LD R1, EMPTY ; Value of TOS when stack EMPTY ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x3000 LDR R0, R6, #0 ; Load the data ADD R6, R6, #1 ; Increment stack pointer AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET EMPTY.FILL x-3000

10-9 Push with Overflow Detection If we try to push too many items onto the stack, an overflow condition occurs. Check for overflow by checking TOS before adding data. Return status code in R5 (0 for success, 1 for overflow) PUSH LD R1, FULL ; Value of TOS when stack FULL ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x2000 ADD R6, R6, #-1 ; decrement stack pointer STR R0, R6, #0 ; store the data AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET FULL.FILL x-2000

10-10 Interrupt Service Routine (ISR) Interrupts were introduced in Chapter 8. 1.External device signals need to be serviced. 2.Processor saves state and starts service routine. 3.When finished, processor restores state and resumes program. Chapter 8 didn’t explain how (2) and (3) occur, because it involves a stack. Now, we’re ready… Interrupt is an unscripted subroutine call, triggered by an external event.

10-11 Processor State What is needed to completely capture the state of a running process? Processor Status Register Privilege [15], Priority Level [10:8], Condition Codes [2:0] Program Counter Pointer to next instruction to be executed. Registers All temporary state of the process that’s not stored in memory.

10-12 Save State. How? Where? Who? Processor Status Register It’s not addressable (no instruction to read it). Program Counter We have no idea when interrupt might occur. Can the ISR save the PC? Registers Can the ISR save registers? So PSR and PC must be saved by HW during an interrupt But where? –In a special location in memory? Well, sort of, but… Interrupts may be nested – an ISR can be interrupted! Solution: Use a stack! Location of stack “hard-wired” – memory set aside for this. Push state to save, pop to restore. Performed automatically when ISR called.

10-13 Supervisor Stack A special region of memory used as the stack for interrupt service routines. Manually set this for the LC-3, using R6 as stack pointer Use R6 as stack pointer. So that our PUSH/POP routines (from before) still work.

10-14 Invoking the Service Routine – The Details 1.Push PSR and PC to Supervisor Stack. 2.Set PSR[15] = 0 (supervisor (aka privilege) mode). 3.Set PSR[10:8] = priority of interrupt being serviced. 4.Set PSR[2:0] = 0. 5.Set MAR = x01vv, where vv = 8-bit interrupt vector provided by interrupting device (e.g., keyboard = x80). 6.Load memory location (M[x01vv]) into MDR. 7.Set PC = MDR; now first instruction of ISR will be fetched. Note: This all happens (automatically) between the STORE RESULT of the last user instruction and the FETCH of the first ISR instruction. Cannot happen in the middle of an instruction

10-15 Returning from Interrupt Special instruction – RTI – that restores state. 1.Pop PC from supervisor stack. (PC = M[R6]; R6 = R6 + 1) 2.Pop PSR from supervisor stack. (PSR = M[R6]; R6 = R6 + 1) (Both done automatically) RTI is a privileged instruction. Can only be executed in Supervisor/Privilege Mode. If executed in User Mode, causes an exception. (More about that later.)

10-16 Example (1) / / / x3006 PC Program A ADD x3006 Executing ADD at location x3006 when Device B interrupts. R6

10-17 Example (2) / / / x3007 PSR for A / / / x6200 PC R6 Program A ADD x3006 Push PSR and PC onto stack, then transfer to Device B service routine (at x6200). x6200 ISR for Device B x6210 RTI AND x6202

10-18 Example (3) / / / x3007 PSR for A x6203 PSR for B x6300 PC R6 Program A ADD x3006 x6200 ISR for Device B AND x6202 ISR for Device C Executing AND at x6202 when Device C interrupts. Push PSR and PC onto stack, then transfer to Device C service routine (at x6300). x6300 x6315 RTI x6210 RTI

10-19 Example (4) / / / x3007 PSR for A x6203 PSR for B x6203 PC R6 Program A ADD x3006 x6200 ISR for Device B AND x6202 ISR for Device C Execute RTI at x6315; pop PC and PSR from stack. x6300 x6315 RTI x6210 RTI

10-20 Example (5) / / / x3007 PSR for A x6203 PSR for B x3007 PC Program A ADD x3006 x6200 ISR for Device B AND x6202 ISR for Device C Execute RTI at x6210; pop PSR and PC from stack. Restore R6. Continue Program A as if nothing happened. x6300 x6315 RTI x6210 RTI R6

10-21 Exception: Internal Interrupt When something unexpected happens inside the processor, it may cause an exception. Examples: Privileged operation (e.g., RTI in user mode) Executing an illegal opcode Divide by zero Accessing an illegal address (e.g., protected system memory) Handled just like an interrupt Vector is determined internally by type of exception Priority is the same as running program

10-22 Another use for a stack: Arithmetic Reverse Polish Notation (RPN) Calculators Instead of registers, some ISA's use a stack for source and destination operations: a zero-address machine. Operations: Push – place something on the top of the stack Pop – remove item on the top of the stack Arithmetic Operations – pop two items from the stack perform operation with those two items push result onto stack

10-23 Comparison – Registers vs. Stack-Based Write E = (A+B)*(C+D) using General Purpose Registers (Assume A, B, C, D, and E are labels for a memory location.)

10-24 Comparison – Registers vs. Stack-Based Write E = (A+B)*(C+D) using a stack:

Comparison – Registers vs. Stack-Based Which instructions are larger? Which requires more instructions? Which requires more memory operations? Why use a stack? Computer has few registers (registers were once expensive) Simple programming model and ISA Java’s machine model is a stack. Reality: stack is stored on-chip in cache 10-25