Chapter 9 Chapter 9 Subroutines and TRAPs l Privileged Instructions l TRAP Routines l Subroutines.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines.
Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
S. Barua – CPSC 240 CHAPTER 5 THE LC-3 Topics Memory organization Registers Instruction set Opcodes.
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.
CSS 372 Oct 2 nd - Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture.
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Chapter 6 Programming in Machine Language The LC-3 Simulator
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 System Calls.
S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Chapters 5 - The LC-3 LC-3 Computer Architecture Memory Map
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.
Overview Projects The Assembly Process Programmed I/O Interrupt Driven I/O.
Chapter 8 Overview Programmed I/O Introduction to Interrupt Driven I/O Project 3.
Chapter 8 I/O Programming Chapter 9 Trap Service Routines Programmed I/O Interrupts Interrupt Driven I/O Trap Service Routines.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Chapter 8 Overview Programmed I/O Interrupt Driven I/O.
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.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Purpose, scope, and expectations of the course Expectations & strategy.
Chapter 10 The Stack l Stack data structure l Interrupt I/O l Arithmetic using a stack.
Computer Science 210 Computer Organization The Instruction Execution Cycle.
The von Neumann Model – Chapter 4 COMP 2620 Dr. James Money COMP
Chapter 8 Input/Output l I/O basics l Keyboard input l Monitor output l Interrupt driven I/O l DMA.
System Calls 1.
Computer Science 210 Computer Organization Introduction to Subroutines.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 5 The LC-3 Instruction Set Architecture ISA Overview Operate instructions Data.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Chapter 9 TRAP Routines and Subroutines. 9-2 System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
The von Neumann Model – Chapter 4 COMP 2620 Dr. James Money COMP
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Chapter 9 TRAP Routines and Subroutines
Chapter 7 & 9.2 Assembly Language
Computer Science 210 Computer Organization
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Computer Science 210 Computer Organization
COSC121: Computer Systems: LC3 Traps and Subroutines
CS 3305 System Calls Lecture 7.
Chapter 8 Input/Output I/O basics Keyboard input Monitor output
Chapter 9 TRAP Routines and Subroutines
Arithmetic using a stack
The LC-3 Instruction Set Architecture Data Movement instructions
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Spring 2018 Midterm 1
Introduction to Computer Engineering
TRAP Routines Subroutines Privileged Instructions
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
TRAP Routines Privileged Instructions Subroutines
Chapter 9 TRAP Routines and Subroutines
School of Computer Science and Technology
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Presentation transcript:

Chapter 9 Chapter 9 Subroutines and TRAPs l Privileged Instructions l TRAP Routines l Subroutines

2 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Privileged Instructions l There are several instructions that are best executed by a supervisor program (OS) rather than a user program: –IO instructions –Loading of memory-mapped registers –Resetting the clock –Halt i.e. instructions where one program can affect the behavior of another. l The CPU can be designed to enforce two modes of operation: –User Mode –Privileged Mode (aka. supervisor, kernel, monitor mode) l Only the supervisor program (OS) can execute privileged instructions.

3 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly TRAP Instructions l TRAPs insulate critical tasks from the user – with or without privilege enforcement l The TRAP mechanism: – A set of trap service routines or TSRs (part of the CPU OS) l We have already seen the basic I/O SRs – A table of the starting addresses of these service routines l Located in a pre-defined block of memory … l … called the Trap Vector Table or System Control Block l In the LC-3: from x0000 to x00FF (only 5 currently in use) – The TRAP instruction l which loads the starting address of the TSR into the PC – Return link l from the end of the TSR back to the original program.

4 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly LC-3 TRAP Routines –GETC (TRAP x20) l Read a single character from KBD. l Write ASCII code to R0[7:0], clear R0[15:8]. –OUT (TRAP x21) l Write R0[7:0] to the monitor. –PUTS (TRAP x22) l Write a string to monitor (address of first character of string is in R0). –IN (TRAP x23) l Print a prompt to the monitor and read a single character from KBD. l Write ASCII code to R0[7:0], clear R0[15:8], echo character to the monitor. –HALT (TRAP x25) l Print message to monitor & halt execution. –PUTSP (TRAP x24) l Print packed string to monitor (address in R0)

5 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly TRAP Instructions l TRAP: A special instruction –A form of subroutine call used to invoke a service routine. –If privilege is being enforced, it switches the execution to privileged mode, and reverts back to user mode when the TSR completes. l R7  (PC) ; the current PC is stored in R7 l PC  Mem[ Zext( IR[7:0] ) ] ; the 8-bit trap vector is loaded to the PC l RET – return instruction –The TSR ends with the RET instruction l PC  (R7) ; the program now picks up where it left off trapvector8

6 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly TRAP Example l Trap Vector Table –Or System Control BLock –In LC-3 l 8 bits specify one of 256 locations (x0000 to x00FF) l The location contains the address of the TRAP service routine. l TRAP & Interrupts –Similar mechanisms –A TRAP is an instruction (event internal to a program). –An interrupt is external to a program (from an I/O device) –Both invoke a supervisor service routine.

