Programmable Interval Timer: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
convert exotic assembler syntax to NASM
Line 418: Line 418:
The PIT's generating a hardware interrupt every ''n'' milliseconds allows you to create a simple timer. Start with a global variable x that contains the delay:
The PIT's generating a hardware interrupt every ''n'' milliseconds allows you to create a simple timer. Start with a global variable x that contains the delay:
<source lang="asm">
<source lang="asm">
SEGMENT .DATA
section .data
CountDown DD 0
CountDown: dd 0
</source>
</source>
Next, every time the timer interrupt is called, decrement this variable until 0 is stored.
Next, every time the timer interrupt is called, decrement this variable until 0 is stored.
<source lang="asm">
<source lang="asm">
SEGMENT .TEXT
section .text
[GLOBAL TimerIRQ]
global TimerIRQ
TimerIRQ:
TimerIRQ:
PUSH EAX
push eax
MOV EAX, CountDown
mov eax, [CountDown]
OR EAX, EAX ; quick way to compare to 0
or eax, or eax ; quick way to compare to 0
JZ TimerDone
jz TimerDone
DEC CountDown
mov eax, [CountDown]
dec eax
mov [CountDown], eax
TimerDone:
TimerDone:
POP EAX
pop eax
IRETD
iretd
</source>
</source>
Finally, create a function <tt>Sleep</tt> that waits the interval, in milliseconds. I assume Pascal calling convention – the called function cleans the stack.
Finally, create a function <tt>sleep</tt> that waits the interval, in milliseconds.
<source lang="asm">
<source lang="asm">
[GLOBAL _Sleep]
[GLOBAL sleep]
_Sleep:
sleep:
PUSH EBP
push ebp
MOV EBP, ESP
mov ebp, esp
PUSH EAX
push eax
MOV EAX, [EBP + 8] ; EAX has value of sole argument
mov eax, [ebp + 8] ; eax has value of sole argument
MOV CountDown, EAX
mov [CountDown], eax
SleepLoop:
SleepLoop:
CLI ; can't be interrupted for test
cli ; can't be interrupted for test
MOV EAX, CountDown
mov eax, [CountDown]
OR EAX, EAX
or eax, eax
JZ SleepDone
jz SleepDone
STI
sti
NOP ; NOP a few times so the interrupt can get handled
nop ; nop a few times so the interrupt can get handled
NOP
nop
NOP
nop
NOP
nop
NOP
nop
NOP
nop
JMP SleepLoop
jmp SleepLoop
SleepDone:
SleepDone:
STI
sti
POP EAX
pop eax
POP EBP
pop ebp
RETN 8
ret
</source>
</source>