我正在写一个两阶段的bootloader这是我的boot.asm
[org 0x7c00]
[bits 16]
xor ax, ax
mov ds, ax
mov es, ax
xor bx, bx
mov ah, 0x0E
mov al, 'A'
int 0x10
jmp 0x8000
cli
hlt
times 510 - ($-$$) db 0
dw 0xAA55
和boot2.asm
[org 0x8000]
[bits 16]
xor ax, ax
mov ds, ax
mov es, ax
xor bx, bx
mov ah, 0x0E
mov al, 'B'
int 0x10
我用它编译它
nasm -f bin -o boot.bin boot.asm
nasm -f bin -o boot2.bin boot2.asm
它编译时没有任何错误或警告。但是,我如何将第2阶段放在0x8000并将stage1和stage2连接在一起?
你可能会问如何将第一和第二阶段合并为一个文件。如果是这样: 在Linux上:
cat boot.bin boot2.bin > final_file.file_format
在Windows上:
copy /b boot.bin+boot2.bin final_file.file_format
要从引导加载程序加载第二个阶段,您可以使用以下代码:
mov ah, 0x02 ; Read disk BIOS call
mov cl, 0x02 ; sector to start reading from
mov al, 1 ; number of sectors that will be read (modify if your second stage grows)
mov ch, 0x00 ; cylinder number
mov dh, 0x00 ; head number
xor bx, bx
mov es, bx ; ES=0x0000
mov bx, 0x8000 ; ES:BX(0x0000:0x8000) forms complete address to read sectors to
; DL should contain the boot disk number passed to the bootloader by the BIOS
int 0x13 ; Make BIOS disk services call (Int 0x13/AH=2) to read sectors
; For simplicity assume the disk read was successful
jmp 0x0000:0x8000 ; FAR JMP to second stage and ensure CS=0x0000
; since CS is not guaranteed to be 0x0000 when control is transferred
; to our bootloader
但是我如何将第2阶段放在0x8000 ......
不幸的是,我不使用“masm”而是使用其他装配工。但我希望你必须将[org 0x7e00]
改为[org 0x8000]
。
...并链接stage1和stage2一起工作?
这并不像你想象的那么容易:
BIOS将把一个扇区(510字节加上2个字节0xAA55
)加载到0x7C00的内存中。使用普通BIOS,无法加载更多数据!
这些510字节(“阶段1”)中的代码必须将“状态2”加载到内存中:它可以使用ah=2
的ah=0x42
或int 0x13
函数来执行此操作。
如果你有自己的软盘格式,这很简单:
将“stage 2”存储在软盘的第二个扇区中并加载第二个扇区。
如果要从文件系统加载“第2阶段”(例如,从FAT格式的磁盘中加载文件),这将更加棘手。