Does NASM need org 0x7C00 to assemble boot sectors correctly?
Does NASM need org 0x7C00 to assemble boot sectors correctly?
Is it necessary to write org 0x7C00 in assembly so that NASM calculates addresses starting from 0x7C00?
-
Octocontrabass
- Member

- Posts: 6245
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
If you want NASM to calculate addresses starting from 0x7C00 instead of starting from 0, yes.
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
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.
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
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
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

- Posts: 63
- Joined: Sat May 04, 2024 7:48 am
- Libera.chat IRC: wishedtobe
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
I suggest you
at the very beginning.
Code: Select all
push word 0x7c0
push word start
retf
start:
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
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.wishedtobe wrote: ↑Tue Jul 29, 2025 1:19 am I suggest youat the very beginning.Code: Select all
push word 0x7c0 push word start retf start:
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
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
- wishedtobe
- Member

- Posts: 63
- Joined: Sat May 04, 2024 7:48 am
- Libera.chat IRC: wishedtobe
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
I forgot that. I'm too accustomed to the instruction retf.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.![]()
Re: Does NASM need org 0x7C00 to assemble boot sectors correctly?
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. 
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
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
