组装 x86 引导加载程序 [英] Assembly x86 Bootloader

查看:23
本文介绍了组装 x86 引导加载程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我找到了以下教程来构建一个简单的引导加载程序:http://mikeos.sourceforge.net/write-your-own-os.html#firstos

So I found the following tutorial for building a simple bootloader: http://mikeos.sourceforge.net/write-your-own-os.html#firstos

这是他示例的开头以及我无法理解的部分:

Here is the start of his example and the area that I'm having trouble understanding:

start:
    mov ax, 07C0h       ; Set up 4K stack space after this bootloader
    add ax, 288         ; (4096 + 512) / 16 bytes per paragraph
    mov ss, ax
    mov sp, 4096

    mov ax, 07C0h       ; Set data segment to where we're loaded
    mov ds, ax

我知道这里的 Mike 正在尝试为他的引导加载程序构建一个堆栈.程序的开始位于内存中的 07C0h.我不明白的是以下行 'add ax, 288 ;(4096 + 512)/每段 16 字节'.为什么他要取堆栈和引导扇区的总量,然后将其除以 16 位寄存器作为堆栈段的开始?堆栈段不应该在引导扇区之后的 20h 开始吗?最后,堆栈指针不应该设置在 (512 + 4096) 的末尾吗?谢谢

I understand that Mike here is trying to build a stack for his bootloader. The start of the program is at 07C0h in memory. What I don't understand is the following line 'add ax, 288 ; (4096 + 512) / 16 bytes per paragraph'. Why is he taking the total amount of the stack and boot sector, then dividing it by the 16-bit registers for the start of the stack segment? Shouldn't the stack segment start at 20h, right after the bootsector? Lastly, shouldn't the stack pointer then be set at the end of (512 + 4096)? Thanks

推荐答案

你说得对.使用当前的实现,代码会留下 4k 的差距.解决这个问题的方法就是你说的,就是将 ss 设置为 bootsector/16 的结尾,并用 4k(不是 4k + 512)加载 sp.因此,正确的实现应该是.

You are right. With the current implementation, the code will leave a gap of 4k. The way to resolve this is what you said, which is to set ss to end of bootsector / 16 and to load sp with 4k (not 4k + 512). Hence the correct implementation would be.

mov ax, 07c0h
add ax, 32     ; (512 / 16) i.e end of bootsector
mov ss, ax
mov sp, 4096

除此之外,我还想提一下,这种类型的引导加载程序将不再在真机上运行,​​您应该专注于 UEFI 等标准来构建引导加载程序.它还负责其他任务,例如切换 CPU 模式.

Aside, from the question, i would also like to mention that this type of bootloaders won't work on real machines anymore and you should focus on standards like UEFI to build a bootloader. It also takes care of other tasks such as switching the cpu modes.

如果你想看到一个基于 UEFI 的引导加载程序和一个简约的内核,请参考我的 github repo https://github.com/RohitRTdev/RTOS

If you wish to see a working UEFI based bootloader with a minimalistic kernel refer to my github repo https://github.com/RohitRTdev/RTOS

这篇关于组装 x86 引导加载程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