Author Topic: What exactly is the purpose of ORG again?  (Read 1728 times)

Offline ben321

  • Full Member
  • **
  • Posts: 107
What exactly is the purpose of ORG again?
« on: December 30, 2015, 06:19:50 AM »
Why would one really need to set the ORG (origin) on an Intel CPU anyway? Since addresses are relative to each other, when compiling it converts labels into relative offsets from where a JMP or CALL opcode is used. It doesn't matter if my first line of code (when compiled) is at address 0 and the second line of code is at address 4, or if the first line of code is at address 4 and the second line of code is at address 8. In both cases, the relative offset from the first byte of the first line of code to the first byte of the second line of code is still 4.

Offline Max

  • Jr. Member
  • *
  • Posts: 7
  • Country: us
Re: What exactly is the purpose of ORG again?
« Reply #1 on: January 17, 2016, 04:48:19 AM »
Short: Absolute jumps
Long:

Before I begin, I believe the ORG directive is exclusive to NASM's flat binary output.

Since addresses are relative to each other

Addresses are not always relative to each other. Let's look at the JMP instruction (skipping the section about far jumps through call gates, which is essentially the same):

Quote from: Intel
Near and Short Jumps.
When executing a near jump, the ... target operand specifies either an absolute offset (that is an offset from the base of the code segment) or a relative offset (a signed displacement relative to the current value of the instruction pointer in the EIP register)...

Far Jumps in Real-Address or Virtual-8086 Mode.
... Here the target operand specifies an absolute far address either directly with a pointer (ptr16:16 or ptr16:32) or indirectly with a memory location... the far address is loaded directly into the CS and EIP registers...

Far Jumps in Protected Mode.
... [For a] far jump to the same privilege level ... [t]he target operand specifies an absolute far address either directly with a pointer (ptr16:16 or ptr16:32) or indirectly with a memory location... The new code segment selector and its descripter are loaded into the CS register, and the offset from the instruction is loaded into the EIP register.
Intel Corporation (2015, December). Intel 64 and IA32 Architectures Software Developer's Manual, Volume 2: Instruction Set Reference, A-Z, p. 3-459. Retrieved from http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html

If you wanted to make sure your code starts at a specific address, you would want to use the ORG directive. Practical examples include interrupt vectors, DMA drivers, ROM, and bootsectors.