3

So far i have an assembly script that lets you boot from it and write hello world to the screen. now i want to be able to write some c code and be able to have my assembly script run it somehow. Im using mingw gcc for my compiler. This is my bootloader in assembly:

 org 7C00h
 jmp short Start  
 Msg:    db "Hello, world"  EndMsg:  Start:  mov bx, 000Fh   
 mov cx, 1       
 xor dx, dx  
 mov ds, dx
 cld               Print:  mov si, Msg   

          Char:   mov ah, 2      
 int 10h
 lodsb


 mov ah, 9   
 int 10h
 inc dl      
 cmp dl, 80      
 jne Skip
 xor dl, dl
 inc dh
 cmp dh, 25    
 jne Skip
 xor dh, dh  
 Skip:   cmp si, EndMsg
 jne Char        
 jmp Print        times 0200h - 2 - ($ - $$)  db 0    
 dw 0AA55h

My question is how to compile the c code so that the assembly script will execute it? also how would i edit my current boot loader so that would be possible?

3

3 Answers 3

4

First, you should select a compiler to use and study its specifics. C programs can be compiled for "hosted" environment which supposes standard initialization, main() as entry point and standard library, and "freestanding" environment where such guarantees aren't provided. To call C code you shall do appropriate environment setup (stack, data segments, etc.) in the bootloader. Details for this are compiler-specific. Converting of object code to form suitable for your bootloader can also be problem. For 16-bit code, there are at least Turbo C and Watcom C available for free use. Watcom is more progressive and modern and is open source now. Note that standard library is unavailable in such setup, so C code shall be specially written for your environment; this includes such underwater stones as e.g. multiplication of 32-bit longs was usually implemented in library, despite you don't write library call.

[In general, I'm confused with your request to use precisely C; as soon as bootloader unlikely needs execution speed, it's more proper to use something more applicable for such context, as Forth. There are ready to use implementations (see FreeBSD bootloader).]

Sign up to request clarification or add additional context in comments.

Comments

1

You need to compile the C code for 16 bits. Then copy it to a given position in the boot device. You'll need to modify your bootloader to load the code into memory and jump to it. Also note, gcc is currently unable to compile to 16 bits. I heard Watcom compiler can, so it should be of use.

Comments

0

Generally you need do the following things:

  1. You'd better prepare a cross-compiler, this would avoid lots of trouble.
  2. Your bootloader should be able to find the directory/file in floppy or CD. For example if you bootloader is installed on the CD, you need find a way to parse the CD content. You can refer to "El Torito" standard for CD media.
  3. Your bootloader should prepare the environments for your c program. For example you C program expects a Protect mode with 32bit, your bootloader should prepare it.
  4. Your bootloader should load the C program to the memory, and jump to it.
  5. In most of the case, you have to write another bootloader. Your #1 bootloader should load the #2 bootloader, and the #2 bootloader load your C program. Your second bootloader can written in assembly, that will be much easier to verify your work.
  6. Your C program should be complied as freestanding mode.

You need google many many times for all the details...

1 Comment

You aren't prevented from using 16-bit C code if you specifically use a compiler that can target 16-bit executables/binaries like MSVC 1.52c; OpenWatcom; or something like Bruce's C Compiler (bcc) which is available on most Linux distros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.