News for hackers. Stuff that matters.
Re:Show HN: A simple way to write standalone C programs for ... by tus88 on Wednesday October 02, 2019 @07:51AM (#21135405)
> they only work in the real-address mode

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

Re:Show HN: A simple way to write standalone C programs for ... by rwmj on Wednesday October 02, 2019 @08:45AM (#21135759)
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.
Re:Show HN: A simple way to write standalone C programs for ... by jacobush on Wednesday October 02, 2019 @10:29AM (#21136822)
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."

Re:Show HN: A simple way to write standalone C programs for ... by dexen on Wednesday October 02, 2019 @09:01AM (#21135911)
>> they only work in the real-address mode

Presumably it would work just fine in the "Unreal mode"[1] - one which is very similar to real, with the added benefit of being 32bit address space, and 32bit instructions by default

[1] https://en.wikipedia.org/wiki/Unreal_mode

Re:Show HN: A simple way to write standalone C programs for ... by qx89l4 on Wednesday October 02, 2019 @12:17PM (#21138112)
I'm not sure if defaulting to 32-bit instructions would still allow calling BIOS, but good point!

In this case the design goal was to show the easiest way to jump into C and start building the actual application, so I just tried to avoid any trickery (even enabling the A20 line)

Re:Show HN: A simple way to write standalone C programs for ... by PixelOfDeath on Wednesday October 02, 2019 @12:29PM (#21138263)
You just set your chosen segment register(s) in protect mode, jump back to real mode, and then can use this segment register(s) in combination with the 32bit prefix to make a 32 bit memory access. All other segment registers are untouched and you can call into the bios just fine.
Re:Show HN: A simple way to write standalone C programs for ... by Crinus on Wednesday October 02, 2019 @08:16AM (#21135559)
XT implies 8086 and there are more real address mode instructions than those available in 8086. AFAIK real address mode can use pretty much any 32bit instructions with a prefix, which is what this project is doing.

(this incompatibility bit me a bit many years ago when i was writing a small real mode OS in assembly - i used a bunch of instructions i thought were available in 8086 but when i tried the OS on a real XT machine it would lock up :-P)

Re:Show HN: A simple way to write standalone C programs for ... by qx89l4 on Wednesday October 02, 2019 @12:25PM (#21138210)
Targeting 8086-level of instructions was actually my first idea. It wasn't possible with gcc/clang, but Turbo C is freeware these days, so... ;) In case you're interested, see https://github.com/luke8086/nf/
Re:Show HN: A simple way to write standalone C programs for ... by oso2k on Wednesday October 02, 2019 @01:14PM (#21138692)
Re:Show HN: A simple way to write standalone C programs for ... by qx89l4 on Thursday October 03, 2019 @12:57PM (#21149113)
Hadn't heard of them, thanks for the links!
Re:Show HN: A simple way to write standalone C programs for ... by Crinus on Thursday October 03, 2019 @06:45AM (#21145523)
As someone mentioned below you could also use OpenWatcom, if for no other reason than it should have a slightly better optimizer (though from my tests i haven't seen much of a difference between the two, at least for code i tried to optimize a lot myself). AFAIK one of the OWv2 contributors is working on a 16bit OS so the 16bit support should be excellent.

OWv2 is also available as a Linux binary so you could use it for crosscompiling in a similar way to GCC.

Re:Show HN: A simple way to write standalone C programs for ... by qx89l4 on Thursday October 03, 2019 @12:55PM (#21149093)
Good point, OpenWatcom is obviously miles ahead of Turbo C, but it's also much more complex. I tinkered with it for a while, and ended up using the latter for its simplicity.

DOSBox can be easily installed on any system, you can save the entire C: in a git repo and it'll probably keep working in any number of years from now :)

Meanwhile, OWC doesn't have packages in popular Linux distros, only a custom binary installer, and on MacOS it doesn't even have that.

Re:Show HN: A simple way to write standalone C programs for ... by ofrzeta on Wednesday October 02, 2019 @03:24PM (#21139905)
You could also use OpenWatcom, I think. https://github.com/open-watcom/open-watcom-v2