Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

System Calls

Certain operations require specialized knowledge


and protection:
• specific knowledge of I/O device registers
Chapter 3 and the sequence of operations needed to use them
• I/O resources shared among multiple users/programs;
TRAP Routines a mistake could affect lots of other users!

and Not every programmer knows (or wants to know)


this level of detail
Subroutines 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. 1. A set of service routines.
2. Operating system code performs operation. • part of operating system -- routines start at arbitrary addresses
(convention is that system code is below x3000)
3. Returns control to user program. • 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
NOTE: PC has already been incremented
9-5 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 ; 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

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


Calling routine -- “caller-save”
must do?
• 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 5-18

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

Attention some points with Subroutines


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)
Arguments • its function (what does it do?)
• A value passed in to a subroutine is called an argument. NOTE: The programmer does not need to know
how the subroutine works, but
• This is a value needed by the subroutine to do its job. what changes are visible in the machine’s state
• Examples: after the routine has run.
Return Values • its arguments (where to pass data in, if any)
• A value passed out of a subroutine is called a return value. • its return values (where to get computed data, if any)
• 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, 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

Generally use “callee-save” strategy,


-- Subroutine -> attention to R2!
except for return values.
; SUBTR subroutine computes difference of two 2s’
• Save anything that the subroutine will alter internally
that shouldn’t be visible when the subroutine returns. ; complement numbers
• It’s good practice to restore incoming arguments to ; IN: R1, R2
their original values (unless overwritten by return value). ; OUT: R0←R1-R2

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

SUBTR ST R2, SaveR2 ; save R2 in memory ST R7, SaveR7 ; save R7 in memory


NOT R2, R2 JSR SUBTR ; call subroutine
ADD R2, R2, #1 ; do not use R7 LD R7, SaveR7 ; restore R7 from memory
; since it is needed to return …
ADD R0, R1, R2 HALT
LD R2, SaveR2 ; restore R2 from memory SaveR7 .BLKW 1 ; memory for saving R7
RET
SaveR2 .BLKW 1 ; memory for saving R2
5-29 5-30

Example: Multiplying two 2s’ complement


Subroutines integers
Subroutine call from user program using JSRR
; MULT subroutine multiplies two 2s' complement numbers
; IN: R1, R2
; R2 ← some value
; OUT R0 = R1 x R2
; R1 ← some value

MULT ST R1, MULTSaveR1 ; store R1


ST R7, SaveR7 ; save R7 in memory
AND R0, R0, #0 ; R0 = 0
LD R3, SUBTRaddr ; load address of SUBTR to R3
LOOPADD ADD R0, R0, R2 ; R0 = R0 + R2
JSRR R3 ; call subroutine
ADD R1, R1, #-1 ; decrease counter
LD R7, SaveR7 ; restore R7 from memory
BRp LOOPADD

HALT LD R1, MULTSaveR1 ; restore R1 from memory
SaveR7 .BLKW 1 ; memory for saving R7 RET
SUBTRaddr .FILL SUBTR MULTSaveR1 .BLKW 1
5-31 7-32

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

Using JSRR, because we don’t know whether SQRT


is within 1024 instructions.
7-33 9-34

Homework 1: write a program with the Homework 2


subroutines in red texts below
Write a program to compute value of the following
polynomial: y= for x > 0
Enter 2 values from 0-9, compute their product. Store it in
- Use MULT subroutine to multiply numbers
a memory location.
- 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

You might also like