请记住,对于计算机而言,秒是非常很长的一段时间(嗯,至少在过去 40-50 年!)
你的小程序获取时间值的速度非常快,所以每秒可以获取数千(甚至百万!)次当前时间。
如果您只想要输出更改时的输出,请尝试以下操作:(我们正在添加比较)
petla:
mov ah,2
int 1ah ; get time
mov ah, [sekunda] ; retrieve last good value
cmp ah, dh ; is it same as last good value?
jz pet1a ; yup, ignore it, loop again!
mov [sekunda], dh ; save seconds
mov ah, [sekunda] ; get seconds value
mov cl, 4 ; ready to shift 4 bits
and ah, 11110000b ; isolate top nybble
shr ah, cl ; shift into low nybble
mov dl, ah ; move into proper register for output
add dl, '0' ; magically transform into ASCII digit
mov ah, 2 ; Select 'write char'
int 21h ; uh... write char!
mov ah, [sekunda] ; hey, this seems familiar!
and ah, 00001111b ; isolate lower nybble!
mov dl, ah ; no, really.. deja vu!
add dl, '0' ; TaaDaa, it's a char!
mov ah, 2 ; Select 'write char'
int 21h ; make it so!
jmp petla ; do it again! (make this a 'ret', see below)
现在这个小程序会每秒不断输出一个新的值……也许不是很有价值……
但是等等!还有更多!
如果您将 LAST 指令 (jmp pet1a) 更改为返回 (ret),我们可以将其用作“wait_for_five_seconds”例程的一部分...如下所示:
wait_for_five_seconds:
mov al,5 ; how many seconds to wait
wait_for_al_seconds: ; explained below
wait_loop:
push ax ; save our counter (al)
call pet1a ; this will call pet1a, which will not return until it has displayed a new second value
pop ax ; retrieve our counter (pet1a changes value of ah/al/ax, remember?)
dec al ; decrease al by one (does not set flags!!)
or al,al ; set flags
jnz wait_loop ; al=0? nope, around we go again!
ret ; go back to whomever called us!
现在,如果您想暂停 5 秒,只需拨打 wait_for_five_seconds 即可!
如果您删除 pet1a 中将字符写出的部分……那么您会有一秒的 silent 延迟。用于暂停。
如果您想暂停 17 秒怎么办?嗯,我们的延迟是基于进入wait_loop部分时al的值...那如果我们预加载al,然后调用等待位呢?
mov al, 17 ; delay for 17 seconds!
call wait_for_al_seconds ; delay for whatever number is in al
但要小心,如果 al 的值为 0... 它必须一直循环... 0, -1, -2, ... -127, -128, 127, 126 ... 2, 1, 0 ... 你会暂停 256 秒! (4 分 16 秒!)。 (请参阅 Signed Number Representations 了解为什么它在 -128 左右变得奇怪)
我希望这对您有所帮助,也许会给您一些想法。这不是一个“暂停”解决方案的想法,因为它不是一个确切的延迟,如果你在新的第二秒计时之前调用pet1a just,那么第一次点击的延迟会很小,然后剩下的时间是整秒......所以所需的 5 秒延迟可能在 4.000001 秒到 5 秒之间。但这是一个开始。