It might seem odd to think about programming in MSDOS in 2018. But if you are vintage computer enthusiast or have to support some old piece of equipment with an MSDOS single board computer, it could be just the thing. The problem is, where do you get a working compiler that doesn’t have to run on the ancient DOS machine? Turns out, gcc can do the trick. [RenéRebe] offers a video demo based on a blog post by [Chris Wellons]. You can see the video, below.
The technique generates COM files, not EXE files, so there are some limitations, such as a 64K file size. The compiler also won’t generate code for any CPU lower than a 80386, so if you have a real 8086, 80186, or 80286 CPU, you are out of luck. The resulting code will run in a real DOS environment on a ‘386 or higher or in a simulator like DOSBox.
You might be thinking why not use the DJGPP port of gcc to DOS. That sounds good, but it actually doesn’t produce true DOS code. It produces code for a DOS extender. In addition, [Chris] had trouble getting it to work with a modern setup.
The only real trick here is using the right combination of gcc flags to create a standalone image with the right codes. A COM file is just a dump of memory, so you don’t need a fancy header or anything. You also, of course, won’t have any library support, so you’ll have to write everything including functions to, say, print on the screen. Of course, you can borrow [Chris’] if you like.
The last pieces of the puzzle include adding a small stub to set up and call main and getting the linker to output a minimal file. Once you have that, you are ready to program like it is 1993. Don’t miss part 2, which covers interrupts.
If you pine away for QuickBasic instead of C, go download this. If you just want to run some old DOS games, that’s as close as your browser.
18 thoughts on “MSDOS Development With GCC”
OpenWatcom does 16-bit code out of the box and it is open source. I’ve been making .COM files and boot sectors with it for years. Amusingly it can produce Win16s executables as well, in case you’re in the mood for some Windows 3.x (or theoretically ArcaOS, the current fork of OS/2)
I haven’t done .com, but I have used GCC to do some x86 bootloaders and other more-primitive-than-com things. It is very convenient using GCC for these sorts of things over other more unusual compilers. There’s a lot of features you can find yourself missing rather fast without GCC.
sure, if you insist on using GCC it’s quite possible. I switched because it was an easier than working around the limitations. What I actually wanted was to get off my butt and finish porting ‘pcc’ to x86 and donate it to the FUZIX team but you know how priorities get in the way of having fun.
And i bet OpenWatcoms output runs on a 8088 too.
“It mind seem odd to think about programming in MSDOS in 2018. ”
Last I checked FreeDOS was still being worked on.
Geesh. It’s been decades since I’ve done anything for DOS. I used DJGPP back then and didn’t really know what I was doing as I was still learning, but I did manage to produce some EXEs. I do, however, have this distinct feeling of having used DJGPP to produce COM-files, too, back in the day; maybe I just remember wrong, or maybe they’ve changed DJGPP over the years, but I certainly do have such a memory.
You know there is a fairly recent (1 year ago) development for GCC for IA16 (16-bit x86), right? I allows you to do the same thing for <386 real-mode targets.
There is experimental far-pointer support here (far keyword):
That’s fantastic news that the IA16 GCC is working. And that’s a great link to ready to go gcc binaries (codebench lite).
It’s been about 3 years since I was hacking my little 80286 program. I ended up doing a fair bit in assembler because C is kind of overkill for what I need. The advantage of C is I can unit test quickly on my “host” system to shake out the dumb bugs before bringing it over to the 16-bit world. Turbo Debug is nice and all, but it isn’t nearly as powerful as gdb + valgrind on my Linux box.
Nothing wrong with DJGPP. There was even a Quake 2 backport for DOS ~recently(2015) done with DJGPP.
Quake 2, in DOS, with pocket driver network and 3Dfx acceleration support! https://virtuallyfun.com/wordpress/2015/09/17/porting-quake-ii-to-ms-dos-pt5-3dfx-gamespy-quake-2-and-the-universe/
There’s still people using 3Dfx cards?! Gods, I Ioved my dual Voodoo2 set up.
I used to spend many countless hours playing Quake II, Half-Life and Unreal running those cards. I think even Carmageddon had Glide support or a patch added later or some such. People just went crazy over those cards. I recall skipping reviews on games if Glide support wasn’t checked off.
Time has not treated that company or the products well. I remember how they eventually closed up shop. People spent a lot of hours trying to keep the old drivers working, or acquiring the last remaining Voodoo cards. (Was Voodoo 5 the last one?) I think there was even a motherboard prototype that cropped up that people went nuts over.
I suppose it would be fun to reassemble the old hardware and fire up an old game from that era.
I’ve ported some stuff to DOS and the easiest way to produce DOS code is to use DJGPP. Obviously, getting it is an issue, therefore you should use this build framework which works for just about everything: https://github.com/andrewwutw/build-djgpp
I’ve still got my (not open) watcom compiler – and for that matter my old copy of turbo pascal. So why is programming on an old board a problem??
I just checked, yep – you can still get turbo pascal as a free download, and it will run on a 8086 with 64K of memory
and turbo C http://edn.embarcadero.com/article/20841
I agree. But with one caveat. A much newer version of gcc would have quite a bit going for it. Some of the older compilers were known to generate fairly awful code. Half the time I just used them to escape out into asmland anyway.
“It mind seem …” ==> “It might seem …”
I’m assuming a large percentage of HaD viewers are into obsolete tech and RF? Just curious because I’m not and don’t get the feeling it’s a topic most hack.
I’ve never seen any of my suggestions/links for hardware (mechanical/electronics) related topics but do see lots of this vintage hardware, high-level software developer, 3D printing, and RF articles.
Just wondering if it’s because that’s what the HaD customer base is, or is it mostly driven by the writers?
Need to compile for 16-bit DOS under Windows? Use Microsoft Visual C 1.52c. This was the last Microsoft C compiler with the ability to generate 16-bit x86 code. If you are a professional (i.e. have money), its available as part of the MSDN subscription (a yearly fee for access to Microsoft’s software library). MASM 6.11 is also available via the MSDN at certain subscription levels. Yes, there are still professionals in the world who have a need to write code for x86 real-mode.
Please be kind and respectful to help make the comments section excellent. (Comment Policy)