使用dec创建延迟的问题 [英] Issue With Using dec to Create a Delay

查看:71
本文介绍了使用dec创建延迟的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始尝试使用Z80程序集进行Gameboy编程,但是我发现有些奇怪.

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

与此同时,我发现写两次dec bc会缩短延迟,但是写3次会使延迟比使用一到两次更长.为什么偶数个dec语句会缩短延迟?

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?

这是调用延迟的代码片段:

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

推荐答案

循环执行的次数取决于加载到bc中的值.您没有指定要使用的值.

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

如果加载的值是偶数,则dec bcdec bcdec bc会导致jr nzsimpleDelay第一次不退出循环,并且bc的值会回绕.这将导致循环执行的次数超出您的预期.

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.

这篇关于使用dec创建延迟的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