views:

58

answers:

3

Hello,

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.

+2  A: 

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.

Alex B
Hmmm is there any sort of "starting" library or does have to be os dependent?
happyCoding25
@happyCoding25, I don't know of any, and nope, this will have to be *hardware-dependent*, since you have no OS :) For example, this doc includes an example of how to write a "hello world" string into video memory on x86: http://www.osdever.net/tutorials/pdf/ckernel.pdf (that is, display it on the screen)
Alex B
Oh ok thanks I guess ill google around for libstdc++
happyCoding25
A: 

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
luiscubal
A: 

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

sauparna
Okay not familiar with g++ but ill look at it. Thanks
happyCoding25