title: Fibonacci Sequence in MIPS using a while loop
body: <p>I'm trying to write a program that allows for the user find to find out the nth fibonacci sequence. I'm new to MIPS however and I cannot seem to get it working. It's confusing the hell out of me.</p>
  
  <pre><code>    .data
      msg: .asciiz "Please enter a number to calculate the fibonnaci sequence for"
      equalsOne: .asciiz "The answer is 1"
      equalsTwo: .asciiz "The answer is 2"
      answer: .asciiz "The answer is "
  
  
  .text
      addi $v0, $zero, 4  #Set $v0 to 4 (Print String)
      la $a0, msg         #Set memory address of msg to $a0
      syscall
      addi $v0, $zero, 5  #Set $v0 to 5 (Read Integer)
      syscall
      add $t0, $zero, $v0 #Store input integer in $t0
      beq $t0, 1, equals1 #If input integer == 1, go to equals1 
      beq $t0, 2, equals2 #If input integer == 2, go to equals2
      addi $t1, $zero, 1  #Stores 1 in register $t1
      addi $a1, $zero, 1  # $a1 = currentNumber
      addi $a2, $zero, 1  # $a2 = oldNumber
  
  fib:    slt $t2, $a1, $t0   #While $a1 &lt; $t0
      bne $t2, 1, exit
      add $a3, $a1, $a2   # $a3 = nextNumber = currentNumber + oldNumber
      add $a2, $a1, $zero #Set oldNumber = currentNumber
      add $a1, $a3, $zero #Set currentNumber = nextNumber
      j fib
  
  equals1: la $a0, equalsOne
      addi $v0, $zero, 4
      syscall
  
  equals2: la $a0, equalsTwo
      addi $v0, $zero, 4
      syscall
  
  exit: addi $v0, $zero, 1    #Print integer
      add $a1, $a1, $zero #Print nextNumber
      syscall
  </code></pre>