Last visit was: Sat Jul 13, 2024 9:10 am
It is currently Sat Jul 13, 2024 9:10 am

 [ 5 posts ] 
 Is the ORG directive a must? 
Author Message

Joined: Sat Jun 16, 2018 2:51 am
Posts: 50
In a system where data and program memory are separate, is the ORG assembler directive needed? It seems using labels would work just fine... For example in program memory all the instructions can be packed together (as there is no need to reserve space for data) and labels can be used to jump between code blocks.

Are there other use cases for ORG apart from memory reservation that I am missing. Maybe special kernel uses?

Thanks for any insight!

Wed Jan 23, 2019 9:44 pm

Joined: Sat Feb 02, 2013 9:40 am
Posts: 2100
Location: Canada
In my opinion it's pretty much a must-have except for trivially simple programs. The assembler or program loader would have to make a ton of assumptions without org directives.

The ORG directive is used to place things at specific memory addresses. For instance, a .com program needs to be placed at address $0100. If software is going to be located in ROM then an ORG directive may be required. The org directive usually works independently for different program segments (code, data, bss, stack, tls, rodata, etc). An org directive can usually be omitted but then a value of zero is assumed for the appropriate counter. You are correct in your case if there are separate code and data areas and code and data are identified separately in software it may be possible to omit the org directive. Note that it’s sometimes desirable to place code in the data area, or data in the core area. Most assemblers expect the programmer to identify the area output to.
Usually there is a way to identify which program segment is being output to with additional directives: (code, data).
The org statement can be useful in data areas for identifying I/O locations. For instance, a video buffer may be identified with an org directive. org directives are also often used to identify vector areas which may be required to be at specific memory locations.
The following example shows the program segment being selected (bss, data or code) followed by an org directive for that segment.
         org      SCRATCHPAD      ; SCRATCHPAD = $FF400000
_inptr            dw      0
_bsptr            dw      0      ; BASIC storage pointer
         org      SCRATCHPAD + $AFF8
__brk_stack      dw      0

      org      SYSDATA         ; SYSDATA = $FFD10000
      code   18 bits
      org      ROMBASE         ; start of ROM memory space

Sometimes the org directive has to be used carefully because the assembler wants to see the orgs increasing in address range. This depends on the assembler. Some better assemblers can re-arrange the code and data automatically so that its in order according to the org statements.

Another use for org is to align code in a assembler that doesn't support the align directive. It might be use to origin code at memory page boundaries or on cache lines for instance.

Robert Finch

Thu Jan 24, 2019 1:22 am WWW

Joined: Tue Dec 11, 2012 8:03 am
Posts: 285
Location: California
I see Rob posted while I was writing. I'll post this anyway.

You usually need to tell the assembler where to start laying down machine code. If it has a default that suits you ok, then I suppose the ORG directive would not be necessary to start.

I use the ORG a ton inside my program flow-control macros for nestable structures in assembly language without labels. I have an article on this at, and longer examples starting about 60% of the way down the page at, and more explanation of how the insides work at . A .lst (list) file output after the assembler runs would show hundreds of ORG occurrences if you have macro expansion turned on.

_________________ lots of 6502 resources

Thu Jan 24, 2019 1:29 am WWW

Joined: Sat Jun 16, 2018 2:51 am
Posts: 50
Ah, I see. Thank you!

Thu Jan 24, 2019 2:05 am

Joined: Thu Dec 05, 2019 7:53 am
Posts: 13
Location: Tokyo, Japan
In any program that's using branches with an absolute target (which is going to be most non-tiny programs on many CPUs, unless you're specifically writing relocatable code) the assembler has to know the address of the target of a non-relative branch. If you don't have an ORG directive, the assembler will have to make an assumption about the start point of the program. That will typically be $0000, so leaving out the ORG directive will be implicitly saying ORG $0000.

Curt J. Sampson -

Sun May 07, 2023 3:06 am
 [ 5 posts ] 

Who is online

Users browsing this forum: CCBot and 0 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software