Assembly BIOS总是无法执行磁盘操作

Assembly BIOS总是无法执行磁盘操作,assembly,nasm,x86-16,bootloader,bios,Assembly,Nasm,X86 16,Bootloader,Bios,我目前正在编写一个引导加载程序,用于加载比引导扇区允许的时间更长的程序。但是,每次我运行程序(我在Virtualbox和QEMU中测试它)时,磁盘读取失败,磁盘重置也失败 引导加载程序设计为在扇区之后立即加载该扇区(这是一个FAT16卷,因此我在磁盘描述中将其设置为保留扇区),然后立即运行该程序。但是,磁盘读取总是失败(CF设置为1),磁盘重置也会失败。这在Virtualbox和QEMU中都会发生 这是我的引导扇区和第二扇区的完整代码: BITS 16 jmp strict short mai

我目前正在编写一个引导加载程序,用于加载比引导扇区允许的时间更长的程序。但是,每次我运行程序(我在Virtualbox和QEMU中测试它)时,磁盘读取失败,磁盘重置也失败

引导加载程序设计为在扇区之后立即加载该扇区(这是一个FAT16卷,因此我在磁盘描述中将其设置为保留扇区),然后立即运行该程序。但是,磁盘读取总是失败(CF设置为1),磁盘重置也会失败。这在Virtualbox和QEMU中都会发生

这是我的引导扇区和第二扇区的完整代码:

BITS 16

jmp strict short main               ; Jump to main bootloader
nop                                 ; Pad out remaining bytes until boot descriptor

; Disk descriptor

OEM_name            db "HOUSE   "   ; Disk label
bytes_sector        dw 0x0200       ; Bytes per sector
sectors_cluster     db 0x01         ; Sectors per cluster
sectors_reserved    dw 0x0002       ; Number of sectors reserved (in this case 1 for MARBLE, the rest for R)
fats                db 0x02         ; Number of file allocation tables
max_root_entries    dw 0x0200       ; Max number of root entries
sectors             dw 0x1040       ; Number of sectors
medium_type         db 0xF0         ; Type of medium (removable or fixed?)
sectors_fat         dw 0x0010       ; Sectors per file allocation table
sectors_track       dw 0x0020       ; Sectors per track
heads               dw 0x0002       ; Number of heads
hidden_sectors      dd 0x00000000   ; Number of sectors before partition
total_sectors       dd 0x00000000   ; Number of sectors in medium (zero because 2B != 0)
drive_number        db 0x00         ; Drive number (for BIOS int 0x13)
drive_signature     db 0x00         ; NOT USED
ext_signature       db 0x29         ; Extended boot signature
volume_serial       dd 0x00000000   ; Volume's serial number
volume_label        db "HOUSEHOUSE "; Volume label
fs_type             db "FAT16   "   ; Filesystem type

main:
    mov sp, 0x1000                  ; 4K of stack

    mov ax, word [sectors_reserved] ; Read all reserved sectors
    sub al, 0x01                    ; Except this one
    mov ah, 0x02                    ; Read disk sectors
    mov bx, r_buffer                ; Read contents into our buffer
    mov ch, 0x00                    ; Cylinder 0
    mov cl, 0x02                    ; Sector 2
    mov dh, 0x00                    ; Head 0
    jmp .read                       ; Read the disk

.reset:
    pusha                           ; Push register states to stack
    call lps
    mov ah, 0x00                    ; Reset disk
    int 0x13                        ; BIOS disk interrupt
    jnc .read                       ; If successsul, read again
    call lps
    mov ah, 0x00                    ; Otherwise, prepare to reboot
    int 0x19                        ; Reboot

.read:
    call lps
    int 0x13                        ; BIOS disk interrupt
    jc .reset                       ; If failed, reset disk
    jmp r_buffer                    ; Otherwise, jump to R

lps:                                ; Debug message
    pusha
    mov ah, 0x0E
    mov al, 0x52
    mov bh, 0x00
    int 0x10
    mov ah, 0x00
    int 0x16
    popa
    ret

    times 510-($-$$) db 0x00        ; Pad remainder of boot sector with zeros
    sig             dw 0xAA55       ; Boot signature

