4

How do I compile 16-bit C code with GCC? I am trying to write a flat bootloader that just writes "Hello World!" to the computer and halts.

int main(int argc, char** argv)
{
    char* value = "Hello World!";
    __asm
    {
        mov si, value
    loop:
        lodsb
        cmp al, 0
    halt:
        je halt
        mov bx, 0x0007 ; Black BG, White TXT
        mov ah, 0x0E   ; Teletype output
        int 0x10
        jmp loop
    }
 }
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
  • 10
    You won't have a `main` -- you need a *free-standing* program, and you need to write your own linker script to produce something that can be used at boot time. Check out [osdev](http://wiki.osdev.org/Expanded_Main_Page) for some tutorials. – Kerrek SB May 06 '12 at 23:53
  • 1
    Btw, I'm working on a C compiler that can generate 16-bit code. See [Smaller C](https://github.com/alexfru/SmallerC). It might work quite well for small programs like presented in the question. The Snake test app/game is pretty much it. Take a look. – Alexey Frunze Dec 10 '13 at 10:01

2 Answers2

6

You don't. You can't. GCC doesn't generate 16-bit x86 code.

Use Open Watcom C/C++ or the ancient Turbo C++ (v 1.01 is freely available online).

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
3

It is possible to write 16 bit boot-loader , kernel in c . You need to put .code16 or .code16gcc at the top of the file to tell the compiler to generate 16 bit object file .

user43609
  • 113
  • 1
  • 2
  • 5