C Compiler in Bios/Real Mode?

Viewed 235

So, i want to program an OS. The bootloader is written in assembly. Now i do the Kernel but it is possible to write it in C? I Tried it on gcc -S but im on windows and the outputfile is: .file "Kernel.c" .text .ident "GCC: (MinGW.org GCC Build-2) 9.2.0"

2 Answers

The "Watcom" compiler supports Real-Mode code. You can even select 8086 as target CPU (however, you can also chose 80286 or 80386).

The compiler supports segmented code, too.

However, at least the old version 10.6 only supports the old OMF object file format (later versions seem to support COFF, but I doubt that COFF is supported for segmented code).

This object file format is quite tricky to convert to a better readable file format.

If your kernel is less than 64K in size, you may create a "DOS COM" file (supported by the compiler).

It is a rather complicated process - but provided you have access to gnu-binutils you can use objcopy:

gcc -c -m16 -Os -ffreestanding -Wall -Wextra -fno-exceptions -o main.o main.c 
ld -melf_i686 -static -nostdlib --nmagic -Ttext 0x1000 -o boot/kernel_entry.elf main.o
objcopy -Obinary boot/kernel_entry.elf kernel.bin

This should output a binary which you can execute using qemu

If you need an example then you can find a good example project with makefile here: https://github.com/cfenollosa/os-tutorial

Related