Execute C program at bootloader level via Assembler

家住魔仙堡 提交于 2019-12-12 05:29:10

问题


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 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.



来源:https://stackoverflow.com/questions/6169071/execute-c-program-at-bootloader-level-via-assembler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!