Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to populate an array with Fibonacci numbers. The Fibonacci sequence begins with 0 and then 1 , each following number is the

Write a program to populate an array with Fibonacci numbers. The Fibonacci sequence begins with 0 and then 1, each following number is
the sum of the previous two numbers. Ex: 0,1,1,2,3,5,8,13. Assume the size of the array is always at least 1. Use the '+' button under the
Registers display to store the size of an integer array in x8 and the address of the first element of the array in the memory in 9.
Ex: If x8 and x9 are initialized in the simulator as 5 and 5000, the data memory starting at address 5000 will contain: this is what i have right now but it's not working not sure what i am doing wrong # Initialize array size and address
lw x8,0(x8) # Load the size of the array into x8
lw x9,0(x9) # Load the address of the first element into x9
# Handle the base cases
addi t0, x0,0 # Fib(0)=0
sw t0,0(x9) # Store the first Fibonacci number
addi t1, x0,1 # Fib(1)=1
sw t1,4(x9) # Store the second Fibonacci number
# Loop to calculate and store the rest of the Fibonacci numbers
addi t2, x0,2 # Start from the third element (index 2)
addi t3, x0,0 # Initialize Fib(i-1)=0
addi t4, x0,1 # Initialize Fib(i-2)=1
fib_loop:
add t5, t3, t4 # Calculate Fib(i)= Fib(i-1)+ Fib(i-2)
sw t5,0(x9) # Store Fib(i) in the array
# Move to the next Fibonacci numbers for the next iteration
add t3, x0, t4 # Fib(i-2) becomes Fib(i-1)
add t4, x0, t5 # Fib(i-1) becomes Fib(i)
addi x9, x9,4 # Move to the next element in the array
addi t2, t2,1 # Increment the index
blt t2, x8, fib_loop # Continue the loop until the array is filled
image text in transcribed

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

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