Page 1 of 1

Bootloader works in QEMU but doesn't in real machine[Solved]

Posted: Tue Oct 18, 2016 5:47 am
by AstroThing
Hello everyone, I have searched for an answer to my problem and haven't found anything, I apologize if the answer is somewhere around here in the forums and I couldn't find it.

I have made a bootloader that works perfectly in QEMU (I also tested Bochs and VirtualBox with no problems) but it behaves unexpectedly in any real machine when booting from an USB.

I believe it must be a problem with my segment registers, here's how I am setting them up:

Code: Select all

[ORG 0x7C00]
[BITS 16]

cli
xor ax, ax
mov ds, ax
mov es, ax

mov ax, 0x20
mov ss, ax
mov sp, 4096
mov bp, sp
sti
Here's my whole assembly code:
https://gist.github.com/AstroThing/f844 ... 8b4486d43d

I'm using NASM to generate a flat binary file and copying it to a USB using dd:

Code: Select all

nasm -f bin boot.asm -o boot.bin
dd if=boot.bin bs=512 count=1 of=/dev/sdb
Where could the problem be?

Re: Bootloader works in QEMU but doesn't in real machine

Posted: Tue Oct 18, 2016 6:07 am
by Octocontrabass
AstroThing wrote:it behaves unexpectedly in any real machine when booting from an USB.
The BIOS might be trying to correct your BPB. Since you don't have a BPB, it'll overwrite part of your code instead.

To get reliable behavior from the BIOS, you must either include a valid BPB or include a valid MBR partition table with one active partition.
AstroThing wrote:I believe it must be a problem with my segment registers, here's how I am setting them up:
Is there any particular reason why you aren't setting SS to zero? The easiest way to avoid segmentation issues in real mode is to set all the segment registers to zero.

Re: Bootloader works in QEMU but doesn't in real machine

Posted: Tue Oct 18, 2016 6:50 am
by AstroThing
Octocontrabass wrote:The BIOS might be trying to correct your BPB. Since you don't have a BPB, it'll overwrite part of your code instead.

To get reliable behavior from the BIOS, you must either include a valid BPB or include a valid MBR partition table with one active partition.
That fixed it! I added a valid FAT16 BPB and it worked. :D
Octocontrabass wrote:Is there any particular reason why you aren't setting SS to zero? The easiest way to avoid segmentation issues in real mode is to set all the segment registers to zero.
No reason at all, I just wanted to try to have 4 KB of stack. :P I will take your advice and set it to zero as well.

Thanks for the help!