Presentation is loading. Please wait.

Presentation is loading. Please wait.

Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures

Similar presentations


Presentation on theme: "Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures"— Presentation transcript:

1 Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
Csci136 Computer Architecture II Lab#4 - Stack and Nested Procedures - Project#1: due on Feb 13, Quiz#1 Feb.9, 2005

2 Stack and Nested Procedures
Why? -may be modified by others… Must be saved before you go! Return address Local parameters Others How? -save to stacks! Before you go… addi $sp,$sp, -framesize sw $ra, framesize-4($sp) # save $ra save other regs if need be Before you return… restore other regs if need be lw $ra, framesize-4($sp) # restore $ra addi $sp,$sp, framesize jr $ra

3 Example: Fibonacci Numbers 1/7
Definition: F(n) = F(n – 1) + F(n – 2), F(0) and F(1) are defined to be 1. C Programming: int fib(int n) { if(n == 0) { return 1; }   if(n == 1) { return 1; }   return (fib(n - 1) + fib(n - 2)); }

4 Example: Fibonacci Numbers 2/7
Now, let’s translate this to MIPS! You will need space for three words on the stack Why three? Return addr($ra) n($a0) temp sum($s0) Write the Prologue: # Space for three words # Save the return address # Save $s0 addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) fib: ___________________

5 Example: Fibonacci Numbers 3/7
Now write the Epilogue: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 # Restore $s0 # Restore return address # Pop the stack frame # Return to caller fin: ___________________ jr $ra_____________

6 Example: Fibonacci Numbers 4/7
Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2)); } # $v0 = 1 # # $t0 = 1 addi $v0, $zero, 1_ _______, _____,_fin addi $t0, $zero, 1_ _______,______,_fin Continued on next slide beq $a0 $zero beq $a0 $t0 ___________________ __if (n == 0) ______ ____________ __if (n == 1)

7 Example: Fibonacci Numbers 5/7
Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { return (fib(n - 1) + fib(n - 2)); } # $a0 = n - 1 # addi $a0, $a0, -1__ sw____, ___________ ___________________ lw____,____________ addi $a0, ___,_____ $a0 0($sp) jal fib $a0, -1 Continued on next slide ___________________ __Need $a0 after jal _ fib(n – 1) ______ __Restore $a0______ __$a0 = n – 2_________

8 Example: Fibonacci Numbers 6/7
Remember that $v0 is caller saved! int fib(int n) { return (fib(n - 1) + fib(n - 2)); } # Place fib(n – 1) # somewhere it won’t get # clobbered # add $s0,_ ___,______ ___________________ add $v0, $v0, $s0__ To the epilogue and beyond _______ $v0 $zero jal fib ____________________ __fib(n – 2) __________ __$v0 = fib(n-1) + fib(n-2)

9 Example: Fibonacci Numbers 7/7
Here’s the complete code for reference fib: addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) addi $v0, $zero, 1 beq $a0, $zero, fin addi $t0, $zero, 1 beq $a0, $t0, fin addi $a0, $a0, -1 sw $a0, 0($sp) jal fib lw $a0, 0($sp) addi $a0, $a0, -1 add $s0, $v0, $zero jal fib add $v0, $v0, $s0 fin: lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra

10 Exercise Show the content of the stack when computing Fib(2).
Assume the top of the stack is at address 0x7FFF and initially the stack is empty. fib(2)=fib(1)+fib(0) fib(1)=fib(0)=1

11 Project#1: MergeSort Procedure Mergesort (input A[1:n], i,j; output B[1:n]) begin Datatype C[1:n]; If i=j then B[i] = A[i]; Return; endif Mergesort (A,i,(i+j)/2;C); /* sorts the first half*/ Mergesort (A,(i+j)/2 +1,j;C); /* sorts the second half*/ Merge(C,i,j;B); /*merges the two sorted halves * *into a single sorted list */ end

12 Project#1: MergeSort Example:

13 Project#1: MergeSort

14 Quiz#1 Give the MIPS code to load a large constant number 0xABCD9876 into register $s0. Why not lw? Compile the C code A[5]=A[4]+100 into MIPS code, where A is an integer array. MIPS Addressing mode Immediate addressing Register addressing Base/Displacement addressing PC-relative addressing

15 Quiz#1 Specify the types of the following MIPS instructions. Describe the changes to PC after each instruction is executed. Lw $s5, 8($sp) Jr $31 Beq $1, $2, 100 J, Jr, Jal …

16 Quiz#1 if(b == 0) { return 0; } if (even(b)) return fmult(2*a,b/2);
Fast multiplication: int fmult(int a,b) { if(b == 0) { return 0; } if (even(b)) return fmult(2*a,b/2); if (odd(b)) return (a+fmult(a, b-1)); } Use stack in your programming! Nested Procedures


Download ppt "Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures"

Similar presentations


Ads by Google