
Subroutines/Functions A subroutine is a program fragment that. Resides in user space (i.e, not in OS) Performs a well-defined task Is invoked (called) by a user program Implementing Returns control to the calling program when finished Functions at the Virtues Machine Level Reuse useful code without having to keep typing it in (and debugging it!) Divide task amonggppg multiple programmers Use vendor-supplied library of useful routines Based on slides © McGraw-Hill Additional material © 2004/2005 Lewis/Martin Modified by Diana Palsetia CIT 593 1 CIT 593 2 LC3 Opcode for CALLING a Subroutine JSR JSR/JSRR – saves the return address in R7 and computes 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 the the starting address of the subroutine and loads it JSR 0 1 0 0 1 PCoffset11 Note: This is PC into PC of next instruction JSR 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 PC-relative mode (just like PC-relative LD/ST) PC 0100000000011001 d Register File Target address of the subroutine is incremented PC + offset BR 512 R0 IR 0100101000000000 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 R1 9 JSR R2 0 1 0 0 1 PCoffset11 R3 SEXT R4 16 16 R5 16 R6 c 0000001000000000 JSRR R7 0100000000011001 16 Base addressing mode B A ADD Target address of the subroutine is obtained from Base Register ALU 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Just like JMP (but PC is saved in R7) 16 16 JSRR 01000 0 0 BaseR 0 0 0 0 0 0 1 0 CIT 593 3 CIT 593 4 1 Returning From a Subroutine Example: 2’s complement routine Use RET .ORIG x3000 DoSomething1 •. Just a special case of JMP i.e. RET == JMP R7 •. •. •. ;need to compute R4 = R1 - R3 Note x3005 ADD R0, R3, #0 ; copy R3 to R0 x3006 JSR TwosComp ; negate If we use JMP to call subroutine instead of JSR/JSRR, x3007 ADD R4, R1, R0 ; add to R1 we can’t use RET to return from subroutine! x3008 BRz DoSomething2 Why not? TwosComp NOT R0, R0 ; R0 is the input to the routine ADD R0, R0, #1 ; add one RET ; return to caller Do Some thing 2 •. •. •. •. CIT 593 5 CIT 593 6 JSRR Information regarding Subroutines this zero means “register mode” How to Pass Information To/From? 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 In registers (simple, fast, but limited number) JSRR 01000 0 0 BaseR 0 0 0 0 0 0 ¾ E.g. R0 contains input and the return value is also place in R0 In memory (many, but expensive) Register File ¾ This is the stack implementation R0 JSRR R5 R1 Both IR 0100000101000000 R2 R3 R4 What should the User of the Function know? R5 000001000011000 Address: or at least a label that will be bound to its address R6 R7 0100010000011001 ¾In high-level we use the subroutine name 16 Function: what it does NtNote: ThiiPCfThis is PC of 16 ¾NOTE: The programmer does not need to know how the next instruction 16 1 0 subroutine works, but what changes are visible in the d 16 machine’s state after the routine has run 0000010000011000 Arguments: what they are and where they are placed PC 0100010000011001 Virtues of JSRR? c Return values: what they are and where they are placed CIT 593 7 CIT 593 8 2 Saving and Restoring Registers Location for argument(s) and return value Remember that any piece of code uses same set of machine registers Caller/Callee must agree on argument & return value location So we must maintain state of machine after the subroutine call Called routine ⇒ “callee-save” Approach 1 Before start , save register(s) that will be altered Every subroutine does what it likes (unless altered value is desired by calling program!) Program needs to look at documentation for each one Before return, restore those same register(s) Values are saved by storing them in memory Approach 2 Calling routine ⇒ “caller-save” Define a consistent calling convention If register value needed later, save register(s) calling the routine ¾ This is much harder as you may need to know the internal workings of a E.g. LC-3 argument/ret-val location fnctionfunction First 4 arguments passed in R0, R1, R2, R3 Single value returned in register other than R7 By convention, callee-saved Subsequent arguments passed in memory CIT 593 9 CIT 593 10 Example of Callee Save for subroutines Stack TwosComp: ;good to save R7 even if u don’t use it Concept ST R7, SAVER7;save R7 A last-in first-out (LIFO) storage structure ST R1, SAVER1;save R1 The first thing you put in is the last thing you take out NOOTR0, R0 ;R0 is the input to the routine The last thing you put in is the first thing you take out AND R1, R1, #0 Not like an array, where you can access any item based on an index ADD R1, R1, #1 ADD R0, R0,R1 Two main operations LD R7, SAVER7 ;restore R7 PUSH: add an item to the stack LD R1, SAVER1 POP: remove an item from the stack RET SAVER7: .FILL x0000 SAVER1: .FILL x0000 CIT 593 11 CIT 593 12 3 A Physical Stack A Software Stack implemented in Memory Coin holder Assume section x3FFFB to x3FFF in memory used for stack Data items don't move in memory, just our idea about where TOP of the stack is (a register is used to keep track of the location) 1995 1996 1998 1998 1982 x3FFB / / / / / / / / / / / / / / / / / / / / / / / 1982 1995 x3FFC / / / / / / / / / / / / 12 TOP 12 1995 x3FFD / / / / / / / / / / / / 5 5 x3FFE / / / / / / / / / / / / 31 31 TOP x3FFF / / / / / / 18 TOP 18 18 TOP x4000 R6x3FFF R6x3FFC R6x3FFE R6 Initial State After After Three After One Push More Pushes One Pop Initial State After After Three After One Push More Pushes Two Pops Last quarter in is the first quarter out (LIFO) By convention, in LC3 R6 holds the Top of Stack (TOS) pointer CIT 593 13 CIT 593 14 Basic Push and Pop Code What happens when we run out of space? Push Overflow ADD R6, R6, #-1 ; decrement stack ptr When we have no more locations and we try to push some data STR R0, R6, #0 ; store data contained in R0 on the stack Underflow The stack is empty and we try to pop the data Pop LDR R0, R6, #0 ; load data from TOS Some how the programmer needs to learn about the ADD R6, R6, #1 ; increment stack ptr overflow/underflow problem Java: Stack Overflow Note C: Segmentation fault Stacks can grow in either direction (toward higher address or toward lower addresses) CIT 593 15 CIT 593 16 4 POP routine w/ Underflow Detection General Implementation POP: LD R1, EMPTY ;R1 = -x4000 Some processors use three registers to track: ADD R2, R6, R1 ;R6 = Stack Pointer MAX Limit Stack Pointer (SP): contains addr of top of stack BRz Failure ;check whether R6 = x4000 /////// / / / / / LDR R0, R6, #0 ;POP value of the stack 8 SP ADD R6, R6, #1 ;update the stack pointer Stack Base: Contains the addr of the bottom location 12 RET BOTTOM Base Failure: AND R5, R5, #0 Stack Limit: contains the addr of the other end of the ADD R5, R5, #1 ;R5 = 1 indicates that the POP was stack (checks for PUSH error) ;not successful E.g. 3 entry stack RET Empty: .FILL xC000 ; - x4000 (stack limit) CIT 593 17 CIT 593 18 Uses of Stack Use: Passing arguments & return value in a subroutine Passing arguments & routines for subroutine calls Use the stack for arguments & return values instead of Instead passing them through registers registers To perform recursive subroutines Calling routine pushes arguments on the stack and calls the Allocatinggp space for local variables inside a subroutine subroutine Called routine pops the passed arguments from the stack and pushes any return value onto the stack Use as Temporary storage Finally, the calling routine then retrieves the return value(s) from Computers without registers store temporary values during the stack and continues execution computation on a stack Why would this be helpful? Interrupt I/O This is especially helpful if a subroutine has many arguments More when we do I/O Also stack is need for recursion CIT 593 19 CIT 593 20 5 Function Call scenario (Recursion Problem) Solution to Recursion problem Main . First call to Foo For each subroutine call, need a mechanism to distinguish JSR Foo SaveR7 contains address of Next its invocation Next . This is known as activation record HALT Second call to Foo SaveR7 contains address of After Activation Record contains Foo ST R7, SaveR7 Invocation-specific data (e.g., local variables, saved registers, AND R0, R0, #0 First return from Foo arguments and returns) . Returns to After JSR Foo Need to store this information per function call and discard when After . the function call is over LD R7, SaveR7 Second return from Foo RET Returns to After again!!! Stack data structure fits this description well Save7 .FILL #0 ¾When function is called we store (push) record on the stack Counter .FILL #0 ¾When function call is over we discard (pop) record from the stack .END CIT 593 21 CIT 593 22 Big Picture Complete view of an activation record Activation Record of a called function: Memory Memory Memory R5 1.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-