我正在尝试构建一个多级引导加载程序,但我陷入了应该将第二阶段读入内存的第一阶段代码,代码使用int 13h
从虚拟软盘读取扇区(.img文件)。这是代码(MASM语法):
.286
.model tiny
.data
org 07c00h
driveNumber db ?
.code
main: jmp short start
nop
start: mov driveNumber,dl ;storing booting drive number
cli
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
sti
reset: mov ah,0h ;resetting the drive to the first sector
mov dl,driveNumber
int 13h
js reset
read: mov ax,1000h ;reading sectors into memory address 0x1000:0
mov es,ax
xor bx,bx
mov ah,02h
mov al,01h ;reading 1 sector
mov ch,01h ;form cylinder #1
mov cl,02h ;starting from sector #2
mov dh,01h ;using head #1
mov dl,driveNumber ;on booting drive
int 13h
jc read
push 1000h ;pushing the memory address into stack
push 0h ;pushing the offset
retf
end main
此代码与最后两个字节中的0x55AA签名一起放在虚拟磁盘的第一个扇区上,第二个阶段代码放在下一个扇区上。
而且,因为我在这里,它没有用!
我在vmware和bochs上都尝试过,两者都给出了相同的东西:没有!
所以我进行了一些测试:
int 13h
的回复,得到了:状态代码(ah
== 00h - >成功),实际扇区读数(al
= 01h - >实际上已经阅读了1个部门。es:bx
添加了一些值,然后运行了阅读过程,在完成之后,我检查了es:bx
处的值,发现它仍然是值I之前提到的,不是应该从该部门读取的价值。所以,我有一个测试告诉我该扇区实际上是读过的,并且测试告诉我没有任何东西读入内存......因此,我被卡住了!
有什么想法吗?
答案 0 :(得分:2)
主要问题是您正在从磁盘上的错误位置读取数据。如果将第二阶段从启动扇区之后的磁盘第二个扇区开始放置,则为Cylinder / Head / Sector(CHS)=(0,0,2)。引导扇区为(0,0,1)。扇区编号从1开始,而圆柱和磁头编号从0开始。
其他潜在问题是(其中许多可以在我的General Bootloader tips中找到):
您的代码依赖于 CS 设置为0000h的事实(因为您使用的ORG为7c00h)。您设置 DS = ES = SS = CS 。除了 DL 中的驱动器号之外,您不应该假设任何段寄存器或通用寄存器的状态。如果需要将 DS 之类的段寄存器设置为0000h,则将其设置为零。
在设置 DS 段之前,将 DL 中的驱动器号写入内存地址driveNumber
。可以将启动驱动器写入一个段,然后稍后从错误的段中读取。如果需要将 DL 保存到内存中,请在设置 DS 段之后执行此操作。 mov driveNumber, dl
在引用driveNumber
时具有 DS 的隐式用法(即:它类似于mov [ds:driveNumber], dl
。
您实际上并未在代码中设置 SP 。您只更新 SS 。谁知道 SP 指向的位置! SS:SP 的组合确定当前的堆栈指针。您可以通过将 SS:SP 设置为0000h:7c00h来设置堆栈,使其在引导加载程序下方向下扩展。这不会干扰Stage2在1000h:0000h的加载。
您无需在段寄存器更新周围放置CLI / STI。一个必须原子的地方是更新 SS:SP 时。如果您写入 SS ,则CPU将禁用中断,直到之后以下指令。如果在 SS 之后立即更新 SP ,则可以将其视为原子操作,并且不需要 CLI / STI < / em>。除了在1980年代初期生产的defective 8088s外,几乎所有处理器都是如此。如果有机会在这样的系统上启动,请考虑将 CLI / STI 放在更新 SS:SP 的代码周围。
尝试重置磁盘后,您拥有js reset
。我相信您打算使用jc
来检查进位标志(CF)而不是符号标志。通常,您通常不必检查重置是否失败。进行重置,然后重新发出驱动器访问命令(即:Disk Read),并在那里捕获任何驱动器错误。在实际的硬件上,您通常会在放弃和中止操作之前尝试3次操作。
看来您启用了.286
指令集,以便此代码可以编译:
push 1000h ; pushing the memory address into stack
push 0h ; pushing the offset
retf
您使用retf
来执行FAR JMP的等效操作,而MASM的早期版本不支持JMP语法。您的代码是正确的,但是您至少需要一个.186
指令,因为Intel 8088/8086处理器不支持PUSH imm8
或PUSH imm16
编码。这是added in the 80186。如果您希望代码在8088/8086上运行,则可以这样进行:
; Version that uses a FAR RET to do same as FAR JMP that works on 8086
mov ax, 1000h ; On 8086 push imm16 doesn't exist
push ax ; Push the code segment (1000h) to execute
xor ax, ax ; Zero AX
push ax ; Push the offset (0) of code to execute
retf ; MASM may not understand a FAR JMP, do RETF instead
尽管该解决方案有效,但编码时间却很长。您可以使用以下代码手动发出FAR JMP(操作码0EAh):
; Early versions of MASM don't support FAR JMP syntax like 'jmp 1000h:0000h'
; Manually encode the FAR JMP instruction
db 0eah ; 0EAh is opcode for a FAR JMP
dw 0000h, 1000h ; 0000h = offset, 1000h segment of the FAR JMP
您可以发出0aa55h
引导签名,并将所有代码和数据放入.code
段中,并使用ORG
来完成引导代码,并将引导代码填充为512字节。引导签名的填充和放置。
要解决上述问题,您的代码应如下所示:
.8086
.model tiny
.code
org 7c00h
main PROC
jmp short start
nop
start:
xor ax, ax
mov ds, ax ; DS=0
cli ; Only need STI/CLI around SS:SP change on buggy 8088
mov ss, ax ; SS:SP = 0000h:7c00h grow down from beneath bootloader
mov sp, 7c00h
sti
mov driveNumber, dl ; Storing booting drive number
jmp read ; Jump to reading (don't need reset first time)
reset:
mov ah, 0h ; Reset the drive before retrying operation
mov dl, driveNumber
int 13h
read:
mov ax, 1000h ; Reading sectors into memory address 0x1000:0
mov es, ax
xor bx, bx
mov ah, 02h
mov al, 01h ; Reading 1 sector
mov ch, 00h ; Form cylinder #0
mov cl, 02h ; Dtarting from sector #2
mov dh, 00h ; Using head #0
mov dl, driveNumber ; On boot drive
int 13h
jc reset
; Early versions of MASM don't support FAR JMP syntax like 'jmp 1000h:0000h'
; Manually encode the FAR JMP instruction
db 0eah ; 0EAh is opcode for a FAR JMP
dw 0000h, 1000h ; 0000h = offset, 1000h segment of the FAR JMP
; Error - end with HLT loop or you could use 'jmp $' as an infinite loop
error:
cli
endloop:
hlt
jmp endloop
main ENDP
; Boot sector data between code and boot signature.
; Don't put in data section as the linker will place that section after boot sig
driveNumber db ?
org 7c00h+510 ; Pad out boot sector up to the boot sig
dw 0aa55h ; Add boot signature
END main
Int 13h/AH=2(读取)和Int 13h/AH=0(重置)仅破坏AX寄存器( AH / AL )。磁盘故障后,无需设置所有参数即可进行另一次读取。
如前所述,在实际硬件上,重试磁盘操作3次很常见。您可以使用 SI 作为磁盘操作的重试计数,因为磁盘读取和重置BIOS调用不使用 SI 。
不必以以下内容开头:
main: jmp short start
nop
start:
除非要插入BIOS参数块(BPB)用作卷引导记录(VBR)。使用软盘驱动器仿真(FDD)在USB设备上启动时,在实际硬件上拥有BPB是good idea。
如果像这样更新16位寄存器的高8位和低8位寄存器:
mov ah,02h
mov al,01h
您可以通过以下方式将它们合并为一条指令:
mov ax, 0201h
实施其他观察中确定的内容后,代码应类似于:
boot.asm :
DISK_RETRIES EQU 3
.8086
.model tiny
IFDEF WITH_BPB
include bpb.inc
ENDIF
.code
org 7c00h
main PROC
IFDEF WITH_BPB
jmp short start
nop
bpb bpb_s<>
ENDIF
start:
xor ax, ax
mov ds, ax ; DS=0
; cli ; Only need STI/CLI around SS:SP change on buggy 8088
mov ss, ax ; SS:SP = 0000h:7c00h
mov sp, 7c00h
; sti
mov ax, 1000h ; Reading sectors into memory address (ES:BX) 1000h:0000h
mov es, ax ; ES=1000h
xor bx, bx ; BX=0000h
mov cx, 0002 ; From cylinder #0
; Starting from sector #2
mov dh, 00h ; Using head #0
mov si, DISK_RETRIES+1 ; Retry count
jmp read ; Jump to reading (don't need reset first time)
reset:
dec si ; Decrement retry count
jz error ; If zero we reached the retry limit, goto error
mov ah, 0h ; If not, reset the drive before retrying operation
int 13h
read:
mov ax, 0201h ; BIOS disk read function
; Reading 1 sector
int 13h ; BIOS disk read call
; This call only clobbers AX
jc reset ; If error reset drive and try again
; Early versions of MASM don't support FAR JMP syntax like 'jmp 1000h:0000h'
; Manually encode the FAR JMP instruction
db 0eah ; 0EAh is opcode for a FAR JMP
dw 0000h, 1000h ; 0000h = offset, 1000h segment of the FAR JMP
; Error - end with HLT loop or you could use 'jmp $' as an infinite loop
error:
cli
endloop:
hlt
jmp endloop
main ENDP
; Boot sector data between code and boot signature.
; Don't put in data section as the linker will place that section after boot sig
org 7c00h+510 ; Pad out boot sector up to the boot sig
dw 0aa55h ; Add boot signature
END main
bpb.inc :
bpb_s STRUCT
; Dos 4.0 EBPB 1.44MB floppy
OEMname db "mkfs.fat" ; mkfs.fat is what OEMname mkdosfs uses
bytesPerSector dw 512
sectPerCluster db 1
reservedSectors dw 1
numFAT db 2
numRootDirEntries dw 224
numSectors dw 2880
mediaType db 0f0h
numFATsectors dw 9
sectorsPerTrack dw 18
numHeads dw 2
numHiddenSectors dd 0
numSectorsHuge dd 0
driveNum db 0
reserved db 0
signature db 29h
volumeID dd 2d7e5a1ah
volumeLabel db "NO NAME "
fileSysType db "FAT12 "
bpb_s ENDS
示例 stage2.asm ,该示例在运行时显示字符串:
.8086
.model tiny
.data
msg_str db "Running stage2 code...", 0
.code
org 0000h
main PROC
mov ax, cs
mov ds, ax
mov es, ax
cld
mov si, offset msg_str
call print_string
; End with a HLT loop
cli
endloop:
hlt
jmp endloop
main ENDP
; Function: print_string
; Display a string to the console on display page 0
;
; Inputs: SI = Offset of address to print
; Clobbers: AX, BX, SI
print_string PROC
mov ah, 0eh ; BIOS tty Print
xor bx, bx ; Set display page to 0 (BL)
jmp getch
chloop:
int 10h ; print character
getch:
lodsb ; Get character from string
test al,al ; Have we reached end of string?
jnz chloop ; if not process next character
ret
print_string ENDP
END main
如果从MASM32 SDK使用ML.EXE和LINK16.EXE,则可以使用以下命令来汇编和链接代码并创建磁盘映像:
ml.exe /Fe boot.bin /Bl link16.exe boot.asm
ml.exe /Fe stage2.bin /Bl link16.exe stage2.asm
copy /b boot.bin+stage2.bin disk.img
如果您希望包含BPB,则可以通过以下方式进行组装和链接:
ml.exe /DWITH_BPB /Fe boot.bin /Bl link16.exe boot.asm
ml.exe /Fe stage2.bin /Bl link16.exe stage2.asm
copy /b boot.bin+stage2.bin disk.img
两个方法都创建一个名为disk.img
的磁盘映像。当disk.img
在BOCHS中启动时,它应显示为:
答案 1 :(得分:0)