views:

407

answers:

2

Hi, everyone ! 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 !

+8  A: 

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.

Jerry Coffin
I still see new projects using FreeDOS (http://www.freedos.org/) from time to time. The last one I recall was for a control loader in a flight simulator.
Judge Maygarden
@Judge Maygarden:I see lots of things I don't believe make any sense. In fairness, I suppose a DOS program could make sense under *limited* circumstances -- though offhand, about the only one I can think of is a hard real-time system that can't afford unpredictable timing of Windows. Even then, it's suspect though -- there are better RT/OSes than DOS.
Jerry Coffin
This was a real-time system, but I admit I thought it was dumb when I saw it. Although, the "free" part of FreeDOS is attractive as opposed to something like VxWorks, RedHawk, LynxOS, etc.
Judge Maygarden
Thank you sir, that was useful. As you may note I'm a student on computer science, and we need to understand these old systems ! But how can i do it on a modern compiler like the new GCC for example ?
Rockr90
@rockr90 as Jerry said DJGPP is a port of GCC (and other GNU tools) to MS-DOS but it works in 32-bit protected mode which is possibly not what you want. For writing 16-bit real mode apps you'll have to use a antique compiler.
Alexandre Jasmin
@rockr90 In case all you want is to test snippets of DOS assembly code you could use an assembler instead.
Alexandre Jasmin
A: 

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

Judge Maygarden
Tried it ! But it does not run, instead a message is printed saying "This Program cannot run in dos mode" don't understand where is the problem ! Thanks
Rockr90
You'll need to build the code using the above mentioned obsolete compiler, THEN run it under DOSBox or an older OS.
phkahler