Copying Your Bootsector to a Floppy Disk

Copying Your Bootsector to a Floppy

NOTE: The version of partCopy used in this tutorial uses different parameters than the newer versions.

So you've made a bootsector and now you want to copy it to a floppy. Before we get to that, you need to make sure of 3 things:

  1. Your bootsector is exactly 512 bytes long.
  2. Your bootsector ends with 0xAA55.
  3. Your bootsector is compiled as a flat binary.
To do the first two steps in NASM, simply end your bootsector with this bit of code:

times 512-($-$$)-2 db 0
dw 0AA55h

For the last step, compile(using NASM) with:

nasm yourbootsector.asm -f bin -o bootsec.bin

Now let's get your bootsector onto a floppy.

I'm assuming that your are using Windows. For Windows your need to go to John Fine's webpage and get his program PartCopy. For Linux you will need to use dd(anyone interested in writting a tutorial on dd?). Now, you need to copy your bootsector to the first 512 bytes of the floppy disk. To just test a bootsector(in this example called bootsec.bin) use PartCopy like this:

partcopy bootsec.bin 0 200 -f0

That copies bytes 0-512(PartCopy uses hex, 0x200=512) of bootsec.bin to the first floppy drive(-f0). Note that after -f0 we could put a destination offset, but since the bootsector has to be at the start of the floppy we just leave out a destination offset(PartCopy's default is 0).

If your bootsector has no bugs, the floppy will be bootable. One thing that you will notice however, is that if you try and read the floppy in Windows/DOS/Linux you are told that the floppy isn't formated. This is because of the way in which we just copied our bootsector to the floppy. The way we did it we overwrote some important information.

To not overwrite this information(the "DOS boot record") we need to add some more code to our bootsector. At the start of your bootsector(right after your org 0x7C00) do this:

jmp short begin
times 0x3B db 0

the rest of your code goes here

Now, this time it will take 2 steps to copy your boot sector to a floppy(if you're using the previous, be sure to format it first):

partcopy bootsec.bin 0 3 -f0
partcopy bootsec.bin 3e 1c2 -f0 3e

And there you have it. A bootable floppy disk with your own bootsector on it!

Download an example with full source code.

Written by K.J.


Report issues via Bona Fide feedback.

2001 - 2024 © Bona Fide OS Development | The Goal | Contributors | How To Help |