Chapter 10 And, Finally... The Stack. 10-2 Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will.

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 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Chapter 10 And, Finally.... Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display A Final Collection of ISA-related.
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Chapter 5 The LC-3.
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.
Overview I/O – memory mapped programmed / interrupt driven Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
Chapter 9 Overview Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
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 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.
Choice for the rest of the semester New Plan –assembler and machine language –Operating systems Process scheduling Memory management File system Optimization.
Computer System Organization S H Srinivasan
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.
Chapters 9 & 10 Midterm next Wednesday (11/19) Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET The Stack SSP & USP Interrupts RTI.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
CSS 372 Oct 2 nd - Lecture 2 Review of CSS 371: Simple Computer Architecture Chapter 3 – Connecting Computer Components with Buses Typical Bus Structure.
Chapter 9 Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET.
Chapter 9 Trap Routines TRAP number (go to service routine) & RET (return from service routine) Subroutines (or Functions) JSR offset or JSRR rn (go to.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
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.
Chapter 10 The Stack l Stack data structure l Interrupt I/O l Arithmetic using a stack.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
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:
Machine Instruction Characteristics
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory 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.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
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
Introduction to Computing Systems and Programming The LC-2.
The Stack An Hong 2015 Fall School of Computer Science and Technology Lecture on Introduction to Computing Systems.
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.
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
The Stack.
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Chapter 10 And, Finally... The Stack
Chapter 9 TRAP Routines and Subroutines
Chapter 10 The Stack.
Chapter 5 The LC-3.
Arithmetic using a stack
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
HKN ECE 220: Spring 2018 Midterm 1
TRAP Routines Subroutines Privileged Instructions
Chapter 9 TRAP Routines and Subroutines
BIC 10503: COMPUTER ARCHITECTURE
Chapter 10 And, Finally... The Stack
Chapter 9 TRAP Routines and Subroutines
TRAP Routines Privileged Instructions Subroutines
Chapter 9 TRAP Routines and Subroutines
HKN ECE 220: Fall 2018 Midterm 1 Andrew Fortunat, Jeff Chang, Srijan Chakraborty, Kanad Sarkar February 16, 2019.
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Presentation transcript:

Chapter 10 And, Finally... The Stack

10-2 Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe four uses: Interrupt-Driven I/O  The rest of the story… Activation Records  Calling conventions in C (more in the second half) Evaluating arithmetic expressions  Store intermediate results on stack instead of in registers Data type conversion  2’s comp binary to ASCII strings

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

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

10-5 A Hardware Implementation Data items move between registers / / / 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 A Software Implementation Data items don't move in memory, just our idea about there 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 x4000x3FFFx3FFCx3FFE R6 By convention, R6 holds the Top of Stack (TOS) pointer. “High memory” (large address) “Low memory” (small address)

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 ; decrement 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 ; EMPTY = -x4000 ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x4000 LDR R0, R6, #0 ADD R6, R6, #1 AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET EMPTY.FILL xC000

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, MAX ; MAX = -x3FFB ADD R2, R6, R1 ; Compare stack pointer BRz FAIL ; with x3FFF ADD R6, R6, #-1 STR R0, R6, #0 AND R5, R5, #0 ; SUCCESS: R5 = 0 RET FAIL AND R5, R5, #0 ; FAIL: R5 = 1 ADD R5, R5, #1 RET MAX.FILL xC005

10-10 Interrupt-Driven I/O (Part 2) 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 state 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 Where to Save Processor State? Can’t use registers.  Programmer doesn’t know when interrupt might occur, so she can’t prepare by saving critical registers.  When resuming, need to restore state exactly as it was. Memory allocated by service routine?  Must save state before invoking routine, so we wouldn’t know where.  Also, interrupts may be nested – that is, an interrupt service routine might also get interrupted! Use a stack!  Location of stack “hard-wired”.  Push state to save, pop to restore.

10-13 Supervisor Stack A special region of memory used as the stack for interrupt service routines. Want to use R6 as stack pointer.  So that our PUSH/POP routines still work.  This means R6 is used for both the user and supervisor stacks. Plus two new registers for saving stack pointers  Supervisor Stack Pointer (SSP) stored in Saved.SSP.  Another register for storing User Stack Pointer (USP): Saved.USP. When switching from User mode to Supervisor mode (as result of interrupt), save R6 to Saved.USP.

10-14 Invoking the Service Routine – The Details 1.If Priv = 1 (user), Saved.USP = R6, then R6 = Saved.SSP. 2.Push PSR and PC to Supervisor Stack. 3.Set PSR[15] = 0 (supervisor mode). 4.Set PSR[10:8] = priority of interrupt being serviced. 5.Set PSR[2:0] = 0. [?] 6.Set MAR = x01vv, where vv = 8-bit interrupt vector provided by interrupting device (e.g., keyboard = x80). 7.Load memory location (M[x01vv]) into MDR. 8.Set PC = MDR; now first instruction of ISR will be fetched. See Fig. C.7 and C.8 in appendix C Note: This all happens between the STORE RESULT of the last user instruction and the FETCH of the first ISR 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) 3.If PSR[15] = 1, R6 = Saved.USP. (If going back to user mode, need to restore User Stack Pointer.) RTI is a privileged instruction.  Can only be executed in Supervisor 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. Saved.SSP

