Write and debug a MIPS program that computes factorial of a given number iterati
ID: 3906049 • Letter: W
Question
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 like the Python code as in the practice problem. 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 have the same prompts to the user and output format and 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. Do not consult code available online for this problem, other than the high level algorithm – you should write your assembly code using only your knowledge of MIPS for register/memory manipulation.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
.data
prompt: .asciiz "Enter a number: "
msg: .asciiz "factorial is "
.text
#prompt and get an int input and save to $t0
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $t0, $v0
#calculate
li $t3, 1 #the factorial initailized to 1
loop1:
ble $t0, 1, end_loop1
mul $t3, $t3, $t0
sub $t0, $t0, 1
b loop1
end_loop1:
#display the results
li $v0, 4
la $a0, msg
syscall
li $v0, 1
move $a0, $t3
syscall
#exit
li $v0, 10
syscall
output
=====
Enter a number: 5
factorial is 120
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.