0

I wrote a custom (VERY basic "Hello world!") bootloader in Assembler and I would like to execute a C program in that. Would the C program work, or fail due to a lost stdio.h file? And how could I bundle the C program along with the bootloader into a single .bin file to dd to a flash drive/CD?

1
  • 3
    the osdev wiki has plenty of information available regarding this type of question. You'll probably find the Bare bones "tutorial" interesting. Commented May 29, 2011 at 17:11

1 Answer 1

7

I'm not sure what you mean by "lost stdio.h", but many C runtime functions, including those prototyped in stdio.h, are implemented using system calls. Without an OS running, those system calls won't work.

It is possible to write C code that runs without an OS, for example most common bootloaders have just a tiny amount of assembler and mostly C code. The trick is to avoid using runtime libraries. Alternatives to syscalls, for e.g. display, are BIOS calls and hardware-specific I/O.

To take just one example, in addition to dynamic allocation, fopen in read mode needs the following low-level operations:

  • Reading a block of data from storage
  • Reading the file system metadata (often, superblock and root directory)
  • Processing file system metadata to find out where the file content is stored
  • Creating a FILE object that contains enough information for fread and fgetc to find the data on disk

You don't have an OS to help with any of that, your C code will need to implement a driver (possibly calling the BIOS) for block read, and implement the behavior of the other steps.

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

4 Comments

Is it possible to create a Unix-like filesystem and store at least libc and all of it's files, and also important Unix system executables such as bash, cd, grep, and everything in the /bin folder?
@Mohit: You can store those binaries in a filesystem, but they won't work, since they depend on syscalls. libc is very OS-specific.
Ok. So how would I combine the C and Assembler? I know to use "call", but how do I get my C files into the .bin?
@Mohit: You probably want to read about how a linker works. Also programs like elf2bin (commonly found in the build system for bootloaders).

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.