Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 732
nosedive25 Avatar asked Sep 12 '26 12:09

nosedive25


2 Answers

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
like image 189
rup Avatar answered Sep 14 '26 02:09

rup


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.

like image 38
Alex B Avatar answered Sep 14 '26 00:09

Alex B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!