5

I don't know how to compile my C kernel for 16 bit real mode. I have tried a variety of compilers with no luck. My bootloader simply loads raw sectors from the floppy (my kernel lives right after the first sector on the disk) to the physical memory address 1000h:0000h then jumps to it. How can I compile my C kernel to work in 16 bit real mode?

my basic kernel:

void OSmain()
{
    unsigned char *videoram = (unsigned char *) 0xb8000;
    videoram[0] = 65;                                     /* character 'A' */
    videoram[1] = 0x07;                                   /* forground, background color. */

    while( 0 )
    {

    }
}

the compilers I have tried are GCC, tinyCC, and DMC. My goal is to get a flat binary file that I can jump to to begin execution.

TheFuzz
  • 2,607
  • 6
  • 29
  • 48
  • 1
    See also http://stackoverflow.com/questions/227762/looking-for-16-bit-x86-compiler – Dietrich Epp Jul 16 '11 at 22:10
  • 1
    Luck doesn't have anything to do with it. Which compiler(s) have you tried, and what were the results? Were you able to get the compiler(s) to generate the desired assembly code? Did you not get the expected results when you booted with your code? – Greg Hewgill Jul 16 '11 at 22:13
  • The Digital Mars compiler is supposed to still support 16-bit targets, though I'm not sure what capability the compiler/linker have to resolve to fixed segment addresses or create position independent code. Nonetheless, it might be worth looking at: http://www.digitalmars.com/features.html – Michael Burr Jul 16 '11 at 22:24
  • For the dmc compiler, what would be an example compile/link command for 16 bit C code? I read some of the documentation and I'm still a little unsure as to what switches I should use for the compiler and linker.... – TheFuzz Jul 17 '11 at 17:54

1 Answers1

6

First, I recommend you check out the OSDev Wiki, which has resources upon resources for developing your own OS and components.

Second, have you considered writing a bootloader in assembly that starts off in real mode, switches to protected mode, and then jumps to your (32-bit) compiled kernel?

Adam Maras
  • 26,269
  • 6
  • 65
  • 91