title: (ASM) Fibonacci Function using fewer registers?
body: <p>I am following a beginner's guide to MIPS ASM.</p>
  
  <p>The question is:</p>
  
  <blockquote>
    <p>Write the program as a counting loop that terminates when the first
    100 terms of the Fibonacci series have been computed.  <strong>Use a register
    for the current term and a register for the previous term. Each
    execution of the loop computes a new current term and then copies the
    old current term to the previous term register.</strong></p>
  </blockquote>
  
  <p>It seems as though the solution should only use 2 registers; previous and current value. And another 2 for counter and '101' (the book says to use 101 in a register for the beq instruction)</p>
  
  <p><strong>Is it possible for me to remove the 'sum' register from my solution?</strong> 
  I'm a beginner programmer and this is blowing my mind.</p>
  
  <p>It's wrong because it does not computer 1,1,3,5,8...
  Instead it computes 1,3,5,8. I don't know how to computer '1' twice by following the book's method.</p>
  
  <p>My solution, based on the question:</p>
  
  <pre><code>## fibonacci series ##
  # $7=sum    $8=prev     $9=current  $10=counter     $11=101 #
  .text
  main:
  
  ori $7,$0,0     # init sum 0
  ori $8,$0,0     # init prev 0
  ori $9,$0,1     # init current 1
  ori $10,$0,0        # init counter 0
  ori $11,$0,101  # init 101
  
  loop:
  beq $10,$11,exit
  addu $7,$8,$9       # sum = prev+current
  or $8,$0,$9         # copy old current to prev
  or $9,$0,$7         # copy sum to new current
  addiu $10,$10,1    # counter+1
  j loop               # go to loop
  sll $0,$0,0
  
  exit:
  j exit
  sll $0,$0,0        # exit
  </code></pre>