1

I have a C program that runs on bare x86 (without an OS) in protected mode. I need to delay the program's execution for a certain amount of time. Currently, I'm doing this:

for(p=0; p<1000000; ++p) asm("pause");

But this looks very very wrong (I do get a delay, but apparently, I have no real control over its duration: the length of the "pause" is undefined).

Now, I'm not really experienced with stuff at such low level, and I've been searching the net for solutions, but so far the only one that I found involved BIOS interrupts, which don't work in pmode (or so I was told).

So, how do I delay execution when in protected mode?

  • What do you have up and running at this point? The PIT would help. Otherwise there are probably ports you can read that have predictable timing (though I don't know any offhand). Perhaps waiting for vertical refresh could work (of course this won't work for very short delays). – user786653 Jul 12 '11 at 21:14
  • If you don't have an OS, why do you have to run in protected mode? – David X Jul 12 '11 at 23:14

1 Answers1

4

The typical way to implement delays is using the system timer, also called PIT on x86 (Programmable Interval Timer) to generate an interrupt. You would set up the system timer (hardware IRQ0) to raise an interrupt after a certain time, write the interrupt handler to set some register or flag on interrupt, and, when you need to delay the execution, loop on the register or flag until the interrupt handler sets it.

Antti
  • 11,944
  • 2
  • 24
  • 29
  • 1
    ...and if your code is privileged, you can execute `HLT` in the loop waiting for the interrupt. – caf Jul 13 '11 at 07:45