16BIT C comliper (Maybe GCC!)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

16BIT C comliper (Maybe GCC!)

Post 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:
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: 16BIT C comliper (Maybe GCC!)

Post 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
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: 16BIT C comliper (Maybe GCC!)

Post by Nessphoro »

There is no flat 16 bit mode.
User avatar
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Re: 16BIT C comliper (Maybe GCC!)

Post by eino »

The Watcom C compiler creates 16bit real mode code...

Can anyone tell if that can be used in unreal mode?
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: 16BIT C comliper (Maybe GCC!)

Post 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:
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: 16BIT C comliper (Maybe GCC!)

Post by VolTeK »

MadZarx wrote:heared
Your subscription is no longer qualified to the customer support here.
Vaclav
Posts: 3
Joined: Sun Jul 21, 2013 11:23 am

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
User avatar
zeitue
Member
Member
Posts: 88
Joined: Fri Dec 14, 2012 6:05 pm
Libera.chat IRC: zeitue
Location: United States, Texas
Contact:

Re: 16BIT C comliper (Maybe GCC!)

Post 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
### Z++; && S++; ###
zeitue is pronounced zeɪtə
Web Site::Bit Bucket
Programming Languages: C, C++, Java, Ruby, Common Lisp, Clojure
Languages: English, zɪ̀ŋ, 日本語, maitraiuen
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: 16BIT C comliper (Maybe GCC!)

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
MadZarx
Member
Member
Posts: 85
Joined: Mon Apr 01, 2013 5:06 am
Location: CMOS :D

Re: 16BIT C comliper (Maybe GCC!)

Post 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
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: 16BIT C comliper (Maybe GCC!)

Post 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.
Post Reply