问题描述
我正在为一个装配8086的学校项目进行工作(使用DOSBox),并且试图将程序延迟0.5秒。
I'm working on a project for school in assembly 8086 (using DOSBox), and I was trying to delay my program for 0.5 second.
我试图创建一个循环,使用int 21h,函数2Ch和 DL
中的百分之一的值将当前时间与初始时间进行比较,但这似乎太慢了...
I tried to create a loop which compares the current time to the initial time, using int 21h, function 2Ch with the hundredths value in DL
, but it seems to be too slow...
mov ah, 2Ch
int 21h
mov al, dl ;hundredths
mov bx, 0
wait_loop:
one_hun:
int 21h
cmp al, dl
je one_hun
mov al, dl
inc bx
cmp bx, 50
jne wait_loop
推荐答案
我宁愿使用BIOS ,可以用作 int 21h
服务的源,使用起来可能会更简单。
I would rather use BIOS int 1Ah
, which is used as source for that int 21h
service any way, and this may be a bit simpler to use.
但是请注意,默认情况下(除非您打算对计时器芯片进行重新编程),这是18.2 tim每秒es,因此要等待半秒钟,您可以等待9(cca。 440至494.51ms)或10(大约495至549.45ms)刻度,精度将限制为默认的+-〜50ms)。
But beware by default (unless you plan to reprogram the timer chip) this is ticking 18.2 times per second, so to wait for half a second you can wait either 9 (cca. 440 to 494.51ms) or 10 (cca. 495 to 549.45ms) ticks, the precision will be limited to the default +-~50ms).
如果您要重新编程计时器芯片,您可能会得到更高的精度,但是不要指望[10]几千秒能在DOS下完美可靠地工作(可能是在现代操作系统下模拟)。
If you will reprogram the timer chips, you may get a bit higher precision, but don't expect something like [ten] thousands of second to work perfectly reliably under DOS (probably emulated under modern OS).
关于您当前的代码: dl
中的百分位数未加1,因此您在计算 bx
那18.2Hz滴答声的数量,而不是百分之一(即您的代码等待〜2.7s,对吧?)。
About your current code: the hundredths in the dl
are not incrementing by one, so you are counting in the bx
the number of those 18.2Hz ticks, not the hundredths (i.e. your code waits for ~2.7s, right?).
也不要在类似的代码中执行 je
,始终使条件< =
或> =
,因为如果由于某种原因(OS暂时没有运行您的代码)而错过了精确的 50
百分之一区别在于,您将创建一个几乎无限运行的循环(直到一次溢出后偶然将其精确地达到了50)。
Also don't do je
in similar code, always make the condition <=
or >=
, because if for whatever reason (OS didn't run your code for a while) miss that exact 50
hundredths difference, you will create loop which will run almost infinitely (until it will hit that exact 50 by accident after one of the overflows).
要按照自己的方式做,您必须计算增量:
To do it your way, you have to calculate the delta:
mov ah, 2Ch
int 21h
mov al, dl ;hundredths
wait_loop:
nop ; burn the CPU a bit less
int 21h
sub dl,al ; calculate delta of hundredths (-99..+99)
jnc delta_positive
add dl,100 ; adjust the delta to be positive 1-99
delta_positive:
cmp dl,50
jb wait_loop
这篇关于使用ah = 2Ch的int 21h延迟程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!