Uploaded by Anh Hào Nguyễn

Ch03 - TRAP routines - subroutines

advertisement
System Calls
Certain 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!
Chapter 3
TRAP Routines
and
Subroutines
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 conveniently
perform low-level, privileged operations
9-2
System Call
LC-3 TRAP Mechanism
1. User program invokes system call.
2. Operating system code performs operation.
3. Returns control to user program.
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 routines
2. Table of starting addresses.
• stored at x0000 through x00FF in memory
• called System Control Block in some architectures
3. TRAP instruction.
In LC-3, this is done through the TRAP mechanism.
• used by program to transfer control to operating system
• 8-bit trap vector names one of the 256 service routines
4. A linkage back to the user program.
• want execution to resume
immediately after the TRAP instruction
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
9-5
NOTE: PC has already been incremented
during instruction fetch stage.
9-6
Conclusion – TRAP and RET
RET (JMP R7)
How do we transfer control back to
instruction 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-7
7-8
2
TRAP Mechanism Operation – TRAP x23
Example: Using the TRAP Instruction
.ORIG x3000
LD
R2, TERM
LD
R3, ASCII
TRAP x23
ADD R1, R2, R0
BRz EXIT
ADD R0, R0, R3
TRAP x21
BRnzp AGAIN
.FILL xFFC9
.FILL x0020
TRAP x25
.END
AGAIN
1. Lookup starting address.
2. Transfer to service routine.
3. Return (JMP R7).
TERM
ASCII
EXIT
;
;
;
;
;
;
;
;
;
;
;
Load negative ASCII ‘7’
Load ASCII difference
input character
Test for terminate
Exit if done
Change to lowercase
Output to monitor...
... again and again...
-‘7’
lowercase bit
halt
9-9
Example: Output Service Routine
.ORIG x0430
ST
R7, SaveR7
ST
R1, SaveR1
; ----- Write character
TryWrite
LDI
R1, CRTSR
BRzp TryWrite
WriteIt
STI
R0, CRTDR
; ----- Return from TRAP
Return
LD
R1, SaveR1
LD
R7, SaveR7
RET
CRTSR
CRTDR
SaveR1
SaveR7
.FILL
.FILL
.FILL
.FILL
.END
xF3FC
xF3FF
0
0
9-10
TRAP Routines and their Assembler Names
; syscall address
; save R7 & R1
vector
; get status
; look for bit 15 on
; write char
symbol routine
x20
GETC
read a single character (no echo)
x21
OUT
output a character to the monitor
; restore R1 & R7
x22
PUTS
; back to user
x23
IN
x25
HALT
stored in table,
location x21
9-11
write a string to the console
print prompt to console,
read and echo character from keyboard
halt the program
9-12
3
Saving and Restoring Registers
Example
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.
AGAIN
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 routine
ASCII
COUNT
Binary
LEA R3, Binary
LD
R6, ASCII ; char->digit template
LD
R7, COUNT ; initialize to 10
TRAP x23
; Get char
ADD R0, R0, R6 ; convert to number
STR R0, R3, #0 ; store number
ADD R3, R3, #1 ; incr pointer
ADD R7, R7, -1 ; decr counter
BRp AGAIN
; more?
BRnzp NEXT
.FILL xFFD0 What’s wrong with this routine?
.FILL #10
What happens to R7?
.BLKW #10
9-13
9-14
Saving and Restoring Registers
Question
Called routine -- “callee-save”
Can a service routine call another service routine?
• Before start, save any registers that will be altered
(unless altered value is desired by calling program!)
• Before return, restore those same registers
If so, is there anything special the calling service routine
must do?
Calling 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 altogether
Values are saved by storing them in memory.
9-15
9-16
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
Like a service routine, but not part of the OS
• not concerned with protecting hardware resources
• no special privilege required
Reasons for subroutines:
• reuse useful (and debugged!) code without having to
keep typing it in
• divide task among multiple programmers
• use vendor-supplied library of useful routines
9-17
JSR
NOTE: PC has already been incremented
during instruction fetch stage.
5-18
JSRR
9-19
NOTE: PC has already been incremented
during instruction fetch stage.
9-20
5
RET
Structure of a subroutine
Returning from a Subroutine
<Label – name of the subroutine>
<save registers>
...
<code of subroutine>
...
<restore registers>
RET
RET (JMP R7) gets us back to the calling routine.
• just like TRAP
9-21
Attention some points with Subroutines
7-22
Using Subroutines
Passing Information to/from Subroutines
In order to use a subroutine, a programmer must know:
• its address (or at least a label that will be bound to its address)
• its function (what does it do?)
NOTE: The programmer does not need to know
how the subroutine works, but
what changes are visible in the machine’s state
after the routine has run.
• its arguments (where to pass data in, if any)
• its return values (where to get computed data, if any)
Arguments
• A value passed in to a subroutine is called an argument.
• This is a value needed by the subroutine to do its job.
• Examples:
Return Values
• A value passed out of a subroutine is called a return value.
• This is the value that you called the subroutine to compute.
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,
we also need to save and restore registers, if needed.
Function: R0←R1-R2
Arguments are passed in R1 and R2
Result is returned in R0
Generally use “callee-save” strategy,
except for return values.
-- Subroutine -> attention to R2!
; SUBTR subroutine computes difference of two 2s’
; complement numbers
; IN: R1, R2
; OUT: R0←R1-R2
• Save anything that the subroutine will alter internally
that shouldn’t be visible when the subroutine returns.
• It’s good practice to restore incoming arguments to
their original values (unless overwritten by return value).
SUBTR
Remember: You MUST save R7 if you call any other
subroutine or service routine (TRAP).
• Otherwise, you won’t be able to return to caller.
9-25
Subroutines
NOT R2, R2
ADD R2, R2, #1 ; ADD R7, R2, #1 if R7 is used
ADD R0, R1, R2
RET
5-26
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’
; complement numbers
; IN: R1, R2
; OUT: R0←R1-R2
Subroutine call from user program using JSR if R7 is used.
SUBTR ST R2, SaveR2 ; save R2 in memory
NOT R2, R2
ADD R2, R2, #1 ; do not use R7
; since it is needed to return
ADD R0, R1, R2
LD R2, SaveR2 ; restore R2 from memory
RET
SaveR2 .BLKW 1
; memory for saving R2
ST R7, SaveR7
JSR SUBTR
LD R7, SaveR7
…
HALT
SaveR7 .BLKW 1
; R2 ← some value
; R1 ← some value
; save R7 in memory
; call subroutine
; restore R7 from memory
; memory for saving R7
5-29
5-30
Example: Multiplying two 2s’ complement
integers
Subroutines
Subroutine call from user program using JSRR
; MULT subroutine multiplies two 2s' complement numbers
; IN: R1, R2
; OUT R0 = R1 x R2
; R2 ← some value
; R1 ← some value
MULT
ST R7, SaveR7
; save R7 in memory
LD R3, SUBTRaddr ; load address of SUBTR to R3
JSRR R3
; call subroutine
LD R7, SaveR7
; restore R7 from memory
…
HALT
SaveR7 .BLKW 1
; memory for saving R7
SUBTRaddr .FILL SUBTR
LOOPADD
ST R1, MULTSaveR1
AND R0, R0, #0
ADD R0, R0, R2
ADD R1, R1, #-1
BRp LOOPADD
LD R1, MULTSaveR1
RET
; store R1
; R0 = 0
; R0 = R0 + R2
; decrease counter
; restore R1 from memory
MULTSaveR1 .BLKW 1
5-31
7-32
8
Subroutimes – Structure of a program
Library Routines
Vendor may provide object files containing
useful subroutines
.ORG
...
<main program>
...
HALT
<Data section>
<source codes of subroutines>
...
.END
• don’t want to provide source code -- intellectual property
• assembler/linker must support EXTERNAL symbols
(or starting address of routine must be supplied to user)
SQAddr
...
.EXTERNAL SQRT
...
LD
R2, SQAddr
JSRR R2
...
.FILL SQRT
; load SQRT addr
Using JSRR, because we don’t know whether SQRT
is within 1024 instructions.
7-33
9-34
Homework 1: write a program with the
subroutines in red texts below
Homework 2
Enter 2 values from 0-9, compute their product. Store it in
a memory location.
Write a program to compute value of the following
polynomial: y=
for x > 0
- Use MULT subroutine to multiply numbers
- Use SUBTR subroutine to subtract numbers
Enter 2 other values from 0-9, compute their product.
Store it in the other memory location.
In main program
- Store input value of x in memory at x4000
- Store result value of y in memory at x4001
Compute the sum of contents in the above memory
locations, print out this sum on screen.
7-35
7-36
9
Develop the homework 2 topic
Besides
Enter integer coefficients and exponents of a polynomial.
All the old topics, again!
And
9.1 – 9.19
7-37
7-38
10
Download