我正在用汇编编写引导加载程序,它似乎在 qemu、bochs 和 virtualbox 上运行良好。但是,它并没有在真实硬件上加载内核(似乎)。
引导加载程序首先将一个字符写入视频内存(用于调试),然后从驱动器读取扇区 2 并远跳转到内核。然后内核将一些字符写入视频内存。
在真机上,我在屏幕上看到引导加载程序中的字符,它卡在那里(闪烁的插入符号)。
我已经尝试将 DS、ES、SI 设置为零,并且我也在设置堆栈段。
我正在使用 bios int 13 function 2 从驱动器读取扇区 2。我有点怀疑它与驱动器编号有关。我都尝试使用在启动时(在 dl 中)传递给引导加载程序的驱动器号,并将其手动设置为 0x0、0x80 和 0x81。
我注意到的一件奇怪的事情是,我用来接近跳转的标签神奇地获得了正确的地址。使用 objdump 我看到例如:jmp 0x2,而使用 gdb 和 qemu,它说:jmp 0x7c02。 CS 和所有其他段寄存器为零。无论我在链接中使用 -Ttext 0x0 还是 -Ttext 0x7c00,引导加载程序在所有模拟器上都能正常工作。当我与 -Ttext 0x7c00 链接时,objdump 说 jmp 0x7c02。
编辑,引导加载程序如下所示:
.code16
.text
movw $0xb800, %ax
movw %ax, %ds
movw $0x0741, (0x0)
xorw %ax, %ax
movw %ax, %ds
movw %ax, %si
movw %ax, %es
movw $0x8000, %ax
movw %ax, %ss
movw $0, %sp
movb $2, %ah
movb $1, %al
movw $0x02, %cx
movb $0x00, %dh
movw $0x5000, %bx
movw %bx, %es
movw $0x0, %bx
int $0x13
ljmpw $0x5000, $0x0000
编辑,第二阶段:
.code16
.text
movw $0xb800, %ax
movw %ax, %ds
movw $0x0742, (0x2)
forever:
jmp forever
最佳答案
如果您的硬件对 USB 驱动器使用软盘模拟,则可能没有正确的 BIOS Parameter Block (BPB)在您的 MBR 中,它无法正常启动。许多 BIOS 将尝试在引导加载程序开始时检测 BPB,甚至可能在引导加载程序加载到内存后使用正确的驱动器几何结构更新值。可能您的引导加载程序未被检测为正确的可引导驱动器,或者 BIOS 在执行之前用驱动器几何信息覆盖了您的某些代码。
下面添加了一个看起来像 2.88MB 软盘的 BPB。
.global _start
.code16
.text
_start:
jmp main
.space 3 - (.-_start)
/* Configuration for a 2.88MB floppy using FAT 12 */
OEMname: .ascii "MYBOOT "
bytesPerSector: .word 512
sectPerCluster: .byte 1
reservedSectors: .word 1
numFAT: .byte 2
numRootDirEntries: .word 240
numSectors: .word 5760
mediaType: .byte 0xf0
numFATsectors: .word 9
sectorsPerTrack: .word 36
numHeads: .word 2
numHiddenSectors: .long 0
numSectorsHuge: .long 0
driveNum: .byte 0
reserved: .byte 0x00
signature: .byte 0x29
volumeID: .long 0x54428E71
volumeLabel: .ascii "NO NAME "
fileSysType: .ascii "FAT12 "
main:
movw $0xb800, %ax
movw %ax, %ds
movw $0x0741, (0x0)
xorw %ax, %ax
movw %ax, %ds
movw %ax, %si
movw %ax, %es
movw $0x8000, %ax
movw %ax, %ss
movw $0, %sp
movb $2, %ah
movb $1, %al
movw $0x02, %cx
movb $0x00, %dh
movw $0x5000, %bx
movw %bx, %es
movw $0x0, %bx
int $0x13
ljmpw $0x5000, $0x0000
.space 510-(.-_start)
.word 0xaa55
关于assembly - 引导加载程序可在模拟器中运行,但不能在真实硬件中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911606/