0

I've started experimenting with Gameboy programming using Z80 assembly, but I've found something kind of weird.

I found a snippet of code used to create a delay:

simpleDelay:
dec bc
ld  a,b
or  c
jr  nz, simpleDelay
ret

While playing around with that, I discovered that writing dec bc twice shortens the delay, but writing it 3 times makes the delay longer than using it once or twice. Why does having an even number of dec statements shorten the delay?

EDIT: Here's the snippet of code calling the delay:

ld  hl,Title2
ld  de, _SCRN0+3+(SCRN_VY_B*5) ;
ld  bc, Title2End-Title2
call    mem_CopyVRAM
call simpleDelay
  • IDK Z80, but is the loop condition checking for `!= 0`? Is your initial count a multiple of 2 but not 3? – Peter Cordes Aug 21 '16 at 02:26
  • 1
    Not directly relevant, but see `DJNZ` for shorter loops; it betrays the logic being applied here too: decrement, compare to zero, jump if the comparison fails. This is that with an OR to compare a 16-bit value rather than merely an 8-bit value — `OR` sets the zero flag and the only way two values can produce zero is if both were individually zero. – Tommy Aug 21 '16 at 15:57
  • @Tommy Oh cool, thanks! –  Aug 21 '16 at 19:28

1 Answers1

4

The number of times the loop gets executed depends on the value loaded into bc. You did not specify what value you are using.

If the value you load is even, dec bc, dec bc, dec bc will cause the jr nz, simpleDelay to not exit the loop the first time around and the value of bc to wraparound. This causes the loop to be executed more times than you expect.

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
Peter Skarpetis
  • 543
  • 3
  • 11