(NASM)(80x86)Bootloader NEEDS xor ax,ax [英] (NASM) (80x86) Bootloader NEEDS xor ax, ax
问题描述
我正在学习如何从osdev中制作一个引导程序.我正在使用NASM汇编我的代码,并使用一台x86机器来运行我的引导程序.这是一小段代码,它打印一个字符并进入无限循环:
I am learning how to make a bootloader from osdev. I'm using NASM to assemble my code, and a x86 machine to run my bootloader. This is a little piece of code which prints a character and enter in a infinite loop:
BITS 16
xor ax, ax
mov ah, 0x0E
mov al, 0x41
int 0x10
jmp $
times 510-($-$$) db 0x00
db 0x55
db 0xAA
我的问题是:为什么注释"xor ax,ax"指令时代码不运行?如您在上面的代码中看到的那样,更改了ax值以存储中断参数,因此该代码应在没有xor指令的情况下运行...
My question is: why doesn't the code run when I comment the 'xor ax, ax' instruction? As you can see in the code above, the ax value is changed to store the interrupt parameters, so the code should run without the xor instruction...
其他注意事项:
-
我使用以下命令在Xubuntu下汇编代码: nasm -f bin -o main.bin main.asm
I'm assembly the code under Xubuntu with this command: nasm -f bin -o main.bin main.asm
我使用以下命令将512字节的机器代码存储到笔式驱动器上: 须藤dd if =/dev/sdb的main.bin
I'm storing the 512-bytes machine code onto a pen drive with this command: sudo dd if=main.bin of=/dev/sdb
我的计算机能够从笔式驱动器启动
My computer is able to start from a pen drive
非常感谢您.
推荐答案
在理论中,您不需要BPB ,而不是 VBR 1 ,并且xor ax, ax
指令的存在不会影响引导.
不过,您应该添加xor bh, bh
(有关更多信息,请参见整数10/AH = 0Eh )
In theory you wouldn't need a BPB when writing a MBR and not a VBR1, and the presence of the xor ax, ax
instruction wouldn't influence the booting.
You should include a xor bh, bh
however (more on Int 10/AH=0Eh)
可悲的是,这只是理论.
特别是对于USB设备,某些固件隐式地假定BPB,包括完整的FDC描述符(具有有效的OS名称).
非常感谢 Michael Petch 强调这一点.
Specially for USB devices a BPB is implicitly assumed by some firmware, including the full FDC descriptor (with a valid OS name).
Many thanks to Michael Petch for stressing this out.
由于引入了 UEFI 实现,尤其是涉及
Since the introduction of UEFI implementations, particularly the parts dealing with CSM (Compatibility Support Module), i.e. legacy booting, writing a fully supported MBR has became tricky.
固件有时会尝试自动检测要使用的引导模式,并且由于所有UEFI设备也是每个规范的旧设备,因此固件必须依靠一些怪癖来区分它们.
The firmware will sometimes try to automatically detect what boot mode to use and since all UEFI devices are also legacy devices per specification, the firmware must rely on some quirk to tell them apart.
即使明确这样告诉我,我的固件也将设备检测为旧版",仅当其中至少一项为真时,才仅:
My firmware detect a device as "legacy", even when explicitly told so, only when at least one of these is true:
- MBR分区表中有一个可引导的非空分区.
完全不检查CHS或LBA中的起始/结束地址. - 第一条指令是
xor ax, ax
(以两种形式:33 C0
或31 C0
). 这是因为大多数引导加载程序所做的第一件事是通过 AX 将段寄存器设置为零.
- There is a bootable, non empty, partition in the MBR partition table.
The starting/ending address, either in CHS or LBA, are not checked at all. - The first instruction is a
xor ax, ax
(in either forms:33 C0
or31 C0
). This is because the first thing most bootloaders do is to set the segment registers to zero through AX.
可能还有其他签名",例如前几个字节的跳转,但是我还没有测试它们.
There may be other "signatures", like a jump at the first bytes, but I haven't tested them (yet).
如果固件未能将设备检测为旧版设备,并且它不是符合UEFI的设备,则将被跳过.
If the firmware fails to detect the device as legacy and it is not a UEFI compliant device, it will be skipped.
您可以使用xor ax, ax
(在这种情况下,建议您使用db 33h, 0c0h
和文档注释),或添加一个虚拟分区条目,如下所示.
You can use the xor ax, ax
(in which case I suggest using of db 33h, 0c0h
and a comment for documentation) or by adding a dummy partition entry, as shown below.
BITS 16
ORG 7c00h ;Soon or later you'll need this
xor bh, bh
mov ah, 0x0E
mov al, 0x41
int 0x10
_loop:
hlt ;Be eco-friendly
jmp _loop
;Pad to the first PTE (Partition Table Entry), it is at 1beh
TIMES 01beh-($-$$) db 00h
dd 80h ;Bootable partition at CHS 0:0:0 (Which is illegal but not checked)
db 01h ;Non empty partition (Type 1 is MS-DOS 2.0 FAT)
;Pad to the end of the sector minus 2
TIMES 510-($-$$) db 00h
dw 0aa55h ;Signature
1 根据dd
命令的参数.
1 According to the parameters of the dd
command.
这篇关于(NASM)(80x86)Bootloader NEEDS xor ax,ax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!