Question: a program which reads in two integers (s and f), and then uses a recursive procedure to add up the values from s to f

 a program which reads in two integers (s and f), and

  1. a program which reads in two integers (s and f), and then uses a recursive procedure to add up the values from s to f in Assembly

(So in C type language that would be

int addnumbers(int smaller, int larger) {

if (smaller == larger)

return smaller;

else

return larger + addnumbers(smaller, larger - 1);

}

Recursion Now we're going to make a recursive procedure (a procedure that calls itself). This is conceptually very similar to the previous example except the program can have a fairly large stack. 1 mysum: 2 bne $a0, $al, ms_recurse 3 move $v0, $al 4 jr fra Let's say we have 3 programs. A calls B, B calls C, C calls D. A{ B()}; B{C()}; C{D()}; D{something}. The way this actually works is D finishes first, gets popped off the execution stack then C gets the result of D, finishes what it was doing, gets popped off, B gets the result of C, finishes, pops off the stack, then A gets the result of B and finishes. Recursion is essentially the same, except it's the same function, just with different parameters at each step. Well, hopefully. If you keep giving it the same parameters, you'll get an infinite loop. We will make a simple recursive function, given two integers (s and f) we will have a recursive function that adds all of the integers from s tof 6 ms_recurse: 7 sub $sp, $sp, 8 8 sw $ra, ($sp) 9 sw $a0, 4($sp) 10 add $a0, $al, 1 11 jal mysum 12 lw $a0, 4($sp) 13 add $v0, $v0, $a0 14 lw $ra, 0($sp) 15 add $sp, $sp, 8 16 jr $ra In C int moySum (int s, int f){ if (s==f) Return f; else Return s+mysun(+1, f); (note that the else is actually redundant, but I put it there for readability) Lab Completion for Today 1. Make a program which reads in two integers (s and f), and then uses a recursive procedure to add up the values from s to f. the MIPS conversion of that is You need to make this work (notice that $a0 and $a1 are never set) (So in C type language that would be int addoumbers(int smaller, int larger) if (smaller == larger) return smaller; else return larger + addnumbers (smaller, larger - 1); Recursion Now we're going to make a recursive procedure (a procedure that calls itself). This is conceptually very similar to the previous example except the program can have a fairly large stack. 1 mysum: 2 bne $a0, $al, ms_recurse 3 move $v0, $al 4 jr fra Let's say we have 3 programs. A calls B, B calls C, C calls D. A{ B()}; B{C()}; C{D()}; D{something}. The way this actually works is D finishes first, gets popped off the execution stack then C gets the result of D, finishes what it was doing, gets popped off, B gets the result of C, finishes, pops off the stack, then A gets the result of B and finishes. Recursion is essentially the same, except it's the same function, just with different parameters at each step. Well, hopefully. If you keep giving it the same parameters, you'll get an infinite loop. We will make a simple recursive function, given two integers (s and f) we will have a recursive function that adds all of the integers from s tof 6 ms_recurse: 7 sub $sp, $sp, 8 8 sw $ra, ($sp) 9 sw $a0, 4($sp) 10 add $a0, $al, 1 11 jal mysum 12 lw $a0, 4($sp) 13 add $v0, $v0, $a0 14 lw $ra, 0($sp) 15 add $sp, $sp, 8 16 jr $ra In C int moySum (int s, int f){ if (s==f) Return f; else Return s+mysun(+1, f); (note that the else is actually redundant, but I put it there for readability) Lab Completion for Today 1. Make a program which reads in two integers (s and f), and then uses a recursive procedure to add up the values from s to f. the MIPS conversion of that is You need to make this work (notice that $a0 and $a1 are never set) (So in C type language that would be int addoumbers(int smaller, int larger) if (smaller == larger) return smaller; else return larger + addnumbers (smaller, larger - 1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!