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

The function f (n) = n! is recursively as F(n) = n ? F (n ? 1). Write a program

ID: 1847080 • Letter: T

Question

The function f (n) = n! is recursively as F(n) = n ? F (n ? 1). Write a program that calculates the factorial

of a number. The name of this program should be factorial.s.

Explanation / Answer

.data # data items, i.e. all initialized and # non-initialized items go in here #strings for making the output look nice str1: .asciiz "Please enter a positive integer: " str2: .asciiz "The factorial of " str3: .asciiz " is " str4: .asciiz "Number negative WRONG ! " newline: .asciiz " " ## Assume that the compiler chooses to use registers for n,fact,i: ## $t0 = n (user entered character) ## $t1 = fact (running fact of the first n integers) ## $t2 = i (integer to be added into sum, runs from 0 to n) ## $t3 = tmp (used for comparison of i and n) .text .globl main # declaration of main as a global variable main: li $t0, 0 li $t1, 1 li $t2, 1 li $t3, 0 puts str1 li $v0, 5 ## replace "get n" of SAL code since MAL syscall ## does not have "get" move $t0, $v0 ## puts newline if : sub $t3, $t0, $t3 ## if n > 0 bltz $t3, else then: while: sub $t3, $t0, $t2 bltz $t3, endwhile loop: mul $t1, $t1, $t2 add $t2, $t2, 1 b while endwhile: puts str2 li $v0, 1 ## replacement of "put n" of SAL code move $a0, $t0 syscall puts str3 li $v0, 1 ## replace "put sum" of SAL code move $a0, $t1 syscall puts newline b end_if # here starts the else part else: puts str4 li $v0, 1 ## replace "put sum" of SAL code move $a0, $t1 syscall puts newline end_if: # the last line of main done # end of main # any subroutines may be inserted here:... # END OF PROGRAM (leave this line here)

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