开发者

Call C++ from an assembly bootloader

I have a small assembly bootloader that I got from this Tutorial. The code for the boot loader can be found here. I want to know if its possible to run c++ from this boot loader. I want to run a simple thing like this:

#include <iostream>
using namespace std;

int main () {
cout << "Hello World!\n";
return 0;
}

But as I can see it brings up 2 issues. First somehow the C++ file has to be included in the compiled bin file. Also #include <iostream>... Is iostream included included in a compiled C++ file or dose it need to be included in some sort of library in 开发者_开发问答the boot loader?

Thanks for any help as this is really puzzling me.


To call a C function from your assembly code, here's a schematic. Using g++ in place of gcc should allow you to use C++ code. But I wonder how much of 'C++' you'd be able to write since you cannot use the library functions, as some of the earlier replies to your question clearly point out. You may end up writing assembly in your C++ code finally!

cboot.c

void bootcode(void) {
 /* code */
}

boot.asm

# some where you have this line
call bootcode
# more code to follow

You compile and link them this way to create the executable prog.

nasm -f boot.o boot.asm

gcc -c cboot.c

gcc -o prog cboot.o boot.o


You will not be able to run any code that has any external dependencies or system calls. This bars a lot of standard library functions, namely all IO functions (C stdio, iostreams), because

  1. they all make system calls one way or another, which are calls into kernel, which you don't have, since your program is the kernel.
  2. they come in some form of external shared library (e.g. libc, libstdc++) that requires a dynamic linker in user space.

You will have to roll your own standard library that works in kernel space on your particular hardware.


First of all, you won't be able to use iostream nor cout unless you implement it or statically link a STL to your bootloader. The STL will probably have to be booloader-specific.

As for calling your main function from Assembly, here's what you'd do:

extern _main
;...
call _main
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