7 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Character Output TSR (OUT) 01.ORIGX0430; System call starting address 02STR1, SaveR1; R1 will be used for polling 03 04; Write the character 05TryWriteLDIR1, DSR; Get status 06BRzpTryWrite; bit 15 = 1 => display ready 07WriteItSTIR0, DDR; Write character in R ; Return from TRAP 0AReturnLDR1, SaveR1; Restore registers 0BRET; Return (actually JMP R7) 0CDSR.FILLxFE04; display status register 0DDDR.FILLxFE06; display data register 0ESaveR1.BLKW1 0F.END ALSO 01.ORIG x FILL x0430

8 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly HALT TSR l Clears the RUN latch MCR[15]: 01.ORIGXFD70; System call starting address 02STR0, SaveR0; Saves registers affected 03STR1, SaveR1; by routine 04STR7, SaveR7; 05 06; Print message that machine is halting 07LDR0, ASCIINewLine 08TRAPx21; Set cursor to new line 09LEAR0, Message; Get start of message 0ATRAPx22; and write it to monitor 0BLDR0, ASCIINewLine 0CTRAPx21 0D 0E; Clear MCR[15] to stop the clock 0FLDIR1, MCR; Load MC register to R1 10LDR0, MASK; MASK = x7FFF (i.e. bit 15 = 0) 11ANDR0, R1, R0; Clear bit 15 of copy of MCR 12STIR0, MCR; and load it back to MCR

9 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly HALT TSR (cont.) 13; Return from the HALT routine 14; (how can this ever happen, if the clock is stopped on line 12??) 15; 16LDR7, SaveR7; Restores registers 17LDR1, SaveR1; before returning 18LDR0, SaveR0 19RET; JMP R7 1A 1B; constants 1CASCIINewLine.FILLx000A 1DSaveR0.BLKW 1 1ESaveR1.BLKW 1 1FSaveR7.BLKW 1 20Message.STRINGZ“Halting the machine” 21MCR.FILLxFFFE 22MASK.FILLx7FFF 23.END

10 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Saving & restoring registers l Protect your values! – Any subroutine call may change values currently stored in a register. – Sometimes the calling program (“caller”) knows what needs to be protected, so it saves the endangered register before calling the subroutine. l e.g. in the HALT routine, which has itself been called by another program, the caller knows that it has precious cargo in R7, which will be overwritten by the TRAP instructions (why??), so it saves R7 to memory at the start of the routine, and restores it from memory before returning to the main program. l This is known as “caller save”

11 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Saving & restoring registers (cont.) – Other times it will be the called program (“callee”) that knows what registers it will be using to carry out its task. l again in the HALT routine, R0 and R1 are used as temporary working space to hold addresses, masks, ASCII values, etc., so they are both saved to memory at the start of the routine, and restored from memory before returning to the main program. l This is known as “callee save” –This applies not only to trap service routines but to all subroutine calls, and is the basis of what are called “scope rules” in higher level languages.

12 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Subroutines l Used for – Frequently executed code segments – Library routines – Team-developed systems l in other words, all the same reasons for using subroutines in higher level languages, where they may be called functions, procedures, methods, etc. l Requirements: – Pass parameters and return values, via registers or memory. – Call from any point & return control to the same point.

13 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly The Call / Return mechanism l The figure illustrates the execution of a program comprising code fragments A, W, X, Y and Z. –Note that fragment A is repeated several times, and so is well suited for packaging as a subroutine:

14 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Jump to Subroutine : JSR/JSRR l A = IR[11] specifies the addressing mode l JSR: jump to subroutine (PC-Relative), IR[11] = 1 –R7  (PC) i.e. PC is saved to R7 –PC  (PC) + Sext( IR[10:0] )i.e PC-Relative addressing, –using 11 bits => label can be within / lines of JSR instruction l JSRR: jump to subroutine (relative base+offset), IR[11] = 0: –R7  (PC) i.e. PC is saved to R7 –PC  (BaseR)i.e Base+Offset addressing, with offset = JSR(R)A 1 Address eval. bits JSR(R)A 0 BaseR

15 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Subroutine call example ; Calling program.ORIG x3000 LD R1, num1 LD R2, num2 JSR multi STR3, prod HALT ; ; Input data & result num1.FILL x0006 num2.FILL x0003 prod.BLKW 1 ; Subroutine multi ; Multiply 2 positive numbers ; Parameters: ; In: R1, R2; Out: R3 ; multiANDR3, R3, #0 ADDR4, R1, #0 BRzzero loopADD R3, R2, R3 ADDR1, R1, # -1 BRploop zeroRET.END Notice any undesirable side-effects? ; Subroutine multi ; Multiply 2 positive numbers ; Parameters: ; In: R1, R2; Out: R3 ; multiANDR3, R3, #0 ADDR4, R1, #0 BRzzero loopADD R3, R2, R3 ADDR1, R1, # -1 BRploop zeroRET.END Notice any undesirable side-effects?

16 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Library Routines l Library –A set of routines for a specific domain application. –Example: math, graphics, GUI, etc. –Defined outside a program. l Library routine invocation –Labels for the routines are defined as external. In LC-3:.ExternalLabel –Each library routine contains its own symbol table. –A linker resolves the external addresses before creating the executable image.

17 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Linking multiple files

18 College of Charleston, School of Science and Math Dr. Anderson, Computer Science Department CS 250 Comp. Org. & Assembly Practice Problems l 9.2, 9.7, 9.10, 9.13, 9.15