Question
The attached C# program (Recursion_Demo.cs) demonstrates the concept of recursion. The program inputs the value of n from the user and then the recursive RDemo
The attached C# program (Recursion_Demo.cs) demonstrates the concept of recursion. The program inputs the value of n from the user and then the recursive RDemo method prints the following sequence:
n (n-1) (n-2) 1 1 2 3 n
Execute the program and check it out for yourself. You will write a MIPS program which MUST use a non-leaf procedure to implement this recursive method. Iterative implementation of the program will NOT be accepted. Your program will define a non-leaf procedure RDemo which will be called recursively for n>=1. RDemo will return when n<1. Your program will print the user prompt and take n as an input in the main program. The output on the screen must be just similar to the C# program - there will be space between the integers. Obviously, your MIPS program will not implement the concept of class.
C# code is blow
using System;
public static class LearnRecursion
{
public static void Main()
{
int n;
Console.Write("Enter an integer: ");
n = Convert.ToInt32(Console.ReadLine());
RDemo(n);
}
public static void RDemo(int n)
{
if (n < 1)
{
return;
}
else
{
Console.Write("{0} ", n);
RDemo(n - 1);
Console.Write("{0} ", n);
return;
}
}
}
Right now I have already down some parts of the code but error on line 18 and line 29. The MIPS code is below.
.text main: # Ask user to enter an integer n li $v0, 4 la $a0, prompt syscall # Get the value of n and set it to t1 li $v0, 5 syscall add $t1, $v0, $zero # Call non-leaf procedure RDemo jal RDemo # Stop the program li $v0, 10 syscall RDemo: beq $t1, -1, END addi $sp # Print the value of n li $v0, 1 move $a0, $t1 syscall # Call non-leaf procedure RDemo sub $t1, $t1, 1 jal RDemo # Print the value of n li $v0, 1 move $a0, $t1 addi $t1 syscall END: jr $ra
.data prompt: .asciiz "Enter an integer:"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started