[SOLVED] What is causing error 01h from interrupt 13h (16 bit real mode)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
bbqribs
Posts: 5
Joined: Sat Aug 23, 2025 8:37 pm

[SOLVED] What is causing error 01h from interrupt 13h (16 bit real mode)

Post by bbqribs »

Trying to read from my floppy disk in 16 bit real mode using int 13h (ah = 02h). This interrupt is setting the carry flag to 1 and loading error 01h "bad command passed to driver" into ah. Expected output is for it to successfully read the disk, but the error persists after retrying 5 times.

Code to read the disk:

Code: Select all

;ax = lba, returns head in dh, cx[0-5] = sector, cx[6-15] = cylinder
lba_chs_conversion:

    push ax
    push dx

    mov dx, 0
    div word [sectors_per_track]    ;ax = lba/spt, dx = lba%spt
    inc dx              ;dx = sector
    mov cx, dx          ;stores sector [0-63] in first 6 bits of cx

    mov dx, 0           
    div word [head_count]       ;ax = (lba/spt)/heads, dx = (lba/spt)%heads
    mov dh, dl
    mov ch, al
    shl ah, 6
    or cl, ah

    pop ax
    mov dl, al
    pop ax

    ret
;ax = lba, cl = number of sectors to read, dl = drive number, es:bx = address to store read data
read_disk:
    mov si, readingfloppy
    call print

    push cx
    call lba_chs_conversion
    pop ax              ;pops cl into ax for int13h
    
    mov ah, 0x02
    mov di, 0x05

.retry:
    pusha
    stc
    int 0x13
    jnc .done

    mov si, readingretry
    call print

    mov al, ah      
    mov ah, 0x0E
    int 0x10 

    popa
    dec di
    jz .fail
    jmp .retry
.done:
    mov si, readingsuccess
    call print
    ret
.fail:
    mov si, readingfail
    call print
    hlt

code in main that calls read_disk:

Code: Select all

main:   
    mov ax, 0
    mov ds, ax
    mov es, ax

    mov ss, ax
    mov sp, 0x7C00
    call cls

    ;get size of root directory
    mov ax, 0x0020
    mul word [directory_entries]
    div word [bytes_per_sector]
    mov cx, ax
    
    mov ax, [number_of_fats]
    mul word [sectors_per_fat]
    add ax, [reserved_sectors]
    push ax
    mov ax, 0x07C0
    mov es, ax
    pop ax
    mov bx, 0x0200
    ;should read the root directory into 0x07C0:0x0200
    mov dl, [drive_number]
    call read_disk
    

What have I done wrong to cause this error?
Last edited by bbqribs on Sat Aug 23, 2025 11:13 pm, edited 3 times in total.
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: What is causing error 01h from interrupt 13h (16 bit real mode)

Post by Octocontrabass »

bbqribs wrote: Sat Aug 23, 2025 8:40 pmWhat have I done wrong to cause this error?
What debugging have you done to try to find the problem? I'd set a breakpoint on the INT instruction to see exactly what values are in the CPU registers at that point.

Might as well include this link too.
bbqribs
Posts: 5
Joined: Sat Aug 23, 2025 8:37 pm

Re: What is causing error 01h from interrupt 13h (16 bit real mode)

Post by bbqribs »

Octocontrabass wrote: Sat Aug 23, 2025 8:49 pm
bbqribs wrote: Sat Aug 23, 2025 8:40 pmWhat have I done wrong to cause this error?
What debugging have you done to try to find the problem? I'd set a breakpoint on the INT instruction to see exactly what values are in the CPU registers at that point.

Might as well include this link too.
Did this, got the following values just before calling int13 for the first time:

Code: Select all

rax: 00000000_6000020e
rbx: 00000000_00000200
rcx: 00000000_00093990
rdx: 00000000_00000000
rsp: 00000000_00007be4
rbp: 00000000_00000000
rsi: 00000000_000e7d95
rdi: 00000000_00000003
r8 : 00000000_00000000
r9 : 00000000_00000000
r10: 00000000_00000000
r11: 00000000_00000000
r12: 00000000_00000000
r13: 00000000_00000000
r14: 00000000_00000000
r15: 00000000_00000000
rip: 00000000_00007d53
Why would there be values in the upper 16 bits of rax/rcx/rsi?
Also, is that an expected value for CX given that I'm just loading the root directory?
I'm sorry for the questions I'm very new to OS development
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: What is causing error 01h from interrupt 13h (16 bit real mode)

Post by Octocontrabass »

bbqribs wrote: Sat Aug 23, 2025 9:27 pmWhy would there be values in the upper 16 bits of rax/rcx/rsi?
You didn't initialize the upper 16 bits of those registers, so they contain whatever garbage was left there by the BIOS, same as any other register you don't initialize.
bbqribs wrote: Sat Aug 23, 2025 9:27 pmAlso, is that an expected value for CX given that I'm just loading the root directory?
No. It looks like your code is trying to access CHS 57/0/144 (or 569/0/16 for a hard disk). On a typical 1.44MB floppy disk, the root directory starts at LBA 19, CHS 0/1/2.
bbqribs
Posts: 5
Joined: Sat Aug 23, 2025 8:37 pm

Re: What is causing error 01h from interrupt 13h (16 bit real mode)

Post by bbqribs »

Solved... byte vs word error in main dereferencing "number_of_fats" (reference to a byte) into ax.
Post Reply