10-17 Example (2) / / / x3007 PSR for A / / / x6200 PC R6 Program A ADD x3006 Saved.USP = R6. R6 = Saved.SSP. Push PSR and PC onto stack, then transfer to Device B service routine (at x6200; better if it was below x3000). x6200 ISR for Device B x6210 RTI

10-18 Example (3) / / / x3007 PSR for A / / / x6203 PC R6 Program A ADD x3006 Executing AND at x6202 when Device C interrupts. x6200 ISR for Device B AND x6202 x6210 RTI

10-19 Example (4) / / / 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 Push PSR and PC onto stack, then transfer to Device C service routine (at x6300). x6300 x6315 RTI x6210 RTI

10-20 Example (5) / / / 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-21 Example (6) / / / 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 Saved.SSP

10-22 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 remains the same as running program

Preview of C: Stack Frames Major support issues posed by subroutines Linkage (how to get there and back) Passing parameters (where are they) Providing storage for local use (finding unique space for each invocation) An activation record is a memory template of fixed size, allocated atomically as part of invoking a subroutine It allocates space to save parameters It provides storage for variables defined in the subroutine It provides a place for saving the return path. A stack of activation records is an efficient way to address all three issues By convention, register R6 is used as the stack frame pointer. 23

10-24 Arithmetic Using a Stack Instead of registers, some ISA's use a stack for source and destination operations: a zero-address machine.  Example: ADD instruction pops two numbers from the stack, adds them, and pushes the result to the stack. Evaluating (A+B)·(C+D) using a stack: (1) push A (2) push B (3) ADD (4) push C (5) push D (6) ADD (7) MULTIPLY (8) pop result Why use a stack? Limited registers. Convenient calling convention for subroutines. Algorithm naturally expressed using FIFO data structure.

10-25 Example: OpAdd POP two values, ADD, then PUSH result.

10-26 Example: OpAdd OpAddJSR POP; Get first operand. ADD R5,R5,#0; Check for POP success. BRp Exit; If error, bail. ADD R1,R0,#0; Make room for second. JSR POP; Get second operand. ADD R5,R5,#0; Check for POP success. BRp Restore1; If err, restore & bail. ADD R0,R0,R1; Compute sum. JSR RangeCheck; Check size. BRp Restore2; If err, restore & bail. JSR PUSH; Push sum onto stack. RET Restore2 ADD R6,R6,#-1 ; Decr stack ptr (undo POP) Restore1 ADD R6,R6,#-1 ; Decr stack ptr ExitRET

10-27 Data Type Conversion Keyboard input routines read ASCII characters, not binary values. Similarly, output routines write ASCII. Consider this program: TRAP x23 ; input from keybd ADD R1, R0, #0 ; move to R1 TRAP x23 ; input from keybd ADD R0, R1, R0 ; add two inputs TRAP x21 ; display result TRAP x25 ; HALT User inputs 2 and 3 -- what happens? Result displayed: e Why? ASCII '2' (x32) + ASCII '3' (x33) = ASCII 'e' (x65)

10-28 ASCII to Binary Useful to deal with mult-digit decimal numbers Assume we've read three ASCII digits (e.g., "259") into a memory buffer. How do we convert this to a number we can use?  Convert first character to digit (subtract x30) and multiply by 100.  Convert second character to digit and multiply by 10.  Convert third character to digit.  Add the three digits together. x32 x35 x39 '2' '5' '9'

10-29 Multiplication via a Lookup Table How can we multiply a number by 100?  One approach: Add number to itself 100 times.  Another approach: Add 100 to itself times. (Better if number < 100.) Since we have a small range of numbers (0-9), use number as an index into a lookup table. Entry 0: 0 x 100 = 0 Entry 1: 1 x 100 = 100 Entry 2: 2 x 100 = 200 Entry 3: 3 x 100 = 300 etc.

10-30 Code for Lookup Table ; multiply R0 by 100, using lookup table ; LEA R1, Lookup100 ; R1 = table base ADD R1, R1, R0 ; add index (R0) LDR R0, R1, #0 ; load from M[R1]... Lookup100.FILL 0 ; entry 0.FILL 100 ; entry 1.FILL 200 ; entry 2.FILL 300 ; entry 3.FILL 400 ; entry 4.FILL 500 ; entry 5.FILL 600 ; entry 6.FILL 700 ; entry 7.FILL 800 ; entry 8.FILL 900 ; entry 9

10-31 Complete Conversion Routine (1 of 3) ; Three-digit buffer at ASCIIBUF. ; R1 tells how many digits to convert. ; Put resulting decimal number in R0. ASCIItoBinary AND R0, R0, #0 ; clear result ADD R1, R1, #0 ; test # digits BRz DoneAtoB ; done if no digits ; LD R3, NegZero ; R3 = -x30 LEA R2, ASCIIBUF ADD R2, R2, R1 ADD R2, R2, #-1; points to ones digit ; LDR R4, R2, #0 ; load digit ADD R4, R4, R3 ; convert to number ADD R0, R0, R4 ; add ones contrib

10-32 Conversion Routine (2 of 3) ADD R1, R1, #-1 ; one less digit BRz DoneAtoB ; done if zero ADD R2, R2, #-1; points to tens digit ; LDR R4, R2, #0 ; load digit ADD R4, R4, R3 ; convert to number LEA R5, Lookup10 ; multiply by 10 ADD R5, R5, R4 LDR R4, R5, #0 ADD R0, R0, R4 ; adds tens contrib ; ADD R1, R1, #-1 ; one less digit BRz DoneAtoB ; done if zero ADD R2, R2, #-1 ; points to hundreds ; digit

10-33 Conversion Routine (3 of 3) LDR R4, R2, #0 ; load digit ADD R4, R4, R3 ; convert to number LEA R5, Lookup100 ; multiply by 100 ADD R5, R5, R4 LDR R4, R5, #0 ADD R0, R0, R4 ; adds 100's contrib ; DoneAtoB RET NegZero.FILL xFFD0 ; -x30 ASCIIBUF.BLKW 4 Lookup10.FILL 0.FILL 10.FILL Lookup100.FILL 0.FILL

10-34 Binary to ASCII Conversion Converting a 2's complement binary value to a three-digit decimal number  Resulting characters can be output using OUT Instead of multiplying, we need to divide by 100 to get hundreds digit.  Why wouldn't we use a lookup table for this problem?  Subtract 100 repeatedly from number to divide. First, check whether number is negative.  Write sign character (+ or -) to buffer and make positive.

10-35 Binary to ASCII Conversion Code (part 1 of 3) ; R0 is between -999 and ; Put sign character in ASCIIBUF, followed by three ; ASCII digit characters. BinaryToASCII LEA R1, ASCIIBUF ; pt to result string ADD R0, R0, #0 ; test sign of value BRn NegSign LD R2, ASCIIplus ; store '+' STR R2, R1, #0 BRnzp Begin100 NegSign LD R2, ASCIIneg ; store '-' STR R2, R1, #0 NOT R0, R0 ; convert value to pos ADD R0, R0, #1

10-36 Conversion (2 of 3) Begin100 LD R2, ASCIIoffset LD R3, Neg100 Loop100 ADD R0, R0, R3 BRn End100 ADD R2, R2, #1 ; add one to digit BRnzp Loop100 End100 STR R2, R1, #1 ; store ASCII 100's digit LD R3, Pos100 ADD R0, R0, R3 ; restore last subtract ; LD R2, ASCIIoffset LD R3, Neg10 Loop100 ADD R0, R0, R3 BRn End10 ADD R2, R2, #1 ; add one to digit BRnzp Loop10

10-37 Conversion Code (3 of 3) End10 STR R2, R1, #2 ; store ASCII 10's digit ADD R0, R0, #10 ; restore last subtract ; LD R2, ASCIIoffset ADD R2, R2, R0 ; convert one's digit STR R2, R1, #3 ; store one's digit RET ; ASCIIplus.FILL x2B ; plus sign ASCIIneg.FILL x2D ; neg sign ASCIIoffset.FILL x30 ; zero Neg100.FILL xFF9C ; -100 Pos100.FILL #100 Neg10.FILL xFFF6 ; -10