How do I do a BPB and MBR in x86 assembly?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

Sup guys. I managed to learn a few things with Assembly. I want to run my OS on physical hardware. I know about INT 13h. How do I do a valid MBR or BPB?

By the way, which places in the manuals do I need to read?

Cheers
Timmy
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by SpyderTL »

You need to be more specific when asking questions.

Do you mean "How do I put a valid MBR on a floppy disk, hard disk, CD-ROM and/or USB drive using Windows and/or Linux?"

Or do you mean "How do I build a valid MBR structure that can be put on a disk in NASM/MASM?"
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

I want a sample to put in a bootloader.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by SpyderTL »

You need to be more specific when asking questions.

Do you mean "How do I put a valid MBR on a floppy disk, hard disk, CD-ROM and/or USB drive using Windows and/or Linux?"

Edit: Did you read the wiki? http://wiki.osdev.org/MBR#x86_Examples
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

That's exactly what I mean but is there something simpler? I'm not being lazy.
User avatar
sleephacker
Member
Member
Posts: 97
Joined: Thu Aug 06, 2015 6:41 am
Location: Netherlands

Re: How do I do a BPB and MBR in x86 assembly?

Post by sleephacker »

Timmy100 wrote:How do I do a valid MBR or BPB?
Timmy100 wrote:I want a sample to put in a bootloader.
Timmy100 wrote:That's exactly what I mean but is there something simpler? I'm not being lazy.
You are asking for information that can be found on google (instead of asking a question because you don't understand the information you found), you're essentially asking others to look up something for you. So by my definition of the word you are in fact being very lazy.

Having said that, this short tutorial is about as simple as it gets, from the first page when googling "bootsector x86 assembly" (I know, googling is a tough job, even for non-lazy people). Since you're probably too busy "not being lazy" to read it and find the link yourself: this is the copy&paste-ready code.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by SpyderTL »

Before we can help, you have to answer a few basic questions.

What type of storage device do you want to boot your physical machine from? Floppy disk, CD-ROM, or USB drive?

And, to be clear, you don't technically need a BPB or an MBR. You only need those if you want other OSes like Windows or Linux to be able to read your disk information.

You can just write some code in assembly, compile it, and copy the results to the first block of a floppy drive, and your machine will execute it.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

Can I still read and write on the floppy in my OS using INT 13h?
OK. Thank you. It's just my computer won't boot it without a BPB (I think so).

Sorry, I got mixed up with MBR. I mean code to put in the MBR. By the way, how can I go to a sector (extended or just change 0 to 80) with INT 13h?
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Octocontrabass »

Timmy100 wrote:Can I still read and write on the floppy in my OS using INT 13h?
You can read and write floppy disks using INT 0x13. (You shouldn't use INT 0x13 in your OS, because it's terrible compared to a real driver.)
Timmy100 wrote:It's just my computer won't boot it without a BPB (I think so).
A real floppy disk does not need a BPB. If you are not using a real floppy disk, you might need a BPB.
Timmy100 wrote:By the way, how can I go to a sector (extended or just change 0 to 80) with INT 13h?
I don't understand your question. What does "go to a sector" mean?
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

Read sector.

I'm using an USB for floppy disk emulation. I am planning on switching to hard drive or USB. How do I use DD on Linux for this by the way? How would I put a file on the first sector of USB? Is it the same as floppy (with bs=512 count=1 conv=notrunc)?
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Octocontrabass »

Timmy100 wrote:Read sector.
You can read a sector using INT 0x13 AH=0x02 or INT 0x13 AH=0x42 (but AH=0x42 will not work on real floppy disks and may not work on emulated floppy disks). For more information about AH=0x42, you can also refer to the standard where it is defined.
Timmy100 wrote:I'm using an USB for floppy disk emulation.
USB floppy disk emulation requires a BPB. Without it, you may end up with hard disk emulation, no boot, or a crash from corrupted data.
Timmy100 wrote:I am planning on switching to hard drive or USB.
In that case, why not use USB hard disk emulation (with a partition table) instead? You can generate a valid partition table using diskpart (Windows) or fdisk/parted (Linux).
Timmy100 wrote:Is it the same as floppy (with bs=512 count=1 conv=notrunc)?
Yes.
Timmy100
Posts: 13
Joined: Sun Apr 09, 2017 12:58 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Timmy100 »

I've just tried writing a sector and booting my bootloader (using VirtualBox). However, it just shows blank. Why is this happening? It doesn't print my string or boot kernel from INT 13h (with 80h as hard disk drive). I'm using 0x7C00 memory address if that helps.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I do a BPB and MBR in x86 assembly?

Post by Octocontrabass »

Timmy100 wrote:Why is this happening?
Probably because there's something wrong with your code. I can't give you a better answer than that without more information.
Post Reply