问题
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:
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
andfgetc
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.
来源:https://stackoverflow.com/questions/6169071/execute-c-program-at-bootloader-level-via-assembler