Professional Documents
Culture Documents
Ch03 - TRAP Routines - Subroutines
Ch03 - TRAP Routines - Subroutines
9-2
9-3 9-4
1
TRAP Instruction TRAP
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 R7
NOTE: PC has already been incremented
9-5 during instruction fetch stage. 9-6
9-7 7-8
2
TRAP Mechanism Operation – TRAP x23 Example: Using the TRAP Instruction
.ORIG x3000
LD R2, TERM ; Load negative ASCII ‘7’
LD R3, ASCII ; Load ASCII difference
AGAIN TRAP x23 ; input character
1. Lookup starting address. ADD R1, R2, R0 ; Test for terminate
2. Transfer to service routine. BRz EXIT ; Exit if done
3. Return (JMP R7). ADD R0, R0, R3 ; Change to lowercase
TRAP x21 ; Output to monitor...
BRnzp AGAIN ; ... again and again...
TERM .FILL xFFC9 ; -‘7’
ASCII .FILL x0020 ; lowercase bit
EXIT TRAP x25 ; halt
.END
9-9 9-10
Example: Output Service Routine TRAP Routines and their Assembler Names
.ORIG x0430 ; syscall address
ST R7, SaveR7 ; save R7 & R1
ST R1, SaveR1
; ----- Write character vector symbol routine
TryWrite LDI R1, CRTSR ; get status
BRzp TryWrite ; look for bit 15 on x20 GETC read a single character (no echo)
WriteIt STI R0, CRTDR ; write char x21 OUT output a character to the monitor
; ----- Return from TRAP
Return LD R1, SaveR1 ; restore R1 & R7 x22 PUTS write a string to the console
LD R7, SaveR7 print prompt to console,
RET ; back to user
x23 IN
read and echo character from keyboard
CRTSR .FILL xF3FC x25 HALT halt the program
CRTDR .FILL xF3FF stored in table,
SaveR1 .FILL 0 location x21
SaveR7 .FILL 0
.END
9-11 9-12
3
Saving and Restoring Registers Example
Must save the value of a register if:
LEA R3, Binary
• Its value will be destroyed by service routine, and LD R6, ASCII ; char->digit template
• We will need to use the value after that action. LD R7, COUNT ; initialize to 10
AGAIN TRAP x23 ; Get char
Who saves? ADD R0, R0, R6 ; convert to number
• caller of service routine? STR R0, R3, #0 ; store number
knows what it needs later, but may not know what gets ADD R3, R3, #1 ; incr pointer
altered by called routine ADD R7, R7, -1 ; decr counter
• called service routine? BRp AGAIN ; more?
knows what it alters, but does not know what will be BRnzp NEXT
needed later by calling routine ASCII .FILL xFFD0 What’s wrong with this routine?
COUNT .FILL #10 What happens to R7?
Binary .BLKW #10
9-13 9-14
4
JSR, JSRR and RET
Subroutines
A 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 finished
JSR JSRR
NOTE: PC has already been incremented NOTE: PC has already been incremented
during instruction fetch stage. 9-19 during instruction fetch stage. 9-20
5
RET Structure of a subroutine
Returning from a Subroutine
<Label – name of the subroutine>
RET (JMP R7) gets us back to the calling routine. <save registers>
• just like TRAP ...
<code of subroutine>
...
<restore registers>
RET
9-21 7-22
9-23 9-24
6
Examples
Saving and Restore Registers -A subroutine to subtract one number from another
Since subroutines are just like service routines, Function: R0←R1-R2
we also need to save and restore registers, if needed. Arguments are passed in R1 and R2
Result is returned in R0
Remember: You MUST save R7 if you call any other SUBTR NOT R2, R2
subroutine or service routine (TRAP). ADD R2, R2, #1 ; ADD R7, R2, #1 if R7 is used
• Otherwise, you won’t be able to return to caller. ADD R0, R1, R2
RET
9-25 5-26
Subroutines Subroutines
When the subroutine is called in User program, we have, but
attention to R7 If SUBTR is located far enough in the memory from the point where it
is called, we may not be able to reach it with JSR instruction
; R2 ← some value
; R1 ← some value To Fix: use JSRR instruction instead
JSR SUBTR ; R1 ← R1 – R2
5-27 5-28
7
Subroutines Subroutines
; SUBTR subroutine computes difference of two 2s’ Subroutine call from user program using JSR if R7 is used.
; complement numbers
; IN: R1, R2 ; R2 ← some value
; OUT: R0←R1-R2 ; R1 ← some value
8
Subroutimes – Structure of a program Library Routines
.ORG Vendor may provide object files containing
useful subroutines
... • don’t want to provide source code -- intellectual property
<main program> • assembler/linker must support EXTERNAL symbols
(or starting address of routine must be supplied to user)
...
...
HALT .EXTERNAL SQRT
<Data section> ...
<source codes of subroutines> LD R2, SQAddr ; load SQRT addr
... JSRR R2
...
.END SQAddr .FILL SQRT
7-35 7-36
9
Develop the homework 2 topic Besides
And
9.1 – 9.19
7-37 7-38
10