120 likes | 272 Views
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.
E N D
CHAPTER 9TRAP 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. 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 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. S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu
TRAP Routines and their Assembler Names S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu
The Trap Vector Table x0020 x0400 x0021 x0430 x0022 x0450 x0023 x04A0 x0024 X04E0 x0025 XFD70 S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu
TRAP Mechanism Operation • Get starting address from the • Trap vector table. • 2. Transfer to service routine. • Return (JMP R7 or RET) to user • program. 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 tokeep typing it in • Divide task among multiple programmers • Use vendor-supplied library of useful routines 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] 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. 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. 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 internallythat 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 othersubroutine or service routine (TRAP). Otherwise, we won’t be able to return to the calling program. S. Barua – CPSC 240 sbarua@fullerton.edu http://sbarua.ecs.fullerton.edu