2

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 !

4

2 回答 2

8

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.

于 2010-04-26T19:52:40.170 回答
0

You may need an x86 emulator like DOSBox to run this code under Windows XP.

于 2010-04-26T20:38:54.927 回答