gpt4 book ai didi

assembly - 启用引导加载程序以加载 USB 的第二个扇区

转载 作者:行者123 更新时间:2023-12-04 11:20:32 26 4
gpt4 key购买 nike

我正在学习汇编语言。我写了一个简单的 bootstrap 。经过测试,它不起作用。这是我的代码:

[bits 16]
[org 0x7c00]

jmp start

data:
wolf_wel_msg db 'Welcome to Bootloader!!!',0x0D,0x0A,0
wolf_kernel_load db 'Loading kernel....',0x0D,0x0A,0
wolf_error_msg db 'Kernel.bin not found!',0x0D,0x0A,0
wolf_error_msg1 db 'Press any key to restart..',0

start:
mov si, wolf_wel_msg
call wolf_print

mov si, wolf_kernel_load
call wolf_print

pushf
stc

mov ah,00
mov dl,00
int 13h

read_sector:
mov ax, 0x0
mov es, ax
xor bx, bx
mov ah, 02
mov al, 01
mov ch, 01
mov cl, 02
mov dh, 00
mov dl, 00
int 13h

jc wolf_error
popf
jmp 0x0:0x1000
cli
hlt

wolf_error:
mov si, wolf_error_msg
call wolf_print
mov si, wolf_error_msg1
call wolf_print
mov ah,00
int 16h
xor ax,ax
int 19h

wolf_print:
lodsb
or al,al
jz exit
mov ah,0x0e
int 10h
jmp wolf_print
exit:
ret

times 510-($-$$) db 0
dw 0xAA55

使用以下命令将此代码放置在 USB 的第一个扇区中:
dd if=f:\boot.bin of=\\.\d: bs=512 count=1

使用以下命令在 USB 的第二个扇区中加载一个简单的程序:
dd if=f:\hello.bin of=\\.\d: bs=512 seek=1 count=1

这是加载到第二个扇区的程序的代码:
[bits 16]
[org 0x1000]

jmp start
data:
msg db 'Hello',0
start:
mov si, msg
jmp print

cli
hlt
print:
lodsb
or al, al
jz exit
mov ah,0x0e
int 10h
jmp print
exit:
ret

为什么我的引导加载程序不工作?我做错了什么吗?有什么建议?

最佳答案

您的代码假定 DS 设置为 0。您不能这样假定。如果您使用 org 0x7c00,代码的第一部分应该明确地将 DS 设置为 0 .

您应该认真考虑通过设置 SS:SP 来定义您的堆栈。你不知道现有的在哪里,或者它是否足够大来处理你打算做的事情。

就在您的引导加载程序被调用之前,BIOS 将使用引导设备编号设置 DL 寄存器。从引导驱动器发出驱动器请求时,不应在代码中将 DL 设置为 0。您应该使用调用引导加载程序时 DL 中存在的值。

您应该使用 CLD 指令来清除方向标志,因为您使用的是希望在内存中向前移动的 LODSB 指令。不能保证方向标志会正确设置,因此您应该使用 CLD(forward) 或 STD(backward) 将其明确设置为您需要的方向。

我在我的 StackOverflow 回答中提供了有关上述问题的更多信息 General Bootloader Tips .

由于您没有使用 BPB那么我强烈建议删除 jmp start作为引导加载程序的第一条指令。而是在代码之后但在引导扇区签名之前移动数据( 0xAA55 )。这样做的原因是一些 BIOS 将尝试根据 JMP 指令查找 BPB,该指令作为引导加载程序的第一条指令出现,如果找到,则会覆盖内存中引导加载程序的部分内容,从而导致潜在的未定义行为。

您的引导加载程序使用此指令启动从第二个扇区加载的第二阶段:

jmp 0x0:0x1000

问题是,当您以这种方式读取设置 ES:BX 的扇区时:
read_sector:
mov ax, 0x0
mov es, ax
xor bx, bx

这会将 ES:BX 设置为 0x0000:0x0000,这显然不是 JMP 期望代码所在的位置。您需要将 ES:BX 设置为您想要的内存位置 INT 13/AH=02h将磁盘扇区读入。

INT 13h/AH=02h 需要正确设置 Cylinder/Head/Sector 编号。扇区从 1 开始编号,但圆柱和头部从零开始。磁盘的第二个扇区位于 Cylinder 0, Head 0, Sector 2。您的代码将 Cylinder 设置为 1 而不是 0。此代码是错误的,因为您确实应该将其设置为 0:
mov ch, 01

在第二阶段,您创建了 print作为一个函数,因为它以 RET 结尾操作说明。 jmp print应改为 call print .

使用上面推荐的所有更改,包括我的一般引导加载程序提示中的更改,您的代码可以修改为:

boot.asm
[bits 16]
[org 0x7c00]

; Use the boot drive number passed to us by BIOS in register DL
start:
xor ax,ax ; We want a segment of 0 for DS for this question
mov ds,ax ; Set AX to appropriate segment value for your situation
mov es,ax ; In this case we'll default to ES=DS
mov bx,0x8000 ; Stack segment can be any usable memory

mov ss,bx ; This places it with the top of the stack @ 0x80000.
mov sp,ax ; Set SP=0 so the bottom of stack will be @ 0x8FFFF

cld ; Set the direction flag to be positive direction

mov si, wolf_wel_msg
call wolf_print

mov si, wolf_kernel_load
call wolf_print

pushf
stc

