You are to write a program in MIPS that computes N! using recursion. Remember N! is the product of all the numbers from 1 to N inclusive, that is 1 x 2 x 3 x (N – 1) x N. It is defined as 1 for N = 0 and is undefined for values less than 0. The programs first requests the user to input the value of N (display a prompt first so the user knows what to do). If the input value is less than 0, the program is to display “N! undefined for values less than 0” and then requests the user to input the value of N again. If the value input is non-negative, it is to compute N! using a recursive function, that is one that calls itself. PLEASE DON'T FORGET TO INCLUDE COMMENTS Since this is an assembly language program, I expect to see comments on almost every line of code in the program. Also, make the code neat: line up the command fields, the register fields, and the comment fields.
You are to write a
The programs first requests the user to input the value of N (display a prompt first so the user knows what to do). If the input value is less than 0, the program is to display “N! undefined for values less than 0” and then requests the user to input the value of N again. If the value input is non-negative, it is to compute N! using a recursive function, that is one that calls itself.
PLEASE DON'T FORGET TO INCLUDE COMMENTS
Since this is an assembly language program, I expect to see comments on almost every line of code in the program. Also, make the code neat: line up the command fields, the register fields, and the comment fields.
MIPS assembly language code is needed to compute the factorial of a number N.
As per the requirement, the MIPS code is given below that calculates the factorial of a number.
- The main program prompts the user to enter a non-negative integer (value >=0).
- It reads the input using a system call and checks if the input is non-negative.
- If the input is negative, the error message is printed. Ask for input again.
- If the input is 0 or positive, the program calls the factorial function.
- The factorial function checks if n is 0. If true, it returns 1 (base case).
- If n is not equal to 0, the function makes a recursive call to factorial(n-1) and multiplies the result by n to get the factorial of n.
- The function uses the stack to save the return address and allocate.
Step by step
Solved in 4 steps with 3 images