Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 mysum: 2 bne $a0, $al, ms_recurse 3 move $v0, $al 4 jr $ra Recursion Now we're going to make a recursive procedure (a procedure

image text in transcribed

1 mysum: 2 bne $a0, $al, ms_recurse 3 move $v0, $al 4 jr $ra 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{CO)}; C{D0}; 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. on 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. 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, ($sp) 15 add $sp,$sp, 8 16 jr $ra 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 In C int maxsum (int s, int f if (s==f) Return f; else Return strxsumis+1, f); 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. (note that the else is actually.ceduodaot, but I put it there for readability) So in C type language that would be the MIPS conversion of that is You need to make this work (notice that $a0 and Sai are never set) int_addnumbers(int smaller, int larger) if (smaller == larger) return smaller; else return larger + addnumbers smaller, larger - 1); 1 mysum: 2 bne $a0, $al, ms_recurse 3 move $v0, $al 4 jr $ra 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{CO)}; C{D0}; 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. on 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. 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, ($sp) 15 add $sp,$sp, 8 16 jr $ra 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 In C int maxsum (int s, int f if (s==f) Return f; else Return strxsumis+1, f); 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. (note that the else is actually.ceduodaot, but I put it there for readability) So in C type language that would be the MIPS conversion of that is You need to make this work (notice that $a0 and Sai are never set) int_addnumbers(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

Students also viewed these Databases questions