Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 NASM 'org' directive meaning

I am following this tutorial as a first foray into bootloader/OS development for x86 using NASM:

http://joelgompert.com/OS/TableOfContents.htm

And I'm on Lesson 4, which is making my bootloader print the "Hello, world" string. I'm not understanding the meaning of the org instruction (directive?).

As I understand it, org defines where the program being executed is loaded into memory. This is needed when using any sort of labels or relative addresses in the program.

Suppose I have a string defined with a label like this in my program:

szHello db 'Hello, world!', 0

And I then later try to reference that label like this (only relevant snippets):

org 0x7c00
xor ax, ax
mov ds, 0
...
mov si, szHello
lodsb
...
int 0x10 ; Print first character of szHello

My question is, why is that not equivalent to this? :

org 0
mov ds, 0x7c00
...
mov si, szHello
lodsb
...
int 0x10

When I run the first example, my string appears correctly. The second example does not work.

Pointers to relevant documentation would also be greatly appreciated, if the issue is a conceptual problem on my part.

like image 899
tdenniston Avatar asked Nov 15 '11 16:11

tdenniston


People also ask

What does org do in NASM?

The function of the ORG directive is to specify the origin address which NASM will assume the program begins at when it is loaded into memory.

What is ORG directive in assembly language?

* ORG - The origin directive sets the location counter to the value specified. Subsequent statements are assigned memory locations starting with the new location counter value. The location counter is a counter in the assembler program that is used to assign storage addresses for the program.

What does ORG directive do?

The . org directive advances the location counter in the current section to new-location.

What are the different directives in NASM?

NASM's directives come in two types: user-level directives and primitive directives. Typically, each directive has a user-level form and a primitive form. In almost all cases, we recommend that users use the user-level forms of the directives, which are implemented as macros which call the primitive forms.


1 Answers

org defines where the program in question EXPECTS to be loaded into memory. Not where it actually is loaded -- that is controlled by whoever does the loading -- but where it expects to be loaded.

like image 131
Chris Dodd Avatar answered Sep 30 '22 10:09

Chris Dodd