Unformatted text preview:

1LC-3 TRAP RoutinesTextbook Chapter 9Summer 2008CMPE12 – Summer 2008 – Slides by ADB 2System Calls Certain operations require specialized knowledgeand protection: specific knowledge of I/O device registersand the sequence of operations needed to use them I/O resources shared among multiple users/programs;a mistake could affect lots of other users! Not every programmer knows (or wants to know)this level of detail Provide service routines or system calls(part of operating system) to safely and convenientlyperform low-level, privileged operations2CMPE12 – Summer 2008 – Slides by ADB 3System Call 1. User program invokes system call 2. Operating system code performs operation 3. Returns control to user program In LC-3, this is done through the TRAP mechanismCMPE12 – Summer 2008 – Slides by ADB 4LC-3 TRAP Mechanism 1. A set of service routines A part of operating system -- routines start at arbitrary addresses  Convention is that system code is below x3000 Up to 256 routines 2. Table of starting addresses 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 link back to the user program Want execution to resume immediately after the TRAPinstruction3CMPE12 – Summer 2008 – Slides by ADB 5Memory map1. User program executes TRAP: load indirect address of TRAP routine code2. Execute TRAP routine and RETurn to instruction following the TRAP in user programCMPE12 – Summer 2008 – Slides by ADB 6TRAPTrap vectoridentifies which system call to invoke8-bit index into table of service routine addresses in LC-3, this table is stored in memory at 0x0000 –0x00FF 8-bit trap vector is zero-extended into 16-bit memory addressWhere to golookup starting address from table; place in PCHow to get backsave address of next instruction (current PC) in R74CMPE12 – Summer 2008 – Slides by ADB 7TRAPCMPE12 – Summer 2008 – Slides by ADB 8Return: RET (JMP R7) How do we transfer control back toinstruction following the TRAP? We saved old PC in R7 JMP R7 gets us back to the user program at the right spot LC-3 assembly language lets us use RET(return) in place of JMP R7 Must make sure that service routine does not change R7, or we won’t know where to return.5CMPE12 – Summer 2008 – Slides by ADB 9Example of TRAP InstructionWhat does this program do?.ORIG x3000LD R2, TERM ; LD R3, ASCII ; AGAIN TRAP x23 ; Input characterADD R1, R2, R0 ; BRz EXIT ; ADD R0, R0, R3 ; Change to lowercaseTRAP x21 ; Output to monitor...BRnzp AGAIN ; TERM .FILL xFFC9 ; -‘7’ASCII .FILL x0020 ; lowercase bitEXIT TRAP x25 ; halt.ENDCMPE12 – Summer 2008 – Slides by ADB 10Output Service Routinestored in table,location x21.ORIG x0430 ; syscall addressST R7, SaveR7 ; save R7 & R1ST R1, SaveR1; ----- Write characterTryWrite LDI R1, CRTSR ; get statusBRzp TryWrite ; look for bit 15 onWriteIt STI R0, CRTDR ; write char; ----- Return from TRAPReturn LD R1, SaveR1 ; restore R1 & R7LD R7, SaveR7RET ; back to userCRTSR .FILL xFE04CRTDR .FILL xFE06SaveR1 .FILL 0SaveR7 .FILL 0.END6CMPE12 – Summer 2008 – Slides by ADB 11TRAP Mechanism Operation1. Lookup starting address.2. Transfer to service routine.3. Return (JMP R7).CMPE12 – Summer 2008 – Slides by ADB 12TRAP Routines and their Assembler Nameswrite a string to the consolePUTSx22print prompt to console,read and echo character from keyboardINx23read a single character (no echo)GETCx20x25x21vectorhalt the programHALToutput a character to the monitorOUTroutinesymbol7CMPE12 – Summer 2008 – Slides by ADB 13Saving and Restoring Registers Must save the value of a register if: Its value will be destroyed by the service routine, and The value will be used after that action Who saves? Caller- vs callee-saved The caller of the service routine? Knows what it needs later, but may not know what gets altered by called routine The called service routine (callee)? Knows what it alters, but does not know what will be needed later by calling routineCMPE12 – Summer 2008 – Slides by ADB 14ExampleWhat’s wrong with this routine?LEA R3, BinryLD R6, ASCII ; char->digit templateLD R7, COUNT ; initialize to 10AGAIN TRAP x23 ; Get charADD R0, R0, R6 ; convert to numberSTR R0, R3, #0 ; store numberADD R3, R3, #1 ; incr pointerADD R7, R7, -1 ; decr counterBRp AGAIN ; more?BRnzp NEXTASCII .FILL xFFD0COUNT .FILL #10Binry .BLKW #108CMPE12 – Summer 2008 – Slides by ADB 15Saving and Restoring Registers Called routine -- “callee-save” Before start, save any registers that will be altered(unless altered value is desired by calling program!) Before return, restore those same registers Calling routine -- “caller-save” Save registers destroyed by own instructions orby called routines (if known), if values needed later Save R7 before TRAP Save R0 before TRAP x23 (input character) Or avoid using those registers altogether Values are saved by storing them in memoryCMPE12 – Summer 2008 – Slides by ADB 16Recommended exercises Ex 9.2, 9.4, 9.5, 9.11 Ex 9.17,


View Full Document

UCSC CMPE 012 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?