Page 1 of 1

ASM and C in a Bootloader

Posted: Sun Jun 01, 2008 9:29 am
by ollie123
Hi guys, I'm back with another question. And here it is:

Is it possible to use Assembly and C in a bootloader? If so, please tell me how. I tried the wiki but I couldn't seem to pick anything up from it.

Thanks,
Ollie. :D :D

Posted: Sun Jun 01, 2008 10:04 am
by itisiuk
for a boot sector i think you can only use asm

but for a bootloader something like grub, you can use anything you want really.

Posted: Sun Jun 01, 2008 10:06 am
by ollie123
itisiuk wrote:for a boot sector i think you can only use asm

but for a bootloader something like grub, you can use anything you want really.
Ok thanks, I'll write my bootsector in ASM. Thanks.

Posted: Sun Jun 01, 2008 10:28 am
by suthers
Its possible to put C in a boot sector, its just really hard and stupid...
Jules

Posted: Sun Jun 01, 2008 10:31 am
by svdmeer
The first part of your bootloader will be 16-bit code. It's possible to create 16-bit code with C (I have seen people doing it in Borland C 2.0) but there are a limited number of compilers and I don't know a free one (free as in free speech, not free beer).

With free compilers like GCC you can make 32-bit code. I wrote the 16-bit parts in assembly, after switch to a protected mode 32-bit codesegment you can use code written in C.

There are some complications: In the bootloader you need to call the BIOS for loading disksectors. You have to choose between writing the routines for loading files in memory in 16-bit realmode (when the memory is full you can switch to protected mode to move the read files to a higher part in memory) or in 32-bit protectedmode (for sector reading you temporary switch back to realmode). If you choose 32-bit you can write everything in C, but you have to write some assembly routines switching to realmode and reading sectors.

Posted: Sun Jun 01, 2008 10:57 pm
by Candy
There is BCC, Bruce's C Compiler. That is a 16-bit compiler but it is a bit awkward to use as it doesn't do C89, let alone any variant after that.

Posted: Mon Jun 02, 2008 6:44 am
by Brynet-Inc
Candy wrote:There is BCC, Bruce's C Compiler. That is a 16-bit compiler but it is a bit awkward to use as it doesn't do C89, let alone any variant after that.
I know, K&R with some ANSI tacked on... using the -ansi argument might make it less-awkward. :lol:

EDIT: http://tack.sourceforge.net/ seems to have 8086 support, this was the C compiler used by Minix once upon a time, BSD licenced now.

"The ACK contains compilers for ANSI C, K&R C, Pascal, Modula-2, Occam 1, and a primitive Basic."

Posted: Mon Jun 02, 2008 8:59 am
by Martijn
Actually, using GAS's '.code16gcc' directive, GCC can be used to write C code for a 16 bit code segment.
GAS will add the necessary address/operand override prefixes to the 32 bit instructions generated by GCC.

Just make sure you stay within your data segment limits. ;)
For a real life example of 16 bit c code, examine the linux boot code. (arch\x86\boot)

Posted: Mon Jun 02, 2008 10:08 am
by jnc100
AFAIK OpenWatcom at least used to support compiling to 16-bit code with some extensions to allow supporting segments.

Regards,
John.