mov ah,00
int 13h

read_sector:
mov ax, 0x0
mov es, ax ; ES = 0
mov bx, 0x1000 ; BX = 0x1000. ES:BX=0x0:0x1000
; ES:BX = starting address to read sector(s) into
mov ah, 02 ; Int 13h/AH=2 = Read Sectors From Drive
mov al, 01 ; Sectors to read = 1
mov ch, 00 ; CH=Cylinder. Second sector of disk
; is at Cylinder 0 not 1
mov cl, 02 ; Sector to read = 2
mov dh, 00 ; Head to read = 0
; DL hasn't been destroyed by our bootloader code and still
; contains boot drive # passed to our bootloader by the BIOS
int 13h

jc wolf_error
popf
jmp 0x0:0x1000
cli
hlt

wolf_error:
mov si, wolf_error_msg
call wolf_print
mov si, wolf_error_msg1
call wolf_print
mov ah,00
int 16h
xor ax,ax
int 19h

wolf_print:
lodsb
or al,al
jz exit
mov ah,0x0e
int 10h
jmp wolf_print
exit:
ret

; Moved the data before the boot signature but after the code
wolf_wel_msg db 'Welcome to Bootloader!!!',0x0D,0x0A,0
wolf_kernel_load db 'Loading kernel....',0x0D,0x0A,0
wolf_error_msg db 'Kernel.bin not found!',0x0D,0x0A,0
wolf_error_msg1 db 'Press any key to restart..',0

times 510-($-$$) db 0
dw 0xAA55

你好.asm
[org 0x1000]

jmp start
data:
msg db 'Hello',0
start:
mov si, msg
call print ; print is a function, use CALL instead of JMP

cli
hlt
print:
lodsb
or al, al
jz exit
mov ah,0x0e
int 10h
jmp print
exit:
ret

由于根据 DD 命令中提供的信息,您似乎正在使用 Windows,因此您可能会遇到另一个问题。我不知道您使用的是哪个 DD,但 of=\\.\d:不写入磁盘(USB 驱动器)的开头,它会写入 D: 所在的分区,而不是磁盘本身的开头。

我建议您使用来自 Chrysocome 的最新 DD .截至今天,最新的是 0.6beta3 .我推荐这个版本,因为它允许您相对于驱动器的开头正确访问磁盘(或 USB 内存棒),而不是相对于特定分区的开头。这可能会导致尝试正确存储第 1 和第 2 个扇区时出现严重问题。使用最新版本,我将这些命令与 一起使用管理员权限 写入 USB 驱动器:
dd if=f:\boot.bin od=d: bs=512 count=1
dd if=f:\hello.bin od=d: bs=512 seek=1 count=1

这假设您的 USB 驱动器位于驱动器 D 上:正如您的问题中所建议的那样。 警告:未能使用正确的驱动器可能会导致其他设备上的数据丢失和损坏!!

如果这些命令正常工作,输出应该类似于:

dd if=boot.bin od=d: bs=512 count=1
rawwrite dd for windows version 0.6beta3.
Written by John Newbigin <jn@it.swin.edu.au>
This program is covered by terms of the GPL Version 2.

Device d: is a link to \\?\Device\HarddiskVolume5 \\?\Device\HarddiskVolume5 is a partition on \Device\Harddisk1
512 100%
1+0 records in
1+0 records out

dd if=hello.bin od=d: bs=512 seek=1 count=1
rawwrite dd for windows version 0.6beta3.
Written by John Newbigin <jn@it.swin.edu.au>
This program is covered by terms of the GPL Version 2.

Device d: is a link to \\?\Device\HarddiskVolume5 \\?\Device\HarddiskVolume5 is a partition on \Device\Harddisk1
28 5%
0+1 records in
0+1 records out


发出这些命令后,Windows 可能会自动检测到驱动器的格式不再正确。不允许 Windows 格式化驱动器。如果您允许它格式化驱动器,它将重新分区并格式化它。这样做会破坏您编写的引导扇区。出现提示时,只需取消可能出现的格式对话框即可。

请记住在将 USB 驱动器从系统中移除之前正确卸载/弹出 USB 驱动器。未能正确卸载可能会导致数据无法正确/完全写入驱动器。

如果您希望为 Bochs、QEMU、DOSbox 等创建磁盘镜像,您可以在命令提示符下使用以下命令创建 720k 软盘:
dd if=/dev/zero of=disk.img bs=1024 count=720    
dd if=f:\boot.bin of=disk.img bs=512 count=1 conv=notrunc
dd if=f:\hello.bin of=disk.img bs=512 seek=1 count=1 conv=notrunc

图片文件 disk.img应该可以被 Bochs、QEMU、DOSbox 等使用,或者写入 720k 软盘以在真实计算机上使用。
/dev/zero看起来像一个典型的 Unix/Linux 设备。我建议您使用的 Windows DD 命令可以理解 /dev/zero作为只生成零的特殊输入设备。 Windows 没有 /dev/zero设备,但 DD 将其视为特殊的内部设备并对其进行模拟。

在 MS Windows 上使用 Bochs 2.6.8 运行时,我看到的是:

enter image description here

在我的带有 16GB USB 内存棒的联想 L520 笔记本电脑(非 EFI BIOS)上,我看到的是:

enter image description here

关于assembly - 启用引导加载程序以加载 USB 的第二个扇区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36044706/

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