Presentation is loading. Please wait.

Presentation is loading. Please wait.

20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.

Similar presentations


Presentation on theme: "20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15."— Presentation transcript:

1 20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15

2 20/06/2015CSE1303 Part B lecture notes 2 Last time  Function calling  jal and jr instructions  Calling convention  for making a function call  Structure of stack  stack frames

3 20/06/2015CSE1303 Part B lecture notes 3 In this lecture  Accessing function parameters  Returning from functions  Recursion

4 20/06/2015CSE1303 Part B lecture notes 4 Stack frames 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 4 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = 0x7FFF0384  $sp = 0x7FFF0380  result1 power ’s stack frame main ’s stack frame

5 20/06/2015CSE1303 Part B lecture notes 5 Local variables 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 4 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = 0x7FFF0384  $sp = 0x7FFF0380  result1 power ’s local variables are accessed as main ’s were, with negative offsets from $fp result is at -4($fp)

6 20/06/2015CSE1303 Part B lecture notes 6 Function parameters 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 4 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = 0x7FFF0384  $sp = 0x7FFF0380  result1 b and e are respectively accessible at +8($fp) and +12($fp) power ’s parameters (arguments) are accessible with positive offset from $fp

7 20/06/2015CSE1303 Part B lecture notes 7 Example: callee /* A function that gets called. */ int power(int b, int e) { int result = 1; int result = 1; /* Keep going while exponent /* Keep going while exponent is positive. */ is positive. */ while (e > 0) while (e > 0) { /* Multiply by base. */ /* Multiply by base. */ result = result * b; result = result * b; /* less to multiply. */ /* less to multiply. */ e--; e--; } /* Return the result /* Return the result to the caller. */ to the caller. */ return result; return result;} #... Continued from # last lecture. loop: # Stop if e <= 0. lw $t0, 12($fp) # e ble $t0, 0, end # result = result * b lw $t0, -4($fp) # result lw $t1, 8($fp) # b mul $t0, $t0, $t1 sw $t0, -4($fp) # result # e-- lw $t0, 12($fp) # e addi $t0, $t0, -1 sw $t0, 12($fp) # e # Repeat loop. j loop end: # Now ready to return. # Continued...

8 20/06/2015CSE1303 Part B lecture notes 8 Example: callee /* A function that gets called. */ int power(int b, int e) { int result = 1; int result = 1; /* Keep going while exponent /* Keep going while exponent is positive. */ is positive. */ while (e > 0) while (e > 0) { /* Multiply by base. */ /* Multiply by base. */ result = result * b; result = result * b; /* less to multiply. */ /* less to multiply. */ e--; e--; } /* Return the result /* Return the result to the caller. */ to the caller. */ return result; return result;} #... Continued from # last lecture. loop: # Stop if e <= 0. lw $t0, 12($fp) # e ble $t0, 0, end # result = result * b lw $t0, -4($fp) # result lw $t1, 8($fp) # b mul $t0, $t0, $t1 sw $t0, -4($fp) # result # e-- lw $t0, 12($fp) # e addi $t0, $t0, -1 sw $t0, 12($fp) # e # Repeat loop. j loop end: # Now ready to return. # Continued...

9 20/06/2015CSE1303 Part B lecture notes 9 Function return  When returning from a function, stack must be restored to its initial state  Achieved by undoing steps made during calling of function, in reverse order

10 20/06/2015CSE1303 Part B lecture notes 10 Function return convention  On function exit, callee: 5.chooses return value by setting register $v0, if necessary 6.deallocates local variables by popping allocated space 7.restores $fp by popping its saved value off stack 8.restores $ra by popping its saved value off stack 9.returns with jr $ra  On return from function, caller: 4.clears function arguments by popping allocated space 5.restores saved temporary registers by popping their values off stack 6.uses the return value found in $v0, if necessary

11 20/06/2015CSE1303 Part B lecture notes 11 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = 0x7FFF0384  $sp = 0x7FFF0380  result81 callee step 5: put return value in register $v0 no effect visible on stack $v0 = 81

12 20/06/2015CSE1303 Part B lecture notes 12 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = 0x7FFF0384  $sp = 0x7FFF0380  result81 callee step 6: deallocate local variables by popping allocated space off stack one local variable to delete

