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 !

Rockr90
  • 35
  • 2
  • 5
  • 4
    Can you elaborate on "it did not work?" Did it compile? Did it crash? Did it run, but not have the desired effect? – Judge Maygarden Apr 26 '10 at 19:51
  • My understanding of INT 21h function 01h is that it should read a character from standard input (blocking until one is available) and return it in the AL register. – Alex Jasmin Apr 26 '10 at 20:18
  • Sorry for the missing details ! I'm using Code Blocks as IDE (so the compiler is GCC ), This code compiles fine, but on execution it generates an error after a second or two, with code:0xc0000005, My platform is Windows XP on intel x86, Thank you. – Rockr90 Apr 26 '10 at 20:21

2 Answers2

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    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 Apr 26 '10 at 19:56
  • @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 Apr 26 '10 at 20:02
  • 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 Apr 26 '10 at 20:13
  • 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 Apr 26 '10 at 20:30
  • 1
    @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. – Alex Jasmin Apr 26 '10 at 21:21
  • @rockr90 In case all you want is to test snippets of DOS assembly code you could use an assembler instead. – Alex Jasmin Apr 26 '10 at 21:33
0

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

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
  • 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 Apr 26 '10 at 20:54
  • You'll need to build the code using the above mentioned obsolete compiler, THEN run it under DOSBox or an older OS. – phkahler Apr 26 '10 at 21:32