0

What am I doing wrong? This code only generates accurate Fibonacci series up to 13 after that it's giving the wrong value.

Output

1, 1, 2, 3, 5, 8, 13, 29, 37

As its should be 21 after 13 and not the 29

Code

org 100h
.data
    num dw 0

.code
main proc
    mov ax, @data
    mov ds, ax

    mov cx, 15
    mov bx, 1       

    mov si, offset num


fibonacci:
    mov dx, [si] ;dx=0,1,1,2,3
    add [si], bx ;num=1,1,2,3,5 
    mov bx, dx ;bx=0,1,1,2    
    mov di, [si]
    add si,2
    mov [si], di   


    loop fibonacci
main endp
end main

ret
Hamza AVvan
  • 377
  • 3
  • 10
  • 1
    Not sure how emu8086 works but if it's anything like a .com file then do not put your `.data` at the start because you will eventually overwrite your code. Also `ret` should probably be before the `main endp`. – Jester Nov 25 '18 at 18:34
  • 1
    Change `num dw 0` to `num dw 16 dup (0)` (assuming masm), and as commented by Jester, move the .data segment. to after main. Move the `ret` to before `main endp` . – rcgldr Nov 25 '18 at 22:38
  • Thanks, @rcgldr. It works by changing `num dw 0` to `num dw 16 dup(0)`. Why don't you post it as an answer? – Hamza AVvan Nov 26 '18 at 11:17

0 Answers0