Download presentation
Presentation is loading. Please wait.
Published byWarren Grant Murphy Modified over 9 years ago
1
Functions and Procedures
2
Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used for code, away from main and other functions (main is itself a function called by the system) u May return a value to caller u Function may use parameters provided by caller
3
Function call u Store parameters where function can find them u Store address where control should return after call u Go to function code u Store information in registers that need to be used by function, so they can be restored u Get parameters u Execute function u Put result where it can be found by caller
4
Function call continued u Restore information in registers u Return control to caller at appropriate address u Caller get results
5
Function call -- issues u How to save return address? u How to provide parameters for function? u How to save information stored in registers? u How to store return value of function so it is available to caller after function terminates
6
Solution 1 u Use fixed location in memory for each function u Nested calls of different functions can work u Recursive calls, direct or mutually recursive, cannot work u No longer used
7
Solution 2 u Use a portion of memory as a stack –allows recursive functions u Need to keep a pointer to top of stack –a register is usually used u Information which can be stored on the stack –parameters (stored by caller) –return address (stored by caller) –register values needed after call (stored by caller or function) –local variables for function (stored by function) –return value (stored by function)
8
Runtime stack SP main PC code memory stack memory parameters SP return address SP PC function 1 function 2
9
PC Runtime stack main function 1 code memory stack memory local variables return value return address parameters PC SP function 2
10
Runtime stack -- second call main code memory stack memory local variables return value return address parameters local variables return value return address parameters function 1 function 2 PC SP
11
Runtime stack -- red calls yellow main function 1 code memory stack memory local variables return value return address parameters function 2 local variables return value return address parameters PC SP parameters SP return address PC SP return value local variables PC
12
After red - yellow call returns main function 1 PC code memory stack memory local variables return value return address parameters SP function 2 local variables return value return address parameters still there no longer used
13
After yellow- red call completes main function 1 PC code memory stack memory local variables return value return address parameters SP function 2 still there no longer used still there no longer used
14
Solution 3 u Store all values which need to be saved in registers u Needs many registers u Faster than using memory u Eventually cannot handle recursive calls which go too deep u Deep stack in registers used in Sun Sparc architecture
15
MIPS solution u Mixed: keep as much as possible in registers u “Spill” register contents to memory stack when needed u Single function calls often use only registers u Deeper function calls always use stack –how much depends on register usage by functions
16
MIPS Register Usage u ra (31) used for function return address u sp (29) used as stack pointer u v0, v1 function return value(s) u a0, a1, a2, a3 function parameters u s registers guaranteed to hold same values across function calls (e.g. value after call is the same as it was before the call) u t registers values may be chnaged by function call without being restored after
17
MIPS function call sequence 1. Caller places parameter values in registers $a0 - $a3 if more than four, place extra on stack 2. Caller saves values of a and t registers on stack, if needed later adjust stack pointer accordingly 3. Jump to function start address and place return address in register $ra jal instruction does this -- return address is PC + 4 4. Allocate memory needed for stack frame by subtracting frame size from $sp (stack pointer register). 5. Save callee-saved registers any s registers which the function will use the $ra registers if this function calls another Execute function
18
MIPS function call sequence - 2 11. Caller uses return value(s) which is (are) in register(s) $v0, $v1 10. Caller restores values of a and t registers from stack, if needed readjust stack pointer accordingly 9. Return to return address which is in $ra jr $ra instruction does this 8. Deallocate memory needed for stack frame by adding frame size to $sp (stack pointer register). 7. Restore callee-saved registers any s registers which the function used the $ra registers if this function called another 6. Place return value(s) in registers $v0-$v1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.