如何在保护模式下延迟执行指定的毫秒数?
我有一个 C 程序,在保护模式下在裸 x86(没有操作系统)上运行。我需要将程序的执行延迟一定的时间。目前,我正在这样做:
for(p=0; p<1000000; ++p) asm("pause");
但这看起来非常非常错误(我确实得到了延迟,但显然,我无法真正控制其持续时间:“暂停”的长度未定义)。
现在,我对如此低级别的东西并没有真正的经验,并且我一直在网上寻找解决方案,但到目前为止我发现的唯一一个涉及 BIOS 中断,它在 pmode 下不起作用(或者所以我被告知)。
那么,在保护模式下如何延迟执行呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现延迟的典型方法是使用系统定时器(在 x86 上也称为 PIT(可编程间隔定时器))来生成中断。您可以设置系统定时器(硬件 IRQ0)以在一定时间后引发中断,编写中断处理程序以在中断上设置某些寄存器或标志,并且当您需要延迟执行时,在寄存器或标志上循环,直到中断处理程序设置它。
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.