0

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.

  • As I read this code, it looks like you are reading a sector into address 0:0x7c00 + 512. But that also seems to be (roughly) the address your code is starting at. Is there any chance the read is wiping out your code? What if you change this to `mov bx, 0x7c00 + 2048`? – David Wohlferd Jul 25 '15 at 05:49
  • First, Don't assume register values. Initialize the segments registers you are going to use. Also in calling `int 13/ah=02` you don't set `dl`. Second, and more important, draw a map of the memory along with what you have assigned a particular area to. Third, Realize that you have made a poor choice for the stack area and so you code cannot work. –  Jul 25 '15 at 07:15

1 Answers1

0

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
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • @MartinRosenau **on the stack (which is located in the memory range 0x7F00-0x8000).** Care to explain why you consider the lower bound to be 0x7F00 in stead of just zero? – Sep Roland Aug 09 '15 at 17:24
  • @user3144770: By writing 0x7F00-0x8000 I actually meant: The stack is located in the memory range X-Y while 0x7F00<=X – Martin Rosenau Aug 11 '15 at 05:25
  • One serious flaw in all this is that the OP never set SS. SS:SP should be set. If you want to grow down from beneath the bootloader then setting SS:SP to 0x0000:0x7c00 would be enough. – Michael Petch Aug 28 '19 at 21:16