回答您的一个问题。这段代码:
startload:
push 0x00000200
ret
几乎相当于几乎绝对跳转到 CS:0x200。我们不知道 CS 中的值是什么,但许多 BIOS 将以 CS=0 和 IP=0x7c00 开头(但并非总是如此)。你不能真正依赖 CS 是一个特定的值,除非你自己设置它。在大多数情况下,CS 可能为零,这意味着您可能会跳转到物理内存地址 0x00200 (0x0000:0x0200)。这恰好位于从物理地址 0x00000 到 0x003FF 的实模式中断表的中间。跳转到该位置可能会导致某种未定义的行为。
您可以将引导加载程序加载到 BOCHS 中,该 BOCHS 具有理解 16 位实模式的合理调试器。您将能够单步执行代码并准确确定 CS 是什么以及它跳转到的位置。
您可能想要完成的工作可以使用以下代码来完成。
这是我之前的Stackoverflow answer 对另一个问题的简单更改。要了解这段代码在做什么,请参阅我之前的回答。
简而言之,BIOS 从物理内存地址 0x7C00 开始的磁盘读取单个磁盘扇区(512 字节)。如果您想加载其他扇区,您必须编写将它们加载到内存中的代码,然后在加载后跳转到该代码。
在此示例中,第一阶段是引导加载程序,第二阶段从磁盘的第 2 扇区(引导扇区之后的扇区)加载单个扇区。在此示例中,我将通过 segment:offset pair 0x7e0:0x0000 在物理地址 0x07e00 处的第一个扇区之后加载第二个扇区。 (0x07e0
bootload.asm
bits 16
ORG 0x7c00 ; Bootloader starts at physical address 0x07c00
; At start bootloader sets DL to boot drive
; Since we specified an ORG(offset) of 0x7c00 we should make sure that
; Data Segment (DS) is set accordingly. The DS:Offset that would work
; in this case is DS=0 . That would map to segment:offset 0x0000:0x7c00
; which is physical memory address (0x0000<<4)+0x7c00 . We can't rely on
; DS being set to what we expect upon jumping to our code so we set it
; explicitly
xor ax, ax
mov ds, ax ; DS=0
cli ; Turn off interrupts for SS:SP update
; to avoid a problem with buggy 8088 CPUs
mov ss, ax ; SS = 0x0000
mov sp, 0x7c00 ; SP = 0x7c00
; We'll set the stack starting just below
; where the bootloader is at 0x0:0x7c00. The
; stack can be placed anywhere in usable and
; unused RAM.
sti ; Turn interrupts back on
reset: ; Resets floppy drive
xor ax,ax ; AH = 0 = Reset floppy disk
int 0x13
jc reset ; If carry flag was set, try again
mov ax,0x07e0 ; When we read the sector, we are going to read to
; address 0x07e0:0x0000 (phys address 0x07e00)
; right after the bootloader in memory
mov es,ax ; Set ES with 0x07e0
xor bx,bx ; Offset to read sector to
floppy:
mov ah,0x2 ; 2 = Read floppy
mov al,0x1 ; Reading one sector
mov ch,0x0 ; Track(Cylinder) 1
mov cl,0x2 ; Sector 2
mov dh,0x0 ; Head 1
int 0x13
jc floppy ; If carry flag was set, try again
jmp 0x07e0:0x0000 ; Jump to 0x7e0:0x0000 setting CS to 0x07e0
; IP to 0 which is code in second stage
; (0x07e0<<4)+0x0000 = 0x07e00 physical address
times 510 - ($ - $$) db 0 ; Fill the rest of sector with 0
dw 0xAA55 ; This is the boot signature
第二阶段将由INT 13h/AH=2h在内存中的引导加载程序之后立即加载,从物理地址0x07e00开始。
stage2.asm
BITS 16
; ORG needs to be set to the offset of the far jump used to
; reach us. Jump was 0x7e0:0x0000 so ORG = Offset = 0x0000.
ORG 0x0000
main:
; Set DS = CS
mov ax, cs
mov ds, ax
; Set to graphics mode 0x13 (320x200x256)
mov ax, 13h
int 10h
; Set ES to the VGA video segment at 0xA000
mov ax, 0xa000
mov es, ax
vga:
; Draw pixel in middle of screen
mov ax, [ycoord]
mov bx, [xcoord]
mov cx, 320
mul cx
add ax, bx
mov di, ax
mov dl, [color]
mov [es:di],dl
; Put processor in an endless loop
cli
.endloop:
hlt
jmp .endloop
; Put Data after the code
xcoord DW 160
ycoord DW 100
color DB 0x0D ; One of the magenta shades in VGA palette
上面的代码是您的 VGA 代码的略微修改版本,因为您的代码存在错误。您的 VGA 代码没有正确地将 ES 段设置为 0xA000,这是 VGA 图形内存的开始;它没有正确地取消引用变量(您使用了它们的地址而不是值);坐标的大小是 BYTE 而不是 WORD;在具有 ASCII 字符值的变量中定义值。我还在代码之后移动了数据。
我修改了代码,在 320x200 显示器的中间画了一个像素,然后无限循环结束程序。
在 Linux 上(或在带有 Chrysocome DD 的 Windows 上)使用 NASM 进行组装,您可以使用以下命令生成 720K 的引导盘:
dd if=/dev/zero of=disk.img bs=1024 count=720
nasm -f bin bootload.asm -o bootload.bin
dd if=bootload.bin of=disk.img conv=notrunc
nasm -f bin stage2.asm -o stage2.bin
dd if=stage2.bin of=disk.img bs=512 seek=1 conv=notrunc
这会构建bootload.bin 并将其放入磁盘映像的第一个扇区,然后构建stage2.bin 并将其放入磁盘映像的第2 扇区。磁盘映像名为disk.img。
你应该可以用 QEMU 来测试它,比如:
qemu-system-i386 -fda disk.img
有关DD 用法的更多信息可以在我的另一个Stackoverflow answers 中找到。巧合的是,这个答案是针对您去年提出的一个问题。