Presentation is loading. Please wait.

Presentation is loading. Please wait.

S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program.

Similar presentations


Presentation on theme: "S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program."— Presentation transcript:

1 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program to request the operating system to perform certain tasks on behalf of the user program. The request made by the user program is often referred to as a service call or a system call.

2 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu LC-3 TRAP Mechanism 1. A set of service routines Part of operating system Routines start at arbitrary addresses Up to 256 routines 2. Table of starting addresses – Trap Vector Table Stored at x0000 through x00FF in memory Called System Control Block in some architectures 3. TRAP instruction Used by program to transfer control to operating system 8-bit trap vector names one of the 256 service routines 4. A linkage back to the user program Want execution to resume immediately after the TRAP instruction

3 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu TRAP Instruction Trap vector Identifies which system call to invoke 8-bit trap vector is zero-extended to 16 bits to form a memory address. The address points to an entry in the Trap Vector Table In LC-3, this table is stored in memory at 0x0000 – 0x00FF Where to go Lookup starting address from the Trap Vector Table Load the address into PC How to get back Save the return address (address of next instruction) in R7. The RET or JMP R7 instruction of LC-3 allows us to do this.

4 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu TRAP Routines and their Assembler Names vectorsymbolroutine x20GETC read a single character (no echo) x21OUT output a character to the monitor x22PUTS write a string to the console x23IN print prompt to console, read and echo character from keyboard x25HALT halt the program

5 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu The Trap Vector Table x0020x0400 x0021x0430 x0022x0450 x0023x04A0 x0024X04E0 x0025XFD70

6 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu TRAP Mechanism Operation 1.Get starting address from the Trap vector table. 2.Transfer to service routine. 3.Return (JMP R7 or RET) to user program.

7 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Subroutines A subroutine is a program fragment that: Part of user space Performs a well-defined task Is invoked (called) by another user program Returns control to the calling program when finished Like a service routine, but not part of the OS Reasons for subroutines: Reuse useful (and debugged!) code without having to keep typing it in Divide task among multiple programmers Use vendor-supplied library of useful routines

8 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Subroutine Call - JSR Instruction Saves current PC (address of next instruction) in R7 and jumps to the location specified by the target address. Saving the return address is called “linking” Bit 11 specifies addressing mode If Bit 11=1, then PC-relative addressing Target address = PC + Sext(IR[10:0]) If Bit 11=0, then register addressing Target address = contents of register specified by IR[8:6]

9 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Returning from a Subroutine The LC-3 instruction RET (JMP R7) gets us back to the calling routine.

10 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Passing Information to/from Subroutines Arguments A value passed into a subroutine is called an argument. This is a value needed by the subroutine to do its job. Return Values A value passed out of a subroutine is called a return value. This is the value that the subroutine computes.

11 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu Saving and Restoring Registers In subroutines and service routines, we need to save and restore registers, if needed. Subroutines generally use “callee-save” strategy, except for return values. Save anything that the subroutine will alter internally that shouldn’t be visible when the subroutine returns. It’s good practice to restore incoming arguments to their original values (unless overwritten by return value). Remember: We MUST save R7 if we call any other subroutine or service routine (TRAP). Otherwise, we won’t be able to return to the calling program.


Download ppt "S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program."

Similar presentations


Ads by Google