Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can soomeone tell me what is worng with this code? In MIPS assembly write a program that calculates the nth Fibonacci number through recursion. Use

Can soomeone tell me what is worng with this code?

In MIPS assembly write a program that calculates the nth Fibonacci number through recursion. Use a main function to get the nth number from the user and print out the result.

.data

msg1: .asciiz "Give a number: "

.text

main:

li $v0, 4

la $a0, msg1

syscall # print msg

li $v0, 5

syscall # read an int

add $a0, $v0, $zero # move to $a0

jal fib # call fib

add $a0, $v0, $zero

li $v0, 1

syscall

li $v0, 10

syscall

fib:

# $a0 = y

# if (y == 0) return 0;

# if (y == 1) return 1;

# return fib(y - 1) + fib(y - 2);

#save in stack

addi $sp, $sp, -12

sw $ra, 0($sp)

sw $s0, 4($sp)

sw $s1, 8($sp)

add $s0, $a0, $zero

addi $t1, $zero, 1

beq $s0, $zero, return0

beq $s0, $t1, return1

addi $a0, $s0, -1

jal fib

add $s1, $zero, $v0 # $s1 = fib(y - 1)

addi $a0, $s0, -2

jal fib # $v0 = fib(n - 2)

add $v0, $v0, $s1 # $v0 = fib(n - 2) + $s1

exitfib:

lw $ra, 0($sp) # read registers from stack

lw $s0, 4($sp)

lw $s1, 8($sp)

addi $sp, $sp, 12 # bring back stack pointer

jr $ra

move $a0, $s0

li $v0, 1 # Print result

syscall

return1:

li $v0,1

j exitfib

return0:

li $v0,0

j exitfib

li $v0 , 10

syscall

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions