Page 1 of 2

16BIT C comliper (Maybe GCC!)

Posted: Sun Jul 28, 2013 1:50 pm
by MadZarx
Hello guys. I'm looking for a 16bit C compiler to build my bootloader in C and use less assembly.
You may say that there are no 16bit C compilers or you don't know any of them. But what do you say if I tell you that GCC may compile 16bit C code? If you take a look at linux kernel source code there is a directory in "/arch/x86/boot" that all the code in there is only for real mode. So when we compile that kernel with GCC it should compile all the code in there and it should produce 16bit C code for the bootloader that is in real mode. But how is it possible? Please tell me if someone knows whats happening there or if I was wrong with this :mrgreen: :mrgreen:

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Jul 28, 2013 2:46 pm
by Nessphoro
Hello newfriend,

Surely you forgot to RTFM or even do a basic search on this very website.
Nevertheless, GCC compiles 16BIT protected mode code. There is no reason in fighting the machine and not embracing assembly for things such as boot loaders and fast-path code segments.

With best regards.



P.S Refrain from using emoticons.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Jul 28, 2013 3:45 pm
by MadZarx
I'm Sorry :oops:
But I don't want 16bit protected mode. I just want 16bit flat C code. How is it possible? :D

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Jul 28, 2013 11:42 pm
by Nessphoro
There is no flat 16 bit mode.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Mon Jul 29, 2013 2:11 am
by eino
The Watcom C compiler creates 16bit real mode code...

Can anyone tell if that can be used in unreal mode?

Re: 16BIT C comliper (Maybe GCC!)

Posted: Mon Jul 29, 2013 2:39 am
by Kevin
Why don't you just run your bootloader in PM? For calling BIOS interrupts, you can have functions in assembly that temporarily switch back to RM. Compared to the complete size of a real bootloader that deserves this name, these functions should be really minimal. And you want to be able to load data to > 1 MB, so RM doesn't quite cut it anyway.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Mon Jul 29, 2013 1:25 pm
by MadZarx
Kevin wrote:Why don't you just run your bootloader in PM? For calling BIOS interrupts, you can have functions in assembly that temporarily switch back to RM. Compared to the complete size of a real bootloader that deserves this name, these functions should be really minimal. And you want to be able to load data to > 1 MB, so RM doesn't quite cut it anyway.
I know that I can switch back to real mode and use C codes. I mean is there any way to use 16 bit flat C code in bootloader(bootsector) instead of assembly?
eino wrote:The Watcom C compiler creates 16bit real mode code...
I have heared about it but I didn't know that it can output 16bit code. I will give it a try :mrgreen:

Re: 16BIT C comliper (Maybe GCC!)

Posted: Mon Jul 29, 2013 2:38 pm
by VolTeK
MadZarx wrote:heared
Your subscription is no longer qualified to the customer support here.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Fri Aug 02, 2013 1:38 pm
by Vaclav
I wanted to do the same, C in real mode with GCC. I found that GCC is 32 bit compiler, it can't generate 16 bit assembly. However, it's assembler, GNU AS can assemble 16 bit instructions for real mode, but you have to inform the assembler about it by the .code16 directive.
You can also tell GAS that "This code is from GCC, but I want to assemble it for 16 bit mode." by .code16gcc, read here: http://sourceware.org/binutils/docs-2.2 ... 16bit.html
I got it working this way: I've created a function in a C file and generated assembly from it with gcc -S. Then added .code16gcc to this, and called the function from another assembly file. Just watch out that the code from gcc will expect you to push 4 bytes to stack, not 2. Also when calling the function, add .code16gcc before it and .code16 after it, as gcc calculated with a 4 byte push for call, not 2 byte which is default. About passing parameters to the C function and back, read about cdecl calling convention, GCC uses this one. Good luck.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Fri Aug 02, 2013 3:32 pm
by zeitue
How about BCC? It was designed for a port of Linux to the 8086 machines.
This is a C-compiler for 8086 cpus which is important for the development of boot loaders or BIOS related 8086 code.

It is possible to run 8086 code under i386 Linux using an emulator, `elksemu', also included in this package.
here is a Debian BCC Package

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Aug 04, 2013 7:58 am
by MadZarx
Vaclav wrote:I wanted to do the same, C in real mode with GCC. I found that GCC is 32 bit compiler, it can't generate 16 bit assembly. However, it's assembler, GNU AS can assemble 16 bit instructions for real mode, but you have to inform the assembler about it by the .code16 directive.
You can also tell GAS that "This code is from GCC, but I want to assemble it for 16 bit mode." by .code16gcc, read here: http://sourceware.org/binutils/docs-2.2 ... 16bit.html
I got it working this way: I've created a function in a C file and generated assembly from it with gcc -S. Then added .code16gcc to this, and called the function from another assembly file. Just watch out that the code from gcc will expect you to push 4 bytes to stack, not 2. Also when calling the function, add .code16gcc before it and .code16 after it, as gcc calculated with a 4 byte push for call, not 2 byte which is default. About passing parameters to the C function and back, read about cdecl calling convention, GCC uses this one. Good luck.
Vaclav are you sure its wroking well without any triple faults or problems?

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Aug 04, 2013 8:16 am
by Combuster
MadZarx wrote:Vaclav are you sure its wroking[sic] well without any triple faults
Of course not. Neither Vaclav nor GCC is going to prevent you from screwing up yourself, and neither is going to take the blame for that when (there is no "if") you do.

You made two serious mistakes here. Fix them first:
1: Not knowing what you're asking for
2: Not trying.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Sun Aug 04, 2013 9:24 am
by bluemoon
I don't know why anyone would want to write boot loader with 16-bit C. It's so obvious that writing a early stage (with do all the necessary real mode functions) with assembly is straightforward*, and any complex logic could be implemented with 32-bit C after entering protected mode or long mode.

* By straightforward, I meant a reasonable early stage boot loader can be implemented in about 500 lines of assembly code.
* In contrast, insist to do that with C you not only end up with almost equal amount of (inline) assembly, but also suffer from the lack of tools.

Re: 16BIT C comliper (Maybe GCC!)

Posted: Mon Aug 05, 2013 1:19 pm
by MadZarx
Combuster I know what I am asking for. I just need a way to compile C code to 16bit object file in some way that Vaclav said how should I do it. I tested it and it worked well. And I know it's much better to build the bootloader in assembly. But what if I wanted to build a simple Real mode kernel (Like mikeos)? So it may take a long time and I should write lots of code. So This is why I need a 16bit C compiler

Re: 16BIT C comliper (Maybe GCC!)

Posted: Tue Aug 06, 2013 5:33 am
by sortie
Don't confuse "Real mode operating system" with "Simple to write and maintain OS". In many ways, it's much nicer in 32-bit protected mode and 64-bit long mode. You are doing yourself a disfavour dealing with Real Mode that died a couple decades ago.