将两个 ORG 放在一起
我正在构建一个引导加载程序,用于引导位于软盘 1000h
部分的内容。我使用 Fasm 来做到这一点(因为我的朋友只使用 Fasm,他正在帮助我),但我更喜欢使用 Nasm,现在我遇到语法问题,那么我想知道如何在Nasm:
org 7C00h
%include "boot.asm"
org 1000h
%include "kernel.asm"
PS:我已经使用 Nasm 语法风格放置了 %include
指令,在 Fasm 上它应该只是 include
。
I'm building a boot loader that boots the content that is located at the 1000h
part of the floppy. I was doing that using Fasm(because my friend only uses Fasm, and he was helping me with this), but I prefer to use Nasm, and now I'm having problems with the syntax, then I want to know how could I do this in Nasm:
org 7C00h
%include "boot.asm"
org 1000h
%include "kernel.asm"
PS: I already put the %include
directive using Nasm-syntax style, on Fasm it should be just include
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的答案是,这在 NASM 中无法完成。
org
语句在 FASM 中的工作方式与 NASM 中的工作方式相同,但在 MASM 中的工作方式有所不同。在 NASM 中,示例代码必须单独组装,然后组合以创建最终图像。
令人高兴的答案是,这是罕见的(也可能是唯一的)需要将具有不同起始地址的代码组合(使用 NASM)或组装(使用 FASM)到单个映像中的情况。
引导扇区由 BIOS 传输到
7C00h
。在介质(软盘、硬盘、USB 闪存驱动器)上,紧随其后的是有效负载,有效负载由引导扇区(引导加载程序)传输到其起始地址。The simple answer is that this cannot be done in NASM.
The
org
statement works the same in FASM as it does in NASM but differently in MASM.In NASM the example code would have to be assembled separately and then combined to create the final image.
The happy answer is that this is the rare (and probably only) case where code with different start addresses needs to be combined (with NASM) or assembled (with FASM) into a single image.
The boot sector is transferred to
7C00h
by the BIOS. It is immediately followed on the media (floppy disk, hard drive, USB flash drive) by the payload which is transferred to it's start address by the boot sector - boot loader.请参阅此处了解您的问题或我的问题的描述我认为这是因为从问题中很难看出。在发布带有“我遇到语法问题”的问题时,最好实际显示语法问题是什么:-)
请参阅此处 获取解决方案(但它可能不起作用,请参见下文)。
基本上,NASM 中的 org 语句旨在设置节的基地址,不能用于任意将字节插入流中。它建议您使用类似以下内容:
但是,您是否考虑过您要做什么。如果您要创建一个平面二进制文件来加载到内存中,我认为您不希望将引导扇区和内核都放在一个文件中。
BIOS 将希望将引导扇区作为 7c00:0 处的单个块加载,并且当内核位于该块的开头时,几乎肯定会感到困惑。我认为您需要做的是创建两个完全独立的平面二进制文件,一个用于引导扇区,另一个用于内核。 BIOS 将加载您的引导扇区,然后您的引导扇区将加载您的内核。
然后您可以将相关的 org 语句放入两个源文件中,您的问题应该会得到解决。
See here for the description of your problem or what I think it is since it's a little hard to tell from the question. It's a good idea when posting questions with "I'm having problems with the syntax" to actually show what the syntax problem is :-)
See here for the solution (but it may not work, see below).
Basically, the
org
statement in NASM is meant to set the base address for the section and cannot be used to arbitrarily insert bytes into the stream. It suggests you use something like:However, have you thought about what you're trying to do. If you're creating a flat binary file to load into memory, I don't think you want both the boot sector and kernel in a single file anyway.
The BIOS will want to load your boot sector as a single chunk at 7c00:0 and will almost certainly be confused when it has the kernel at the start of that chunk. I think what you will need to do is to create two totally separate flat binary files, one for the boot sector and another for the kernel. BIOS will load your boot sector, then your boot sector will load your kernel.
Then you can put the relevant
org
statement in the two source files and your problem should hopefully be solved.虽然
org
在 NASM bin 格式源中只允许一次,但您可以使用其多部分支持来创建我认为与具有两个org
的 FASM 输出完全相同的内容指令。请参阅 https://www.nasm。 us/xdoc/2.14.02/html/nasmdoc7.html#section-7.1.3这是具有第二阶段的引导加载程序的样子:
While
org
is only allowed once in NASM bin format source, you can use its multi-section support to create what I believe is the exact same as your FASM output with twoorg
directives. Refer to https://www.nasm.us/xdoc/2.14.02/html/nasmdoc7.html#section-7.1.3This is what a bootloader with such a second stage would look like: