Issue With Using dec to Create a Delay

2019-09-05 05:24发布

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

1条回答
Rolldiameter
2楼-- · 2019-09-05 05:32

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.

查看更多
登录 后发表回答