0

I'm trying to design an algorithm in NASM which is supposed to compute the nth Fibonacci number. I wrote some code, but, when run, it outputs only 1 and I cannot understand why. My idea was to call Fibonacci for n-1 and for n-2, where the parameter n is stored within AX register, and the result is stored at BX.

%include "io.inc"

section .text
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    xor eax, eax
    xor ebx, ebx
    push word 12
    call fibonacci
    pop word [trash]
    PRINT_UDEC 1, ax
    ret
    
fibonacci: push ebp
           mov ebp, esp
           
           mov ax, [ebp + 8];se incarca ax cu parametrul primit prin stiva
           cmp ax, 0
           jne l1
           mov bx, 0
           jmp l3
l1:        cmp ax, 1
           jne l2
           mov bx, 1
           jmp l3
l2:        sub ax, 2;n-2
           push ax
           call fibonacci
           pop dx;resotre the stack
           mov [ebp - 2], bx
           
           mov ax, [ebp + 8];se incarca bx cu parametrul prinit prin stiva
           sub ax, 1;n-1
           push ax
           call fibonacci
           pop dx;restore the stack
           add bx, [ebp - 2]
           
l3:        mov esp, ebp
           pop ebp
           ret
    
section .data
    trash dw 1
pauk
  • 350
  • 4
  • 15
  • Though I could implement Fibonacci iterative, I want to improve my skills in assembly recursion. – pauk Nov 26 '20 at 16:09
  • Separate from other bugs, you normally don't want to use 16-bit pushes in 32-bit mode. There's no good reason to use 16-bit partial registers at all here. – Peter Cordes Nov 26 '20 at 16:21

0 Answers0