Question: MIPS Assembly Code help I've written the code below but I need help with the factorial method. Can you please explain the steps? Thank you.

MIPS Assembly Code help

I've written the code below but I need help with the factorial method. Can you please explain the steps? Thank you.

.text main: # Prompt user to input non-negative number la $a0,prompt li $v0,4 syscall

li $v0,5 #Read the number(n) syscall

move $t2,$v0 # moves n to $t2.

# Call function to get sum_odd_factorial #n move $a0,$t2 #alternatively, addi $a0, $t2, 0 jal sum_odd_factorial #call sum_odd_factorial (n) move $t3,$v0 #result is in $t3

# Output message and n la $a0,result #Print C_ li $v0,4 syscall

move $a0,$t2 #Print n li $v0,1 syscall

la $a0,result2 #Print = li $v0,4 syscall

move $a0,$t3 #Print the answer li $v0,1 syscall

la $a0,endl #Print ' ' li $v0,4 syscall

# End program li $v0,10 syscall

#int sum_odd_factorial(int n){int sum = 0; #for(int i = 0; i <= n;i+2)sum+=factorial(i); #return sum;} sum_odd_factorial: # Compute and return sum_odd_factorial number #Push $ra $s0, $s1, $s2 to stack addi $sp,$sp,-16 sw $ra,0($sp) #storing return address in stack sw $s0,4($sp) #storing first saved register in stack sw $s1,8($sp) #storing second saved register in stack sw $s2,12($sp) #storing third saved register in stack

move $s0, $a0 #n is now stored in $s0 move $s2, $zero #sum is now stored in $s2 and initialized to zero addi $s1, $zero, 1 #i is stored in $s1 and initialized to 1 for: slt $t0, $s0, $s1 # THIS IS $s1, $s0 bne $t0, $zero, exit #body of for loop move $a0, $s1 jal factorial add $s2, $s2, $v0 #sum += factorial(i) #end of for loop addi $s1, $s1, 2 j for exit: move $v0, $s2 #returning sum lw $ra,0($sp) #restoring return address from stack lw $s0,4($sp) #restoring first saved register from stack lw $s1,8($sp) #restoring second saved register in stack lw $s2,12($sp) #restoring third saved register from stack addi $sp,$sp,16 jr $ra

#int factorial(int n){ # int x = 1; for loop (x *=i) #} factorial: #compute the factorial of a number

.data prompt: .asciiz "This program calculates the sum of an odd factorial. Enter a non-negative number less than 100: " result: .asciiz "C_" result2: .asciiz " = " endl: .asciiz " "

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!