MikeOS 引导加载程序中的堆栈段

发布于 2024-09-09 00:26:03 字数 490 浏览 3 评论 0原文

我不明白这段代码:

mov ax, 07C0h   ; Set up 4K of stack space above buffer
add ax, 544     ; 8k buffer = 512 paragraphs + 32 paragraphs (loader)
cli             ; Disable interrupts while changing stack
mov ss, ax
mov sp, 4096
sti             ; Restore interrupts
  • mov ax, 07C0h - 这里 BIOS 加载我们的 代码。但什么是“4K”?千字节?我 没明白:)
  • 添加 ax, 544 - 为什么又是“8K”?为什么我们要加上 544?为什么不是512?
  • mov sp, 4096 - 在这里我们设置堆栈指针。

在设置堆栈指针之前,我们为什么要做所有这些操作呢?

I don't understand this piece of code:

mov ax, 07C0h   ; Set up 4K of stack space above buffer
add ax, 544     ; 8k buffer = 512 paragraphs + 32 paragraphs (loader)
cli             ; Disable interrupts while changing stack
mov ss, ax
mov sp, 4096
sti             ; Restore interrupts
  • mov ax, 07C0h - here BIOS loads our
    code. But what is '4K'? Kilobytes? I
    didn't get it :)
  • add ax, 544 - Why again '8K'? And why we add 544? Why not 512?
  • mov sp, 4096 - Here we set stack pointer.

What for do we do all these manipulations, before we set stack pointer?

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

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2024-09-16 00:26:04

我认为最后一行的评论总结了这一点:

buffer:             ; Disk buffer begins (8k after this, stack starts)

内存布局看起来像这样:

+-------------------+ <-- 07C0:0000, where the BIOS loads the boot sector
| 512 bytes of code |
+-------------------+
| 8KB set aside for |
|   a disk buffer   |
+-------------------+ <-- SS:0000
|   4KB of stack    |
+-------------------+ <-- SS:1000 = SS:SP

关于段落的评论有点迟钝;我发现用字节来思考更容易,16 个字节组成一个段落。

这些幻数的原因:

  • 从段 07C0 开始,BIOS 在此加载代码
  • 跳过 512 字节,以说明代码本身(512 字节 = 32 段)
  • 跳过 8KB,为磁盘缓冲区留出空间(8,192字节 = 512 段)
  • 将 SS 放在 4KB 块的开头。 512+8192 = 8,704 字节 = 544 个段落
  • 将 SP 放在该块的末尾。放在最后是因为栈需要在内存中向上增长。

请注意,数字 4096 = 4KB 在代码中正常显示,因为 SP 寄存器需要一个以字节为单位的值。所有其他值都在段落中,因为它们与 SS(段寄存器)相关。

I think the comment on the last line sums it up:

buffer:             ; Disk buffer begins (8k after this, stack starts)

The memory layout looks like this:

+-------------------+ <-- 07C0:0000, where the BIOS loads the boot sector
| 512 bytes of code |
+-------------------+
| 8KB set aside for |
|   a disk buffer   |
+-------------------+ <-- SS:0000
|   4KB of stack    |
+-------------------+ <-- SS:1000 = SS:SP

The comment about paragraphs is slightly obtuse; I find it easier to think in bytes, where 16 bytes makes one paragraph.

The reason for these magic numbers:

  • Start at segment 07C0, where the BIOS loads the code
  • Skip past 512 bytes, to account for the code itself (512 bytes = 32 paragraphs)
  • Skip past 8KB, to set aside space for the disk buffer (8,192 bytes = 512 paragraphs)
  • Put SS at the start of a 4KB block. 512+8192 = 8,704 bytes = 544 paragraphs
  • Put SP at the end of that block. Put it at the end because the stack needs to grow upwards in memory.

Note that the number 4096 = 4KB appears as normal in the code, because the SP register needs a value in bytes. All the other values are in paragraphs because they relate to SS, which is a segment register.

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