Answer to Question #327085 in Assembler for Faisal

Question #327085

Write an Assembly language program that calculate the factorial of an integer number.

1
Expert's answer
2022-04-11T02:07:36-0400
.data
Input:    .asciiz "\nEnter the number: "
Output:    .asciiz "! = "
ErrorOutput:    .asciiz "Error, negative input is not defined"
OverflowOutput:    .asciiz "Out of boundary, overflow"
endP:    .asciiz "\n\nEnd of program"

# code segment
    .text
    .globl main
main:

    li $v0, 4                 # Print string
    la $a0, Input            # "Enter the number:"
    syscall
    
# Take integer    
    li $v0, 5               # Read integer into $v0.
    syscall
 
    add $a0, $v0, $zero       # Pass integer to input argument register $a0
    slt $t7,$a0,0            # IF user entered number <0
    beq $t7,$0,, nextTest
                            # THEN  
    li $v0, 4                 # Print string
    la $a0, ErrorOutput        # "Error, negative input is not defined"
    syscall
    j exitP    
    
nextTest:
    blt $a0,13, calculate    # IF user entered number <13 THEN calculate    
    li $v0, 4                 # ELSE Print string
    la $a0, OverflowOutput    # "Out of boundary, overflow"
    syscall    
    j exitP        

calculate:
    li $v0, 1             # Print integer number from $a0.
    syscall
    
    jal fact        # jmp the Factorial Function.
                    # the input argument in register $a0             
                    # Function "fact" returns the result in $v0.    
    add $t0, $v0, $zero     # Move factorial result into register $t0
    
# display the result of the Factorial Function
    li $v0, 4       # Print string
    la $a0, Output    # "! = "
    syscall
    
    add $a0, $t0, $zero     # Move Factorial to $a0
    li $v0, 1                 # Print integer from $a0.
    syscall    

exitP:      nop
    li $v0, 4                 # Print string
    la $a0, endP            # "End of program"
    syscall
    
    li $v0, 10            # finished .. stop .. return
    syscall                # make the syscall
    
##################################################
# function     Factorial
# input argument in $a0
# result in $v0
##################################################
fact:        
# base case
    bgt $a0, 0, recurion    # IF (n <= 0)  
    li $v0, 1                 # $v0 = 1 (return register)
    jr $ra                     # return  

recurion:                     # ELSE
    addi $sp, $sp, -8         # stack push (space for two words)
    sw $a0, 4($sp)             # save  n to stack
    sw $ra, 0($sp)             # save $ra (return addr) to stack
    addi $a0, $a0, -1         # (n – 1)
    jal fact                  # v0 = factorial(n – 1)
    
    addi $a0, $a0, 1         #next n
    mul $v0, $v0, $a0         #n*(n-1)
    lw $ra, 0($sp)            # retun $ra from stack
    lw $a0, 4($sp)             # return n from stack
    addi $sp, $sp, 8         # stack pop
    jr $ra                     # return 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS