gpt4 book ai didi

assembly - 在实模式下使用 BIOS 格式化磁盘时出错

转载 作者:行者123 更新时间:2023-12-01 15:00:09 25 4
gpt4 key购买 nike

我在大学学习 assembly 。我正在尝试编写个人格式例程,但遇到了一些问题。该例程在启动时启动,但在第一次中断 (INT 13h/AH=7) 后进入错误流程。打印AH status值表明错误是:

0Eh control data address mark detected (hard disk)

这是我的 Bootstrap 代码:

[BITS 16]
[ORG 0x7C00]

init:
call main
ret


main:
xor ax, ax
mov ds, ax

mov si, string0
call print
call delay
mov si, string1
call print
call delay
mov si, string2
call print
call delay
mov si, string3
call print
call delay
mov si, string4
call print
call delay
mov si, string5
call print
call delay
mov si, string6
call print
call delay
mov si, string7
call print
call delay
mov si, string8
call print
call delay
mov si, string9
call print
call delay
mov si, string10
call print
call delay
mov si, string11
call print
call delay
mov si, string12
call print
call delay
mov si, string13
call print
call delay
mov si, string14
call print
call delay
mov si, string15
call print
call delay
mov si, string16
call print
call delay
mov si, string17
call print
call delay
mov si, string18
call print
call delay
mov si, string19
call print
call delay
mov si, string20
call print
call delay
mov si, string21
call print
call delay
mov si, string22
call print
call delay
mov si, string23
call print
call delay
mov si, string24
call print
call delay
mov si,string25
call print
call delay

call read_and_print
cmp al,'A'
jne error
call read_and_print
cmp al,'P'
jne error
call read_and_print
cmp al,'O'
jne error
call read_and_print
cmp al,'C'
jne error
call read_and_print
cmp al,'A'
jne error
call read_and_print
cmp al,'L'
jne error
call read_and_print
cmp al,'Y'
jne error
call read_and_print
cmp al,'P'
jne error
call read_and_print
cmp al,'S'
jne error
call read_and_print
cmp al,'E'
jne error


mov si,String27
call print
call menu
mov si,String35
call print
jmp $

menu:

mov si,String30
call print

call only_read
cmp al, 'A'
je zero_routine
cmp al, 'B'
je exit_routine
jmp menu

exit_routine:
mov si, String29
call print
ret

zero_routine: ;Command Shell;

mov si,String31
call print

call only_read
cmp al, 'A'
je A_Command
cmp al, 'B'
je B_Command
cmp al, 'C'
je exit_routine


A_Command:

xor eax,eax

mov ah,0x00 ; Reset the disk ;
mov dl, 0x80; First Disk ;
int 13h
mov ah,0x01 ; Get Disk Status ;
mov dl, 0x80; First Disk ;
int 13h
cmp ah, 0x00
jne f_error
mov si,String36
call print
;;;;;;;;;;;;;;;;;;;;;
; AH 07h ;
; AL Interleave ;
; CH Track ;
; CL Sector ;
; DH Head ;
; DL Drive ;
;;;;;;;;;;;;;;;;;;;;;



mov ah, 0x07
mov al, 0x00
mov ch, 0x00
mov dl, 0x80
mov dh, 0x00

call format
jmp zero_routine

B_Command:

xor eax,eax

mov ah,0x00 ; Reset the disk ;
mov dl, 0x81; Second Disk ;
int 13h
mov ah,0x01 ; Get Disk Status ;
mov dl, 0x81; Second Disk ;
int 13h
cmp ah, 0x00
jne f_error
mov si,String36
call print

;;;;;;;;;;;;;;;;;;;;;
; AH 07h ;
; AL Interleave ;
; CH Track ;
; CL Sector ;
; DH Head ;
; DL Drive ;
;;;;;;;;;;;;;;;;;;;;;


mov ah, 0x07
mov al, 0x00
mov cl, 0x00
mov ch, 0x00
mov dl, 0x81
mov dh, 0x00

call format
jmp zero_routine

format:

mov si, String32
call print
int 13h ; Format Drive ;
cmp ah, 0x00
jne f_error
mov si, String33
call print
ret

f_error:

mov si, String34
call print

xor al,al
add al,ah

mov ah, 0Ah ; Write Character
mov bh, 00h ; Page = 0
mov cx, 01h ; Times = 1
int 10h
jmp $

error:
mov si,String26
call print
jmp $


print:

mov bl,0x0A
mov ah, 0x0E
mov bh, 0x00

nextchar:
lodsb
or al, al
jz return
int 0x10
jmp nextchar

return:
ret

delay:

mov ah, 86h
mov cx, 0x00
mov dx, 0x8000
int 15h

ret

only_read:

mov ah, 00h ; Read Character
int 16h
ret

read_and_print:

mov ah, 00h ; Read Character
int 16h

push ax ; Save the current character

mov ah, 0Ah ; Write Character
mov bh, 00h ; Page = 0
mov cx, 01h ; Times = 1
int 10h

cursor_forward:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;AX = 0, CH = Start scan line, CL = End scan line, DH = Row, DL = Column;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


mov ah, 03h ; Read cursor position
mov bh, 00h ; Page = 0;
int 10h

mov ah,02h ; Set Cursor Position
mov bh,00h ; Page = 0
add dl,01h ; Column++
int 10h

pop ax

ret


string0 db "MEMORY Check 0401B + 204R81B OK",`\n`,`\r`,0
string1 db "JA Hi-SYS BOOT!",`\n`,`\r`,0
string2 db "Copyright (C) 2014,2015",`\n`,`\r`,0
string3 db "CO-CPU Check 256seg OK",`\n`,`\r`,0
string4 db "I/O VECTORS Check OK",`\n`,`\r`,0
string5 db "ROOTING TABLES Check OK",`\n`,`\r`,0
string6 db "STATUS ANALYZER Check SLAVE OK",`\n`,`\r`,0
string7 db "VIRUS PROTECTION Check GREEN OK",`\n`,`\r`,0
string8 db "----- SYSTEM CONFIGURATION -----",`\n`,`\r`,0
string9 db "addr PSP blks size owner/parameters",`\n`,`\r`,0
string10 db "---- ---- ---- ------- ----------------------",`\n`,`\r`,0
string11 db "D0E0 sys 1 5296 kozaic",`\n`,`\r`,0
string12 db "D22C sys 1 2416 ersdrv",`\n`,`\r`,0
string13 db "D2C4 2081 1 16384 smalldrv",`\n`,`\r`,0
string14 db "D6C5-DBFE 1 21392 <free>",`\n`,`\r`,0
string15 db "DE02-E000 1 8160 <free>",`\n`,`\r`,0
string16 db "--- UMB total: 53 TB ---",`\n`,`\r`,0
string17 db "0586 sys 1 2144 shimem",`\n`,`\r`,0
string18 db "060D sys 1 3968 hemm386",`\n`,`\r`,0
string19 db "0706 sys 1 3312 smalldrv",`\n`,`\r`,0
string20 db "07ED sys 1 13568 adam8b CON",`\n`,`\r`,0
string21 db "0C18 sys 4 65424 <config>",`\n`,`\r`,0
string22 db "1CD6 <-- 1 15008 share 7L:500",`\n`,`\r`,0
string23 db "2081 <-- 1 13712 smalldrv",`\n`,`\r`,0
string24 db "23DB-9FFF 1 508464 <free>",`\n`,`\n`,`\r`,0
string25 db "---- Insert Password ----",`\n`,`\r`,0
String26 db " ---- Error... Please Reboot----",`\n`,`\r`,0
String27 db " ---- OK ----",`\n`,`\r`,0
String28 db "---- Error... No Such Subroutine ----",`\n`,`\r`,0
String29 db `\n`,`\r`,"---- Exiting----",`\n`,`\r`,0
String30 db "---- Menu: A = Command Shell, B = Exit ----",0
String31 db `\n`,`\r`,"---- Menu: A = Format Disk 1, B = Format Disk 2, C = Exit ----",0
String32 db `\n`,`\r`,"---- Formatting Disk - Please Wait... ---- ",`\n`,`\r`,0
String33 db "---- Disk Formatted ----",`\n`,`\r`,0
String34 db `\n`,`\r`,"---- Error While Formatting ---- ",0
String35 db "---- You Can Now Reboot Your Computer ---- ",0
String36 db `\n`,`\n`,`\r`,"---- Disk Resetted ----",`\n`,`\r`,0

最佳答案

这个答案不是尝试调试代码,而是建议一种机制来克服这个引导加载程序的严重问题。我知道这可能无法回答您关于 INT 13h/AH=07h 的具体问题.


你原来的问题没有提到启动媒体。如果它使用的是软盘/硬盘驱动器,那么您将不得不担心这些问题:

  • BIOS 仅将启动磁盘的前 512 个字节 读入内存。
  • 您的代码在最后 2 个字节中缺少引导扇区签名 (0xAA55)。对于某些虚拟机而言,这可能不是问题,但通常会导致引导扇区无法被许多 BIOS 视为可引导。
  • 显式设置 SS:SP 堆栈指针,这样您就不会在引导加载程序代码中覆盖它

代码和数据超过 512 字节。 (我相信将近 2k)。虽然 BIOS 只读取前 512 个字节(第一个扇区),但没有什么能阻止您从引导扇区之后的扇区手动加载数据。引导扇区是#1,扇区#2 是紧随其后的扇区。您可以使用 INT 13/AH=02h读取磁盘扇区。从Ralf Brown的中断列表中我们发现它是这样定义的:

DISK - READ SECTOR(S) INTO MEMORY

AH = 02h
AL = number of sectors to read (must be nonzero)
CH = low eight bits of cylinder number
CL = sector number 1-63 (bits 0-5)
high two bits of cylinder (bits 6-7, hard disk only)
DH = head number
DL = drive number (bit 7 set for hard disk)
ES:BX -> data buffer

Return:

