Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Anyone can find the mistake in this mips. # C code: # int fib(int n){ # if(n<= 1

ID: 3915220 • Letter: A

Question

Anyone can find the mistake in this mips.

# C code:
# int fib(int n){
# if(n<= 1){
# return n;
# }
# else{
# return fib(n-1)+fib(n-2);
# }
# }


#MIPS code:
.data
# a massage for user to input the number n
masg1: .asciiz "Enter a number: "
# a massage for print out the result for the user
masg2: .asciiz "The result is "


.text
main:
# print out the massage1 to the user
li $v0, 4
la $a0, masg1
syscall
# get the input n from the user
li $v0, 5
syscall
# save the input n in $s2
move $s2, $v0
# put the $s2 == n in to $a0 then call fib
move $a0, $s2
jal fib
# move the result from $v0 into $s3
move $s3, $v0

# print out the result string
li $v0, 4
la $a0, masg2
syscall

# print out the result
li $v0, 1
move $a0, $s3
syscall

# exit the program
li $v0, 10
syscall


fib:
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $a0, 0($sp)

# set $t3 == 1
addi $t3, $zero, 1
# if n is not < 1, the check if n >1
bgt $a0, $t3, keep
# if n is not >1, so n <= 1
move $v0, $a0
# jump to return N
jr $ra

keep:
# get n -1
addi $a0, $a0 -1
# get fib(n-1)
jal fib
# save the fib(n-1) in $s0
sw $v0
# n -2
addi $a1, $a1, -1
# get the fib (n-2)
jal fib
# get fib(n-1)+fib(n-2)
add $a0, $a0, $s0


return:
# pop out the stack
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
# return the answer
jr $ra

Explanation / Answer

Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you

# C code:
# int fib(int n){
# if(n<= 1){
# return n;
# }
# else{
# return fib(n-1)+fib(n-2);
# }
# }


#MIPS code:
.data
# a massage for user to input the number n
masg1: .asciiz "Enter a number: "
# a massage for print out the result for the user
masg2: .asciiz "The result is "


.text
main:
# print out the massage1 to the user
li $v0, 4
la $a0, masg1
syscall
# get the input n from the user
li $v0, 5
syscall
# save the input n in $s2
move $s2, $v0
# put the $s2 == n in to $a0 then call fib
move $a0, $s2
jal fib
# move the result from $v0 into $s3
move $s3, $v0

# print out the result string
li $v0, 4
la $a0, masg2
syscall

# print out the result
li $v0, 1
move $a0, $s3
syscall

# exit the program
li $v0, 10
syscall


fib:
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $a0, 0($sp)

# check if recursive case
bgt $a0, 1, recursive_case
# if n is not >1, so n <= 1
move $v0, $a0
# jump to return N
b return

recursive_case:
# get n -1
addi $a0, $a0 -1
# get fib(n-1)
jal fib
# save the fib(n-1) in $s0
move $s0, $v0
# n -2
addi $a0, $a0, -1
# get the fib (n-2)
jal fib
# get fib(n-1)+fib(n-2)
add $v0, $v0, $s0

return:
# pop out the stack
lw $a0, 0($sp)
lw $s0, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
# return the answer
jr $ra

output
----
Enter a number: 0
The result is 0
-- program is finished running --

Enter a number: 1
The result is 1
-- program is finished running --

Enter a number: 2
The result is 1
-- program is finished running --

Enter a number: 3
The result is 2
-- program is finished running --

Enter a number: 4
The result is 3
-- program is finished running --

Enter a number: 5
The result is 5
-- program is finished running --

Enter a number: 6
The result is 8
-- program is finished running --

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote