sarthak
sarthak

Reputation: 794

Unable to read from Disk in QEmu

I have the following code where I am trying to read from the disk using QEmu:

; Read some sectors from the boot disk using our disk_read function
[org 0x7c00]
mov [BOOT_DRIVE] , dl 
; BIOS stores our boot drive in DL , so store this for later
mov bp , 0x8000
mov sp , bp
; Here we set our stack safely out of the
; way , at 0x8000
mov bx , 0x9000
; Load 5 sectors to 0 x0000 ( ES ):0 x9000 ( BX )
mov dh , 5
; from the boot disk.
mov dl , [BOOT_DRIVE]
call disk_load
jmp $
%include "print_string.asm" ; Re - use our print_string function
%include "disk_load.asm"
BOOT_DRIVE : db 0
; Bootsector padding
times 510-($-$$) db 0
dw 0xaa55
;Loading additional two sectors from the disk we booted from.
times 256 dw 0xdada
times 256 dw 0xface

The include files are:
print_string.asm- which prints the string

print_string : ;Prints string stored at starting address [bx]
pusha
mov ah, 0x0e
print :
mov al, [bx]
int 0x10
add bx, 0x1
mov cl, [bx]
cmp cl, 0 ;0 marks the end of the string
jne print
mov al, 0x20 ;prints space " " at the end of the string
int 0x10
popa
ret

disk_load.asm-

; load DH sectors to ES : BX from drive DL
disk_load :
push dx
; Store DX on stack so later we can recall
; how many sectors were request to be read ,
; even if it is altered in the meantime
mov ah , 0x02
; BIOS read sector function
mov al , dh
; Read DH sectors
mov ch , 0x00
; Select cylinder 0
mov dh , 0x00
; Select head 0
mov cl , 0x02
; Start reading from second sector ( i.e.
; after the boot sector )
int 0x13
; BIOS interrupt
jc disk_error ; Jump if error ( i.e. carry flag set )
pop dx
cmp dh , al
jne disk_error
ret ; Restore DX from the stack
disk_error :
mov bx , DISK_ERROR_MSG
call print_string
jmp $
; Variables
DISK_ERROR_MSG db "Disk read error",0

In this code I am unable to read the disk and getting the "Disk read error" message getting printed on QEmu. I checked the code in GDB and found that the carry flag was set after reading the disk using BIOS.
Why is this happening?

Upvotes: 2

Views: 1656

Answers (1)

Jester
Jester

Reputation: 58762

According to my tests, this works if you use a floppy image. If you use a hard disk image, it needs to be at least 3kiB in size, otherwise qemu won't like it. I assume this is your problem.

Upvotes: 5

Related Questions