ASM and C in a Bootloader

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.
Post Reply
User avatar
ollie123
Member
Member
Posts: 26
Joined: Tue May 27, 2008 2:42 am
Location: Great Yarmouth, Norfolk, United Kingdom

ASM and C in a Bootloader

Post 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
itisiuk
Member
Member
Posts: 98
Joined: Mon Mar 24, 2008 1:46 pm

Post 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.
User avatar
ollie123
Member
Member
Posts: 26
Joined: Tue May 27, 2008 2:42 am
Location: Great Yarmouth, Norfolk, United Kingdom

Post 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.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Post by suthers »

Its possible to put C in a boot sector, its just really hard and stupid...
Jules
svdmeer
Member
Member
Posts: 87
Joined: Tue May 06, 2008 9:32 am
Location: The Netherlands

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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."
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Martijn
Posts: 22
Joined: Tue Feb 26, 2008 3:43 am
Location: The Netherlands

Post 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)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

AFAIK OpenWatcom at least used to support compiling to 16-bit code with some extensions to allow supporting segments.

Regards,
John.
Post Reply