0

I can't figure out what I'm doing exactly, I think I might be missing some stuff.

I need to write a Fibonacci sequence by the length of nth times through user input (iterative method).

For example if user input is 6: output would be:

5

0, 1, 1, 2, 3, 5

I was given a pseudocode for calculating the fibonacci:

fib(n):
 a = 0
 b = 1
 for i from 0 to n - 1:
 array [i] = a
 temp = b
 b += a
 a = temp

this is what I came up with:

.data
fib:        .space 100
fib_size:   .word 100
title:      .asciiz "Calculating the fibonacci sequence \n"
prompt:     .asciiz "Insert the nth fibonacci sequence f(n): "
output:     .asciiz "The result is: "

.text

.globl main

main:
la $t1, fib     #loading addresses
la $t2, fib_size
lw $t2, 0($t2)      #load array size value to t1 

la $a0, title
li $v0, 4       #print the title 
syscall

la $a0, prompt      #print the prompt
li $v0, 4
syscall

li $v0, 5       #read the input
syscall

move $s0, $v0       #store the input into register $s0; n variable; $s0 = $v0 
addi $s0, $s0, -1

li $s1, 0       #a = 0
li $s2, 1       #b = 1

li $s3, 0       #temp = initialized to 0 

li $s4, 0       #i = 0
fib_loop:       
slt $t3, $0, $s4    #i > 0
slt $t4, $s4, $s0   #i < n - 1
and $t3, $t3, $t4   #i > 0 && i < n -1
beq $t0, $0, end_loop   #if fails, end the loop

sw $s1, 0($t2)      # array[i] = a ; store a in array position of fib_size
addu $t2, $t2, 4    # increase the array position by 1

move $s3, $s2       #temp = b
addu $s2, $s2, $s1  #b = b + a
move $s1, $s3       #a = temp

j fib_loop      #loop

end_loop:
la $t1, fib

la $a0, output      #print the output
li $v0, 4
syscall

lw $a0, -4($t2)     #grab the word from the last position of the array
li $v0, 1       #print the integer (nth of our fibonacci sequence)
syscall

output_loop:
slt $t5, $t1, $t2   #if fib @ 0 < fib_size @ whatever position it reaches on input loop
beq $t5, $0, exit_output_loop   #if fails, exit the loop

lw $a0, 0($t1)      #get integer from fib[0]
li $v0, 1       #print integer
syscall

li $a0, ','     #print the comma
li $v0, 11
syscall

li $a0, ' '     #print the space char
li $v0, 11
syscall

addu $t0, $t0, 4    #increment the fib array position by 1

j output_loop

exit_output_loop:
li $v0 10       #exit
syscall

edit: fixed the sw problems. Now i got runtime errors? "lw $a0, -4($t2)"

Jordles
  • 45
  • 2
  • 9
  • `lw`/`sw` operate on memory. You just want to transfer between registers, use `move`. – Jester Mar 18 '19 at 01:52
  • how would i recieve the input for n without storing the memory? – Jordles Mar 18 '19 at 02:04
  • By keeping the input in a register. The purpose of registers is holding data you're working with, and you don't need `n` to be stored anywhere in memory when your program exits, so there's no need for the original `n` to exist anywhere after you read it into a register with a MARS system call. – Peter Cordes Mar 18 '19 at 02:15
  • It's your choice where you want it. If you want it **in memory at the address pointed to by `$s0` then** you can use `sw $v0, ($s0)` (notice the parentheses). If you just want to copy it to another register then use `move`. It was not clear from your question which one you wanted, but since you otherwise use `lw` and `sw` with proper addresses I assumed you just wanted a transfer between registers. In fact your further use of `$s0` still tells me you want this. – Jester Mar 18 '19 at 02:21
  • @Jester oh ok thanks guys. Yeah i just wanted more practice with lw/sw and I dont use move that often, but ill try it out and see how it works. – Jordles Mar 18 '19 at 02:43
  • @Jester ok so now i got runtime errors. I have a question. I'm storing all these values I get from my fib loop and then i put them in the fib_size array right? The other normal fib array is used like an iterator to keep track on output_loop . I'm wondering if im getting runtime errors cause im storing all these values wrong. – Jordles Mar 18 '19 at 02:58
  • `fib_size` is just a constant showing how big the actual `fib` array is. It's initialized as `.word 100`. You used `$t2` instead of `$t1` in at least `sw $s1, 0($t2)` and `addu $t2, $t2, 4`. – Jester Mar 18 '19 at 12:58
  • @Jester ah ok, i think i got it figured out ty – Jordles Mar 19 '19 at 04:37

0 Answers0