6

I've tried nearly every C compiler with every setting, and I have yet to find a C compiler that can compile C into Intel x86 code that is suitable for Real Mode kernel development. I don't need any spiels on why you should use protected mode; I just need to know which C compiler (and if necessary, what settings to run it with) is required to compile a 16-bit, standalone binary with no OS dependencies, etc. Just bare-metal in Real Mode, and nothing more.

I'm pretty sure this is not a duplicate of [16 bit C code for real mode kernel]: 16 bit C code for real mode kernel because they accepted switching to Protected Mode as an answer, and I explicitly stated that the result MUST work for Real Mode.

Community
  • 1
  • 1
Joseph Caruso
  • 175
  • 1
  • 11
  • "I know this is off topic but I'll post it anyway and put the onus on the reader to tell me where I should have posted it instead." Um, no. That is not how this works... – John3136 Sep 15 '16 at 04:02
  • Why exactly was my question down-voted? A simple "This question belongs at ________ rather than here" would have sufficed. – Joseph Caruso Sep 15 '16 at 04:02
  • @John3136 Well, where do you propose this question be asked? Is there another StackOverflow branch in which fits this category? – Joseph Caruso Sep 15 '16 at 04:04
  • 1
    There are other questions like this recently. (Past 2 days). I recommend OpenWatcom with the JLOC linker to do 16-bit bootloader work. But if you want to go the dangerous path of pseudo hacked up GCC in real mode, here is a simple boot loader that prints hello world with BIOS interrupts: http://www.capp-sysware.com/misc/ircasm/gccboot/ – Michael Petch Sep 15 '16 at 04:23
  • I have did a lot of research on this and it is a shame I cannot put answer to this question. But let me put an answer for you. If you check C compiler history almost all compilers are derived from portable C. Also there is The Amsterdam Compiler Kit which can compile 16bit dos com files and it is BSD licensed which is way better than openwatcom license... Can see some more tools here: https://www.thefreecountry.com/compilers/cpp.shtml – Abdurrahim May 13 '21 at 23:48

1 Answers1

-5

C is not a language designed to run in real mode and therefore unfortunatly does not allow 16-bit compiling.

use NASM assembly or any other assembly language to get what you want. its a little learning curve but once it clicks your understanding of how the computer functions and OSDev will fly though the roof.

I suggest looking at this wiki and forum full of like-minded people and tutorials http://wiki.osdev.org/Main_Page

Good luck!

grantperry
  • 139
  • 4
  • 18
  • I do appreciate your detailed answer with links and explanation, but I'm pretty familiar with osdev (I'm a member there :) I'm fluent in Intel-syntax assembly and use NASM all the time.. I was just wondering if C could do the job as well. I was hoping it could be done, but oh well.. Thank you :) – Joseph Caruso Sep 15 '16 at 04:08
  • From research and prior wonder I have run to the conclusion that C is not a viable option for real mode :( sorry i could not help :) – grantperry Sep 15 '16 at 04:10
  • On the contrary! You did help. You answered my question as it needed to be answered, which is all I asked for, so thank you. :) – Joseph Caruso Sep 15 '16 at 04:11
  • Really? What mode a C program compiled with ancient compilers from MS and Borland for DOS were running? Not talking about K&R compilers for PDP11 – Serge Sep 15 '16 at 04:12
  • 1
    It's not that C (the language) isn't designed to run in Real Mode. There are 16-bit compilers, like bcc. The big problem is that the usual compilers (gcc and clang) don't have much in the way of 16-bit support. It is true that pure ISO C doesn't have segmentation as part of the language, so if you need to work with multiple segments it might be easier to just use asm directly. – Peter Cordes Sep 15 '16 at 04:14
  • Is there a LLVM backend for x86 real mode? Should be easy to find out. – BitTickler Sep 15 '16 at 04:14
  • I am not a pro at the DOS side of things but I believe programs are compiled in 16-bit and must run with DOS as the host, providing higher level functions, which I believe may not have the desired effect of the original question @Serge – grantperry Sep 15 '16 at 04:15
  • @PeterCordes I tried BCC... Sadly, it took a perfectly good C source file and produced errors from the most basic of C code. Not sure if it's even safe to call it a C compiler with how basic of C code it choked on – Joseph Caruso Sep 15 '16 at 04:16
  • http://lists.llvm.org/pipermail/llvm-dev/2014-January/069344.html albeit a bit aged creates the impression, that llvm has a 16 bit target facility in place. – BitTickler Sep 15 '16 at 04:16
  • @JosephCaruso: Like what? I don't think it's particularly well maintained, but I assumed it at least supported C89. Did it choke on C99 features like `for (int i=0 ; i<10 ; i++){}`? C89 is technically still C, so if that's the problem, then you should say that you're not interested in being limited to an obsolete form of the language either. – Peter Cordes Sep 15 '16 at 04:20
  • @teenHack42 you are incorrect. C program could run on a bare metal cpu with help of a small bootstrap assembly code. – Serge Sep 15 '16 at 04:20
  • @BitTickler: gcc also has a `-m16` mode, but it basically still generates the same instructions as for 32-bit mode but with `.code16`, so most instructions have an operand-size and/or address-size prefix. (e.g. `int` is still 32-bit). Is that what the LLVM backend is like? – Peter Cordes Sep 15 '16 at 04:21
  • 7
    "C is not a language designed to run in real mode and therefore unfortunatly does not allow 16-bit compiling." - um, what? When do you think C was created, and on what computers? And what features of C make it incompatible with real mode? – user253751 Sep 15 '16 at 04:27
  • Not only C but even C++ was compiling for 16bit: https://winworldpc.com/product/visual-c/1x – Abdurrahim May 14 '21 at 11:03