Top Banner
03/25/22 CSE1303 Part B lecture notes 1 Functions, part 2 Functions, part 2 Lecture B15 Lecture B15 Lecture notes, section B15 Lecture notes, section B15
59

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

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.

04/18/23 CSE1303 Part B lecture notes 1

Functions, part 2Functions, part 2

Lecture B15Lecture B15

Lecture notes, section B15Lecture notes, section B15

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

04/18/23 CSE1303 Part B lecture notes 2

Last timeLast time

Function callingFunction calling jaljal and and jrjr instructions instructions

Calling conventionCalling convention for making a function callfor making a function call

Structure of stackStructure of stack stack framesstack frames

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

04/18/23 CSE1303 Part B lecture notes 3

In this lectureIn this lecture

Accessing function parametersAccessing function parameters Returning from functionsReturning from functions RecursionRecursion

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

04/18/23 CSE1303 Part B lecture notes 4

Stack framesStack frames

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

44

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp = =

0x7FFF0384 0x7FFF0384

$sp$sp = = 0x7FFF0380 0x7FFF0380 resultresult 11

powerpower’s ’s stack framestack frame

mainmain’s ’s stack framestack frame

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

04/18/23 CSE1303 Part B lecture notes 5

Local variablesLocal variables

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

44

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp = =

0x7FFF0384 0x7FFF0384

$sp$sp = = 0x7FFF0380 0x7FFF0380 resultresult 11

powerpower’s ’s local local

variables variables are are

accessed as accessed as mainmain’s were, ’s were,

with with negative negative

offsets from offsets from $fp$fp

resultresult is at is at -4($fp)-4($fp)

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

04/18/23 CSE1303 Part B lecture notes 6

Function parametersFunction parameters

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

44

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp = =

0x7FFF0384 0x7FFF0384

$sp$sp = = 0x7FFF0380 0x7FFF0380 resultresult 11

bb and and ee are are respectively respectively accessible at accessible at +8($fp)+8($fp) and and +12($fp)+12($fp)

powerpower’s ’s parameters parameters (arguments) (arguments)

are are accessible accessible

with with positivepositive offset from offset from

$fp$fp

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

04/18/23 CSE1303 Part B lecture notes 7

Example: calleeExample: callee/* A function that gets called. *//* A function that gets called. */

int power(int b, int e)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# ... Continued from# last lecture.# last lecture.loop: loop: # Stop if e <= 0.# Stop if e <= 0. lw $t0, 12($fp) lw $t0, 12($fp) # e# e ble $t0, 0, endble $t0, 0, end

# result = result * b# result = result * b lw $t0, -4($fp) lw $t0, -4($fp) # result# result lw $t1, 8($fp) lw $t1, 8($fp) # b# b mul $t0, $t0, $t1mul $t0, $t0, $t1 sw $t0, -4($fp) sw $t0, -4($fp) # result# result

# e--# e-- lw $t0, 12($fp) lw $t0, 12($fp) # e# e addi $t0, $t0, -1addi $t0, $t0, -1 sw $t0, 12($fp) sw $t0, 12($fp) # e# e

# Repeat loop.# Repeat loop. j loopj loop

end:end: # Now ready to return. # Now ready to return.# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 8

Example: calleeExample: callee/* A function that gets called. *//* A function that gets called. */

int power(int b, int e)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# ... Continued from# last lecture.# last lecture.loop: loop: # Stop if e <= 0.# Stop if e <= 0. lw $t0, 12($fp) lw $t0, 12($fp) # e# e ble $t0, 0, endble $t0, 0, end

# result = result * b# result = result * b lw $t0, -4($fp) lw $t0, -4($fp) # result# result lw $t1, 8($fp) lw $t1, 8($fp) # b# b mul $t0, $t0, $t1mul $t0, $t0, $t1 sw $t0, -4($fp) sw $t0, -4($fp) # result# result

# e--# e-- lw $t0, 12($fp) lw $t0, 12($fp) # e# e addi $t0, $t0, -1addi $t0, $t0, -1 sw $t0, 12($fp) sw $t0, 12($fp) # e# e

# Repeat loop.# Repeat loop. j loopj loop

end:end: # Now ready to return. # Now ready to return.# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 9

Function returnFunction return

When When returningreturning from a function, from a function, stack must be restoredstack must be restored to its initial to its initial statestate