CF set on error
if AH = 11h (corrected ECC error), AL = burst length
CF clear if successful
AH = status (see #00234)
AL = number of sectors transferred (only valid if CF set for some
BIOSes)

在您的情况下,我们将仅从 BIOS 在 DL 寄存器中传递的引导驱动器中读取 8 个扇区(4096 字节)。我们将在 BIOS 加载原始 512 字节之后立即将它们读入内存中的 0x0000:0x7e00。将主要数据和代码放在 bootloader 的前 512 字节之后,将初始化和磁盘读取的代码放在 bootloader 的前 512 字节可能更容易。

我们可以填充引导扇区的前 512 个字节并添加磁盘签名:

times 510 - ($-$$) db 0            ; Fill empty bytes to pad out first
; sector with zeroes
dw 0aa55h ; Last 16-bit word of boot sector should be 0xaa55

生成的代码可能类似于:

[BITS 16]
[ORG 0x7C00]

init:
xor ax, ax
mov ds, ax ; DS=0
mov es, ax ; ES=0
mov ss, ax
mov sp, 7c00h ; Place SS:SP at 0000h:7c00h
cld

;AH = 02h
;AL = number of sectors to read (must be nonzero).
;CH = low eight bits of cylinder number
;CL = sector number 1-63 (bits 0-5)
; Sector numbers start at 1, not 0!
;high two bits of cylinder (bits 6-7, hard disk only)
;DH = head number
;DL = drive number (bit 7 set for hard disk). Passed in by BIOS
;ES:BX -> data buffer. ES set to 0 earlier

mov ax, 0208h ; AH=2 disk read, AL = number sectors to read = 8 (4k)
mov cx, 0002h ; CH=Cylinder number 0, CL=sector to start reading = 2
; Sector 2 = sector right after boot sector
xor dh, dh ; DH=head number = 0
mov bx, 7e00h ; ES:BX = memory to read into. ES=0, BX=7e00h right
; after the first 512 bytes read by BIOS
int 13h ; Int 13h/AH=02 disk read
; Should check carry flag for disk error. Leave
; as exercise for reader.
call main
ret

times 510 - ($-$$) db 0 ; Fill empty bytes to pad out first
; sector with zeroes
dw 0aa55h ; Last 16-bit word of boot sector should be 0xaa55

; This label will start at the 513th byte of the file (first byte of sector 2)
main:

mov si, string0
call print
call delay
mov si, string1
call print
call delay
mov si, string2
call print
call delay
mov si, string3
call print
call delay
mov si, string4
call print
call delay
mov si, string5
call print
call delay
mov si, string6
call print
call delay
mov si, string7
call print
call delay
mov si, string8
call print
call delay
mov si, string9
call print
call delay
mov si, string10
call print
call delay
mov si, string11
call print
call delay
mov si, string12
call print
call delay
mov si, string13
call print
call delay
mov si, string14
call print
call delay
mov si, string15
call print
call delay
mov si, string16
call print
call delay
mov si, string17
call print
call delay
mov si, string18
call print
call delay
mov si, string19
call print
call delay
mov si, string20
call print
call delay
mov si, string21
call print
call delay
mov si, string22
call print
call delay
mov si, string23
call print
call delay
mov si, string24
call print
call delay
mov si,string25
call print
call delay

call read_and_print
cmp al,'A'
jne error
call read_and_print
cmp al,'P'
jne error
call read_and_print
cmp al,'O'
jne error
call read_and_print
cmp al,'C'
jne error
call read_and_print
cmp al,'A'
jne error
call read_and_print
cmp al,'L'
jne error
call read_and_print
cmp al,'Y'
jne error
call read_and_print
cmp al,'P'
jne error
call read_and_print
cmp al,'S'
jne error
call read_and_print
cmp al,'E'
jne error


mov si,String27
call print
call menu
mov si,String35
call print
jmp $

menu:

mov si,String30
call print

call only_read
cmp al, 'A'
je zero_routine
cmp al, 'B'
je exit_routine
jmp menu

exit_routine:
mov si, String29
call print
ret

zero_routine: ;Command Shell;

mov si,String31
call print

call only_read
cmp al, 'A'
je A_Command
cmp al, 'B'
je B_Command
cmp al, 'C'
je exit_routine


A_Command:

xor eax,eax

mov ah,0x00 ; Reset the disk ;
mov dl, 0x80; First Disk ;
int 13h
mov ah,0x01 ; Get Disk Status ;
mov dl, 0x80; First Disk ;
int 13h
cmp ah, 0x00
jne f_error
mov si,String36
call print
;;;;;;;;;;;;;;;;;;;;;
; AH 07h ;
; AL Interleave ;
; CH Track ;
; CL Sector ;
; DH Head ;
; DL Drive ;
;;;;;;;;;;;;;;;;;;;;;



mov ah, 0x07
mov al, 0x00
mov ch, 0x00
mov dl, 0x80
mov dh, 0x00

call format
jmp zero_routine

B_Command:

xor eax,eax

mov ah,0x00 ; Reset the disk ;
mov dl, 0x81; Second Disk ;
int 13h
mov ah,0x01 ; Get Disk Status ;
mov dl, 0x81; Second Disk ;
int 13h
cmp ah, 0x00
jne f_error
mov si,String36
call print

;;;;;;;;;;;;;;;;;;;;;
; AH 07h ;
; AL Interleave ;
; CH Track ;
; CL Sector ;
; DH Head ;
; DL Drive ;
;;;;;;;;;;;;;;;;;;;;;


mov ah, 0x07
mov al, 0x00
mov cl, 0x00
mov ch, 0x00
mov dl, 0x81
mov dh, 0x00

call format
jmp zero_routine

format:

mov si, String32
call print
int 13h ; Format Drive ;
cmp ah, 0x00
jne f_error
mov si, String33
call print
ret

f_error:

mov si, String34
call print

xor al,al
add al,ah

mov ah, 0Ah ; Write Character
mov bh, 00h ; Page = 0
mov cx, 01h ; Times = 1
int 10h
jmp $

error:
mov si,String26
call print
jmp $


print:

mov bl,0x0A
mov ah, 0x0E
mov bh, 0x00

nextchar:
lodsb
or al, al
jz return
int 0x10
jmp nextchar

return:
ret

delay:

mov ah, 86h
mov cx, 0x00
mov dx, 0x8000
int 15h

ret

only_read:

mov ah, 00h ; Read Character
int 16h
ret

read_and_print:

mov ah, 00h ; Read Character
int 16h

push ax ; Save the current character

mov ah, 0Ah ; Write Character
mov bh, 00h ; Page = 0
mov cx, 01h ; Times = 1
int 10h

cursor_forward:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;AX = 0, CH = Start scan line, CL = End scan line, DH = Row, DL = Column;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


mov ah, 03h ; Read cursor position
mov bh, 00h ; Page = 0;
int 10h

mov ah,02h ; Set Cursor Position
mov bh,00h ; Page = 0
add dl,01h ; Column++
int 10h

pop ax

ret



string0 db "MEMORY Check 0401B + 204R81B OK",`\n`,`\r`,0
string1 db "JA Hi-SYS BOOT!",`\n`,`\r`,0
string2 db "Copyright (C) 2014,2015",`\n`,`\r`,0
string3 db "CO-CPU Check 256seg OK",`\n`,`\r`,0
string4 db "I/O VECTORS Check OK",`\n`,`\r`,0
string5 db "ROOTING TABLES Check OK",`\n`,`\r`,0
string6 db "STATUS ANALYZER Check SLAVE OK",`\n`,`\r`,0
string7 db "VIRUS PROTECTION Check GREEN OK",`\n`,`\r`,0
string8 db "----- SYSTEM CONFIGURATION -----",`\n`,`\r`,0
string9 db "addr PSP blks size owner/parameters",`\n`,`\r`,0
string10 db "---- ---- ---- ------- ----------------------",`\n`,`\r`,0
string11 db "D0E0 sys 1 5296 kozaic",`\n`,`\r`,0
string12 db "D22C sys 1 2416 ersdrv",`\n`,`\r`,0
string13 db "D2C4 2081 1 16384 smalldrv",`\n`,`\r`,0
string14 db "D6C5-DBFE 1 21392 <free>",`\n`,`\r`,0
string15 db "DE02-E000 1 8160 <free>",`\n`,`\r`,0
string16 db "--- UMB total: 53 TB ---",`\n`,`\r`,0
string17 db "0586 sys 1 2144 shimem",`\n`,`\r`,0
string18 db "060D sys 1 3968 hemm386",`\n`,`\r`,0
string19 db "0706 sys 1 3312 smalldrv",`\n`,`\r`,0
string20 db "07ED sys 1 13568 adam8b CON",`\n`,`\r`,0
string21 db "0C18 sys 4 65424 <config>",`\n`,`\r`,0
string22 db "1CD6 <-- 1 15008 share 7L:500",`\n`,`\r`,0
string23 db "2081 <-- 1 13712 smalldrv",`\n`,`\r`,0
string24 db "23DB-9FFF 1 508464 <free>",`\n`,`\n`,`\r`,0
string25 db "---- Insert Password ----",`\n`,`\r`,0
String26 db " ---- Error... Please Reboot----",`\n`,`\r`,0
String27 db " ---- OK ----",`\n`,`\r`,0
String28 db "---- Error... No Such Subroutine ----",`\n`,`\r`,0
String29 db `\n`,`\r`,"---- Exiting----",`\n`,`\r`,0
String30 db "---- Menu: A = Command Shell, B = Exit ----",0
String31 db `\n`,`\r`,"---- Menu: A = Format Disk 1, B = Format Disk 2, C = Exit ----",0
String32 db `\n`,`\r`,"---- Formatting Disk - Please Wait... ---- ",`\n`,`\r`,0
String33 db "---- Disk Formatted ----",`\n`,`\r`,0
String34 db `\n`,`\r`,"---- Error While Formatting ---- ",0
String35 db "---- You Can Now Reboot Your Computer ---- ",0
String36 db `\n`,`\n`,`\r`,"---- Disk Resetted ----",`\n`,`\r`,0

特别说明:如果您在某些情况下从 CD 启动,则不需要这样做。正如@RossRidge 在评论中指出的那样:

The El Torito CD-ROM boot spec does allow bootloaders to be more than one (512 byte) sector long when using a "no emulation" boot. If fact often they're 4 sectors long since this is the same size of physical CD-ROM sector (2048 bytes). Some BIOSes apparently have trouble if the length isn't a multiple of the 2048 byte physical sector size

问题中的汇编代码非常接近 2048 字节。如果它变得更大,那么您可能会开始遇到您的代码/数据没有完全被 BIOS 加载的情况。


潜在的格式问题

快速查看您的格式代码会发现一些问题:

mov ah, 0x07
mov al, 0x00
mov cl, 0x00
mov ch, 0x00
mov dl, 0x81
mov dh, 0x00

call format
jmp zero_routine

format:

mov si, String32
call print
int 13h ; Format Drive ;

问题:

  • 您为 Int 13h/AH=7h 设置寄存器调用但不要将 ES:BX 设置为指向格式缓冲区。 BX 尚未设置为有效偏移量,并且您的原始代码未将 ES 归零。该调用的文档是:

FIXED DISK - FORMAT DRIVE STARTING AT GIVEN TRACK (XT,PORT)

AH = 07h
AL = interleave value (XT only)
ES:BX = 512-byte format buffer (see AH=05h)
CH = cylinder number (bits 8,9 in high bits of CL)
CL = sector number
DH = head
DL = drive

Return:

AH = status code (see #00234)

格式缓冲区在 Int 13h/AH=5h 中有模糊的讨论。 :

ES:BX -> 512-byte format buffer
the first 2*(sectors/track) bytes contain F,N for each sector
F = sector type
00h for good sector
20h to unassign from alternate location
40h to assign to alternate location
80h for bad sector
N = sector number
  • 严重错误:您可能会覆盖所有寄存器,因为 formatint 13h 之前调用了 print 。一个简单的解决方法可能是在为格式设置寄存器之前打印格式字符串。

  • CL 中的扇区编号从 1 而非零开始。

下面的代码甚至没有设置CL:

mov ah, 0x07
mov al, 0x00
mov ch, 0x00
mov dl, 0x80
mov dh, 0x00

call format
jmp zero_routine
  • 我不确定 INT 13h/AH=07h 是否适用于 XT/较旧的可移植设备和早期驱动器以外的设备。我找到了一些 documentation这表明可能是这种情况:

INT 13h, 07h (7) Format Disk Starting at Cylinder fixed disk

Initializes each sector on a specified cylinder and all subsequent cylinders with sector address and size information. Only XTs may use this service.

您可能必须使用较低级别的 BIOS 例程,例如 INT 13h/AH=5h这是 FIXED DISK - FORMAT CYLINDER。即使那样,我也不确定现在这种低级格式是否适用于硬盘。 可能不需要调用此函数。我不确定。自 80 年代和 90 年代初以来,我个人还没有尝试过通过 BIOS 进行如此低级的格式化。

关于assembly - 在实模式下使用 BIOS 格式化磁盘时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38143945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com