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.
ASM and C in a Bootloader
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.
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.
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
I know, K&R with some ANSI tacked on... using the -ansi argument might make it less-awkward.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.
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."
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)
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)