-3

Im trying to do a loop that calculate the first 10 number in Fibonacci number. I use "loop" at the and of the code, and it doesn't work.

    xor cx, cx
    mov cx, 8
    mov bx, offset Array
    mov [byte ptr bx], 0
    mov [byte ptr bx+1], 1
    mov bx, 1

Fibo:
    inc bx
    mov al, [byte ptr bx-1]
    mov [byte ptr bx], al
    mov al, [byte ptr bx-2]
    add [byte ptr bx], al
    loop Fibo
Asher
  • 57
  • 1
  • 7
  • 2
    You should avoid `loop`. It's slow, and implicitly uses `[r/e]cx`, which means that in 32- or 64-bit mode the code you've posted could loop much more than you expect. – EOF Jul 11 '16 at 17:04
  • See also http://stackoverflow.com/questions/32659715/assembly-language-x86-how-to-create-a-loop-to-calculate-fibonacci-sequence – Peter Cordes Jul 11 '16 at 18:16

3 Answers3

1

The loop is OK, but you destroy your bx by accident. Try to debug it with debugger.

(downvoting, because it's obvious if you debug it, even on paper without HW).

Ped7g
  • 16,236
  • 3
  • 26
  • 63
1

I think you should use "byte ptr [bx]" instead of "[byte ptr bx]" for all of your pointer types.

Also, this line is probably going to cause a bug.

mov bx, 1

It resets bx, so it won't point to the address of Array anymore. Use "inc bx" instead.

Ariestinak
  • 86
  • 3
  • But when I use Instead of "loop", that: dec cx- cmp cx, 0- jne Fibo - It works. – Asher Jul 11 '16 at 17:58
  • 1
    There are many ways that this can still work. One of the being that Array's relative address in memory being 0. But you want a program that always works. Not one that just works sometimes. :) – Ariestinak Jul 12 '16 at 02:01
1

The loop was correct. I was confused about how to debug loops. sorry.

Asher
  • 57
  • 1
  • 7