title: Recursive Fibonacci in MASM Assembly
body: <p>So I am creating a program to give the nth term of the Fibonacci sequence. I am supposed to implement the following logic using recursive MASM assembly. </p>
  
  <pre><code>int fib(int n){ 
   if ((n == 1) || (n == 2)) 
       return n - 1; 
   else 
       return fib(n-1) + fib(n-2); 
  }
  </code></pre>
  
  <p>The issue I seem to be having is retaining the values as the program cycles through until reaching 1. I am fairly unexperienced with recursion and I feel like I am missing something in that aspect. I am not sure how to retain the values to add them.</p>
  
  <pre><code>.code
  main PROC
      mov ecx,0
      push 4          ; calculate the nth fib
      call Fib            ; calculate fib (eax)
      call WriteDec
      call Crlf
      exit
  main ENDP
  
  Fib PROC
      add ecx,1
      push ebp
      mov  ebp,esp
      mov  eax,[ebp+8]    ; get n
      cmp  eax,2      ; n == 2?
      je   exception2     
      cmp  eax,1      ; n == 1?
      je   exception2         
      dec eax
      push eax            ; Fib(n-1)
      call fib
  
      add eax,
      jmp Quit
  
  
  Exception2:
      dec eax
  Quit:
      pop  ebp            ; return EAX
      ret  4          ; clean up stack
  Fib ENDP
  
  END main
  </code></pre>