1

I've been looking into OS development, and have been working on a bootsector, trying to get it to print to the screen.

[org 0x7c00]

; Print 'Hello World' to the screen
mov bx, MSG
call print_string

; Hang
jmp $


; Includes
%include "boot_sect_print.asm"


; Global Variables
MSG: db "Hello World", 0


; Padding
times 510-($-$$) db 0
; Magic Number
dw 0xaa55

And here's the print function code:

; Print a string from bx
print_string:
    pusha

; Loop through and print each character
start:
    mov al, [bx]
    cmp al, 0
    je done

    mov ah, 0x0e
    int 0x10

    add bx, 1
    jmp start

done:
    popa
    ret

It's working perfectly when I run it on an emulator (qemu), but when I put the code on a usb and boot my PC from it some strange behavior is occurring. I've confirmed that printing works, because if I move a single character into 'al' then call 'int 0x10' it prints fine on my PC. However, when accessing a string from memory, 'MSG' in this case, rather than loading the correct values from memory, it's loading something different, and thus not printing correctly. I did some testing and it appears to be getting 255 (0xff) from the first byte at 'MSG' rather than 'H'. I don't really know what to do it at this point, because it seems to work on fine on qemu but not on an actual PC.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
mkoz
  • 11
  • 2
  • 3
    Initialize the segment registers. Search this site, there are plenty of examples. – Margaret Bloom Nov 21 '20 at 09:04
  • 2
    Initialize DS with 0 since you are using `ORG 0x7c00` . The start of your bootloader code would be `xor ax, ax` `mov ds, ax` . I have general bootloader tips here that discuss the problems not setting the segment registers: https://stackoverflow.com/a/32705076/3857942 . If you are booting on real hardware using USB and the BIOS s set to USB floppy emulation (FDD) then you could encounter another problem with not having a BIOS Parameter Block (BPB): https://stackoverflow.com/a/47320115/3857942 – Michael Petch Nov 21 '20 at 13:54
  • 1
    Thank you so much, I managed to get it working, and have now bootstrapped into the kernel. The BPB was not an issue but it was interesting to read about. It was the segment registers that were causing all my issues. I thought I understood how the ORG directive and the segments worked in real mode, but I guess I need to do some further reading. I noticed you've written some stuff about the ORG directive Michael, so I'll look through that. Thanks for the help! – mkoz Nov 21 '20 at 18:05
  • Do not add the solution to your question. Instead, post it as an answer. – Cody Gray - on strike Nov 22 '20 at 06:36

0 Answers0