致电 C++从程序集引导加载程序

发布于 2024-09-10 07:53:10 字数 554 浏览 4 评论 0原文

我有一个从教程获得的小型程序集引导加载程序。引导加载程序的代码可以在此处找到。我想知道是否可以从这个引导加载程序运行 c++。我想运行一个像这样的简单的事情:

#include <iostream>
using namespace std;

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

但正如我所看到的,它带来了两个问题。首先,C++ 文件必须包含在编译后的 bin 文件中。另外#include... iostream 是否包含在已编译的 C++ 文件中,或者是否需要包含在引导加载程序中的某种库中?

感谢您的帮助,因为这真的让我很困惑。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-09-17 07:53:10

要从汇编代码调用 C 函数,下面是一个示意图。使用 g++ 代替 gcc 应该允许您使用 C++ 代码。但我想知道你能写多少“C++”,因为你不能使用库函数,正如之前对你的问题的一些答复明确指出的那样。您最终可能会在 C++ 代码中编写汇编!

cboot.c

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

boot.asm

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

您可以通过这种方式编译和链接它们以创建可执行文件prog

nasm -f boot.o boot.asm

gcc -c cboot.c

gcc -o prog cboot.o boot.o

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
停滞 2024-09-17 07:53:10

您将无法运行任何具有任何外部依赖项或系统调用的代码。这限制了很多标准库函数,即所有 IO 函数(C stdio、iostreams),因为

  1. 它们都以一种或另一种方式进行系统调用,这些调用是对内核的调用,而您没有,因为您的程序 是内核。
  2. 它们以某种形式的外部共享库(例如libc、libstdc++)出现,需要用户空间中的动态链接器。

您必须推出自己的标准库,该库可以在特定硬件上的内核空间中运行。

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.

吲‖鸣 2024-09-17 07:53:10

首先,除非您实现它或将 STL 静态链接到引导加载程序,否则您将无法使用 iostream 或 cout。 STL 可能必须是特定于 booloader 的。

至于从 Assembly 调用 main 函数,您需要执行以下操作:

extern _main
;...
call _main

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文