Does NASM need org 0x7C00 to assemble boot sectors correctly?

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
8 posts • Page 1 of 1
karim
Posts: 20
Joined: Mon Jun 23, 2025 8:03 pm

Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by karim »

Is it necessary to write org 0x7C00 in assembly so that NASM calculates addresses starting from 0x7C00?
Octocontrabass
Member
Member
Posts: 5968
Joined: Mon Mar 25, 2013 7:01 pm

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by Octocontrabass »

If you want NASM to calculate addresses starting from 0x7C00 instead of starting from 0, yes.
CeriseSky
Posts: 6
Joined: Sun Jun 15, 2025 10:54 am

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by CeriseSky »

Yes. Say you have a label "msg" that stores some text. You'll need to access msg directly to retrieve its data; without the org statement, nasm would use 0x80 (for example) as msg's actual address, but the address it will actually be in will be 0x7c80 because that's where the bios put it
If I see one more magic number in wiki code then I'm going to lose it.
BenLunt
Member
Member
Posts: 985
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by BenLunt »

Must you use org 0x7C00? No. Should you use org 0x7C00? Probably.

I believe the org 0x7C00 is only half the equation and here's why.

Giving a value using org, only defines the offset part of the equation. It isn't going to make any difference what offset you use if you don't also set the segment portion as well.

For example, if the BIOS set DS to 0x07C0 and you use an org of 0x7C00, you aren't going to get the address you think you are. When you think about what value you will use with the org line, you must also think about what matching value you must place in the desired segment register.

Using a value of 0x07C0 in DS, you then most surely should use a value of 0x0000 in the org line. However, since many segment:offset combinations can point to the same physical address, you can use many of these combinations to get the same result.

Can I set DS to 0x0123 and use an org line value of 0x69D0? Absolutely. (0x0123 * 16) + 0x69D0 = 0x7C00.

The usual question comes to light when the CS segment register is in question. Must I use an org of 0x7C00? Again, the answer is no. The BIOS has placed you at physical address 0x7C00. As long as you use a CS and org combination to point to that same address, it doesn't matter what that combination is. Therefore, as long as you use all relative branching (jmps, jccs, and calls), don't use a cs: override, or use a few other rarely used techniques, you really don't have to worry what the CS:IP register set is. If the BIOS didn't set them to a valid value, your boot code wouldn't be executed anyway.

There are two most used techniques. The first, is to set DS and ES to 0x07C0 and use an org of 0x0000. The second is to set DS and ES to 0x0000 and use an org of 0x7C00. Either way, you must not assume a value in the DS or ES register, and must set them to one or the other value when using one of these two techniques.

To further this, a normal technique is to set SS to 0x0000 and SP to 0x7C00 so that the first push to the stack will be just below the boot sector.

That's just my 2 cents.

-- Ben
https://www.fysnet.net/osdesign_book_series.htm
wishedtobe
Member
Member
Posts: 60
Joined: Sat May 04, 2024 7:48 am
Libera.chat IRC: wishedtobe

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by wishedtobe »

I suggest you

Code: Select all

push word 0x7c0
push word start
retf
start:
at the very beginning.
eekee
Member
Member
Posts: 960
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by eekee »

[引用]
wishedtobe wrote: Tue Jul 29, 2025 1:19 am I suggest you

Code: Select all

push word 0x7c0
push word start
retf
start:
at the very beginning.
I'm not very practiced in x86 asm, but isn't this just a longer, unclear version of JMP 0x07c0:0000? Also, it makes the assumption that SS:SP points to a region of RAM which doesn't conflict with anything. I've heard many times that you should never do this, always set SS:SP first. ;)
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
wishedtobe
Member
Member
Posts: 60
Joined: Sat May 04, 2024 7:48 am
Libera.chat IRC: wishedtobe

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by wishedtobe »

[引用]
eekee wrote: Tue Jul 29, 2025 2:34 am I'm not very practiced in x86 asm, but isn't this just a longer, unclear version of JMP 0x07c0:0000? Also, it makes the assumption that SS:SP points to a region of RAM which doesn't conflict with anything. I've heard many times that you should never do this, always set SS:SP first. ;)
I forgot that. I'm too accustomed to the instruction retf.
eekee
Member
Member
Posts: 960
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?

Post by eekee »

Programmed return is powerful. Some Forth code uses it for weird and wonderful tricks as the stack is accessible in high level code. It's also a neat way to start a new process. And there's a lot to be said for not bothering to remember every x86 instruction. :D
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
Post Reply

8 posts • Page 1 of 1

Return to "OS Development"