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

main: Sa0, msg_str # load the address of the message \"Enter some Number : \" #

ID: 3907435 • Letter: M

Question

main: Sa0, msg_str # load the address of the message "Enter some Number : " # prepare for syscall 4, printing string to the user # syscall 4 # prepare for syscall 5, reading integer from the user # syscall 5 # Now input integer is in $v0 . We cocy it to $a0 for syscall $vO, 5 $a0, $v0 fac syscall move function call # call "fac” function and jump to fac tag # Now the result of fac (n) is in $v0. Copy it to $a0 for mov $a0, v0 sysc 1 # prepare for syscall 1, printing an integer # syscall 1 # prepare for syscall 10, finish syscall $v0,10 syscall fac "ret-one" if input is 0 branch # # make room for stack push . we must do this before recursive $a0, $zero, ret-one to addi $sp, $sp,-8 call Sa0,0 (Ssp) Sra ,4(Ssp) $a0 , $a0 ,-1 fac Sto,o (Ssp) Sra ,4 (Ssp) $sp, $sp,8 Sw SW addi # push input n to the stack # push return address to the stack # recursive call #pop input n back from the stack # pop return address from the stack # restore the stack addi # do n!-(n-1): * n, and prepare value n! for return # exit procedure mul ret ret one: $vO,1 # prepare value 1 for return ret:

Explanation / Answer

ScreenShot

------------------------------------------------------------------------------

Code

#variable declaration
.data
     PromptMessage: .asciiz "Please enter a number to find factorial:"
     Factorial: .asciiz "Factorial of the given number="
     mulOperator: .asciiz "*"
     equal: .asciiz "="   
     number: .word 0
#main program
.text
     #Prompt to read number
     la $a0,PromptMessage
     li $v0,4
     syscall
     #read user entered number
     li $v0,5
     syscall
     #store number in number specified address
     sw $v0,number
     #result prompt
     la $a0,Factorial
     li $v0,4
     syscall
     #load number in a0 for function call
     lw $a0,number
     #call fact finding function
     jal fact
     #result store in to
     move $t0,$v0
     #before result display add equal sign
     la $a0,equal
     li $v0,4
     syscall
     #move reult into a0 for display
     move $a0,$t0
     li $v0,1
     syscall
     #end of the program
     li $v0,10
     syscall
#Recursively factorial finding function
fact:
     #stach space to return values
     subu $sp,$sp,8
     sw $ra,0($sp)
     sw $s0,4($sp)
     #store number in t0
     move $t0,$a0
     #display number
     li $v0,1
     syscall
     #counter to check the number until 1
     beq $t0,1,returnFact
     #display mult symbol
     la $a0,mulOperator
     li $v0,4
     syscall
     #store each result in s0
     move $s0,$t0
     #decrement counter to get next number
     sub $t0,$t0,1
     #move number into ao for recursive function call
     move $a0,$t0
     jal fact
     #multiply each numbers and store result in v0
     mul $v0,$s0,$v0
returnFact:
     #restore stack space
     lw $ra,0($sp)
     lw $s0,4($sp)
     addu $sp,$sp,8
     #return from function call result in v0
     jr $ra

-------------------------------------------------------------------

Output

Please enter a number to find factorial:5
Factorial of the given number=5*4*3*2*1=120
-- program is finished running --

------------------------------------------------------------

Note:-

I assume you need to display result in this format.

If you need any clarification , let me know