r_buffer:                           ; Space in memory for loading R

r_start:                            ; Beginning of second sector
    mov ax, 0x07C0
    add ax, 0x0220
    mov ss, ax
    mov ax, 0x07C0
    mov ds, ax

    mov si, success                 ; Successful
    call print_str                  ; Print!
    hlt                             ; Halt here

print_str:                          ; Prints string pointed to by REGISTER SI to cursor location (si=str)
    pusha                           ; Push register states to stack
    mov ah, 0x0E                    ; Print in teletype mode
    mov bh, 0x00                    ; Page 0

.repeat:
    lodsb                           ; Load next character from si
    cmp al, 0x00                    ; Is this a null character?
    je .ret                         ; If it is, return to caller
    int 0x10                        ; Otherwise, BIOS interrupt
    jmp .repeat                     ; Do the same thing

.ret:
    popa                            ; Restore register states
    ret                             ; Return to caller

.data:
    success         db "!", 0x00    ; Success!

    times 1024-($-$$) db 0x00       ; Pad remainder of reserved sectors with zeros

正如我所说,第二个扇区中的代码应该是运行的,但由于磁盘重置失败,这并没有发生。

@ecm在我看到的大多数东西上都能看到。在访问任何数据之前,您需要设置段寄存器,并确保您有一个适合您在段寄存器(尤其是DS)中加载的值的
ORG
(原点)。默认情况下,当没有
ORG
指令(或等效指令)时,默认值为0x0000

在实模式下,每个逻辑地址由两个组件组成——一个段和一个偏移量。CPU计算的物理地址基于公式(段*16)+偏移量。将
ORG
视为起始偏移量。如果使用0x0000段,则需要0x7c00的偏移量。(0x0000*16)+0x7c00=0x7c00。0x7c00是引导加载程序启动的位置。由于您正在将扇区写入0x7e00以上的内存,因此将堆栈设置为0x0000:0x7c00会更简单,以便从引导加载程序下方向下扩展到内存的开头

由于您使用的是类似于
LODSB
的字符串指令,因此应确保使用
CLD
指令清除方向标志(DF),以便字符串操作在内存中向前推进。当引导加载程序开始执行时,您不能指望DF是清除的


当读取磁盘时,会发生重击。如果出现错误,则需要使用2重新加载AH,并使用要读取的扇区数重新加载AL。如果重新编写代码,可以使用SI存储要临时读取的扇区数。通常不必检查磁盘重置是否失败,只需重新执行读取操作即可。请尝试该操作几次,然后进入故障状态/重新启动。我修改了你的代码,在DI中设置了重试次数。每次重置完成后,重试次数将减少1。如果重试计数>=0,则尝试读取。如果重试次数为,则应在main的开头设置段寄存器(ds、es、ss)。重置后,您也应将ax重置为02xxh。你应该在顶部写上“org 7C00h”。在拥有hlt的第二个扇区中,应该在hlt之后添加一个跳转,跳转回到hlt。请指定您现在获得的确切输出。谢谢!我这样做了,但仍然得到同样的结果。当我在QEMU中运行这个程序时,我得到第一个“R”,我按下一个键,另一个R,我按下一个键,最后一个R出现。这就是所发生的一切。您也有一个无与伦比的pusha,尽管这似乎不太可能是您的问题的原因。您是否将此引导扇区安装到软盘类型的设备上?如果它位于分区设备上,则应设置隐藏扇区字段,并将其用作寻址第二个扇区的基础。(这将需要一个正确的CHS到LBA数字计算。)实际上,如果你像这里一样使用保留的扇区,你不需要org 7C00h,而是将ds初始化为7C0h就足够了(就像你在第二阶段所做的那样)。为了测试的目的,第一个版本的代码我将重试次数设置为0。更新会根据描述和注释将其设置为3。
BITS 16

org 0x7c00

jmp strict short main               ; Jump to main bootloader
nop                                 ; Pad out remaining bytes until boot descriptor

; Disk descriptor

OEM_name            db "HOUSE   "   ; Disk label
bytes_sector        dw 0x0200       ; Bytes per sector
sectors_cluster     db 0x01         ; Sectors per cluster
sectors_reserved    dw 0x0002       ; Number of sectors reserved (in this case 
                                    ; 1 for MARBLE, the rest for R)
fats                db 0x02         ; Number of file allocation tables
max_root_entries    dw 0x0200       ; Max number of root entries
sectors             dw 0x1040       ; Number of sectors
medium_type         db 0xF0         ; Type of medium (removable or fixed?)
sectors_fat         dw 0x0010       ; Sectors per file allocation table
sectors_track       dw 0x0020       ; Sectors per track
heads               dw 0x0002       ; Number of heads
hidden_sectors      dd 0x00000000   ; Number of sectors before partition
total_sectors       dd 0x00000000   ; Number of sectors in medium (zero because 2B != 0)
drive_number        db 0x00         ; Drive number (for BIOS int 0x13)
drive_signature     db 0x00         ; NOT USED
ext_signature       db 0x29         ; Extended boot signature
volume_serial       dd 0x00000000   ; Volume's serial number
volume_label        db "HOUSEHOUSE "; Volume label
fs_type             db "FAT16   "   ; Filesystem type

main:
    xor ax, ax                      ; AX = 0
    mov ds, ax                      ; DS = ES = 0
    mov es, ax
    mov ss, ax                      ; SS:SP = 0x0000:0x7c00 (grows down below bootloader)
    mov sp, 0x7c00
    cld                             ; Set forward direction for string instructions

    mov si, word [sectors_reserved] ; Read all reserved sectors
    dec si                          ; Except this one. SI = sectors to read
    mov di, 3                       ; retry count of 3 and then give up

    mov bx, r_buffer                ; Read contents into our buffer
    mov ch, 0x00                    ; Cylinder 0
    mov cl, 0x02                    ; Sector 2
    mov dh, 0x00                    ; Head 0
    jmp .read                       ; Read the disk

.reset:
    call lps
    dec di                          ; Reduce retry count
    jge .read                       ; If retry >= 0 then try again

                                    ; Otherwise retry count exceeded - reboot
    mov ah, 0x00
    int 0x19                        ; Reboot

.read:
    mov ax, si                      ; Transfer sector read count to AX
    mov ah, 0x02                    ; Read disk sectors
    call lps
    int 0x13                        ; BIOS disk interrupt
    jc  .reset                      ; If failed, reset disk
    jmp 0x0000:r_start              ; Otherwise, jump to r_start. Set CS=0

lps:                                ; Debug message
    pusha
    mov ah, 0x0E
    mov al, 0x52
    mov bh, 0x00
    int 0x10
    mov ah, 0x00
    int 0x16
    popa
    ret

    times 510-($-$$) db 0x00        ; Pad remainder of boot sector with zeros
    sig             dw 0xAA55       ; Boot signature

r_buffer:                           ; Space in memory for loading R

r_start:                            ; Beginning of second sector
    mov si, success                 ; Successful
    call print_str                  ; Print!

    cli
.endloop:
    hlt                             ; Halt here
    jmp .endloop

print_str:                          ; Prints string pointed to by REGISTER SI 
                                    ;     to cursor location (si=str)
    pusha                           ; Push register states to stack
    mov ah, 0x0E                    ; Print in teletype mode
    mov bh, 0x00                    ; Page 0

.repeat:
    lodsb                           ; Load next character from si
    cmp al, 0x00                    ; Is this a null character?
    je .ret                         ; If it is, return to caller
    int 0x10                        ; Otherwise, BIOS interrupt
    jmp .repeat                     ; Do the same thing

.ret:
    popa                            ; Restore register states
    ret                             ; Return to caller

.data:
    success         db "!", 0x00    ; Success!

    times 1024-($-$$) db 0x00       ; Pad remainder of reserved sectors with zeros