Achieved by Achieved by undoing stepsundoing steps made made during calling of function, during calling of function, in in reverse orderreverse order

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

04/18/23 CSE1303 Part B lecture notes 10

Function return Function return conventionconvention

On On function exitfunction exit, , calleecallee::5.5. chooses chooses return valuereturn value

by setting register by setting register $v0$v0, if necessary, if necessary

6.6. deallocates local deallocates local variablesvariables by popping by popping allocated spaceallocated space

7.7. restores restores $fp$fp by by popping its saved popping its saved value off stackvalue off stack

8.8. restores restores $ra$ra by by popping its saved popping its saved value off stackvalue off stack

9.9. returnsreturns with with jr $rajr $ra

On On returnreturn from from function, function, callercaller::4.4. clears function clears function

argumentsarguments by popping by popping allocated spaceallocated space

5.5. restores saved restores saved temporary registerstemporary registers by by popping their values popping their values off stackoff stack

6.6. usesuses the the return valuereturn value found in $v0, if found in $v0, if necessarynecessary

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

04/18/23 CSE1303 Part B lecture notes 11

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp = =

0x7FFF0384 0x7FFF0384

$sp$sp = = 0x7FFF0380 0x7FFF0380 resultresult 8181

callee step 5: put callee step 5: put return value in return value in

register register $v0$v0

no effect visible no effect visible on stackon stack

$v0 = 81$v0 = 81

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

04/18/23 CSE1303 Part B lecture notes 12

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp = =

0x7FFF0384 0x7FFF0384

$sp$sp = = 0x7FFF0380 0x7FFF0380 resultresult 8181

callee step 6: callee step 6: deallocate local deallocate local

variables by variables by popping allocated popping allocated

space off stackspace off stack

one local variable one local variable to deleteto delete

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

04/18/23 CSE1303 Part B lecture notes 13

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$fp$fp == $sp$sp ==

0x7FFF03840x7FFF0384

callee step 6: callee step 6: deallocate local deallocate local

variables by variables by popping allocated popping allocated

space off stackspace off stack

one local variable one local variable to deleteto delete

result is now “gone”result is now “gone”

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

04/18/23 CSE1303 Part B lecture notes 14

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

0x0040005C0x0040005C

0x7FFF03A00x7FFF03A0

saved $rasaved $ra

saved $fpsaved $fp$sp$sp = =

0x7FFF0384 0x7FFF0384

callee steps 7 and callee steps 7 and 8: restore saved 8: restore saved values of $fp and values of $fp and $ra by popping off $ra by popping off

stackstack

can do both these can do both these steps at oncesteps at once

$fp$fp = 0x7FFF03A0 = 0x7FFF03A0

$ra$ra = 0x0040005C = 0x0040005C

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

04/18/23 CSE1303 Part B lecture notes 15

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

callee steps 7 and callee steps 7 and 8: restore saved 8: restore saved values of $fp and values of $fp and $ra by popping off $ra by popping off

stackstack

$fp$fp = 0x7FFF03A0 = 0x7FFF03A0

$ra$ra = 0x0040005C = 0x0040005C

$sp$sp = = 0x7FFF038C 0x7FFF038C

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

popping popping $fp$fp makes it point makes it point back to back to mainmain’s stack frame’s stack frame

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

04/18/23 CSE1303 Part B lecture notes 16

Example: calleeExample: callee

33

44

??????resultresult

expexp

basebase

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

0x7FFF03880x7FFF0388

0x7FFF03840x7FFF0384

0x7FFF03800x7FFF0380

$sp$sp = = 0x7FFF038C 0x7FFF038C

callee step 9: callee step 9: return by return by

executing executing jr $rajr $ra

nothing visible on nothing visible on stackstack

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

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

04/18/23 CSE1303 Part B lecture notes 17

Example: calleeExample: callee/* A function that gets called. *//* A function that gets called. */

int power(int b, int e)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;}}

# ... Continue# ... Continuedd

# Return result in $v0.# Return result in $v0. lw $v0, -4($fp) lw $v0, -4($fp) # result# result

# Remove local var.# Remove local var. addu $sp, $sp, 4addu $sp, $sp, 4

# Restore $fp & $ra# Restore $fp & $ra lw $fp, 0($sp)lw $fp, 0($sp) lw $ra, 4($sp)lw $ra, 4($sp) addu $sp, $sp, 8addu $sp, $sp, 8

