Question: USING FUNCTIONS FACT AND LI WRITE A MAIN PROGRAM TO CALCULATE FACTORIAL fact: slti $t0, $a0, 1 # test for n < 1 beq $t0,

USING FUNCTIONS FACT AND LI WRITE A MAIN PROGRAM TO CALCULATE FACTORIAL

fact:

slti $t0, $a0, 1 # test for n < 1

beq $t0, $zero, L1 # if n >= 1, go to L1

li $v0, 1 # return 1

jr $ra # return to instruction after jal

L1:

addi $sp, $sp, -8 # adjust stack for 2 items

sw $ra, 4($sp) # save the return address

sw $a0, 0($sp) # save the argument n

addi $a0, $a0, -1 # n >= 1; argument gets (n 1)

jal fact # call fact with (n 1)

lw $a0, 0($sp) # return from jal: restore argument n

lw $ra, 4($sp) # restore the return address

addi $sp, $sp, 8 # adjust stack pointer to pop 2 items

mul $v0, $a0, $v0 # return n * fact (n 1)

jr $ra # return to the caller

#THIS IS WHAT I HAVE SO FAR, CAN YOU PLEASE HELP ME CORRECT, IT DOES NOT NEED TO BE RECURSIVE IT JUST NEEDS TO BE A SIMPLE LOOP FACTORIAL #PROGRAM

.text

main:

la $a0, greet # display greetings

li $v0, 3

syscall # syscall on print greetings

dri:

la $a0, enter # display message that asks reader

li $v0, 3

syscall # system call on print question

li $v0, 6 # value of n is read

syscall # system call

slt $t0, $v0, $zero # condition of program end

bne $t0, $zero, close # if user types a number less than zero it closes

move $s0, $v0 # save value of n move $a0, $v0 # place value of n in $a0

jal fact # call fact function

move $s1, $v0 # fact value is saved

move $a0, $s0 # result is displayed

li $v0, 1 # value of n is printed

syscall # system call

la $a0, answer # displays the textual portion of the answers

li $v0, 3

syscall # system call

move $a0, $s1 # print factorial

li $v0, 1

syscall # system call

la $a0, newline # display newline

li $v0, 3

syscall # system call

j dri # process is repeated until user decides

close:

la $a0, bye # farewell message

li $v0, 3

syscall # system call

li $v0, 10 # exit from the program

syscall.data

.data

greet: .asciiz "Welcome to the factorial tester! "

enter: .asciiz "Enter a value for n (or a negative value to exit): "

answer: .asciiz "! is "

newline: .asciiz " "

bye: .asciiz "Come back soon! "

# end fact.s

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!