I was trying to write a bootloader in the assembly language to a USB drive, to test it on another computer. The bootloader (should) work with intel processors. The computer I'm testing it on (windows 7 with intel celeron and 2 GB ram) should be running it, it even has an option for booting from a USB drive in the BIOS (and it detects the drive). When I run the boot from the drive, it boots into windows. It should be displaying my custom-OS, which is basically just a Hello-World type assembly program. I am assuming this is because it could not find the operating system: Either the OS-tag (which I found should be "dw 0xAA55") is not correct, the USB drive isn't bootable, or I'm just not putting the bootloader bin file in the first sector.

My questions are: How do I test if (a) the USB drive is capable of booting an OS, (b) if the OS is being recognized, and (c) if the bin file is in the correct sector (the first sector).

Here is the assembly code:

org 7C00h

jmp 0x0:start

mov ax, 0x9000 ;Set up stack
mov ss, ax     ;Tell processor where stack is
mov sp, 0xFB00 ;Set stack offset

mov AH, 0Eh ;Tell bios we're writing a char to screen!
mov AL, 'T' ;Load a new character into memory
int 10h     ;Call BIOS video interrupt

jmp start   ;And so on and so on

times 510-($-$$) db 0 ;Fill rest of sector up with 0s to make this 512B (a sector)
dw 0xAA55             ;Let BIOS know this is an OS! (defines a word)

I am rather new to assembly, so if the comments after some of the lines don't match up to what they should be, then this is probably why. Thanks for your help!


You cannot access MBR from file system, it's a special sector, it's always outside any partitions you create.

To write into actual MBR you need to use dd(under linux) or PartCopy(for windows). I've never used PartCopy, so i cannot vouch for it, but dd worked for me.

dd if=bootsector.bin of=/dev/sda bs=512 count=1

Where sda is the name of your hard drive or usb device and bootsector.bin is a flat binary

Note that "of" option should point to the device itself, and NOT to partition on that device, i.e sda1, sda2

You can compile a flat binary with nasm using -f bin option

Likewise you can test if you bootsector is there by reading from first sector into file:

dd if=/dev/sda of=mbr_contents.bin bs=512 count=1

Open it with some hex editor and see if it's the same as your original, or disassemble it.

You can also clean the bootsector by writing from zero device:

dd if=/dev/zero of=/dev/sda bs=512 count=1

How do you know the OS is being recognized? If it hits your bootsector, it wouldn't try to load another bootsector, even if your code crashed. BIOS just checks for 0xAA55 tag, loads the 512 bytes into memory and runs CPU on it in real flat mode(default), BIOS basically hands over you the control. Whatever happens next will be result of YOUR code running. So if your code crashes, computer will just sit there and waste electricity.

As far as i know all usb drives are capable of storing a bootloader, it's just that some mother boards can be glitchy when trying to load from usb drives. I used a spare hard drive to play around, it seems more reliable.

Bonus trick: you might find it tiring to have to reboot your computer every time, so consider using a virtual machine. VirtualBox is great but there're others. What's useful about virtual box is that you can boot it from real hard drive(or usb stick). You can attach virtual box to real hard drive by creating a proxy virtual file system:

VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk
  -rawdisk /dev/sda

Then you just use the vmdk file as a regular virtual file system and write your bootloader into sda as usual. Very handy for testing!

Source: Virtual Box Manual, Chapter 9, Advanced Topics Section: Access to entire physical hard disk

You could probably write into virtual file system itself, but i haven't found an easy way to do it, those files have unusual formats, so you can't just use dd on them.

Some other useful things to read on custom OS and bootloaders:

OS Dever

OS Developmnet Series by Broken Thorn Entertainment

  • HxDEdit can also be used for editing bootsectors and whatnot.
    – Bigbio2002
    Jan 7 '13 at 3:43

a) Try booting a different OS from a stick. There must be instructions on how to e.g. put Windows on an USB stick. If that worked, you know your machine can do it.

b) If you know that your box boots any OS, you know it boots your OS when it crashes or displays your "hello". You know it didn't recognize the OS when it boots to your regular partition. More info here: http://en.wikipedia.org/wiki/Master_boot_record

c) The first sector is reachable by opening the disk itself. Have a look at this: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx , paragraph "Physical Disks and Volumes"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.