你好世界引导加载程序不工作

2023-12-24

我一直在学习教程在这个网页上 http://viralpatel.net/taj/tutorial/hello_world_bootloader.php这将逐步创建一个显示 Hello World 的引导加载程序。

第二个教程(我们尝试输出“A”)完美地工作,但第一个教程对我来说根本不起作用! (BIOS 完全忽略软盘并直接引导至 Windows)。尽管任何解释都将不胜感激,但这不是什么问题。

真正的问题是我无法让第三个教程工作。相反,在输出“Hello World”时,我在屏幕的左下角看到了一个不寻常的字符(和闪烁的光标)。它看起来有点像圆角矩形内的笑脸。有谁知道如何让 Hello World 按预期显示?


你说“直接启动进入Windows”,所以我假设你使用的是物理PC。未来需要注意的是:始终使用模拟器进行开发!这更容易。我喜欢 Bochs for OSDaving 因为它有很好的调试功能。现在,讨论可能的解决方案。

有很多有缺陷的 BIOS 违反了 IBM PC 对 0x7C00 加载地址的非正式规范。

当你进行汇编时,这可能会带来很多内存地址等问题。所以让开头看起来像这样:

[BITS 16] ;tell the assembler that its a 16 bit code
[ORG 0x7C00] ;this tells the assembler where the code will be loaded at when it runs on your machine. It uses this to compute the absolute addresses of labels and such.

jmp word 0:flush ;#FAR jump so that you set CS to 0. (the first argument is what segment to jump to. The argument(after the `:`) is what offset to jump to)
;# Without the far jmp, CS could be `0x7C0` or something similar, which will means that where the assembler thinks the code is loaded and where your computer loaded the code is different. Which in turn messes up the absolute addresses of labels.
flush: ;#We go to here, but we do it ABSOLUTE. So with this, we can reset the segment and offset of where our code is loaded.
mov BP,0 ;#use BP as a temp register
mov DS,BP ;#can not assign segment registers a literal number. You have to assign to a register first.
mov ES,BP ;#do the same here too
;#without setting DS and ES, they could have been loaded with the old 0x7C0, which would mess up absolute address calculations for data. 

看,一些负载0x07C0:0000和最大负载(并且其被认为是适当的)在0x0000:7C00。这是相同的平面地址,但不同的段设置确实会搞砸绝对内存地址。因此,让我们消除汇编器的“魔力”,看看它是什么样子(注意我不保证地址完全正确。我不知道所有操作码的大小)

jmp word 0:0x7C04 ;# 0x7C04 is the address of the `flush` label 
...

所以,我们跳转到一个绝对地址。

接着。如果我们不这样做会发生什么?

以这个程序为例:

mov ax,[mydata]
hlt

mydata: dw 500 ;#just some data

这会分解成类似的东西

mov ax,[0x7C06] 

哦,它使用绝对寻址,那怎么会出错呢?好吧,如果 DS 实际上是怎么办?0x7C0?然后而不是得到预期的汇编程序0:0x7C06它会得到0x7C0:0x7C06哪个是not同一个单位地址。

我希望这有助于您理解。但这确实是一个复杂的主题,需要一段时间的低级编程才能完全理解。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

你好世界引导加载程序不工作 的相关文章

随机推荐