如何用BIOS中断13h向硬盘写入

发布于 2024-12-29 19:38:58 字数 486 浏览 3 评论 0原文

我想将引导加载程序复制到硬盘的第一个扇区(512)(我应该使用BIOS中断13h),我发现了这段代码:

mov bx, buffer1       ; set BX to the address (not the value) of BlahBlah 
mov ah,03h            ;When ah=, int13 reads a disk sector
mov al,5              ;Al is how many sectors to read

mov cl,0              ;Sector Id
mov dh,0              ;Head
mov dl,80h            ;Drive (0 is floppy)
mov cx,512            ;One sector /2   

mov ah, 0x3           ; set function 2h
int 0x13  

但是它不起作用!

I want to copy my boot loader to first sector(512) of hard disk within itself (I should use bios interrupt 13h) and I found this code:

mov bx, buffer1       ; set BX to the address (not the value) of BlahBlah 
mov ah,03h            ;When ah=, int13 reads a disk sector
mov al,5              ;Al is how many sectors to read

mov cl,0              ;Sector Id
mov dh,0              ;Head
mov dl,80h            ;Drive (0 is floppy)
mov cx,512            ;One sector /2   

mov ah, 0x3           ; set function 2h
int 0x13  

bu it does not work!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗弃M 2025-01-05 19:38:58

你的代码非常混乱。为了正确使用 int 13hAH = 3,您还需要设置 ESBX 所在的段) code> 驻留,例如 ES:BX 是应该读取和写入硬盘的缓冲区的地址),CX 是柱面和扇区的组合数字(<代码>圆柱体 = CL[7:6] || CH,扇区 = CL[5:0])。

假设您想从物理地址 5000h 向硬盘 0 上的 CHS 0:0:1 写入一个扇区(512 字节),您的代码将如下所示:

xor ax, ax
mov es, ax    ; ES <- 0
mov cx, 1     ; cylinder 0, sector 1
mov dx, 0080h ; DH = 0 (head), drive = 80h (0th hard disk)
mov bx, 5000h ; segment offset of the buffer
mov ax, 0301h ; AH = 03 (disk write), AL = 01 (number of sectors to write)
int 13h

您还应该记住检查执行中断后进位标志是否已设置。函数是否正确执行就一目了然了。如果已设置,则 AH 寄存器将包含错误代码。

Your code is very messy. In order to properly use int 13h with AH = 3, you need to also set ES (the segment in which BX resides, e.g. ES:BX is the address of the buffer which should be read and written to the hard disk), and CX to a combination of the cylinder and sector number (cylinder = CL[7:6] || CH, sector = CL[5:0]).

Assuming that you want to write one sector (512 bytes) from the physical address 5000h to CHS 0:0:1 on hard disk 0, your code would look something like this :

xor ax, ax
mov es, ax    ; ES <- 0
mov cx, 1     ; cylinder 0, sector 1
mov dx, 0080h ; DH = 0 (head), drive = 80h (0th hard disk)
mov bx, 5000h ; segment offset of the buffer
mov ax, 0301h ; AH = 03 (disk write), AL = 01 (number of sectors to write)
int 13h

You should also remember to check whether the Carry Flag has been set after executing the interrupt. It will be clear if the function has been executed properly. If it's set, then the AH register will contain an error code.

属性 2025-01-05 19:38:58

BIOS 功能有输入参数。如果您没有正确输入所有参数,BIOS 功能将无法猜测您的意思。对于您正在使用的 BIOS 功能,请查看:http://www.ctyme。 com/intr/rb-0608.htm

据我所知,您缺少 CH 和 ES 的正常值,因此 BIOS 可以将数据从完全不同的地址写入完全不同的扇区。另请注意,CL 是 CX 寄存器的最低半部分 - 将值加载到 CL 中,然后通过将某些内容加载到 CX 中来覆盖它是没有意义的。

BIOS 函数也返回值。在您的情况下,BIOS 可能会返回一个状态代码,告诉您出了什么问题,并且因为您不检查,所以您不知道是否出了什么问题,或者如果出了问题是什么。

BIOS functions have input parameters. If you don't get all of the input parameters right, the BIOS function isn't able to guess what you meant. For the BIOS function you're using have a look at: http://www.ctyme.com/intr/rb-0608.htm

As far as I can tell, you're missing sane values for both CH and ES, so the BIOS can write data from a completely different address to a completely different sector. Also note that CL is the lowest half of the CX register - there's no point loading a value into CL and then overwriting it by loading something into CX.

BIOS functions return values too. In your case the BIOS may be returning a status code that tells you what went wrong, and because you don't check you don't know if anything went wrong or what it was if it did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文