nasm - confusion regarding functionality of ORG directive

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.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: nasm - confusion regarding functionality of ORG directiv

Post by iansjack »

No, I don't think you do understand the functionality.

The ORG directive is an instruction to the assembler, not the program loader. It tells the assembler that the program will be loaded at a particular location but it's up to the loader to ensure that is done. For example, DOS would load .com files at CS:100h, so an "ORG 100h" instruction was necessary to assemble a .com binary.

You know what address you are going to load your binary to; use the appropriate ORG directive to tell the assembler, else it doesn't know.

Any instruction that refers to an absolute address will be affected by the directive.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: nasm - confusion regarding functionality of ORG directiv

Post by Brendan »

Hi,
shahsunny712 wrote:Just one question now:
Let's say I compile my .asm file and generate a .bin file. I have given ORG 0x500 in my asm.
How is this value stored in the .bin to indicate that 0x500 should be used for the starting address ? I'm not aware of any instruction for this.
The ORG effects every single instruction that uses an opcode that contains an address; but it's not stored otherwise.

For example:

Code: Select all

    org 0x12345
foo:
    mov eax,foo    ;This will be encoded as "mov eax,0x12345" because of the ORG
    jmp eax        ;You'd better hope that you got the ORG right!
Of course "ORG" itself is just shorthand. Consider this code, which has no need for ORG at all and uses NASM's "multiple section support for flat binary" feature instead:

Code: Select all

    SECTION .text progbits start=0x12345
    SECTION .data progbits follows=.text
    SECTION .bss nobits follows=.data

foo:
    mov eax,foo    ;This will be encoded as "mov eax,0x12345" because of the .text section's start address
    jmp eax        ;You'd better hope that you got the  .text section's start address right!
This is effectively the same as the version that does use ORG (except it's more typing, and far more powerful).

You'll also notice that this is very similar to what you might see in a simple linker script. This should give you a clear idea of what ORG does - it sets the virtual address of the .text section (which effects all instructions that use addresses of anything in the .text section), which in turn effects the virtual address of all sections that follow the .text section (which effects all instructions that use addresses of anything in sections that follow the .text section).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
shahsunny712
Posts: 20
Joined: Wed Jan 29, 2014 11:57 am

Re: nasm - confusion regarding functionality of ORG directiv

Post by shahsunny712 »

Got it now. Thanks a lot guys !! :)
Post Reply