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.
nasm - confusion regarding functionality of ORG directive
Re: nasm - confusion regarding functionality of ORG directiv
Hi,
For example:
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:
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
The ORG effects every single instruction that uses an opcode that contains an address; but it's not stored otherwise.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.
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!
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!
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.
-
- Posts: 20
- Joined: Wed Jan 29, 2014 11:57 am
Re: nasm - confusion regarding functionality of ORG directiv
Got it now. Thanks a lot guys !!