How to write on hard disk with bios interrupt 13h

2019-03-22 16:14发布

问题:

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!

回答1:

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.



回答2:

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.