Write a program that ask the user to input a number between 0-10 (n). Then your
ID: 3552023 • Letter: W
Question
Write a program that ask the user to input a number between 0-10 (n). Then your program finds the factorial of the number
You need to ask user for input (n) and check to see if the number is between 0-10. If the number is not between 0-10 then you need to ask for another number. if the number is between 0-10 then you pass the number to Fact subroutine to calculate n! and send the result to main for printing.
Explanation / Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 fact: slti $t0, $a0, 2 # if i < 2 (i.e i == 1) beq $t0, $zero, cont # if i >= 2 go to cont addi $v0, $zero, 1 # else make the resturn value 1 jr $ra cont: # OPERATION 1: save into stack addi $sp, $sp, -8 # make space in the stack sw $ra, 0($sp) # save the return address sw $a0, 4($sp) # save the argument value # OPERATION 2: compute fact(n - 1) addi $a0, $a0, -1 jal fact # OPERATION 3: restore from stack lw $ra, 0($sp) # get old return address from stack lw $a0, 4($sp) # get old argument value from stack addi $sp, $sp, 8 # return stack pointer to original value, thus erasing all values # OPERATION 4: finally n * fact(n - 1) mult $v0, $a0 # multiply n * fib(n - 1) mflo $v0 # gets the result of the multiplication from the low register jr $raRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.