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!
Your code is very messy. In order to properly use
int 13h
withAH = 3
, you need to also setES
(the segment in whichBX
resides, e.g.ES:BX
is the address of the buffer which should be read and written to the hard disk), andCX
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 :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.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.