# Return to caller.# Return to caller. jr $rajr $ra

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

04/18/23 CSE1303 Part B lecture notes 18

Example: calleeExample: callee/* A function that gets called. *//* A function that gets called. */

int power(int b, int e)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;}}

# ... Continue# ... Continuedd

# Return result in $v0.# Return result in $v0. lw $v0, -4($fp) lw $v0, -4($fp) # result# result

# Remove local var.# Remove local var. addu $sp, $sp, 4addu $sp, $sp, 4

# Restore $fp & $ra# Restore $fp & $ra lw $fp, 0($sp)lw $fp, 0($sp) lw $ra, 4($sp)lw $ra, 4($sp) addu $sp, $sp, 8addu $sp, $sp, 8

# Return to caller.# Return to caller. jr $rajr $ra

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

04/18/23 CSE1303 Part B lecture notes 19

Example: callerExample: caller/* C program which calls a/* C program which calls a function. */function. */

#include <stdio.h>#include <stdio.h>#include <stdlib.h>#include <stdlib.h>

int power(int b, int e);int power(int b, int e);

int main()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 = result = power(base, exp)power(base, exp);;

printf("%d", result);printf("%d", result);

exit(0);exit(0);}}

now back in caller, about to now back in caller, about to assign return value of assign return value of

function to function to mainmain’s local ’s local variable variable resultresult

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

04/18/23 CSE1303 Part B lecture notes 20

Example: callerExample: caller

caller step 4: clear caller step 4: clear function function

arguments by arguments by popping them off popping them off

stackstack

bb and and ee are no are no longer needed, longer needed, destroy themdestroy them

33

44

??????resultresult

expexp

basebase

$sp$sp = = 0x7FFF038C 0x7FFF038C

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

arg 2 (e)arg 2 (e)

arg 1 (b)arg 1 (b) 33

00

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

04/18/23 CSE1303 Part B lecture notes 21

Example: callerExample: caller

bb and and ee are no are no longer needed, longer needed, destroy themdestroy them

33

44

??????resultresult

expexp

basebase

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

$sp$sp = = 0x7FFF0394 0x7FFF0394

caller step 4: clear caller step 4: clear function function

arguments by arguments by popping them off popping them off

stackstack

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

04/18/23 CSE1303 Part B lecture notes 22

Example: callerExample: caller

caller step 5: caller step 5: restore saved restore saved

temporary temporary registers by registers by

popping their popping their values off stackvalues off stack

we didn’t save we didn’t save any, so nothing to any, so nothing to

do heredo here

33

44

??????resultresult

expexp

basebase

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

$sp$sp = = 0x7FFF0394 0x7FFF0394

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

04/18/23 CSE1303 Part B lecture notes 23

Example: callerExample: caller

caller step 6: use caller step 6: use return value, return value,

found in register found in register $v0$v0

store returned store returned value into a value into a

variablevariable

33

44

??????resultresult

expexp

basebase

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

$sp$sp = = 0x7FFF0394 0x7FFF0394

$v0 = 81$v0 = 81

main stores main stores return value return value

into local into local variable variable resultresult

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

04/18/23 CSE1303 Part B lecture notes 24

Example: callerExample: caller

caller step 6: use caller step 6: use return value, return value,

found in register found in register $v0$v0

store returned store returned value into a value into a

variablevariable

33

44

8181resultresult

expexp

basebase

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

0x7FFF03900x7FFF0390

0x7FFF038C0x7FFF038C

$sp$sp = = 0x7FFF0394 0x7FFF0394

$v0 = 81$v0 = 81

main stores main stores return value return value

into local into local variable variable resultresult

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

04/18/23 CSE1303 Part B lecture notes 25

Example: callerExample: caller

Done!Done!

after the end of after the end of the function call the function call

the stack has the stack has been returned to been returned to its original stateits original state

33

44

8181resultresult

expexp

basebase

$fp$fp = = 0x7FFF03A0 0x7FFF03A0

0x7FFF039C0x7FFF039C

0x7FFF03980x7FFF0398

0x7FFF03940x7FFF0394

0x7FFF03A00x7FFF03A0

$sp$sp = = 0x7FFF0394 0x7FFF0394

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

04/18/23 CSE1303 Part B lecture notes 26

Example: callerExample: caller/* C program which calls a/* C program which calls a function. */function. */

#include <stdio.h>#include <stdio.h>#include <stdlib.h>#include <stdlib.h>

int power(int b, int e);int power(int b, int e);

int main()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# ... main function, continued # Last lecture, we # Last lecture, we # finished with this ...# finished with this ... ## jal power jal power

# Remove arguments, they# Remove arguments, they # are no longer needed.# are no longer needed. # 2 * 4 = 8 bytes.# 2 * 4 = 8 bytes. addu $sp, $sp, 8addu $sp, $sp, 8

# Store return value# Store return value # in result.# in result. sw $v0, -4($fp) sw $v0, -4($fp) # result# result

# Print result.# Print result. li $v0, 1li $v0, 1 lw $a0, -4($fp) lw $a0, -4($fp) # result# result syscallsyscall

# deallocate main’s locals# deallocate main’s locals addu $sp, $sp, 12addu $sp, $sp, 12 li $v0, 10 li $v0, 10 # exit# exit syscallsyscall

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

04/18/23 CSE1303 Part B lecture notes 27

Example: callerExample: caller/* C program which calls a/* C program which calls a function. */function. */

#include <stdio.h>#include <stdio.h>#include <stdlib.h>#include <stdlib.h>

int power(int b, int e);int power(int b, int e);

int main()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# ... main function, continued # Last lecture, we # Last lecture, we # finished with this ...# finished with this ... ## jal power jal power

# Remove arguments, they# Remove arguments, they # are no longer needed.# are no longer needed. # 2 * 4 = 8 bytes.# 2 * 4 = 8 bytes. addu $sp, $sp, 8addu $sp, $sp, 8

# Store return value# Store return value # in result.# in result. sw $v0, -4($fp) sw $v0, -4($fp) # result# result

# Print result.# Print result. li $v0, 1li $v0, 1 lw $a0, -4($fp) lw $a0, -4($fp) # result# result syscallsyscall

# deallocate main’s locals# deallocate main’s locals addu $sp, $sp, 12addu $sp, $sp, 12 li $v0, 10 li $v0, 10 # exit# exit syscallsyscall

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

04/18/23 CSE1303 Part B lecture notes 28

Function calling Function calling conventionconvention

In summary, In summary, callercaller::1.1. saves temporary registerssaves temporary registers by pushing their by pushing their

values on stackvalues on stack2.2. pushes pushes argumentsarguments on stack on stack3.3. callscalls the function with the function with jaljal instruction instruction ((function runs until it returnsfunction runs until it returns, then:), then:)4.4. clears function argumentsclears function arguments by popping by popping

allocated spaceallocated space5.5. restores saved temporary registersrestores saved temporary registers by by

popping their values off the stackpopping their values off the stack6.6. usesuses the the return valuereturn value found in $v0 found in $v0

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

04/18/23 CSE1303 Part B lecture notes 29

Function calling Function calling conventionconvention

In summary, In summary, calleecallee::1.1. saves saves $ra$ra by pushing its value on stack by pushing its value on stack2.2. saves saves $fp$fp by pushing its value on stack by pushing its value on stack3.3. copies copies $sp$sp to to $fp$fp4.4. allocates allocates local variableslocal variables ((body of function goes herebody of function goes here, then:), then:)5.5. chooses chooses return valuereturn value by setting register by setting register $v0$v06.6. deallocates local variablesdeallocates local variables by popping by popping

allocated spaceallocated space7.7. restores restores $fp$fp by popping its saved value by popping its saved value8.8. restores restores $ra$ra by popping its saved value by popping its saved value9.9. returnsreturns with with jr $rajr $ra

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

04/18/23 CSE1303 Part B lecture notes 30

RecursionRecursion

Function calling convention works Function calling convention works exactly the exactly the samesame for for recursive recursive functionsfunctions don’t need to do anything specialdon’t need to do anything special

Each invocationEach invocation of the function has of the function has its its own stack frameown stack frame local variables and parameterslocal variables and parameters

• with their current valueswith their current values return addressreturn address

• where to return towhere to return to

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

04/18/23 CSE1303 Part B lecture notes 31

Recursion exampleRecursion example/* Main program to call/* Main program to call factorial function. */factorial function. */

#include <stdlib.h>#include <stdlib.h>#include <stdio.h>#include <stdio.h>

