Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using MARS program (MIPS) 1. Write a program that ask the user to input a number

ID: 3594487 • Letter: U

Question

Using MARS program (MIPS)

1.

Write a program that ask the user to input a number between 0-10 (n). Then your program finds the factorial of the number (n!).

n(!) = 1*2*3*4....*(n-1)*n.
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.
Sample input and output could be:

Enter an integer between 0-10 -1
Invalid number
Enter an integer between 0-10 0

(0!)=1

Enter an integer between 0-10 4
(4!)=24

Enter an integer between 0-10 12
Invalid Number
Enter an integer between 0-10 6

(6!)=720

Explanation / Answer

Given below is the code with output . Please don't forget to rate the answer if it helped. thank you .


.data
prompt: .asciiz "Enter a number in range 0-10: "
invalidmsg: .asciiz "Invalid number "
openbracket : .asciiz "("
closebracket: .asciiz "!) = "
.text
ask:
#prompt and get an int input and save to $t0
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $t0, $v0
blt $t0, 0, invalid #if number is < 0, its invalid input
bgt $t0, 10, invalid #if number > 10 , its invalid
b valid
invalid:
#show error message
li $v0, 4
la $a0, invalidmsg
syscall
b ask
valid:
#set up parameter and call factloop function
move $a0, $t0
jal factorial

#get the result from $v0 and store in $t1
move $t1, $v0

#display the results
li $v0, 4
la $a0, openbracket
syscall

li $v0, 1
move $a0, $t0
syscall


li $v0, 4
la $a0, closebracket
syscall

li $v0, 1
move $a0, $t1
syscall

#exit
li $v0, 10
syscall

#-----------------------------------
factorial:
li $t3, 1 #the factorial initailized to 1
loop1:
ble $a0, 1, end_factloop
mul $t3, $t3, $a0
sub $a0, $a0, 1
b loop1
end_factloop:
move $v0, $t3 #store the return value in v0
jr $ra

output

Enter a number in range 0-10: -1
Invalid number
Enter a number in range 0-10: 0
(0!) = 1
-- program is finished running --

Enter a number in range 0-10: 4
(4!) = 24
-- program is finished running --

Enter a number in range 0-10: 12
Invalid number
Enter a number in range 0-10: 720
Invalid number
Enter a number in range 0-10: 6
(6!) = 720
-- program is finished running --

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote