MIPS program: 1. Write and debug a MIPS program that computes factorial of a giv
ID: 3860001 • Letter: M
Question
MIPS program: 1. Write and debug a MIPS program that computes factorial of a given number iteratively (not recursively). This is a MIPS program with looping.
Instructions and Hints: Begin by writing the factorial calculation in your favorite high-level programming language and including the entire program as a comment in your MIPS program . Your high-level code must work correctly for any value from 0! upward (remember that 0! =1 by definition) for answers that will fit in 32 bits. Your high level language implementation and your MIPS implementation must use the SAME algorithm (same type of loop, same number of and location of branches, no try statements etc.) You will want to use an algorithm that will minimize the number of branches necessary to make the assembly version simpler
Explanation / Answer
Solution:-
The factorial program is implemented iteratively in C language. Where user asked to enter the number and stores in integer variable a1. Another variable a0 is taken as result and multiplicand. The final result is stored in a0 then print a0. To calculate factorial while loop is used.
The same program developed in MIPS assembly language. Similar iterative method is used to calculate factorial using while loop. The MIPS assembly program is included full comments to ease of understand. Also pseudocode is given in comments to prove that both C program and MIPS assembly program uses same algorithm and same loop to calculate factorial.
---------------------------------------------------------------------------------------------------
The C implementation is given below -
---------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a1,a0;
a0=1;
clrscr();
printf("Enter a Number to Find Factorial: ");
scanf("%d",&a1);
while(a1>1)
{
a0 *= a1;
--a1;
}
printf("The Factorial of %d is : %d",a1,a0);
getch();
}
---------------------------------------------------------------------------------------------------
The MIPS assembly implementation is given below -
---------------------------------------------------------------------------------------------------
#while(a1>1) {
# a0 *= a1;
# --a1;
#}
#getch();
---------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.