13 20/06/2015CSE1303 Part B lecture notes 13 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $fp = $sp = 0x7FFF0384   callee step 6: deallocate local variables by popping allocated space off stack one local variable to delete result is now “gone”

14 20/06/2015CSE1303 Part B lecture notes 14 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 0x0040005C 0x7FFF03A0 saved $ra saved $fp $sp = 0x7FFF0384  callee steps 7 and 8: restore saved values of $fp and $ra by popping off stack can do both these steps at once $fp = 0x7FFF03A0 $ra = 0x0040005C

15 20/06/2015CSE1303 Part B lecture notes 15 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 callee steps 7 and 8: restore saved values of $fp and $ra by popping off stack $fp = 0x7FFF03A0 $ra = 0x0040005C $sp = 0x7FFF038C  $fp = 0x7FFF03A0  popping $fp makes it point back to main ’s stack frame

16 20/06/2015CSE1303 Part B lecture notes 16 Example: callee 3 4 ???result exp base 0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0 0x7FFF0388 0x7FFF0384 0x7FFF0380 $sp = 0x7FFF038C  callee step 9: return by executing jr $ra nothing visible on stack $fp = 0x7FFF03A0 

17 20/06/2015CSE1303 Part B lecture notes 17 Example: callee /* A function that gets called. */ int power(int b, int e) { int result = 1; int result = 1; /* Keep going while exponent /* Keep going while exponent is positive. */ is positive. */ while (e > 0) while (e > 0) { /* Multiply by base. */ /* Multiply by base. */ result = result * b; result = result * b; /* less to multiply. */ /* less to multiply. */ e--; e--; } /* Return the result /* Return the result to the caller. */ to the caller. */ return result; return result;} #... Continued # Return result in $v0. lw $v0, -4($fp) # result # Remove local var. addu $sp, $sp, 4 # Restore $fp & $ra lw $fp, 0($sp) lw $ra, 4($sp) addu $sp, $sp, 8 # Return to caller. jr $ra

18 20/06/2015CSE1303 Part B lecture notes 18 Example: callee /* A function that gets called. */ int power(int b, int e) { int result = 1; int result = 1; /* Keep going while exponent /* Keep going while exponent is positive. */ is positive. */ while (e > 0) while (e > 0) { /* Multiply by base. */ /* Multiply by base. */ result = result * b; result = result * b; /* less to multiply. */ /* less to multiply. */ e--; e--; } /* Return the result /* Return the result to the caller. */ to the caller. */ return result; return result;} #... Continued # Return result in $v0. lw $v0, -4($fp) # result # Remove local var. addu $sp, $sp, 4 # Restore $fp & $ra lw $fp, 0($sp) lw $ra, 4($sp) addu $sp, $sp, 8 # Return to caller. jr $ra

19 20/06/2015CSE1303 Part B lecture notes 19 Example: caller /* C program which calls a function. */ function. */ #include #include int power(int b, int e); int main() { int base; int base; int exp; int exp; int result; int result; scanf("%d", &base); scanf("%d", &base); scanf("%d", &exp); scanf("%d", &exp); result = power(base, exp); result = power(base, exp); printf("%d", result); printf("%d", result); exit(0); exit(0);} now back in caller, about to assign return value of function to main ’s local variable result

20 20/06/2015CSE1303 Part B lecture notes 20 Example: caller caller step 4: clear function arguments by popping them off stack b and e are no longer needed, destroy them 3 4 ???result exp base $sp = 0x7FFF038C  $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C arg 2 (e) arg 1 (b) 3 0

21 20/06/2015CSE1303 Part B lecture notes 21 Example: caller b and e are no longer needed, destroy them 3 4 ???result exp base $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C $sp = 0x7FFF0394  caller step 4: clear function arguments by popping them off stack

22 20/06/2015CSE1303 Part B lecture notes 22 Example: caller caller step 5: restore saved temporary registers by popping their values off stack we didn’t save any, so nothing to do here 3 4 ???result exp base $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C $sp = 0x7FFF0394 

23 20/06/2015CSE1303 Part B lecture notes 23 Example: caller caller step 6: use return value, found in register $v0 store returned value into a variable 3 4 ???result exp base $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C $sp = 0x7FFF0394  $v0 = 81 main stores return value into local variable result

24 20/06/2015CSE1303 Part B lecture notes 24 Example: caller caller step 6: use return value, found in register $v0 store returned value into a variable 3 4 81result exp base $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 0x7FFF0390 0x7FFF038C $sp = 0x7FFF0394  $v0 = 81 main stores return value into local variable result

25 20/06/2015CSE1303 Part B lecture notes 25 Example: caller Done! after the end of the function call the stack has been returned to its original state 3 4 81result exp base $fp = 0x7FFF03A0  0x7FFF039C 0x7FFF0398 0x7FFF0394 0x7FFF03A0 $sp = 0x7FFF0394 

26 20/06/2015CSE1303 Part B lecture notes 26 Example: caller /* C program which calls a function. */ function. */ #include #include int power(int b, int e); int main() { int base; int base; int exp; int exp; int result; int result; scanf("%d", &base); scanf("%d", &base); scanf("%d", &exp); scanf("%d", &exp); result = power(base, exp); result = power(base, exp); printf("%d", result); printf("%d", result); exit(0); exit(0);} #... main function, continued # Last lecture, we # finished with this... # jal power # Remove arguments, they # are no longer needed. # 2 * 4 = 8 bytes. addu $sp, $sp, 8 # Store return value # in result. sw $v0, -4($fp) # result # Print result. li $v0, 1 lw $a0, -4($fp) # result syscall # deallocate main’s locals addu $sp, $sp, 12 li $v0, 10 # exit syscall

27 20/06/2015CSE1303 Part B lecture notes 27 Example: caller /* C program which calls a function. */ function. */ #include #include int power(int b, int e); int main() { int base; int base; int exp; int exp; int result; int result; scanf("%d", &base); scanf("%d", &base); scanf("%d", &exp); scanf("%d", &exp); result = power(base, exp); result = power(base, exp); printf("%d", result); printf("%d", result); exit(0); exit(0);} #... main function, continued # Last lecture, we # finished with this... # jal power # Remove arguments, they # are no longer needed. # 2 * 4 = 8 bytes. addu $sp, $sp, 8 # Store return value # in result. sw $v0, -4($fp) # result # Print result. li $v0, 1 lw $a0, -4($fp) # result syscall # deallocate main’s locals addu $sp, $sp, 12 li $v0, 10 # exit syscall

28 20/06/2015CSE1303 Part B lecture notes 28 Function calling convention  In summary, caller: 1.saves temporary registers by pushing their values on stack 2.pushes arguments 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 $v0

29 20/06/2015CSE1303 Part B lecture notes 29 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

30 20/06/2015CSE1303 Part B lecture notes 30 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

31 20/06/2015CSE1303 Part B lecture notes 31 Recursion example /* Main program to call factorial function. */ factorial function. */ #include #include int factorial(int p); int main() { int n; int n; scanf("%d", &n); scanf("%d", &n); printf("%d", factorial(n)); printf("%d", factorial(n)); exit(0); exit(0);} 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 n $fp = 0x7FFF001C  $sp = 0x7FFF0018 

32 20/06/2015CSE1303 Part B lecture notes 32 Recursion example /* Main program to call factorial function. */ factorial function. */ #include #include int factorial(int p); int main() { int n; int n; scanf("%d", &n); scanf("%d", &n); printf("%d", factorial(n)); printf("%d", factorial(n)); exit(0); exit(0);} 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 n $fp = 0x7FFF001C  $sp = 0x7FFF0018  3 arg 1 (p) argument is at 0($sp)

33 20/06/2015CSE1303 Part B lecture notes 33 Recursion example: caller /* Main program to call factorial function. */ factorial function. */ #include #include int factorial(int p); int main() { int n; int n; scanf("%d", &n); scanf("%d", &n); printf("%d", factorial(n)); printf("%d", factorial(n)); exit(0); exit(0);}.text main: # 1 * 4 = 4 bytes local. move $fp, $sp subu $sp, $sp, 4 li $v0, 5 syscall sw $v0, -4($fp) # n # Call factorial. # 1 * 4 = 4 bytes arg. subu $sp, $sp, 4 lw $t0, -4($fp) # n sw $t0, 0($sp) # arg 1 jal factorial # Clear argument. addu $sp, $sp, 4 move $a0, $v0 # returned li $v0, 1 # print int syscall # deallocate main’s locals addu $sp, $sp, 4 li $v0, 10 # exit syscall

