title: Recursion Fibonacci NASM
body: <p>I am trying to write a fibonacci function recursive in assembly, but I am not sure if I know what I am doing. That's what I have so far but I am getting segmentation fault.
  The code gets a number from user and then calculate that number so if n=9 the output is 34.</p>
  <p>Code:</p>
  <pre><code>extern printf, scanf
  section .data
  
  format db &quot;%s %d&quot;,10,0
  format2 db '%ld',0
  format3 db &quot;%s &quot;,10,0
  str1 db &quot;Chose a number: &quot;,0
  str2 db &quot;Result is: &quot;,0
  
  section .bss
  n:      resq 1
  
  section .text
  global _start
  
  _start:
  
          call print1 ;choose a number
          mov rdi, format2
          mov rsi, n
          mov rax, 0
          call scanf ;getting number
          
          mov eax, n
          call fib_n
          call print2 ;printing result
  
  exit:   mov rax,60
          xor rdi, rdi
          syscall
  
  fib_n:
      push    rdi
      xor     eax, eax
      cmp     edi, 2
      jb      end            ; fib(1) = 0                                                                                                                                                                     
      mov     eax, 1
      je      end            ; fib(2) = 1                                                                                                                                                                     
      dec     edi
      call    fib_n          ; -&gt; EAX                                                                                                                                                                         
      push    rax            ; (1) Preserve intermediate result!                                                                                                                                              
      dec     edi
      call    fib_n          ; -&gt; EAX                                                                                                                                                                         
      add     eax, [rsp]     ; fib(n) = fib(n-2) + fib(n-1)                                                                                                                                                   
      add     rsp, 8         ; (1)                                                                                                                                                                            
  end:
      pop     rdi
      ret
  
  print1:
          mov rdi, format3
          mov rsi,str1                                                                                                   
          mov rax,0
          call printf                                                                                                                                                               
          ret
  
  
  print2:
          mov rdi,format                                                                                                                                                                    
          mov rsi, str2                                                                                                                                                                
          mov rdx, [n]
          mov rax,0
          call printf                                              \                                                                                                                
          ret
  
  
  </code></pre>