Reading from a floppy

Programming, for all ages and all languages.
Post Reply
chezzestix
Member
Member
Posts: 118
Joined: Mon May 05, 2008 5:51 pm

Reading from a floppy

Post by chezzestix »

I'm having trouble doing it. Here is my code:

Code: Select all

start:
mov ax, 0x07C0
mov ds, ax
mov es, ax

mov si,fat  ;method of checking if floppy is a FAT
mov al,[si]
cmp al,'F'
jne notfat
inc si
mov al,[si]
cmp al,'A'
jne notfat
inc si
mov al,[si]
cmp al,'T'
jne notfat

read:  ;Read first directory table (spfat = sectors per fat table, reserved = # of reserved sectors) 
mov cl,[spfat]
add cl,[spfat]
add cl,[reserved]
mov ah,02h
mov al,01h
mov ch,00h
mov dh,01h
mov dl,00h
mov bx,200h
int 13h
cmp ah,80h  ;if the floppy is simply busy try again until it isnt
je read
cmp ah,00
jne readerr

mov si,read1
call int_print

mov si,0x7E00
call int_print

jmp final

int_print:  ;interrupt based printing
mov AH,0Eh
mov BH,01h
mov AL,[si]
int 10h
inc si
cmp byte [si],0
jne int_print
ret

notfat:  ;print nofat error
mov si,nofat
call int_print
jmp final

readerr: ;print read error
mov cl,ah
mov ch,0
mov si,troublereading
call int_print
ror ax,8
mov ah,0Eh
mov bh,01h
ror cx,4
mov al,cl
add al,48
ror cx,8
int 10h
mov cl,0
rol cx,4
mov al,cl
add al,48
int 10h
jmp final

read1 db 'Read1',0Dh,0Ah,0
nofat  db 'Err_1',0
troublereading db 'Err_2 Code:',0
Its meant to in a very simple manner read FAT12 floppies. However when Int13/AH=02h executes I get error code 20h "controller failure". What is going on here?

Thanks
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Reading from a floppy

Post by bewing »

Well, you could try doing an Int13h AH=0 reset before your first read attempt. Is this running on real hardware, or an emulator?
chezzestix
Member
Member
Posts: 118
Joined: Mon May 05, 2008 5:51 pm

Re: Reading from a floppy

Post by chezzestix »

The reset works but doesn't solve the issue. So there is nothing wrong with the drive just with the ah 02h command.

Its running in an emulator, Sun VirtualBox.
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: Reading from a floppy

Post by Selenic »

chezzestix wrote:The reset works but doesn't solve the issue. So there is nothing wrong with the drive just with the ah 02h command.

Its running in an emulator, Sun VirtualBox.
If it doesn't work in an emulator, then your code's wrong. Real hardware can sometimes fail (which is why should try several attempts at reading the floppy, in case of temporary errors) but a pure emulator won't unless your hard drive is dying (in which case, why aren't you out replacing it instead? :P )
chezzestix
Member
Member
Posts: 118
Joined: Mon May 05, 2008 5:51 pm

Re: Reading from a floppy

Post by chezzestix »

You would be absolutely right.... if the emulator wasn't using my physical A: drive. :P No one has any idea what could be the issue?

Edit:
Int13/AH=08h returns that the first drive is indeed a 1.44MB. The drive exists in the correct form, it responds to resets but then the controller fails when you ask to read the disk.

Edit 2:
Ugh... Verify disk sectors (Int13/AH=04H) works too... The controller just wont read.
Last edited by chezzestix on Sun Apr 18, 2010 3:04 pm, edited 1 time in total.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Reading from a floppy

Post by quok »

chezzestix wrote:The reset works but doesn't solve the issue. So there is nothing wrong with the drive just with the ah 02h command.
So after trying a reset and issuing the read command again you still get the same error code from the interrupt? Or is it a different error? Or does the code fail somewhere else?

Your method of checking if the floppy is a FAT formatted floppy is completely broken, btw. You should NOT rely on the filesystem type field of the BPB to be anything at all. You really must do the proper checks as specified in the FAT documentation.
chezzestix wrote:You would be absolutely right.... if the emulator wasn't using my physical A: drive. :P No one has any idea what could be the issue?
Ah, so you have the issues of running in an emulator and the issues of using real hardware. I'd suggest getting your code to work on the emulator with a disk image first, and then try getting it to work on real hardware as well.
chezzestix
Member
Member
Posts: 118
Joined: Mon May 05, 2008 5:51 pm

Re: Reading from a floppy

Post by chezzestix »

So after trying a reset and issuing the read command again you still get the same error code from the interrupt? Or is it a different error? Or does the code fail somewhere else?
The code as it currently stands:

Code: Select all

start:
mov ax, 0x07C0
mov ds, ax
mov es, ax

mov ah,08H  ;Drive verification
mov dl,00h
int 13h
cmp ah,00h
jne vererr  ;chance to throw code
mov ah,90h
add ah,bl
cmp bl,04h
jl vererr   ;throw code if less than 1.44MB

mov si,fat  ;Janky Fat check
mov al,[si]
cmp al,'F'
jne nofat
inc si
mov al,[si]
cmp al,'A'
jne nofat
inc si
mov al,[si]
cmp al,'T'
jne nofat

mov si,0

read:
mov ah,00h ;drive reset
mov dl,00h
int 13h
cmp ah,00h
jne reserr ;chance to throw code

mov cl,[spfat] ;Read Attempt
add cl,[spfat]
add cl,[reserved]
mov ah,02h
mov al,01h
mov ch,00h
mov dh,00h
mov dl,00h
mov bx,200h
int 13h
cmp ah,00h
jne thrice ;if code check thrice

mov si,read1
call int_print

mov si,0x7E00
call int_print

jmp final

thrice:   ;throw three errors before reporting
inc si
cmp si,2
jle read
jmp readerr

int_print:
mov ah,0Eh
mov bh,01h
mov al,[si]
int 10h
inc si
cmp byte [si],0
jne int_print
ret

readerr:  ;procedural error stuff
inc [er]
reserr:
inc [er]
nofat:
inc [er]
vererr:
inc [er]

printerr:
push ax
mov si,em
call int_print
pop cx
mov ah,0Eh
mov bh,01h
mov cl,ch
mov ch,0
ror cx,4
mov al,cl
add al,48
int 10h
mov cl,0
rol cx,4
mov al,cl
add al,48
int 10h
mov al,'-'
int 10h
mov al,[er]
add al,48
int 10h
jmp final

read1 db 'Read1',0Dh,0Ah,0  ;variables
em db 'Err ',0
er db 0

final:
Every command dealing with the floppy has a chance to throw an error. None of the command return an error except for Drive Read Attempt and it constantly returns error code 20
Your method of checking if the floppy is a FAT formatted floppy is completely broken, btw. You should NOT rely on the filesystem type field of the BPB to be anything at all. You really must do the proper checks as specified in the FAT documentation.
Yea its a placeholder method. It can be relied on with windows formatted floppies. *shrugs*
Ah, so you have the issues of running in an emulator and the issues of using real hardware. I'd suggest getting your code to work on the emulator with a disk image first, and then try getting it to work on real hardware as well.
Suggestion noted, thanks.
Post Reply