
Show HN: A simple way to write standalone C programs for i386 - qx89l4
https://github.com/luke8086/boot2c
======
rwmj
We actually wrote an optionrom in C for qemu a few years back. The main
challenge was making it work on all the different C compilers that qemu
supports because it makes some assumptions about the layout of the final
binary which are not necessarily true for modern optimizing compilers. Anyhow
if ever use qemu on x86 with the -kernel option then you are running this
code.

[https://git.qemu.org/?p=qemu.git;a=blob;f=pc-
bios/optionrom/...](https://git.qemu.org/?p=qemu.git;a=blob;f=pc-
bios/optionrom/linuxboot_dma.c)

~~~
qx89l4
I feel your pain, so sorry that you still need to support the .code16gcc hack!
My first approach was actually to avoid GCC/clang altogether, and use Turbo C
instead. It came with its own set of problems, but the output was much more
predictable. In case you're interested, you can find the code at
[https://github.com/luke8086/nf](https://github.com/luke8086/nf)

~~~
oso2k
tkchia [0] has been working on getting gcc to emit 16-bit clean code (no
32-bit code prefixes) along with ancillary tooling like binutils, a couple
different libc's, and some of the things you'd expect from a 16-bit x86 C
compiler like far pointers and DOS-specific libc functions [0]. The use case
is to build the FreeDOS Kernel and ELKS 16-bit Linux Kernel from a 32-bit or
64-bit Linux system.

[0] [https://github.com/tkchia/gcc-ia16](https://github.com/tkchia/gcc-ia16)

[1] [https://github.com/tkchia/build-
ia16/releases](https://github.com/tkchia/build-ia16/releases)

------
saagarjha
The code was surprisingly short and readable. If you have a minute, I’d
suggest taking a peek; it’s pretty well designed.

~~~
frank_nitti
Very refreshing approach, as someone deeply interested in this topic but
without the time to dive in to the docs myself

------
Udo
I wonder what a minimal version of this would look like for x64 UEFI machines.
Does anyone know whether such a project exists?

~~~
dantle
It's pretty easy. With CL.exe (Microsoft's Visual Studio compiler) just use
the /SUBSYSTEM:EFI_APPLICATION flag. You may have to manually specify the
entry point. Note that there's no default cstdlib, so if you want to use the
EFI APIs (e.g. for a "printf" function or other things from the article),
you'll need to tell the compiler about the header definitions (as you would
any C API).

------
ape4
If you could get a TCP/IP stack working, it could be used for a simple IoT
device.

------
nodefourtytwo
Isn't that a unikernel [0]?

I've been wanting to try Hermitcore [1] for a while.

[0]
[https://en.wikipedia.org/wiki/Unikernel](https://en.wikipedia.org/wiki/Unikernel)

[1]
[https://github.com/hermitcore/libhermit](https://github.com/hermitcore/libhermit)

------
tus88
> they only work in the real-address mode

So it's not for i386, it's for XT by the sounds of it.

~~~
rwmj
No, he's using -m16 -march=i386 which creates 32 bit code that boots in 16 bit
mode using the 32 bit size prefix on most instructions, and that will only
work on 80386 and above.

~~~
jacobush
This is so insane, I had no idea such a thing was possible:

16 vs 32 bit It is possible to use 32-bit instructions in the real-address
mode, they just need to be marked with address-size and operand-size prefixes.
The -m16 option for clang/gcc, and .code16 directive in GNU assembler, do
exactly that. The resulting code is 32-bit, only marked everywhere with those
prefixes. It's not compatible with actual 16-bit CPUs.

 _" Unfortunately the 32-bit addresses still cannot exceed the boundary of the
segment (65535), otherwise they'll trigger an exception. QEMU doesn't emulate
this behaviour, so it's useful to occasionally test with Bochs."_

------
ColanR
If the available RAM were somehow expanded to the full amount available in
hardware, it seems like this could be an interesting type of HPC system. No
bloat, simple execution.

------
jmpman
Awesome. I’ve wondered the same. Thanks for the tutorial.

------
dusted
That's neat :)

------
peteradio
Comes with snake.

------
ngcc_hk
Today is not the best day as a HongKonger. But that is a good source and I put
aside when I have the heart and soul as a 386 hacker. God bless. Good work.

------
Accujack
Millennial discovers DOS. Film at 11.

;)

~~~
blacksmith_tb
It's running only on BIOS, no operating system.

~~~
Accujack
That's literally what DOS is... a loading program that itself ran from a boot
sector and provided services layered on top of BIOS calls.

~~~
qx89l4
I "discovered" DOS 25 years ago, not sure what's your point?

------
ape4
640KB ought be enough memory for anyone \- Bill Gates

~~~
pacaro
"I've said some stupid things and some wrong things, but not that. No one
involved in computers would ever say that a certain amount of memory is enough
for all time."

An actual Bill Gates quote

