Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for 16-bit x86 compiler

I am working on an embedded systems project and have run into an issue of the compiler being programatically embedded in the Paradigm C++ IDE. I would like to be able to automate building.

The processor is the AMD186ES. I am not working with the OS - just baremetal stuff. I need to generate real-mode 16-bit 8086 machine code from C++.

My googling indicates that G++ can build such code.

My questions are:

Can g++ be configured to build this machine code?

Are there other C++ compilers that can do it as well?

like image 804
Paul Nathan Avatar asked Oct 22 '08 22:10

Paul Nathan


5 Answers

Your best bet is probably OpenWatcom, which includes a C++ compiler. Back in the early-to-mid 90s, I believe this was the best C/C++ compiler around. It was open-sourced a few years ago.

like image 181
Simon Howard Avatar answered Oct 18 '22 10:10

Simon Howard


I am currently using gnu as (part of binutils and the assembler used for gcc) and I have successfully been assembling 16bit assembly code with the following:

as <file>
ld --oformat binary -Ttext 0x0 -e start <file>

with my assembly files starting out with:

.code16
.globl start
.text
start:

since its plain binary omitting the lines,

.globl start
start:

will simply yield an warning, even though flat binaries need no entry point.


something I learned the hard way;

-Ttext 0x0

is critical, otherwise the .text segment is pushed outside of 16bit addressing range (don't ask me why)

I am personally still learning assembly, so this is just my way, not necessarily the best way.


EDIT: If you are writing boot code, you should change

-Ttext 0x0

to

-Ttext 0x7c00

this will offset your memory addresses by 0x7c00 since boot code is usually loaded at 0x7c00 by the BIOS.

like image 35
Hawken Avatar answered Oct 18 '22 09:10

Hawken


Doesn't your chip vendor (AMD, I guess) have any pointers to compilers for the chip?

If not, you may be able to use some 16-bit DOS compilers - but you'll have several potential big problems:

  1. getting a library for the compiler that is not dependent on the BIOS or MS-DOS
  2. debugging
  3. linkers for embedded systems usually have specific support for locating code in specific memory regions. That's not usually included in compilers for DOS, but you may be able to find some sort of linker/locator that'll do the trick for you.

A couple of compilers that are still supported and generate 16-bit code are:

  • Digital Mars
  • Open Watcom
like image 40
Michael Burr Avatar answered Oct 18 '22 11:10

Michael Burr


This google search shows a series of links for setting gcc up as a cross compiler. To get it to target something other than a standard ELF binary you can frig the output. This link discusses excluding the standard libraries and customising the output format. You may have to do some fiddling to get it to work.

As an alternative openwatcom.org has an open-source version of the Watcom C compiler, which might also be able to do what you want.

like image 7
ConcernedOfTunbridgeWells Avatar answered Oct 18 '22 09:10

ConcernedOfTunbridgeWells


There's a patch for GCC 4.3: New back end ia16: 16-bit Intel x86

And here's an update for it. Note that it probably doesn't work very well, for example, the updated post says: "Constructors and destructors are now supported, but for some reason they only work on the elks configuration."

This Docker container has a build of it: https://registry.hub.docker.com/u/ysangkok/ia16-gcc-rask

I didn't manage to make DOS binaries yet: How do I assemble GAS assembly and link it with the Open Watcom C library?

like image 6
Janus Troelsen Avatar answered Oct 18 '22 09:10

Janus Troelsen