您可以挂钩定时器中断 (08h) 并配置 PIT 以获得高达 1.2 Mhz 的速率。
这是一些旧的 TASM 风格的程序集,展示了如何做到这一点:
tmTimerHandler PROC
push ds
mov ds,cs:tmDataSeg
add ds:tmTicker,65536
pop ds
jmp cs:tmOldTimer
tmTimerHandler ENDP
tmInit PROC
mov tmDataSeg,ds
mov tmTicker,65536
push es
; Save the old timer interrupt vector
mov ax,3508h
int 21h
mov dword ptr tmOldTimer+0,ebx
mov word ptr tmOldTimer+4,es
pop es
; Install our own timer interrupt vector
push ds
mov ax,2508h
push cs
pop ds
mov edx,OFFSET tmTimerHandler
int 21h
pop ds
; Configure the PIT to generate interrupts
; at the maximum rate
mov al,34h
out 43h,al
xor al,al ; zero divisor
out 40h,al
out 40h,al
ret
tmInit ENDP
tmClose PROC
push ds
mov ax,2508h
lds edx,tmOldTimer
int 21h
pop ds
ret
tmClose ENDP
; Returns the current tick count in eax
tmGetTimer PROC
pushf
cli
xor eax,eax
out 43h,al
in al,40h
mov ah,al
in al,40h
xchg ah,al
neg eax
add eax,tmTicker
popf
ret
tmGetTimer ENDP
.data
tmOldTimer df 0
tmDataSeg dw 0
tmTicker dd 0