为什么我的引导加载程序的堆栈段位于 0x3FF(实模式 IVT 结束)?

发布于 2024-08-31 06:00:31 字数 394 浏览 6 评论 0原文

“地址 0x500 是 BIOS 使用的最后一个地址”是 维基百科 的内容 -

“00000000-000003FF实模式IVT(中断向量表)”是osdev.org 关于 BIOS 内存映射的文章说道。

那么你能告诉我为什么 NASM 将我的 .com 文件的堆栈指针放置到 0x3FF 而我的指令指针从 0x7C00 开始吗​​?对我来说,SP 最直观的位置就是 0x7C00 的正下方。

"address 0x500 is the last one used by the BIOS" is what Wikipedia
-

"00000000-000003FF Real Mode IVT (Interrupt Vector Table)" is what osdev.org's article over the BIOS memory map says.

So can you tell me why NASM places my .com file's stack pointer to 0x3FF while my instruction pointer starts at 0x7C00? To me the most intuitive place for SP would be right below 0x7C00.

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

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

发布评论

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

评论(4

不忘初心 2024-09-07 06:00:31

简单的答案是引导扇区有一个 BIOS 使用的有效(可能很小)堆栈。

令人高兴的答案是 3FFh 对于某些 BIOS 来说是有效的堆栈位置。事实上,在本例中它是 IVT 的下部,因为 BIOS 不使用这些中断向量。这不是什么秘密。

BIOS 堆栈区
BIOS 使用 30:0000h - 30:00FFh(中断向量表顶部)作为堆栈区域。该区域用于BIOS计算和临时存储。 AMIBIOS 不支持的 INT C0h 到 FFh 地址通常会占用此空间。

AMIBIOS 1993 程序员指南,第 181 页

The simple answer is that the boot sector has a valid (and probably small) stack which was used by the BIOS.

The happy answer is that 3FFh is a valid stack location for some BIOS. The fact that in this case it is the lower part of the IVT is because those interrupt vectors are not used by the BIOS. It is no secret.

BIOS Stack Area
The BIOS uses 30:0000h - 30:00FFh (on top of the interrupt vector table) as a stack area. This area is used for BIOS calculations and temporary storage. The addresses for INTs C0h through FFh, not supported in the AMIBIOS, would ordinarily occupy this space.

Programmer's Guide to the AMIBIOS 1993, page 181

寻找我们的幸福 2024-09-07 06:00:31

我不认为 BIOS 会为您维护有效的堆栈。因此,您应该在您拥有的任何可用内存中自行设置一个堆栈。我在引导加载程序中的一般启动顺序如下:

[BITS 16]
[ORG 0x7C00]
xor ax,ax ;AX=0
mov ds,ax
mov es,ax ;can be omitted
mov ss,ax
mov sp,0x7000 ;or replace with some other nice valid piece of memory
jmp word 0:begin ;BIOSes are sometimes buggy and load you initially with CS=7C0
begin:
;....

NASM 不会执行您告诉它的任何操作。这就是使用汇编的要点。每行汇编代码都有 1:1 比例的计算机执行的操作码。因此,如果 BIOS 没有为您设置堆栈,并且您的汇编代码中没有任何位置设置堆栈,那么堆栈将处于某种无效状态。 Nasm 不会插入魔术代码来设置堆栈。

I don't believe BIOSes are expected to maintain a valid stack for you. So you should setup a stack yourself in whatever free memory you have. My general startup sequence in bootloaders is as so:

[BITS 16]
[ORG 0x7C00]
xor ax,ax ;AX=0
mov ds,ax
mov es,ax ;can be omitted
mov ss,ax
mov sp,0x7000 ;or replace with some other nice valid piece of memory
jmp word 0:begin ;BIOSes are sometimes buggy and load you initially with CS=7C0
begin:
;....

NASM Does not do anything than what you tell it. This is the point of using assembly. Every line of assembly code has a 1:1 ratio of opcodes executed by the computer. So, if the BIOS does not setup a stack for you, and no where in your assembly code do you setup a stack, then the stack will be in some invalid state. Nasm will not insert magic code to setup a stack.

影子的影子 2024-09-07 06:00:31

我不熟悉 NASM,但我很确定它将您的堆栈段寄存器 (SS) 设置为 0 以外的值,因此 SS:SP 指向完全不同的地方。

编辑:等等,您的指针是否设置为03FF?

I'm not familiar with NASM, but I'm pretty sure it sets your Stack Segment register (SS) to something else than 0, so SS:SP points somewhere entirely different.

Edit: Wait, is your segment or pointer set to 03FF?

故事↓在人 2024-09-07 06:00:31

改变寄存器内容的不是汇编器(通过可执行文件中的任何隐藏命令,在本例中是引导程序)。

根据Intel手册,导致SP包含如此奇数值的原因都不是CPU。
http://www.intel.com/design/pentiumii/manuals/243192。 htm

因为不存在运行代码的现有操作系统,导致 SP(和其他寄存器)状态的唯一选择是 BIOS。不幸的是,BIOS 是闭源的“商业秘密”,因此“为什么”的问题将得到解答。 http://www.coreboot.org/Welcome_to_coreboot 上的 Coreboot 可能会给出一些提示,说明为什么事情是这样的确实如此,但 Coreboot 似乎有时会做一些与传统 BIOS 非常不同的事情......

It's not the assembler that alters the contents of register(s) (by any hidden commands in the executable file, in this case the bootleader).

According to Intel manual it's neither the CPU that causes SP to contain such odd value.
http://www.intel.com/design/pentiumii/manuals/243192.htm

Because there's no existing OS in which the code is run the only option left to cause the state of SP (and other registers) is BIOS. Unfortunately BIOSes are closed source "trade secrets" and thus the question "why" will be left answered. Coreboot at http://www.coreboot.org/Welcome_to_coreboot might give some hints to why things are like they are but Coreboot seems to do things sometimes very different from traditional BIOSes...

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