如何在 C/C++ 中调用 DOS 中断使用内联汇编进行编程?
我需要从 C/C++ 程序调用一些 DOS 中断(服务),我尝试了以下内联 asm 代码: (读一个字)
int main()
{
asm(
"movb $0x01, %ah;"
"int $0x21"
);
system("PAUSE");
}
但是没用!我想知道我在这里做错了什么!另外如果有另一种方法来调用dos中断! 谢谢 !
I need to call some DOS interrupts (Services) from a C/C++ program, I tried the following inline asm code:
(Read a character)
int main()
{
asm(
"movb $0x01, %ah;"
"int $0x21"
);
system("PAUSE");
}
But it did not work ! I would like to know what have i done wrong here ! Also if there is another way to call dos interrupts !
Thank You !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能使用 DOS 程序中的 DOS 中断,因此要实现此功能,您需要一个非常古老的 C++ 编译器,例如 Visual C++ 1.0 或 1.5,或者 Turbo C++/Borland C++ 到 4.5 或可能 5.0 等版本。然后你必须修复你的汇编代码——你写的看起来像 AT&T 语法,但我知道的所有 DOS 编译器都使用 Intel 语法。有一个半例外:djgcc。这是 gcc 的一个古老版本,在 DOS 扩展器下运行,因此它使用 AT&T 语法,并且仍然支持一组类似 DOS 的中断(尽管您实际上使用的是 DOS 扩展器,而不是 DOS 本身)。
即便如此,该程序也只能在支持 DOS 程序的系统上运行(微软正在迅速将其从 Windows 中删除——例如,它在所有 x64 版本的 Windows 中都不存在)。
DOS 已经过时太久了,为它编写新代码已经没有意义了。如果您想读取这样的密钥,请编写一个 Windows 程序,并使用类似
ReadConsoleInput
代替。编辑:好吧,如果你真的想这样做,明显的方法是选择 DOS 扩展程序 并将当前版本的 gcc 移植到其中。另一种可能性是选择一个编译器,例如 OpenWatcom 或 Digital Mars 仍在维护并已移植到 DOS 扩展程序。
You can only use DOS interrupts from DOS programs, so to make this work, you'd need a really ancient C++ compiler like Visual C++ 1.0 or 1.5, or Turbo C++/Borland C++ up through something like 4.5 or possibly 5.0. Then you'd have to fix your assembly code -- what you've written looks like AT&T syntax, but all the DOS compilers of which I'm aware use Intel syntax. There is one semi-exception to that: djgcc. This an ancient version of gcc that runs under a DOS extender, so it uses AT&T syntax, and still supports a set of DOS-like interrupts (though you're really using the DOS extender, not DOS per se).
Even then, the program would only run on a system that supports DOS programs (and Microsoft is quickly dropping that from windows -- e.g., it's absent in all the x64 versions of Windows).
DOS has been obsolete long enough that writing new code for it doesn't make sense. If you want to read a key like that, write a Windows program, and use something like
ReadConsoleInput
instead.Edit: Okay, if you really want to do this, the obvious way would be to pick a DOS extender and port a current version of gcc to it. The other possibility would be to pick a compiler like OpenWatcom or Digital Mars that's still maintained and already ported to a DOS extender.
您可能需要像 DOSBox 这样的 x86 模拟器才能在 Windows XP 下运行此代码。
You may need an x86 emulator like DOSBox to run this code under Windows XP.