;libmbr.asm 
;Written by, Sohail Qayum Malik<sohail@stupidcomputing.com>

 .MODEL small
 .DOSSEG

 .STACK 0000h

 .DATA

 .CODE

     _readSector                         PROC   near

       push bp
       mov  bp, sp

       
       push bx
       push es


       ;register set ES:BX should have the address of buffer
       ;which is atleast 0x0200 bytes long.
       push ds
       pop  es
       mov  bx, word ptr [bp + 04h]

       ;get head number, so the max head could be 0xFF.
       mov  dh, byte ptr [bp + 06h]
       ;get drive number.
       mov  dl, byte ptr [bp + 07h]
       ;get the sector number, bits[0-5] = sector(max 63)
       ;bits[6-7] = the bits[8-9] of 10 bit cylinder number.
       mov  cl, byte ptr [bp + 08h]
       ;get cylinder number, bits[0-7] of 10 bits cylinder number,
       ;max could be 0x03FF.
       mov  ch, byte ptr [bp + 09h]
       ;get number of sectors to read, so the max sectors read colud be
       ;0xFF
       mov  al, byte ptr [bp + 0Ah]

       ;BE VERY CAREFULL THE VALUE SHOULD BE 02H NOT 03H.
       mov  ah, 02h
       int  13h

       jc   fail
       mov  ax, 0000h    

fail:
       pop  es
       pop  bx
       

       pop  bp
       retn

     _readSector                         ENDP

     _writeSector                        PROC   near

       push bp
       mov  bp, sp

       push bx
       push es

       ;register set ES:BX should have the address of buffer
       ;which is atleast 0x0200 bytes long.
       push ds
       pop  es
       mov  bx, word ptr [bp + 04h]

       ;get head number, so the max head could be 0xFF.
       mov  dh, byte ptr [bp + 06h]
       ;get drive number.
       mov  dl, byte ptr [bp + 07h]
       ;get the sector number, bits[0-5] = sector(max 63)
       ;bits[6-7] = the bits[8-9] of 10 bit cylinder number.
       mov  cl, byte ptr [bp + 08h]
       ;get cylinder number, bits[0-7] of 10 bits cylinder number,
       ;max could be 0x03FF.
       mov  ch, byte ptr [bp + 09h]
       ;get number of sectors to read, so the max sectors written colud be
       ;0xFF
       mov  al, byte ptr [bp + 0Ah]

       ;BE VERY CAREFULL THE VALUE SHOULD BE 03H NOT 02H.
       mov  ah, 03h
       int  13h
       jc   fail
       mov  ax, 0000h    

fail:
       pop  es
       pop  bx
       
       pop  bp
       retn

     _writeSector                        ENDP


 END


 PUBLIC _readSector
 PUBLIC _writeSector