Issue With Using dec to Create a Delay

孤人 提交于 2019-12-12 04:27:05

问题


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:


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.



来源:https://stackoverflow.com/questions/39060303/issue-with-using-dec-to-create-a-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!