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"> |
||
section .data |
|||
CountDown |
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"> |
||
section .text |
|||
global TimerIRQ |
|||
TimerIRQ: |
TimerIRQ: |
||
push eax |
|||
mov eax, [CountDown] |
|||
or eax, or eax ; quick way to compare to 0 |
|||
jz TimerDone |
|||
mov eax, [CountDown] |
|||
dec eax |
|||
mov [CountDown], eax |
|||
TimerDone: |
TimerDone: |
||
pop eax |
|||
iretd |
|||
</source> |
</source> |
||
Finally, create a function <tt> |
Finally, create a function <tt>sleep</tt> that waits the interval, in milliseconds. |
||
<source lang="asm"> |
<source lang="asm"> |
||
[GLOBAL |
[GLOBAL sleep] |
||
sleep: |
|||
push ebp |
|||
mov ebp, esp |
|||
push eax |
|||
mov eax, [ebp + 8] ; eax has value of sole argument |
|||
mov [CountDown], eax |
|||
SleepLoop: |
SleepLoop: |
||
cli ; can't be interrupted for test |
|||
mov eax, [CountDown] |
|||
or eax, eax |
|||
jz SleepDone |
|||
sti |
|||
nop ; nop a few times so the interrupt can get handled |
|||
nop |
|||
nop |
|||
nop |
|||
nop |
|||
nop |
|||
jmp SleepLoop |
|||
SleepDone: |
SleepDone: |
||
sti |
|||
pop eax |
|||
pop ebp |
|||
ret |
|||
</source> |
</source> |
||