DOC PREVIEW
UW-Madison CS/ECE 252 - TRAP Routines 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:

Chapter 9 TRAP Routines 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 RoutinesChapter 9TRAP Routines andSubroutines9-2Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.System CallsCertain 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 detailProvide service routines or system calls(part of operating system) to safely and convenientlyperform low-level, privileged operations9-3Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.System Call1. 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 mechanism.9-4Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.LC-3 TRAP Mechanism1. 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.•stored at x0000 through x00FF in memory•called System Control Block in some architectures3. 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.•want execution to resume immediately after the TRAP instruction9-5Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.TRAP InstructionTrap 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 addressWhere to go•lookup starting address from table; place in PCHow to get back•save address of next instruction (current PC) in R79-6Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.TRAPNOTE: PC has already been incrementedduring instruction fetch stage.9-7Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.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.9-8Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.TRAP Mechanism Operation1. Lookup starting address.2. Transfer to service routine.3. Return (JMP R7).9-9Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Example: Using the TRAP Instruction.ORIG x3000LD R2, TERM ; Load negative ASCII ‘7’LD R3, ASCII ; Load ASCII differenceAGAIN TRAP x23 ; input characterADD R1, R2, R0 ; Test for terminateBRz EXIT ; Exit if doneADD R0, R0, R3 ; Change to lowercaseTRAP x21 ; Output to monitor...BRnzp AGAIN ; ... again and again...TERM .FILL xFFC9 ; -‘7’ASCII .FILL x0020 ; lowercase bitEXIT TRAP x25 ; halt.END9-10Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Example: Output 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 x219-11Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.TRAP 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 program9-12Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Saving 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 routine9-13Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.ExampleLEA 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?9-14Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Saving 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 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 altogetherValues are saved by storing them in memory.9-15Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or


View Full Document
Download TRAP Routines 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 TRAP Routines 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 TRAP Routines 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?