Question: 2 . c ) Implement a version of your program that allows: ( i ) the user to enter a value for the variable n

2.c) Implement a version of your program that allows: (i) the user to enter a value for
the variable n ; and (ii) prints the value of fib(n) in the terminal.
2c only please, this is the code I already have. Please build upon this:
main:
li $a0,3 # Initialize n to 3
jal fib # Call fib(n)
move $a0, $v0 # Move result to $a0 for printing
# Exit program
li $v0,10
syscall
fib:
addi $sp, $sp,-8 # Allocate stack space
sw $ra,4($sp) # Save return address
sw $a0,0($sp) # Save argument n
li $v0,0 # Default return value 0
beq $a0, $zero, end # if (n ==0) return 0
li $v0,1 # Default return value 1
li $t0,1
beq $a0, $t0, end # if (n ==1) return 1
# Calculate fib(n-1)
addi $a0, $a0,-1
jal fib
move $t1, $v0 # Save fib(n-1) in $t1
# Calculate fib(n-2)
lw $a0,0($sp)
addi $a0, $a0,-2
jal fib
add $v0, $v0, $t1 # fib(n)= fib(n-1)+ fib(n-2)
end:
lw $a0,0($sp) # Restore argument n
lw $ra,4($sp) # Restore return address
addi $sp, $sp,8 # Deallocate stack space
jr $ra # Return
2 . c ) Implement a version of your program that

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 Programming Questions!