Presentation is loading. Please wait.

Presentation is loading. Please wait.

28/06/2015CMPUT 229 1 Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.

Similar presentations


Presentation on theme: "28/06/2015CMPUT 229 1 Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions."— Presentation transcript:

1 28/06/2015CMPUT 229 1 Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions

2 28/06/2015CMPUT 229 2 Recursive functions  A function that calls itself  Terminating condition  Stack frame implementation

3 28/06/2015CMPUT 229 3 Function calling convention  In summary, caller: 1.saves temporary registers by pushing their values on stack 2.pushes arguments (both “value” and “variable” on stack 3.calls the function with jal instruction  (function runs until it returns, then:) 4.clears function arguments by popping allocated space 5.restores saved temporary registers by popping their values off the stack 6.uses the return value found in both $v0 and popped “variable” arguments

4 28/06/2015CMPUT 229 4 Function calling convention  In summary, callee: 1.saves $ra by pushing its value on stack 2.saves $fp by pushing its value on stack 3.copies $sp to $fp 4.allocates local variables  (body of function goes here, then:) 5.chooses return value by setting register $v0 6.deallocates local variables by popping allocated space 7.restores $fp by popping its saved value 8.restores $ra by popping its saved value 9.returns with jr $ra

5 28/06/2015CMPUT 229 5 Recursion  Function calling convention works exactly the same for recursive functions  don’t need to do anything special  Each invocation of the function has its own stack frame  local variables and parameters with their current valueswith their current values  return address where to return towhere to return to

6 28/06/2015CMPUT 229 6 Example  Fibonacci’s numbers fib(0) = 0 fib(1) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) for n > 1 fib(n) = fib(n-1) + fib(n-2) for n > 1

7 28/06/2015CMPUT 229 7 Fibonacci Numbers

8 28/06/2015CMPUT 229 8 Implementation of Recursive Functions  Different conventions can be used for writing recursive functions  MIPS  $a0 for taking input parameters  $v0 for returning results  Callers save $t0, $t1, …,  Callees save $s0, $s1, …,

9 28/06/2015CMPUT 229 9 Implementation of Recursive Functions  Other conventions  Callees save all registers

10 28/06/2015CMPUT 229 10  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Useful and important

11 28/06/2015CMPUT 229 11  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Frame pointers  Used by compilers but difficult to understand

12 28/06/2015CMPUT 229 12  More restricted conventions  All arguments are passed with the stack frame, including the value and variable parameters  Frame pointers  Used by compilers but difficult to understand

13 28/06/2015CMPUT 229 13  Two different implementations of fibonacci numbers  Simple one (fib.a) Using $a0 and $v0 to pass argumentsUsing $a0 and $v0 to pass arguments Callees save all the registersCallees save all the registers  Stack frame (fib_stack.a) Using the stack frame for passing the arguments to and from the functionUsing the stack frame for passing the arguments to and from the function


Download ppt "28/06/2015CMPUT 229 1 Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions."

Similar presentations


Ads by Google