I am working on this 'how to build an os':
https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf
I am testing my code using qemu. I am at a part of the code where I am using bios system calls to read from memory. This is the section of the code that makes the call.
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 dh, 0x00 ; Select head 0
mov ch, 0x00
mov cl, 0x02 ; Start reading from second sector
int 0x13 ; BIOS interrupt
jc disk_error ; Jump if error ( i.e. carry flag set )
pop dx ; Restore DX from the stack
cmp dh , al ; if AL ( sectors read ) != DH ( sectors expected )
jne disk_error ; display error message
ret
disk_error :
mov bx, DISK_ERROR_MSG
call print_string
jmp $
DISK_ERROR_MSG:
db "Disk read error", 0
I copied exactly what was posted in the tutorial and I doubled checked everything to make sure it makes sense. It should work, but when I run the code, the carry flag is set and the disk_error function gets called. I checked the value of register ah for the return value, which was 0xc. I looked up that value and if I understand correctly, it means the floppy disk was not found.
The thing is, when I comment out the error check, the memory read is actually successful. I completed the tutorial and I was able to successfully boot my kernel from memory.
What is happening? Why am I getting an error flag and return value saying the disk could not be found if the read is actually successful?