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

Add code to let the program print Fibonacci series on console. # C code: # int f

ID: 3915288 • Letter: A

Question

Add code to let the program print Fibonacci series on console.

# 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

Explanation / Answer

This programs print only the last number returned by the fib(). To print all the numbers in the series, move print statement to fib() from main. Modified code is as below:

.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

# print out the result string

li $v0, 4

la $a0, masg2

syscall

# put the $s2 == n in to $a0 then call fib

move $a0, $s2

jal fib

# 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

# To print fib(n-1)+fib(n-2)

# move the result from $v0 into $s3

move $s3, $v0

# print out the result

li $v0, 1

move $a0, $s3

syscall

return:

# To print exit case

# move the result from $v0 into $s3

move $s3, $v0

# print out the result

li $v0, 1

move $a0, $s3

syscall

# 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

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