Search code examples
assemblyqemuosdev

Assembly INT 0x13: Not getting an error when trying to read from disk


Whenever I run this in QEMU, I don't seem to get an error, but the message isn't printed on the screen, so I'm not sure what's really happening that I can't see. Here is my code:

[org 0x7c00]
mov bp, 0x8000
mov sp, bp

mov si, name
call print_string
mov si, version
call print_string
call rd_dsk
mov si, testmsg
call print_string
jmp $

;Print
print_char:
  mov ah, 0x0e
  int 0x10
  ret
print_string:
  screen:
    lodsb
    cmp al, 0
    je screen_end
    mov ah, 0x0e
    int 0x10
    jmp screen
  screen_end:
  ret
print_hex:
  mov si, hex_temp
  mov bx, dx
  shr bx, 12
  mov bx, [bx+hex_alph]
  mov [hex_temp+2], bl
  mov bx, dx
  shr bx, 8
  and bx, 0x000f
  mov bx, [bx+hex_alph]
  mov [hex_temp+3], bl
  mov bx, dx
  shr bx, 4
  and bx, 0x000f
  mov bx, [bx+hex_alph]
  mov [hex_temp+4], bl
  mov bx, dx
  and bx, 0x000f
  mov bx, [bx+hex_alph]
  mov [hex_temp+5], bl
  call print_string
  ret

;Read
rd_dsk:
  mov ah, 0x02
  mov al, 0x01
  mov ch, 0x00
  mov dh, 0x00
  mov cl, 0x02
  mov bx, 0x00
  mov es, bx
  mov bx, 0x7c00 + 512
  int 0x13
  jc rd_dsk_error
   ret

; Error
rd_dsk_error:
  mov si, rd_dsk_error_msg
  call print_string
  jmp $

;Misc Data
name:
  db 'PurityOS ',0
version:
  db 'v0.0.1.2 ',0
hex_temp:
  db '0x????',0
hex_alph:
  db '0123456789ABCDEF'

; Error Messages
rd_dsk_error_msg:
  db 'Error reading the disk.',0

times 510-($-$$) db 0
dw 0xaa55

;Data beyond BootSector
testmsg:
  db 'Reading Second Sector',0

I expected to get this output:

PurityOS v0.0.1.2 Reading Second Sector

However, I only get this:

PurityOS v0.0.1.2

Also, if I put the "call rd_dsk" at the top:

[org 0x7c00]
mov bp, 0x8000
mov sp, bp

call rd_dsk
mov si, name
call print_string
mov si, version
call print_string
mov si, testmsg
call print_string
jmp $

I don't get any output, I only get the blinking cursor, as if I only have "jmp $"

I assemble the .asm file using NASM, and I run it with QEMU. "qemu -fda ..." doesn't work. I get "The program 'qemu' can be found in the following packages" which none of the packages helped after being installed. I just run "qemu-system-x86_64 ./Asm/bootsector.bin" to open QEMU.

Thank you in advance.


Solution

  • Your stack pointer is initially located at 0x0000:0x8000.

    When you perform a "call" or "int" instruction the return address is pushed on the stack (which is located in the memory range 0x7F00-0x8000).

    When you overwrite this memory the system will crash.

    Solution:

    Change 0x8000 to 0x7BFC in this line:

    mov bp, 0x8000
    mov sp, bp