int factorial(int p);int factorial(int p);

int main()int main(){{ int n;int n;

scanf("%d", &n);scanf("%d", &n);

printf("%d", factorial(n));printf("%d", factorial(n));

exit(0);exit(0);}}

330x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

nn$fp$fp = 0x7FFF001C = 0x7FFF001C

$sp$sp = 0x7FFF0018 = 0x7FFF0018

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

04/18/23 CSE1303 Part B lecture notes 32

Recursion exampleRecursion example/* Main program to call/* Main program to call factorial function. */factorial function. */

#include <stdlib.h>#include <stdlib.h>#include <stdio.h>#include <stdio.h>

int factorial(int p);int factorial(int p);

int main()int main(){{ int n;int n;

scanf("%d", &n);scanf("%d", &n);

printf("%d", printf("%d", factorial(n)factorial(n)););

exit(0);exit(0);}}

330x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

nn$fp$fp = 0x7FFF001C = 0x7FFF001C

$sp$sp = 0x7FFF0018 = 0x7FFF0018 33arg 1 (p)arg 1 (p)argument is at argument is at

0($sp)0($sp)

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

04/18/23 CSE1303 Part B lecture notes 33

Recursion example: callerRecursion example: caller/* Main program to call/* Main program to call factorial function. */factorial function. */

#include <stdlib.h>#include <stdlib.h>#include <stdio.h>#include <stdio.h>

int factorial(int p);int factorial(int p);

int main()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.textmain:main: # 1 * 4 = 4 bytes local. # 1 * 4 = 4 bytes local. move $fp, $spmove $fp, $sp subu $sp, $sp, 4subu $sp, $sp, 4

li $v0, 5li $v0, 5 syscallsyscall sw $v0, -4($fp) sw $v0, -4($fp) # n# n

# Call factorial.# Call factorial. # 1 * 4 = 4 bytes arg.# 1 * 4 = 4 bytes arg. subu $sp, $sp, 4subu $sp, $sp, 4 lw $t0, -4($fp) lw $t0, -4($fp) # n# n sw $t0, 0($sp) sw $t0, 0($sp) # arg 1# arg 1 jal factorialjal factorial # Clear argument.# Clear argument. addu $sp, $sp, 4addu $sp, $sp, 4 move $a0, $v0move $a0, $v0 # returned # returned li $v0, 1 li $v0, 1 # print int# print int syscallsyscall

# deallocate main’s locals# deallocate main’s locals addu $sp, $sp, 4addu $sp, $sp, 4 li $v0, 10 li $v0, 10 # exit# exit syscallsyscall

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

04/18/23 CSE1303 Part B lecture notes 34

Recursion example: callerRecursion example: caller/* Main program to call/* Main program to call factorial function. */factorial function. */

#include <stdlib.h>#include <stdlib.h>#include <stdio.h>#include <stdio.h>

int factorial(int p);int factorial(int p);

int main()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.textmain:main: # 1 * 4 = 4 bytes local. # 1 * 4 = 4 bytes local. move $fp, $spmove $fp, $sp subu $sp, $sp, 4subu $sp, $sp, 4

li $v0, 5li $v0, 5 syscallsyscall sw $v0, -4($fp) sw $v0, -4($fp) # n# n

# Call factorial.# Call factorial. # 1 * 4 = 4 bytes arg.# 1 * 4 = 4 bytes arg. subu $sp, $sp, 4subu $sp, $sp, 4 lw $t0, -4($fp) lw $t0, -4($fp) # n# n sw $t0, 0($sp) sw $t0, 0($sp) # arg 1# arg 1 jal factorialjal factorial # Clear argument.# Clear argument. addu $sp, $sp, 4addu $sp, $sp, 4 move $a0, $v0move $a0, $v0 # returned # returned li $v0, 1 li $v0, 1 # print int# print int syscallsyscall

# deallocate main’s locals# deallocate main’s locals add $sp, $sp, 4add $sp, $sp, 4 li $v0, 10 li $v0, 10 # exit# exit syscallsyscall

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

04/18/23 CSE1303 Part B lecture notes 35

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

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

04/18/23 CSE1303 Part B lecture notes 36

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

......

330x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

nn

$fp$fp = 0x7FFF000C = 0x7FFF000C

$sp$sp = 0x7FFF0008 = 0x7FFF0008

33pp

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

resultresult is at is at -4($fp)-4($fp)

pp is at is at 8($fp)8($fp)

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

04/18/23 CSE1303 Part B lecture notes 37

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

factorial: factorial: # Function entry.# Function entry. subu $sp, $sp, 8subu $sp, $sp, 8 sw $ra, 4($sp)sw $ra, 4($sp) sw $fp, 0($sp)sw $fp, 0($sp) move $fp, $spmove $fp, $sp # 1 * 4 = 4 bytes local.# 1 * 4 = 4 bytes local. subu $sp, $sp, 4subu $sp, $sp, 4

# if (p <= 1) ...# if (p <= 1) ... lw $t0, 8($fp) # plw $t0, 8($fp) # p bgt $t0, 1, recbgt $t0, 1, rec

# result = 1# result = 1 li $t0, 1li $t0, 1 sw $t0, -4($fp) sw $t0, -4($fp) # result# result j endj end

# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 38

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

factorial: factorial: # Function entry.# Function entry. subu $sp, $sp, 8subu $sp, $sp, 8 sw $ra, 4($sp)sw $ra, 4($sp) sw $fp, 0($sp)sw $fp, 0($sp) move $fp, $spmove $fp, $sp # 1 * 4 = 4 bytes local.# 1 * 4 = 4 bytes local. subu $sp, $sp, 4subu $sp, $sp, 4

# if (p <= 1) ...# if (p <= 1) ... lw $t0, 8($fp) # plw $t0, 8($fp) # p bgt $t0, 1, recbgt $t0, 1, rec

# result = 1# result = 1 li $t0, 1li $t0, 1 sw $t0, -4($fp) sw $t0, -4($fp) # result# result j endj end

# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 39

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

# # ... Continued... Continuedrec: rec: # Recursive call.# Recursive call. # 1 * 4 = 4 bytes arg.# 1 * 4 = 4 bytes arg. subu $sp, $sp, 4subu $sp, $sp, 4

# argument 1 = p-1# argument 1 = p-1 lw $t0, 8($fp) lw $t0, 8($fp) # p# p sub $t0, $t0, 1 sub $t0, $t0, 1 # p-1# p-1 sw $t0, 0($sp) sw $t0, 0($sp) # arg 1# arg 1

jal factorialjal factorial

# Clean up argument.# Clean up argument. addu $sp, $sp, 4addu $sp, $sp, 4

# Multiply by p.# Multiply by p. lw $t0, 8($fp) lw $t0, 8($fp) # p# p mul $t0, $v0, $t0mul $t0, $v0, $t0 # Store result.# Store result. sw $t0, -4($fp) sw $t0, -4($fp) # result# result

# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 40

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

# # ... Continued... Continuedrec: rec: # Recursive call.# Recursive call. # 1 * 4 = 4 bytes arg.# 1 * 4 = 4 bytes arg. subu $sp, $sp, 4subu $sp, $sp, 4

# argument 1 = p-1# argument 1 = p-1 lw $t0, 8($fp) lw $t0, 8($fp) # p# p sub $t0, $t0, 1 sub $t0, $t0, 1 # p-1# p-1 sw $t0, 0($sp) sw $t0, 0($sp) # arg 1# arg 1

jal factorialjal factorial

# Clean up argument.# Clean up argument. addu $sp, $sp, 4addu $sp, $sp, 4

# Multiply by p.# Multiply by p. lw $t0, 8($fp) lw $t0, 8($fp) # p# p mul $t0, $v0, $t0mul $t0, $v0, $t0 # Store result.# Store result. sw $t0, -4($fp) sw $t0, -4($fp) # result# result

# Continued ...# Continued ...

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

04/18/23 CSE1303 Part B lecture notes 41

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

# # ... Continued again... Continued again

end: # return resultend: # return result lw $v0, -4($fp) # resultlw $v0, -4($fp) # result

# Destroy local variable# Destroy local variable addu $sp, $sp, 4addu $sp, $sp, 4

# Function exit.# Function exit. lw $fp, 0($sp)lw $fp, 0($sp) lw $ra, 4($lw $ra, 4($ssp)p) addu $sp, $sp, 8addu $sp, $sp, 8 jr $rajr $ra

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

04/18/23 CSE1303 Part B lecture notes 42

Recursion example: calleeRecursion example: callee/* The factorial function. *//* The factorial function. */

int factorial(int p)int factorial(int p){{ int result;int result;

if (p <= 1)if (p <= 1) {{ /* Base case. *//* Base case. */ result = 1;result = 1; }} elseelse {{ /* Recursive case. *//* Recursive case. */ result =result = factorial(p-1) * p;factorial(p-1) * p; }}

return result;return result;}}

# # ... Continued again... Continued again

end: # return resultend: # return result lw $v0, -4($fp) # resultlw $v0, -4($fp) # result

# Destroy local variable# Destroy local variable addu $sp, $sp, 4addu $sp, $sp, 4

# Function exit.# Function exit. lw $fp, 0($sp)lw $fp, 0($sp) lw $ra, 4($lw $ra, 4($ssp)p) addu $sp, $sp, 8addu $sp, $sp, 8 jr $rajr $ra

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

04/18/23 CSE1303 Part B lecture notes 43

RecursionRecursion

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

$fp$fp = 0x7FFF001C = 0x7FFF001C

$sp$sp = 0x7FFF0018 = 0x7FFF0018 stack frame stack frame of of mainmain

during during mainmain, about , about to call to call

factorial(3)factorial(3)

33nn

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

04/18/23 CSE1303 Part B lecture notes 44

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFF001C = 0x7FFF001C

$sp$sp = 0x7FFF0014 = 0x7FFF0014

stack frame stack frame of of mainmain

passing argument passing argument to to factorial(3)factorial(3), ,

just before just before jaljal

33nn

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

04/18/23 CSE1303 Part B lecture notes 45

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFF000C = 0x7FFF000C

stack stack frame of frame of mainmain

after entry to after entry to factorial(3)factorial(3)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult$sp$sp = 0x7FFF0008 = 0x7FFF0008

stack frame of stack frame of factorial(3)factorial(3)

pp > 1, so must > 1, so must recurserecurse

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

04/18/23 CSE1303 Part B lecture notes 46

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFF000C = 0x7FFF000C

stack stack frame of frame of mainmain

about to call about to call factorial(2)factorial(2)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFF0004 = 0x7FFF0004

stack frame of stack frame of factorial(3)factorial(3)

22arg 1 (p)arg 1 (p)

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

04/18/23 CSE1303 Part B lecture notes 47

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFFC = 0x7FFEFFFC

stack stack frame of frame of mainmain

after entry to after entry to factorial(2)factorial(2)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFEFFF8 = 0x7FFEFFF8

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

??????resultresult

stack frame of stack frame of factorial(3)factorial(3)

pp > 1, so > 1, so must must

recurserecurse

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

04/18/23 CSE1303 Part B lecture notes 48

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFFC = 0x7FFEFFFC

stack stack frame of frame of mainmain

about to call about to call factorial(1)factorial(1)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFEFFF4 = 0x7FFEFFF4

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

??????resultresult

stack frame of stack frame of factorial(3)factorial(3)

11arg 1 (p)arg 1 (p)

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

04/18/23 CSE1303 Part B lecture notes 49

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFEC = 0x7FFEFFEC

stack stack frame of frame of mainmain

after entry to after entry to factorial(1)factorial(1)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFEFFE8 = 0x7FFEFFE8

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

??????resultresult

stack frame of stack frame of factorial(3)factorial(3)

11arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFEFFFC0x7FFEFFFCsaved $fpsaved $fp??????resultresult

stack frame of stack frame of factorial(1)factorial(1)

pp <= 1, so <= 1, so resultresult = 1 = 1

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

04/18/23 CSE1303 Part B lecture notes 50

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFEC = 0x7FFEFFEC

stack stack frame of frame of mainmain

about to about to return from return from

factorial(1)factorial(1)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFEFFE8 = 0x7FFEFFE8

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

??????resultresult

stack frame of stack frame of factorial(3)factorial(3)

11arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFEFFFC0x7FFEFFFCsaved $fpsaved $fp11resultresult

stack frame of stack frame of factorial(1)factorial(1)

return return result result (1) (1)

in in $v0$v0

$v0$v0 = 1 = 1

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

04/18/23 CSE1303 Part B lecture notes 51

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFFC = 0x7FFEFFFC

stack stack frame of frame of mainmain

returned back returned back to to

