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?
-
3the osdev wiki has plenty of information available regarding this type of question. You'll probably find the Bare bones "tutorial" interesting.Mat– Mat2011-05-29 17:11:02 +00:00Commented May 29, 2011 at 17:11
1 Answer
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
freadandfgetcto 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.
4 Comments
libc is very OS-specific.elf2bin (commonly found in the build system for bootloaders).