引导加载程序未加载第二个扇区

Sta*_* OS 1 assembly bios vga bootloader x86-16

我用汇编写了一个引导加载程序。下面是它的工作原理:

  1. 首先,BIOS 正常加载引导加载程序。指示进入200h。

  2. 在 200h 处,有一些代码位于 200h 和 21Eh 之间。它只是切换到 VGA 模式,使用 VGA 功能在坐标 1,1 上绘制一个洋红色像素。它永远循环该代码。

然而,当我加载它时,它只是在闪烁的光标上移动,这是普通的 VGA .bin 文件不会做的,并且会显示一个像素。我检查了一个像素,但什么也没看到。我看到的意思是 VGA 代码没有运行,引导加载程序只是加载了,没有别的。

引导加载程序代码:

 cli
 startload:
 push 0x00000200
 ret

 times 510-($-$$) db 0
 db 0x55
 db 0xAA
Run Code Online (Sandbox Code Playgroud)

您可以看到它只是转到下一个扇区(从 200h 开始)和 200h-21Eh 处的代码:

BITS 16
org 200h
data:
xcoord DB '1'
ycoord DB '1'
color DB 'D'
.code:
vga:
mov ax, 13h
int 10h
mov ax, ycoord
mov bx, xcoord
mov cx, 320
mul cx
add ax, bx
mov di, ax
mov dl, color
mov [es:di],dl
jmp vga
Run Code Online (Sandbox Code Playgroud)

(是的,我知道这不是 230h 字节,这是编译输出的大小,即 230h。)

问题是什么?注意:这里不是在讨论如何让它进入第二个扇区。它在问为什么它不去那里。我还没有找到任何解决方案。

Mic*_*tch 5

回答您的问题之一。这段代码:

startload:
push 0x00000200
ret
Run Code Online (Sandbox Code Playgroud)

几乎相当于几乎绝对跳转到 CS:0x200。我们不知道CS 中的值是什么,但许多 BIOS 会以 CS=0 和 IP=0x7c00 开头(但情况并非总是如此)。除非你自己设置,否则你不能真正依赖CS是一个特定的值。在大多数情况下,CS可能为零,这意味着您可能会跳转到物理内存地址 0x00200 (0x0000:0x0200)。这恰好位于从物理地址 0x00000 到 0x003FF 运行的实模式中断表的中间。跳转到该位置可能会导致某种未定义的行为。

您可以将引导加载程序加载到 BOCHS 中,BOCHS 具有理解 16 位实模式的合理调试器。您将能够逐步执行代码并准确确定CS是什么以及它跳转到的位置。


您可能想要完成的工作可以使用以下代码完成。这是我之前对不同问题的Stackoverflow 回答的简单更改。要了解此代码的作用,请参阅我之前的回答。

简而言之,BIOS 从物理内存地址 0x7C00 开始从磁盘读取单个磁盘扇区(512 字节)。如果要加载其他扇区,则必须编写将它们加载到内存中的代码,然后在加载后跳转到该代码。

在此示例中,第一阶段是引导加载程序,它从磁盘的扇区 2(引导扇区之后的扇区)加载单个扇区的第二阶段。在这个例子中,我将通过segment:offset pair 0x7e0:0x0000在物理地址 0x07e00 的第一个扇区之后立即加载第二个扇区。(0x07e0<<4)+0x0000 = 0x07e00。

引导加载文件

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
Run Code Online (Sandbox Code Playgroud)

第二阶段将在内存中的引导加载程序之后由INT 13h/AH=2h加载,从物理地址 0x07e00 开始。

阶段2.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
Run Code Online (Sandbox Code Playgroud)

上面的代码是你的 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
Run Code Online (Sandbox Code Playgroud)

这将构建bootload.bin并将其放置到磁盘映像的第一个扇区中,然后将stage2.bin其构建并放置在磁盘映像的扇区 2 中。磁盘映像称为disk.img.

您应该能够使用QEMU进行测试,例如:

qemu-system-i386 -fda disk.img
Run Code Online (Sandbox Code Playgroud)

有关DD使用的更多信息可以在我的其他Stackoverflow 答案之一中找到。巧合的是,这个答案是针对您去年提出的一个问题。

  • @PeterCordes:我会在 MS-DOS 上使用 `copy /b` 而不是 `type`,但随后担心的是,已知某些虚拟环境会拒绝使用不是众所周知的软盘大小的大小。由于 OP 没有提到环境,所以我在安全方面进行了播放。通常,当我给出答案时,我会包含 DD 命令以生成众所周知的软盘格式,例如 720k(或 1.44MB、360K、1.2M)的原因。 (2认同)
  • @PeterCordes 您想在 MS-DOS(或 Windows 命令行)上使用 `copy /b a.bin+b.bin disk.img`,因为 `type` 会将文件视为文本文件并在之后停止阅读第一个 CTRL-Z 字符。 (2认同)