8

我有一个简单的程序。它必须从硬盘驱动器(不是 mbr)读取第一个扇区,并将其写入 0 扇区(mbr)。但它不起作用。我认为它与错误的 DAP 有关。谢谢。

    [bits   16]
    [org    0x7c00]

;clear screen
start:
    mov     ax, 0x3
    int     0x10

;reset the hard drive
    xor     ah, ah
    mov     dl, 0x80
    int     0x13
    jnz     error

;read the second sector
    mov     si, DAP
    mov     ah, 0x42
    int     0x13

    mov     si, data
    call    print_string
    jmp     $

DAP:
    db      0x10    ;size of DAP
    db      0x0     ;zero
    db      0x1     ;number of sectors to read
    db      0x0     ;zero
;point to memory
    dw      0x0     ;offset
    dw      0x0     ;segment
    dq      0x1     ;disk address

DAP2:
    db      0x10
    db      0x0
    db      0x1
    db      0x0
    dw      0x0
    dw      0x0
    dd      0x0
    dd      0x0            

print_string:
    mov     ax, 0xb800
    mov     es, ax
    xor     di, di
    mov     cx, 8
    rep     movsw
    ret
data: db 'H',2,'e',2,'l',2,'l',2
error:db 'E',2,'r',2,'r',2
    times   510 - ($ - $$) db 0
    dw      0xaa55   

UPD:新代码

    [bits   16]
    [org    0x7c00]

;clear screen
start:
;    mov     ah, 0
;    push    ax
;    pop     ds
    mov     ax, 0x3
    int     0x10

;reset the hard drive
    xor     ah, ah
    mov     dl, 0x80
    int     0x13
    jc      error

;read the second sector
    mov     si, DAP
    mov     ah, 0x42
    int     0x13

    mov     si, data
    call    print_string
    jmp     $

DAP:
    db      0x10    ;size of DAP
    db      0x0     ;zero
    db      0x1     ;number of sectors to read
    db      0x0     ;zero
;point to memory
    dw      0x0     ;offset
    dw      0x8c00  ;segment
    dq      0x1     ;disk address

DAP2:
    db      0x10
    db      0x0
    db      0x1
    db      0x0
    dw      0x0
    dw      0x8c00
    dq      0x2            

print_string:
    mov     ax, 0xb800
    mov     es, ax
    xor     di, di
    mov     si, 0x8c00
    mov     cx, 8
    rep     movsw
    ret

data: db 'H',2,'e',2,'l',2,'l',2
error:db 'E',2,'r',2,'r',2
endp:
    times   510 - ($ - $$) db 0
    dw      0xaa55 

PS我正在使用Bochs。

4

4 回答 4

25

有点恋尸癖;希望您的组装技能在此期间有所提高。但以防万一...

引用@Alexey Frunze 的话,“你需要注意你在做什么”。除了其他答案中详述的错误外,以下是我的一些观察:


你的模拟器太客气了

  • 您的代码似乎是引导加载程序。您假设 BIOS 将在 加载您的代码0x0000:0x7C00,但您不能确定它实际上没有在0x07C0:0000或任何其他等效地址加载它。阅读细分

  • 您未能初始化任何段寄存器。您可能会侥幸逃脱,因为您的模拟器很友善,并且正确初始化cs,dsesto 0x0000

您可以像这样解决这两个问题:

[bits 16]
[org 0x7C00]

    jmp 0x0000:start_16 ; ensure cs == 0x0000

start_16:
    ; initialise essential segment registers
    xor ax, ax
    mov ds, ax
    mov es, ax

根本的误解

  • 如果发生错误,您会直接跳转到字符串,而不是可执行代码。上帝只知道如果发生这种情况计算机会做什么。

  • 您检查驱动器复位的返回值 (CF),而不是读取本身。如果读取失败,您应该重置驱动器并再次尝试读取。如果驱动器打嗝,请在循环中尝试几次(例如 3 次)。如果驱动器重置失败,则可能是更严重的问题,您应该保释。


更安全的方法

我建议使用int 0x13, ah = 0x02. 您正在使用可能并非所有系统都支持的扩展 BIOS 功能(仿真器支持可能很不稳定,更不用说在某些现代硬件上发现的惰性 BIOS 实现了)。你处于真实模式 - 你不需要做任何花哨的事情。最好进入保护模式,以编写 PM 驱动程序来处理磁盘 I/O 的长期目标。

只要您处于实模式,这里有一个独立的函数,它可以使用简单的 BIOS 函数从磁盘读取一个或多个扇区。如果您事先不知道需要哪个扇区,则必须添加额外的检查以处理多轨读取

; read_sectors_16
;
; Reads sectors from disk into memory using BIOS services
;
; input:    dl      = drive
;           ch      = cylinder[7:0]
;           cl[7:6] = cylinder[9:8]
;           dh      = head
;           cl[5:0] = sector (1-63)
;           es:bx  -> destination
;           al      = number of sectors
;
; output:   cf (0 = success, 1 = failure)

read_sectors_16:
    pusha
    mov si, 0x02    ; maximum attempts - 1
.top:
    mov ah, 0x02    ; read sectors into memory (int 0x13, ah = 0x02)
    int 0x13
    jnc .end        ; exit if read succeeded
    dec si          ; decrement remaining attempts
    jc  .end        ; exit if maximum attempts exceeded
    xor ah, ah      ; reset disk system (int 0x13, ah = 0x00)
    int 0x13
    jnc .top        ; retry if reset succeeded, otherwise exit
.end:
    popa
    retn

您的打印功能采用彩色监视器(通过写入 0xB8000 的视频内存)。同样,您处于实模式。把事情简单化。使用 BIOS 服务:

; print_string_16
;
; Prints a string using BIOS services
;
; input:    ds:si -> string

print_string_16:
    pusha
    mov  ah, 0x0E    ; teletype output (int 0x10, ah = 0x0E)
    mov  bx, 0x0007  ; bh = page number (0), bl = foreground colour (light grey)
.print_char:
    lodsb            ; al = [ds:si]++
    test al, al
    jz   .end        ; exit if null-terminator found
    int  0x10        ; print character
    jmp  .print_char ; repeat for next character
.end:
    popa
    retn

示例用法

load_sector_2:
    mov  al, 0x01           ; load 1 sector
    mov  bx, 0x7E00         ; destination (might as well load it right after your bootloader)
    mov  cx, 0x0002         ; cylinder 0, sector 2
    mov  dl, [BootDrv]      ; boot drive
    xor  dh, dh             ; head 0
    call read_sectors_16
    jnc  .success           ; if carry flag is set, either the disk system wouldn't reset, or we exceeded our maximum attempts and the disk is probably shagged
    mov  si, read_failure_str
    call print_string_16
    jmp halt                ; jump to a hang routine to prevent further execution
.success:
    ; do whatever (maybe jmp 0x7E00?)


read_failure_str db 'Boot disk read failure!', 13, 10, 0

halt:
    cli
    hlt
    jmp halt

最后但并非最不重要的...

您的引导加载程序没有设置堆栈。我提供的代码使用堆栈来防止寄存器垃圾。在引导加载程序 (< 0x7C00) 之前几乎有30KiB可用,因此您可以在引导加载程序开始附近的某个地方简单地执行此操作:

xor ax, ax
cli         ; disable interrupts to update ss:sp atomically (AFAICT, only required for <= 286)
mov ss, ax
mov sp, 0x7C00
sti

呸!很多东西要消化。请注意,我已尝试保持独立功能的灵活性,以便您可以在其他 16 位实模式程序中重复使用它们。我建议您尝试编写更多模块化代码,并坚持这种方法,直到您更有经验为止。

例如,如果您对使用扩展读取函数一无所知,也许您应该在堆栈上编写一个接受 DAP 或指向其中的指针的函数。当然,首先将数据推送到那里会浪费代码空间,但是一旦到达那里,您就可以简单地调整必要的字段以进行后续读取,而不是让大量 DAP 占用内存。堆栈空间可以稍后回收。

不要灰心,组装需要时间,而且非常注重细节......在工作中抨击这些东西并不容易,所以我的代码中可能有错误!:)

于 2014-07-23T10:03:50.553 回答
8

首先,您需要检查cf而不是zf查看 BIOS 调用是否成功。更正您的jnz error.

其次,您似乎依赖于ds等于0。不能保证为0。将其设置为0。

同上flags.df,它不保证为 0。将其设置为 0。查看 和 上的rep文档。movs*cld

第三,您要求 BIOS 读取扇区并将其写入内存中的物理地址 0。这样做会覆盖中断向量表(从那里开始并占用 1KB)并损坏系统,需要重新启动。选择更好的地址。最好是在内存中的引导扇区结束之后。但是您还需要确保堆栈不存在,因此您还需要将堆栈设置到已知位置。

你需要注意你在做什么。

于 2013-03-19T11:37:52.907 回答
2

最小的 NASM BIOS 示例,它加载带有代码的扇区并跳转到它,没有错误检查和 DAP:

use16
org 0x7C00

    ; For greater portability you should
    ; do further initializations here like setup the stack and segments. 

    ; Load stage 2 to memory.
    mov ah, 0x02
    mov al, 1
    ; This may not be necessary as many BIOS setup is as an initial state.
    mov dl, 0x80
    mov ch, 0
    mov dh, 0
    mov cl, 2
    mov bx, stage2
    int 0x13

    jmp stage2

    ; Magic bytes.    
    times ((0x200 - 2) - ($ - $$)) db 0x00
    dw 0xAA55

stage2:

    ; Print 'a'.
    mov ax, 0x0E61
    int 0x10

    cli
    hlt

    ; Pad image to multiple of 512 bytes.
    times ((0x400) - ($ - $$)) db 0x00

编译并运行:

nasm -f bin -o main.img main.asm
qemu-system-i386 main.img

预期结果:a打印到屏幕上,然后程序停止。

在 Ubuntu 14.04 上测试。

Saner GAS 示例在我的 GitHub 上使用链接器脚本和更正确的初始化(段寄存器、堆栈)。

于 2015-10-05T07:45:07.870 回答
-2
load:
; Load sectors routine : bootdrv-drive , snum-sectors to load
;ES:BX where to load ex:  mov ax,0000h mov es,ax  mov bx, 7c00h
        push bx
        push ds
        mov verify,0h
.reset:
        cmp verify,5h
        je Err1
        add verify,1h
        mov ax, 0               ; Reset Disk
        mov dl, [BootDrv]       ; Drive to reset
        int 13h                 ;
        jc .reset               ; Failed -> Try again

        pop ds
        pop bx
        mov verify,0h
.read:
        cmp verify,5h
        je Err1
        add verify,1h
        mov ah, 2               ; Interrupt 13h,2 => Read disk sectors
        mov al, snum            ; how many sectors to read
        mov cx, fsect           ; cl-first sector to be r/w ch-track containing sector
        mov dh, head               ; head=0
        mov dl, [BootDrv]       ; Drive=boot drive
        int 13h                 ; Read! ES:BX = data from disk
        jc .read                ; failed -> Try again
        retn
于 2013-11-22T08:16:33.853 回答