factorial(2)factorial(2)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

22resultresult

stack frame of stack frame of factorial(3)factorial(3)

resultresult = = return value return value (1) × (1) × pp (2) (2)

$v0$v0 = 1 = 1

$sp$sp = 0x7FFEFFF8 = 0x7FFEFFF8

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

04/18/23 CSE1303 Part B lecture notes 52

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FFEFFFC = 0x7FFEFFFC

stack stack frame of frame of mainmain

about to return about to return from from

factorial(2)factorial(2)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp??????resultresult

$sp$sp = 0x7FFEFFF8 = 0x7FFEFFF8

stack frame of stack frame of factorial(2)factorial(2)

22arg 1 (p)arg 1 (p)

0x004000B40x004000B4saved $rasaved $ra

0x7FFF000C0x7FFF000Csaved $fpsaved $fp

22resultresult

stack frame of stack frame of factorial(3)factorial(3)

$v0$v0 = 2 = 2

return return result result (2) (2)

in in $v0$v0

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

04/18/23 CSE1303 Part B lecture notes 53

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FF = 0x7FFF000CF000C

stack stack frame of frame of mainmain

returned back returned back to to

factorial(3)factorial(3)

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp66resultresult

stack frame of stack frame of factorial(3)factorial(3)

$v0$v0 = 2 = 2

resultresult = = return value return value (2) × (2) × pp (3) (3)

$sp$sp = 0x7FF = 0x7FFF0008F0008

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

04/18/23 CSE1303 Part B lecture notes 54

RecursionRecursion

33

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

arg 1 (p)arg 1 (p)

$fp$fp = 0x7FF = 0x7FFF000CF000C

stack stack frame of frame of mainmain

about to return about to return fromfrom

factorial(factorial(33))

33nn

0x004000480x00400048saved $rasaved $ra

0x7FFF001C0x7FFF001Csaved $fpsaved $fp66resultresult$sp$sp = 0x7FF = 0x7FFF0008F0008

stack frame of stack frame of factorial(3)factorial(3)

$v0$v0 = = 66

return return resultresult in in

$v0$v0

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

04/18/23 CSE1303 Part B lecture notes 55

RecursionRecursion

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

$fp$fp = 0x7FF = 0x7FFF001CF001C

33nn

$v0$v0 = = 66

returned back to returned back to mainmain

stack stack frame of frame of mainmain

$sp$sp = 0x7FF = 0x7FFF0018F0018

print out return print out return value in value in $v0$v0 (6) (6)

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

04/18/23 CSE1303 Part B lecture notes 56

RecursionRecursion

0x7FFF001C0x7FFF001C

0x7FFF00180x7FFF0018

0x7FFF00140x7FFF0014

0x7FFF00100x7FFF0010

0x7FFF000C0x7FFF000C

0x7FFF00080x7FFF0008

0x7FFF00040x7FFF0004

0x7FFF00000x7FFF0000

0x7FFEFFFC0x7FFEFFFC

0x7FFEFFF80x7FFEFFF8

0x7FFEFFF40x7FFEFFF4

0x7FFEFFF00x7FFEFFF0

0x7FFEFFEC0x7FFEFFEC

0x7FFEFFE80x7FFEFFE8

$fp$fp = 0x7FF = 0x7FFF001CF001C

33nn$sp$sp = 0x7FF = 0x7FFF0018F0018

$v0$v0 = = 66

print out return print out return value in value in $v0$v0 (6) (6)

stack stack frame of frame of mainmain

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

04/18/23 CSE1303 Part B lecture notes 57

Covered in this lectureCovered in this lecture

Accessing function parametersAccessing function parameters Returning from functionsReturning from functions RecursionRecursion

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

04/18/23 CSE1303 Part B lecture notes 58

Going furtherGoing further

Official MIPS stack frame Official MIPS stack frame conventionconvention doesn’t use doesn’t use $fp$fp at all! at all! slightly more efficient than CSE1303 slightly more efficient than CSE1303

conventionconvention can be generated by compilerscan be generated by compilers hard for humans to write/understandhard for humans to write/understand

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

04/18/23 CSE1303 Part B lecture notes 59

Next timeNext time

Analysis of translationAnalysis of translation efficiencyefficiency

How to write better C codeHow to write better C code

Reading:Reading:

Lecture notes section B16Lecture notes section B16