1

Recently I have started learning bare-metal assembly programming (NASM) and got myself a headache right in the beginning. After learning basic syntax I tried to write code that will draw something in real mode on screen in text or as graphics. I have found many simple examples of how to do that, but somehow none of them work for me, even if I literally copy-paste it and write it to USB stick with HxD and boot from it on my PC. It usually outputs something that does look like the thing I wanted to draw (pixel, line, square etc.), but with a lot of additional garbage, like random pixels/letters. But the interesting thing is that if I try to load the compiled file to QEMU with qemu-system-x86_64.exe, it works just fine.
Here is a simple code I can show to give you an example of what is going on:

bits    16

mov ah, 00h
mov al, 03h
int 10h

mov ax, 0xB800 ; address of text videomemory for al=3h
mov ds, ax

write_to_memory:           ;remember this line for future
mov dword [80 * 0], 'H I '
mov dword [80 * 1], 'H I '
mov dword [80 * 2], 'H I '
mov dword [80 * 3], 'H I '
mov dword [80 * 4], 'H I '
mov dword [80 * 5], 'H I '
mov dword [80 * 6], 'H I '

end:
jmp end ; Can I suspend execution this way?

times 510 - ($-$$) db 0
; Bootloader magic number
dw 0xaa55

The message should simply draw "HI" a few times on the screen like this (QEMU): enter image description here
But instead this very file (but booted on a real PC) draws just a few letters and some weird symbol:enter image description here
And the more I try to output information, the more gibberish begins to be drawn. For example, I simply made that code loop to write_to_memory instead of the end mark to continuously "update" information in memory. In QEMU it looks exactly as before as it should (yes?), but on a real PC it starts outputting a large number of different symbols on the screen in random places from time to time changing some of them to other symbols.
I made a picture of it too:enter image description here
Note that it is not an issue related to my PC; I tested it on a few different ones.

How can this be so that the code works but so strangely?

Please help me because after spending 3 days on trying to solve this issue to no avail I am completely dead inside...
Any ideas would be appreciated. Thanks in advance.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Haramoyo
  • 35
  • 1
  • 4
  • 5
    Check this out: https://stackoverflow.com/questions/41911606/bootloader-works-in-emulators-but-not-on-real-hardware/41919719#41919719 – ecm May 04 '22 at 20:27
  • 4
    This answer has some additional information about the likely BPB culprit: https://stackoverflow.com/a/47320115/3857942 – Michael Petch May 05 '22 at 03:10

0 Answers0