Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The prof gave his program: fact: slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 # if n >= 1,

The prof gave his program:

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

Starting with that code, add a driver program that will allow you to test the factorial function. Here is a sample execution of the final program:

Welcome to the factorial tester!

Enter a value for n (or a negative value to exit): 0 0! is 1

Enter a value for n (or a negative value to exit): 6 6! is 720

Enter a value for n (or a negative value to exit): 9 9! is 362880

Enter a value for n (or a negative value to exit): 11 11! is 39916800

Enter a value for n (or a negative value to exit): 20 20! is -2102132736

Enter a value for n (or a negative value to exit): 100 100! is 0

Enter a value for n (or a negative value to exit): 3 3! is 6

Enter a value for n (or a negative value to exit): -1

Come back soon!

Your program should produce output that mirrors mine. Be sure to document your code (as I did in leaf_example.s). Submit a separate file called fact.s as well as placing your code in this assignment submission; the Mentor will clarify what I mean by this. Look carefully at the execution results. Clearly, there is a problem! There is no way that 20 factorial is a negative number! What is the largest value of n that we can use and get a correct result? What is that correct result (you might want to verify with a calculator)? Notice that I get a value of 0 for 100 factorial. Likewise, for 101 factorial, and so on. What is the largest value of n that produces a non-zero result (of course, it is still an incorrect result)? What do you think the problem is here?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions