Unformatted text preview:

System Calls (TRAPS) and SubroutinesSystem CallsSystem CallLC-3 TRAP MechanismTRAP InstructionTRAPRET (JMP R7)TRAP Mechanism OperationExample: Using the TRAP InstructionExample: Output Service RoutineTRAP Routines and their Assembler NamesSaving and Restoring RegistersExampleSlide 14QuestionWhat about User Code?SubroutinesJSR InstructionJSRJSRR InstructionJSRRReturning from a SubroutineExample: Negate the value in R0Passing Information to/from SubroutinesUsing SubroutinesSaving and Restore RegistersSlide 27CountChar Algorithm (using FirstChar)CountChar ImplementationFirstChar AlgorithmFirstChar ImplementationLibrary RoutinesQuestions?Slide 341System Calls (TRAPS) and System Calls (TRAPS) and Subroutines Subroutines Patt and Patel Ch. 9Patt and Patel Ch. 92System CallsSystem CallsCertain operations require Certain operations require specialized knowledge specialized knowledge and and protectionprotection::–specific knowledge of I/O device registers and 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) Not every programmer knows (or wants to know) this level of detailthis level of detailProvide Provide service routinesservice routines oror system callssystem calls (part of (part of operating system) to safely and conveniently operating system) to safely and conveniently perform low-level, perform low-level, privilegedprivileged operations operations3System CallSystem Call1. User program invokes system call.1. User program invokes system call.2. Operating system code performs 2. Operating system code performs operation.operation.3. Returns control to user program.3. Returns control to user program.In LC-3, this is done through the TRAP mechanism.4LC-3 TRAP MechanismLC-3 TRAP Mechanism1. A set of service routines.1. A set of service routines.–part of operating system -- routines start at arbitrary addresses(convention is that system code is below x3000)–up to 256 routines2. Table of starting addresses.2. Table of starting addresses.–stored at x0000 through x00FF in memory–called System Control Block in some architectures3. TRAP instruction.3. TRAP instruction.–used by program to transfer control to operating system–8-bit trap vector names one of the 256 service routines4. A linkage back to the user program.4. A linkage back to the user program.–want execution to resume immediately after the TRAP instruction5TRAP InstructionTRAP Instruction•Trap vectorTrap 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 goWhere to go–lookup starting address from table; place in PC•How to get backHow to get back–save address of next instruction (current PC) in R76TRAPTRAPNOTE: PC has already been incremented during instruction fetch stage.7RET (JMP R7)RET (JMP R7)How do we transfer control back to instruction How do we transfer control back to instruction following the TRAP?following the TRAP?•We saved old PC in R7.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 Must make sure that service routine does not change R7, or we won’t know where to return.change R7, or we won’t know where to return.8TRAP Mechanism TRAP Mechanism OperationOperation1. Lookup starting address.2. Transfer to service routine.3. Return (JMP R7).9Example: Using the TRAP Example: Using the TRAP InstructionInstruction.ORIG x3000.ORIG x3000LDLDR2, TERMR2, TERM ; ; Load negative ASCII ‘7’Load negative ASCII ‘7’LDLDR3, ASCIIR3, ASCII ; ; Load ASCII differenceLoad ASCII differenceAGAINAGAINTRAPTRAP x23 x23 ; ; input characterinput characterADDADDR1, R2, R0R1, R2, R0 ; ; Test for terminateTest for terminateBRzBRzEXITEXIT ; ; Exit if doneExit if doneADDADDR0, R0, R3R0, R0, R3 ; ; Change to lowercaseChange to lowercaseTRAPTRAP x21 x21 ; ; Output to monitor...Output to monitor...BRnzp AGAINBRnzp AGAIN ; ; ... again and again...... again and again...TERMTERM.FILL.FILL xFFC9 xFFC9 ; ; -‘7’-‘7’ASCIIASCII.FILL.FILL x0020 x0020 ; ; lowercase bitlowercase bitEXITEXITTRAPTRAP x25 x25 ; ; halthalt.END.END10Example: Output Service RoutineExample: Output Service Routine.ORIG x0430.ORIG x0430; ; syscall addresssyscall addressSTSTR7, SaveR7R7, SaveR7; ; save R7 & R1save R7 & R1STSTR1, SaveR1R1, SaveR1; ; ----- Write character----- Write characterTryWriteTryWriteLDILDIR1, CRTSRR1, CRTSR; ; get statusget statusBRzpBRzpTryWriteTryWrite; ; look for bit 15 onlook for bit 15 onWriteItWriteItSTISTIR0, CRTDRR0, CRTDR; ; write charwrite char; ; ----- Return from TRAP----- Return from TRAPReturnReturnLDLDR1, SaveR1R1, SaveR1; ; restore R1 & R7restore R1 & R7LDLDR7, SaveR7R7, SaveR7RETRET; ; back to userback to userCRTSRCRTSR.FILL.FILLxF3FCxF3FCCRTDRCRTDR.FILL.FILLxF3FFxF3FFSaveR1SaveR1.FILL.FILL00SaveR7SaveR7.FILL.FILL00.END.ENDstored in table,location x2111TRAP Routines and their Assembler TRAP Routines and their Assembler NamesNamesvectorvectorsymbosymbollroutineroutinex20x20GETCGETCread a single character (no read a single character (no echo)echo)x21x21OUTOUToutput a character to the output a character to the monitormonitorx22x22PUTSPUTSwrite a string to the consolewrite a string to the consolex23x23ININprint prompt to console,read print prompt to console,read and echo character from and echo character from keyboardkeyboardx25x25HALTHALThalt the programhalt the program12Saving and Restoring RegistersSaving and Restoring RegistersMust save the value of a register if:Must save the value of a register if:–Its value will be destroyed by service routine, and–We will need to use the value after that action.Who saves?Who saves?–caller of service routine?•knows what it needs later, but may not know what gets altered by called routine–called service routine?•knows what it alters, but does not know what will be needed later by calling routine13ExampleExampleLEALEAR3, BinaryR3, BinaryLDLDR6, ASCIIR6, ASCII ; char->digit template ; char->digit templateLDLDR7, COUNTR7, COUNT ; initialize to 10 ; initialize to 10AGAINAGAINTRAPTRAPx23x23 ; Get char ; Get charADDADDR0, R0, R6R0, R0, R6 ; convert to number ; convert to


View Full Document

UCSC CMPE 012 - System Calls and Subroutines

Download System Calls and Subroutines
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 System Calls and Subroutines 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 System Calls and Subroutines 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?