34 20/06/2015CSE1303 Part B lecture notes 34 Recursion example: caller /* Main program to call factorial function. */ factorial function. */ #include #include int factorial(int p); int main() { int n; int n; scanf("%d", &n); scanf("%d", &n); printf("%d", factorial(n)); printf("%d", factorial(n)); exit(0); exit(0);}.text main: # 1 * 4 = 4 bytes local. move $fp, $sp subu $sp, $sp, 4 li $v0, 5 syscall sw $v0, -4($fp) # n # Call factorial. # 1 * 4 = 4 bytes arg. subu $sp, $sp, 4 lw $t0, -4($fp) # n sw $t0, 0($sp) # arg 1 jal factorial # Clear argument. addu $sp, $sp, 4 move $a0, $v0 # returned li $v0, 1 # print int syscall # deallocate main’s locals add $sp, $sp, 4 li $v0, 10 # exit syscall

35 20/06/2015CSE1303 Part B lecture notes 35 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;}

36 20/06/2015CSE1303 Part B lecture notes 36 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result;...... 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 n $fp = 0x7FFF000C  $sp = 0x7FFF0008  3p 0x00400048 saved $ra 0x7FFF001C saved $fp ???result result is at -4($fp) p is at 8($fp)

37 20/06/2015CSE1303 Part B lecture notes 37 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} factorial: # Function entry. subu $sp, $sp, 8 sw $ra, 4($sp) sw $fp, 0($sp) move $fp, $sp # 1 * 4 = 4 bytes local. subu $sp, $sp, 4 # if (p <= 1)... lw $t0, 8($fp) # p bgt $t0, 1, rec # result = 1 li $t0, 1 sw $t0, -4($fp) # result j end # Continued...

38 20/06/2015CSE1303 Part B lecture notes 38 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} factorial: # Function entry. subu $sp, $sp, 8 sw $ra, 4($sp) sw $fp, 0($sp) move $fp, $sp # 1 * 4 = 4 bytes local. subu $sp, $sp, 4 # if (p <= 1)... lw $t0, 8($fp) # p bgt $t0, 1, rec # result = 1 li $t0, 1 sw $t0, -4($fp) # result j end # Continued...

39 20/06/2015CSE1303 Part B lecture notes 39 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} #... Continued rec: # Recursive call. # 1 * 4 = 4 bytes arg. subu $sp, $sp, 4 # argument 1 = p-1 lw $t0, 8($fp) # p sub $t0, $t0, 1 # p-1 sw $t0, 0($sp) # arg 1 jal factorial # Clean up argument. addu $sp, $sp, 4 # Multiply by p. lw $t0, 8($fp) # p mul $t0, $v0, $t0 # Store result. sw $t0, -4($fp) # result # Continued...

40 20/06/2015CSE1303 Part B lecture notes 40 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} #... Continued rec: # Recursive call. # 1 * 4 = 4 bytes arg. subu $sp, $sp, 4 # argument 1 = p-1 lw $t0, 8($fp) # p sub $t0, $t0, 1 # p-1 sw $t0, 0($sp) # arg 1 jal factorial # Clean up argument. addu $sp, $sp, 4 # Multiply by p. lw $t0, 8($fp) # p mul $t0, $v0, $t0 # Store result. sw $t0, -4($fp) # result # Continued...

41 20/06/2015CSE1303 Part B lecture notes 41 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} #... Continued again end: # return result lw $v0, -4($fp) # result # Destroy local variable addu $sp, $sp, 4 # Function exit. lw $fp, 0($sp) lw $ra, 4($sp) addu $sp, $sp, 8 jr $ra

42 20/06/2015CSE1303 Part B lecture notes 42 Recursion example: callee /* The factorial function. */ int factorial(int p) { int result; int result; if (p <= 1) if (p <= 1) { /* Base case. */ /* Base case. */ result = 1; result = 1; } else else { /* Recursive case. */ /* Recursive case. */ result = result = factorial(p-1) * p; factorial(p-1) * p; } return result; return result;} #... Continued again end: # return result lw $v0, -4($fp) # result # Destroy local variable addu $sp, $sp, 4 # Function exit. lw $fp, 0($sp) lw $ra, 4($sp) addu $sp, $sp, 8 jr $ra

43 20/06/2015CSE1303 Part B lecture notes 43 Recursion 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 $fp = 0x7FFF001C  $sp = 0x7FFF0018  stack frame of main during main, about to call factorial(3) 3n

