- #1

Margarita0076

- 5

- 0

- Homework Statement
- MIPS - Fibonacci Series.

- Relevant Equations
- Write and test the fib function in two linked files (Fib.asm, fib_main.asm).

**Write and test the fib function in two linked files (Fib.asm, fib_main.asm).**Your solution must be made up of a function called fib(N, &array) to store the first N elements of the Fibonacci sequence into an array in memory. The value N is passed in $a0, and the address of the array is passed in register $a1. The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5, 8, 13 …

Your solution must also include an assembly 'main' program that accepts an integer from the user less than or equal to 45. Call the fib function and print the results. Prompt the user for the input. It can be assumed that the input value is <= 45.

**An unsigned 32-bit integer up to 45.**

__Required Input:__**Your output should look something like the following example.**

__Required Output:__How many elements of the Fibonacci series? 20

1

1

2

3

5

8

13

21

34

55

89

144

233

377

610

987

1597

2584

4181

6765

I wrote this code in MIPS, but when I create function separate my code is not work. What is wrong in my code? Thank you.

Code:

```
# MAIN METHOD
.data
prompt1: .asciiz "Enter the sequence length "
prompt2: .asciiz "The Fibonacci value is: "
array: .space 180
newline: .asciiz " "
.text
.globl main
# Print prompt1
main:
la $a1,array
li $t0,0
li $t1,1
sw $t1,($a1)
loop:
li $v0, 4
la $a0, prompt1
syscall
# Read integer
li $v0, 5
syscall
move $a0,$v0
jal fib
bgt $a0,45,loop
move $s1,$a0
move $s2,$a0
subi $s1,$s1,1
# Print prompt2
li $v0, 4
la $a0, prompt2
syscall
# FIB FUNCTION
.text
.globl fib
fib:
beqz $s1,print
move $t2,$t1
add $t1,$t1,$t0
move $t0,$t2
addi $a1,$a1,4
sw $t1,($a1)
subi $s1,$s1,1
j fib
print:
la $a1,array
loop1:
beqz $s2,exit
li $v0, 1
lw $a0, ($a1)
syscall
la $a0,newline #for newline
li $v0,4
syscall
addi $a1,$a1,4
subi $s2,$s2,1
j loop1
jr $ra
```

Last edited by a moderator: