Question: 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



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. 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 to f. In C: int mySum (int s, int f){ if (s==f) return f; else return s+mySum(s+1,f); \} the MIPS conversion of that is 1 mysum: 2 bne $a0,$a1,ms_recurse 3 move $v0,$a1 4 jr $ra 5 6 ms_recurse: 7 sub $sp,$sp,8 8 sw $ra,($sp) 9 sw $a0,4($sp) 10 add $a0,$a0,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 Ine IvIIrs code given above is tor the recursion mysum tunction. You neea to compiete tne code to make it work (notice that $a0 and $a1 are never set) Lab Completion for Today - Make a program which reads in two integers (s and f). - Display the result on the screen. The following image shows an example of the output screen. This program uses a recursion function to sum from s to f. For example, if s equals 1 and f equals 5 , then sum equals 15 . Please enter s: 1 Please enter f:5 15 -- program is finished running
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
