DOC PREVIEW
UCSC CMPE 012 - System Calls and Subroutines

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMPE12 Cyrus BazeghiSystem Calls (TRAPS) and SubroutinesCh. 9CMPE12 Cyrus Bazeghi2System CallsCertain operations require specialized knowledge and protection:– 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) this level of detailProvide service routinesorsystem calls(part of operating system) to safely and conveniently perform low-level, privileged operationsCMPE12 Cyrus Bazeghi3System Call (service routines)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 theTRAP mechanism.CMPE12 Cyrus Bazeghi4LC-3 TRAP Mechanism1. A set of service routines.– part of operating system -- routines start at arbitrary addresses– System code by convention is typically below address x3000– up to 256 routines2. Table of starting addresses.– stored at x0000 through x00FF in memory– called “System Control Block” or “Vector Table” in some architectures3. TRAP instruction.– used by user 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.– want execution to resume immediately after the TRAP instructionCMPE12 Cyrus Bazeghi5TRAP Instruction•Trap vector (trapvect8)– 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– saves address of next instruction (current PC) in R7 before changing PCCMPE12 Cyrus Bazeghi6TRAPNOTE: PC has already been incremented during instruction fetch stage.CMPE12 Cyrus Bazeghi7RET (JMP R7)How do we transfer control back to instruction following the TRAP?•Save 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 it won’t know where to return.CMPE12 Cyrus Bazeghi8TRAP Mechanism Operation1. Lookup starting address.2. Transfer to service routine.3. Return (JMP R7).CMPE12 Cyrus Bazeghi9Example: Using the TRAP Instruction; This code just takes upper case characters and converts ; to lower case and prints them. Terminates with a “7” .ORIG x3000LD R2, TERM ; Load negative ASCII ‘7’LD R3, ASCII ; Load ASCII differenceAGAIN TRAP x23 ; input characterADD R1, R2, R0 ; Test for terminate: =7?BRz EXIT ; Exit if doneADD R0, R0, R3 ; Change to lowercaseTRAP x21 ; Output to monitor...BRnzp AGAIN ; ... again and again...TERM .FILL xFFC9 ; -„7‟in 2SCASCII .FILL x0020 ; lowercase offsetEXIT TRAP x25 ; halt.ENDCMPE12 Cyrus Bazeghi10The OUT Service Routine.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 xF3FCCRTDR .FILL xF3FFSaveR1 .FILL 0SaveR7 .FILL 0.ENDstored in table,location x21CMPE12 Cyrus Bazeghi11TRAP Routines and their Assembler Namesvector symbol routinex20 GETCread a single character (no echo)x21 OUToutput a character to the monitorx22 PUTSwrite a string to the consolex23 INprint prompt to console,read and echo character from keyboardx25 HALThalt the programCMPE12 Cyrus Bazeghi12Saving and Restoring RegistersMust 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?– 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 routineCMPE12 Cyrus Bazeghi13ExampleLEA R3, BinaryLD 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 #10Binary .BLKW 10What’s wrong with this routine?What happens to R7?CMPE12 Cyrus Bazeghi14Saving and Restoring RegistersCalled 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 registersCalling routine --“caller-save”– Save registers destroyed by own instructions or by called routines (if known), if values needed later• save R7 before TRAP• save R0 before TRAP x23 (input character)– Or avoid using those registers altogetherValues are saved by storing them in memory.CMPE12 Cyrus Bazeghi15QuestionCan a service routine call another service routine?– Sure, PUTS calls OUTIf so, is there anything special the calling service routine must do?– Better save R7CMPE12 Cyrus Bazeghi16What about User Code?Service routines provide three main functions:1. Shield programmers from system-specific details.2. Write frequently-used code just once.3. Protect system resources from malicious/clumsyprogrammers.Are there any reasons to provide the same functions for non-system (user) code?CMPE12 Cyrus Bazeghi17SubroutinesA subroutine is a program fragment that:– lives in user space– performs a well-defined task– is invoked (called) by another user program– returns control to the calling program when finishedLike a service routine, but not part of the OS– not concerned with protecting hardware resources– no special privilege requiredReasons for subroutines:– reuse useful (and debugged!) code without having tokeep typing it in– divide task among multiple programmers– use vendor-supplied libraryof useful routinesCMPE12 Cyrus Bazeghi18JSR InstructionJumps to a location (like a branch but unconditional), and saves current PC (addr of next instruction) in R7.– saving the return address is called “linking”– target address is PC-relative (PC + Sext(IR[10:0]))– bit 11 specifies addressing mode• if =1, PC-relative: target address = PC + Sext(IR[10:0])• if =0, register: target address = contents of


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?