61

can anyone give me a comprehensive description about ORG directive?
When and why is it used in assembly written applications?

Using Nasm on x86 or AMD64.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
sepisoad
  • 2,201
  • 5
  • 26
  • 37
  • 5
    Hint: There is more than one CPU architecture in wide use, and more than one assembler for at least some of those architectures. If you'd specify which assembler for which machine language, we could be more helpful. – David Thornley Aug 04 '10 at 15:23

7 Answers7

51

ORG is used to set the assembler location counter. This may or may not translate to a load address at link time. It can be used to define absolute addresses, e.g. when defining something like interrupt vectors which may need to be at a fixed address, or it can be used to introduce padding or generate a specific alignment for the following code.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 5
    Note also that the meaning of the `ORG` directive can vary between assemblers. For instance, using it to produce padding as Paul mentions will work on MASM, but [not on NASM](http://developer.apple.com/mac/library/documentation/DeveloperTools/nasm/nasmdo10.html#section-10.1.3). – bcat Aug 05 '10 at 12:40
  • 38
    `ORG' is an abbreviation for "origin". – starblue Aug 05 '10 at 19:13
  • @starblue right, "2.6.15 ORG - Define Origin" from MACRO-80 Assembler manual – roolebo Mar 05 '19 at 15:34
  • This whole time I thought it stood for "organize", as in "organize my code to start at the specified address." – puppydrum64 Nov 23 '22 at 11:26
12

ORG is merely an indication on where to put the next piece of code/data, related to the current segment.

It is of no use to use it for fixed addresses, for the eventual address depends on the segment which is not known at assembly time.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
4
      During the assemble time ORG directive resets the MLC (memory location counter) to the address specified in the ORG directive.

Syntax: ORG note: may be a unsigned absolute value or any symbol or symbol + .

example:- to observe this instruction working you need any assemble listing which uses ORG directive.

location
0000A4 00 89 TAB DC 256AL1(*-TAB)
0001A4 00000194 90 ORG TAB+240
000194 F0F1F2F3F4F5F6F7 91 DC C'1234567'

Here in the above the TAB symbol is assigned to MLC address 0A4. in the next instruction ORG sets the MLC to TAB+240 address location which is x'194' (~ x'A4' + 240 in decimal). basically this set up is setup a table with length 256 and from 240 th location to store some character constants so that I can use it for TR instruction.

  • 1
    If only it worked that way in NASM... :q But nope, in NASM, org can be used only once. One cannot "reset" the MLC that way, which sucks :q It makes writing self-relocating code much harder than it could be. – SasQ Jun 07 '19 at 20:00
  • @SasQ: In NASM's `-f bin` output format you can use multiple sections and control their layout using `start=` and `follows=`, and also set up sections to count so as to support relocated use by using `vstart=` or `vfollows=`. – ecm Jun 07 '22 at 06:36
4

ORG means origin ORG is used for specific addressing in microprocessor and microcontroller programming.

For example:

.org 0000H

This means we want to start our program from the 0000H address.

0

ORG xxxx ORG is not an assembly language instruction; it is an assembler directive instruction. It tells the assembler that the instruction from here onwards should be placed at location starting xxxx

0

ORG (abbr. for ORiGin) is an assembly directive and is not an instruction. It defines where the machine code (translated assembly program) is to place in memory. As for ORG 100H this deals with 80x86 COM program format (COMMAND) which consists of only one segment with a maximum of 64k bytes. Also, It can be used to define absolute addresses, introduce padding, or generate a specific alignment...

MiMFa
  • 981
  • 11
  • 14
-6

its the location in memory where you want the binary program to be loaded to, if any.

I prefer not to use org, and just issue out straight opcode/value to hardware. you can always store values in ax and transfer between bx, cx, dx.

I'm writing my own assembler to dish out opcode/value without having to worry about sending it to memory first before executing,

Its so much faster just to execute opcodes on the spot as they're being read, rather than trying to cache them into memory risking overloading the stack which might burn out your cpu

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Jason
  • 17
  • 1
  • 1
    Its the offset in ram memory you want the program to load to from 0x00000000h. ORG 100h = put my binary code into memory at location 0x00000000h + 100h, or 0x00000100h on 64 bit machines. – Jason May 27 '12 at 02:05
  • 12
    The first sentence is correct. The remainder looks like nonsense to me. Wether you use registers or memory to store values has nothing to do with the use of `org`. And you are aware of the fact that the instruction pointer in the cpu always points to a _memory_ location? And that the cpu reads opcodes always from memory or caches? – Gunther Piez May 29 '12 at 12:53