1

Recently i was learning x86 assembly and for practice decided to roll up my own Boot loader. Before working with int13H in the bootloader itself i decided to give a try on reading a sector ( logical 19) on floppy disk. But unfortunately after writing down the code and running it on QEMU no data was loaded itself. I guess i might be wrong somewhere . Here is my code .

read_sector:
    mov     ah,02H                  ; Function code to Read Sector
    mov     al,01                   ; Read one sector
    lea     bx,[SECTOR]             ; Address of input buffer
    mov     ch,00                   ; Track 0
    mov     cl,02                   ; Sector 1
    mov     dh,01                   ; Head number 1
    mov     dl,00                   ; Drive Number ( 0 - Floppy)
    int     13H                     ; call the routine
    mov     ah,0EH
    add     al,48
    int     10H
    ret

To make things clear i am actually reading:

Logical sector 19: Track- 0 Head -1 Sector- 2

In the code SECTOR represents a label using which i get access to write as well as read the input data from sector.

My print function ( Pretty basic) code:

print:

    mov al,03H
    int 10H
    .repeat:
        lodsb
        mov ah,0EH
        cmp al,0
        je  .done
        int  10H
        jmp .repeat     
    .done:
        ret

Sorry for the minor things left in the code :(

Regarding calling this function and accessing(printing) the data here is the code.

    call    read_sector 
    mov     si,[SECTOR]

But running it shows nothing on the screen . I also referred to the Ralf Brown files and checked out everything on ( int 13H fun:02H). i also checked the return code in ax and carry flag and the carry flag was set indicating a successful read. With no other sources in my mind the first place to refer was stack overflow. I would be extremely thankful if anyone helps me out.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    "Recently i was learning x86 assembly and for practice decided to roll up my own Boot loader." not to discourage you, but you decided to start in a notoriously hostile environment... Probably a simpler start would be a simple DOS COM program - you still have full control of the machine, but the environment is already in a decent state and you have some more services (e.g. file I/O). – Matteo Italia Aug 24 '18 at 09:40
  • I have already worked on it from few months. Now i wanted to make my own Operating system. Thats why i went down to the metal – Cosmo Ferrito Aug 24 '18 at 09:43
  • It is actually my dream to create operating systems sir. So i can work in any hostile environment – Cosmo Ferrito Aug 24 '18 at 09:44
  • "the carry flag was set" - If the carry flag was set, then the read has failed. Do you mean that the carry flag was cleared? Also, when you call int 10, you aren't setting a value in bh. – David Wohlferd Aug 24 '18 at 09:55
  • The buffer is at ES:BX. Do you set ES correctly so it corresponds the segment where BX (Sector) is? It is possible you are having issues with setting the segment registers and not matching the ORG directive being used as well. Unless you posted all your code to make this [mcve] it is a guessing game.PS: The boot drive doesn't need to be hard coded. It is set in _DL_ when the BIOS transfers control to your bootloader. You can use (or save to memory and reuse) that value for the disk BIOS calls. – Michael Petch Aug 24 '18 at 13:27
  • Also have to make sure DS is set expected so your `print` function prints the data from the right segment where the label you are printingis located or it will likely print nothing if that memory is filled with zeros.You may wish to consider debugging a bootloader with BOCHS as it has a very good debugger that understands real mode segment and offset addressing and handles real mode debugging quite well. – Michael Petch Aug 24 '18 at 13:32
  • Thanks a lot sir. I forgot to set the ES register. Upon setting it properly i was able to obtain the output. If you could make it as an answer i could set it correct. Thanks for the same. @ Michael Petch – Cosmo Ferrito Aug 25 '18 at 05:12

1 Answers1

0

Problems like these are usually related to the segment (ES) register not being set properly. Int 13h/AH=2h is documented this way:

AH = 02h
AL = number of sectors to read (must be nonzero)
CH = low eight bits of cylinder number
CL = sector number 1-63 (bits 0-5)
high two bits of cylinder (bits 6-7, hard disk only)
DH = head number
DL = drive number (bit 7 set for hard disk)
ES:BX -> data buffer

ES needs to be set to the segment that the data buffer offset resides in.

When you set up things your bootloader or JMP to code in another segment you should always make sure that the segment registers are loaded with the appropriate values. The segments you use will depend on what ORG directive you are using in your code.

I have general bootloader tips that may be of use.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198