Computer Science 210 Computer Organization Introduction to Subroutines.
-
Upload
isabel-dean -
Category
Documents
-
view
217 -
download
0
Transcript of Computer Science 210 Computer Organization Introduction to Subroutines.
Computer Science 210Computer Organization
Introduction to Subroutines
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
Subroutines• 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
• Jumps to a location (like a branch but unconditional), and saves current PC (address 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 register IR[8:6]
Data Path for JSR
• Just like JSR, except Register addressing mode.– target address is Base Register
– bit 11 specifies addressing mode
• What important feature does JSRR providethat JSR does not?
Data Path for JSRR
Returning from Subroutine
• RET (JMP R7) gets us back to the calling routine.– works just like in TRAP
;; Author: Ken Lambert
;; This program resets the value of the variable NUMBER to its absolute value, using the ABS subroutine
.ORIG x3000
;; Pseudocode design: number = abs(number)
;; Main program register usage:; R1 = number
; Main program codeLD R1, NUMBER ; Set argument for absJSR ABSST R1, NUMBER ; Use returned valueHALT
; Data for main programNUMBER .BLKW 1
;; Subroutine ABS; Converts the number in R1 to its absolute value; Input parameter R1 = the number to convert; Output parameter R1 = the number convertedABS ADD R1, R1, #0 ; if R1 < 0
BRzp ENDABS NOT R1, R1 ; R1 = -R1ADD R1, R1, #1
ENDABS RET
.END
Example: Absolute Value
Interface to Trap Service Routines
• Registers serve as input parameters and output parameters
• Examples: – OUT and PUTS use R0 as an input parameter– GETC and IN in use R0 as an output parameter
Passing Data to Subroutines
• Input parameters– The values passed in to a subroutine are called its
input parameters.– These values are needed by the subroutine to do its
job.– Examples:
• In ABS routine, R1 is the number to be converted
• In OUT service routine, R0 is the character to be printed.
• In PUTS routine, R0 is address of string to be printed.
Returning Data from a Subroutine
• Output parameters– Values passed back from a subroutine are
called output parameters.– These are the results of the subroutine’s
computation.– Examples:
• In ABS routine, converted value is returned in R1.
• In GETC service routine, character read from the keyboard is returned in R0.
;; Author: Ken Lambert
;; This program subtracts the number in the variable SECOND from FIRST and;; and stores the result in DIFF
.ORIG x3000
;; Pseudocode design: diff = first - second
;; Main program register usage:; R1 = first; R2 = second; R3 = diff
; Main program codeLD R1, FIRST ; Set the parameter for the minuendLD R2, SECOND ; Set the parameter for the subtrahendJSR SUBST R3, DIFF ; Store the returned differenceHALT
; Main program data variablesFIRST .BLKW 1SECOND .BLKW 1DIFF .BLKW 1
;; Subroutine SUB ; Subtracts R2 from R1 and stores result in R3 ; Input parameters: R1 (minuend) and R2 (subtrahend) ; Output parameter: R3 (difference)SUB NOT R3, R2
ADD R3, R3, #1 ADD R3, R1, R3RET
.END
Example: Subtraction
Note that the SUB routine does not modify its input parameters!
The Namespace• A subroutine should communicate with its caller
only via registers
• A subroutine may have its own data variables, but should not access anyone else’s data variables (those of other routines or the main program)
• A subroutine should leave its input registers unchanged (may save and restore)
Saving and Restoring Registers• Called routine -- “callee-save”
– At startup, save any registers that will be altered(unless altered value is desired by calling program!)
– Before return, restore those same registers
• 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
Saving and Restoring Registers
• Generally use “callee-save” strategy,except for return values.– 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).
• Remember: You MUST save R7 if you call any other subroutine or TRAP.
;; Author: Ken Lambert
;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND
.ORIG x3000
;; Main program register usage:; R1 = initial value of FIRST; R2 = initial value of SECOND
; Main program codeLD R1, FIRSTLD R2, SECONDJSR ORDERST R1, FIRSTST R2, SECOND HALT
; Main program dataFIRST .BLKW 1SECOND .BLKW 1
;; Subroutine ORDER; Guarantees that R1 <= R2 ; Input parameters: R1 and R2; Output parameters: R1 and R2; R3 = temporary working storageORDER ST R3, ORDERR3 ; Save R3
JSR SUBBRnz ENDORD ; Exit if difference <= 0ADD R3, R2, #0 ; Swap values in R1 and R2ADD R2, R1, #0ADD R1, R3, #0
ENDORD LD R3, ORDERR3 ; Restore R3RET
; Data variable for subroutine ORDERORDERR3 .BLKW 1
;; Subroutine SUB . . .
Example: ORDER makes R1 <= R2
Is there a bug here?
;; Author: Ken Lambert
;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND
.ORIG x3000
;; Main program register usage:; R1 = initial value of FIRST; R2 = initial value of SECOND
; Main program codeLD R1, FIRSTLD R2, SECONDJSR ORDERST R1, FIRSTST R2, SECOND HALT
; Main program dataFIRST .BLKW 1SECOND .BLKW 1
;; Subroutine ORDER; Guarantees that R1 <= R2 ; Input parameters: R1 and R2; Output parameters: R1 and R2; R3 = temporary working storageORDER ST R3, ORDERR3 ; Save R3 ST R7, ORDERR7 ; Save R7
JSR SUBBRnz ENDORD ; Exit if difference <= 0ADD R3, R2, #0 ; Swap values in R1 and R2ADD R2, R1, #0ADD R1, R3, #0
ENDORD LD R3, ORDERR3 ; Restore R3 LD R7, ORDERR7 ; Restore R7
RET
; Data variables for subroutine ORDERORDERR3 .BLKW 1ORDERR7 .BLKW 1
;; Subroutine SUB . . .
Example: ORDER makes R1 <= R2
Back up ret addressbefore another call