如何用bios中断13h在硬盘上写字

Oce*_*lot 1 assembly bootstrapping bios bootloader

我想将我的引导加载程序复制到自身内部的第一个扇区(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  
Run Code Online (Sandbox Code Playgroud)

不行不通!

Dan*_*zar 9

你的代码非常混乱.为了正确使用int 13hAH = 3,你还需要设置ES(段中BX驻留,例如ES:BX是应该被读取并写入到硬盘缓冲区的地址),并且CX在气缸和扇区号的组合(cylinder = CL[7:6] || CH,sector = CL[5:0]).

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

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
Run Code Online (Sandbox Code Playgroud)

您还应该记住在执行中断后检查是否已设置进位标志.如果功能已正确执行,将会很清楚.如果已设置,则AH寄存器将包含错误代码.