44 20/06/2015CSE1303 Part B lecture notes 44 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFF001C  $sp = 0x7FFF0014  stack frame of main passing argument to factorial(3), just before jal 3n

45 20/06/2015CSE1303 Part B lecture notes 45 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFF000C  stack frame of main after entry to factorial(3) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFF0008  stack frame of factorial(3) p > 1, so must recurse

46 20/06/2015CSE1303 Part B lecture notes 46 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFF000C  stack frame of main about to call factorial(2) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFF0004  stack frame of factorial(3) 2 arg 1 (p)

47 20/06/2015CSE1303 Part B lecture notes 47 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFFC  stack frame of main after entry to factorial(2) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFEFFF8  stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp ???result stack frame of factorial(3) p > 1, so must recurse

48 20/06/2015CSE1303 Part B lecture notes 48 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFFC  stack frame of main about to call factorial(1) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFEFFF4  stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp ???result stack frame of factorial(3) 1 arg 1 (p)

49 20/06/2015CSE1303 Part B lecture notes 49 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFEC  stack frame of main after entry to factorial(1) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFEFFE8  stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp ???result stack frame of factorial(3) 1 arg 1 (p) 0x004000B4 saved $ra 0x7FFEFFFC saved $fp ???result stack frame of factorial(1) p <= 1, so result = 1

50 20/06/2015CSE1303 Part B lecture notes 50 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFEC  stack frame of main about to return from factorial(1) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFEFFE8  stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp ???result stack frame of factorial(3) 1 arg 1 (p) 0x004000B4 saved $ra 0x7FFEFFFC saved $fp 1result stack frame of factorial(1) return result (1) in $v0 $v0 = 1

51 20/06/2015CSE1303 Part B lecture notes 51 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFFC  stack frame of main returned back to factorial(2) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp 2result stack frame of factorial(3) result = return value (1) × p (2) $v0 = 1 $sp = 0x7FFEFFF8 

52 20/06/2015CSE1303 Part B lecture notes 52 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFEFFFC  stack frame of main about to return from factorial(2) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp ???result $sp = 0x7FFEFFF8  stack frame of factorial(2) 2 arg 1 (p) 0x004000B4 saved $ra 0x7FFF000C saved $fp 2result stack frame of factorial(3) $v0 = 2 return result (2) in $v0

53 20/06/2015CSE1303 Part B lecture notes 53 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFF000C  stack frame of main returned back to factorial(3) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp 6result stack frame of factorial(3) $v0 = 2 result = return value (2) × p (3) $sp = 0x7FFF0008 

54 20/06/2015CSE1303 Part B lecture notes 54 Recursion 3 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 arg 1 (p) $fp = 0x7FFF000C  stack frame of main about to return from factorial(3) 3n 0x00400048 saved $ra 0x7FFF001C saved $fp 6result $sp = 0x7FFF0008  stack frame of factorial(3) $v0 = 6 return result in $v0

55 20/06/2015CSE1303 Part B lecture notes 55 Recursion 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 $fp = 0x7FFF001C  3n $v0 = 6 returned back to main stack frame of main $sp = 0x7FFF0018  print out return value in $v0 (6)

56 20/06/2015CSE1303 Part B lecture notes 56 Recursion 0x7FFF001C 0x7FFF0018 0x7FFF0014 0x7FFF0010 0x7FFF000C 0x7FFF0008 0x7FFF0004 0x7FFF0000 0x7FFEFFFC 0x7FFEFFF8 0x7FFEFFF4 0x7FFEFFF0 0x7FFEFFEC 0x7FFEFFE8 $fp = 0x7FFF001C  3n $sp = 0x7FFF0018  $v0 = 6 print out return value in $v0 (6) stack frame of main

57 20/06/2015CSE1303 Part B lecture notes 57 Covered in this lecture  Accessing function parameters  Returning from functions  Recursion

58 20/06/2015CSE1303 Part B lecture notes 58 Going further  Official MIPS stack frame convention  doesn’t use $fp at all!  slightly more efficient than CSE1303 convention  can be generated by compilers  hard for humans to write/understand

59 20/06/2015CSE1303 Part B lecture notes 59 Next time  Analysis of translation  efficiency  How to write better C code Reading: Lecture notes section B16


Download ppt "20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15."

Similar presentations


Ads by Google