# Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.

## Presentation on theme: "Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003."— Presentation transcript:

Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003

2 MIPS instructions Arithmetic –add –add immediate (addi) –load immediate (li) –sub –subtract immediate (subi) –multiply (mult) Data transfer –load byte (lb) –load word (lw) –store byte (sb) –store word (sw) Conditional branch –branch on equal (beq) –branch on not equal (bne) –set on less than (slt) Unconditional jump –jump (j) –jump register (jr)

3 Use of new instructions Program counter (PC): An immediate is a constant (ordinary number).

4 Calling conventions Convention observed by caller and callee Analogies –Spies –Classes –Car use

5 Conventions for borrowing car

6 Caller’s responsibilities Place arguments where procedure can access them (\$a0..\$a3) Transfer control addi \$a0, \$zero, 3 addi \$a1, \$zero, 6 j sum Example: sum(3, 6)

7 Callee’s responsibilities Do the work, using the arguments in \$a0..\$a3 Put return value where caller can access it (\$v0) Return control to the calling procedure public int sum(int a, int b) { return a+b; } sum: 200: 204:

8 Caller’s responsibilities Place arguments where procedure can access them (\$a0..\$a3) Save current program counter before transferring control: jump and link (jal) –\$ra  PC+4 –PC  Example: sum(3, 6) 0: addi \$a0, \$zero, 3 4: addi \$a1, \$zero, 6 8: jal sum 12:...

9 Sample call Caller 200: add \$a0, \$zero, 3 204: add \$a1, \$zero, 6 208: jal sum 212:... Callee sum: 500: add \$v0, \$a0, \$a1 504: jr \$ra Program counter (PC): Return address (\$ra):

10 Practice public int ____(int a, int b) { if (a < b) return b; else return a; }

11 Factorial public int fact (int n) { if (n == 0) return 1; else return n * fact(n-1); } fact: # n is in \$a0 bne \$a0, \$zero, else_clause addi \$v0, \$zero, 1 j return else_clause: return:

12 Factorial (2) fact: bne \$a0, \$zero, else_clause # compare n to 0 addi \$v0, \$zero, 1 # return value = 1 jr \$ra # return to caller else_clause: #Save \$ra and \$a0 subi \$a0, \$a0, 1 # n = n - 1 jal fact # make recursive call #Restore old values of \$ra and \$a0 mult \$v0, \$v0, \$a0 # multiply fact(n-1)*n jr \$ra # return to caller

13 Where? Registers Memory –Fixed location –Stack

14 Stack Operations –push –pop Implementation –A free region of memory –Top of stack indicated by stack pointer (\$sp)

15 Implementation Stack pointer (\$sp) points to last element pushed; e.g., \$sp = 216. Push –Decrement stack pointer –Store value to top of stack Pop –Load value from top of stack –Increment stack pointer

16 Optimization Straightforward code # Push \$r1 # Push \$r2 # Push \$r3 Optimized code

17 Factorial (3) else_clause: subi \$sp, \$sp, __ # create space on stack for two items sw \$ra, 4(\$sp) # store \$ra on stack sw \$a0, 0(\$sp) # store \$a0 on stack subi \$a0, \$a0, 1 # n = n - 1 jal fact # make recursive call lw \$ra, __(\$sp) # restore \$ra from stack lw \$a0, __(\$sp) # restore \$a0 from stack addi \$sp, \$sp, __ # restore \$sp to original value mult \$v0, \$v0, \$a0 # multiply fact(n-1)*n jr \$ra # return to caller

18 Register convention “caller saved” “callee saved”

19 Practice fact(5) + fact(6) put 5 in \$a0 call fact save result of fact(5) put 6 in \$a0 call fact add result of fact(6) to saved result of fact(5)

20 Procedure to-do list Allocate any needed storage Save callee-saved registers that might be modified To make a procedure call –Save caller-saved registers on stack –Put arguments in \$a0..\$a3 –Jump and link (jal) –Restore caller-saved registers from stack Restore callee-saved registers Put return value in \$ra Jump to the return address (jr \$ra)

21 Factorial (4) fact: # Store callee-saved values subi \$sp, \$sp, 8 # create space on stack for two items sw \$ra, 4(\$sp) # store \$ra (return address) on stack sw \$a0, 0(\$sp) # store \$a0 (n) on stack bne \$a0, \$zero, else_clause # if (n>0) goto else_clause # Base case addi \$v0, \$zero, 1 # prepare to return the value 1 addi \$sp, \$sp, 8 # return stack pointer to its original value jr \$ra # return to caller else_clause: subi \$a0, \$a0, 1 # n = n - 1 jal fact # make recursive call lw \$ra, 4(\$sp) # restore \$ra from stack lw \$a0, 0(\$sp) # restore \$a0 from stack addi \$sp, \$sp, 8 # return stack pointer to its original value mult \$v0, \$v0, \$a0 # multiply fact(n-1)*n jr \$ra # return to caller

Download ppt "Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003."

Similar presentations