AnyCPU
http://anycpu.org/forum/

Is the ORG directive a must?
http://anycpu.org/forum/viewtopic.php?f=7&t=566
Page 1 of 1

Author:  quadrant [ Wed Jan 23, 2019 9:44 pm ]
Post subject:  Is the ORG directive a must?

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!

Author:  robfinch [ Thu Jan 24, 2019 1:22 am ]
Post subject:  Re: Is the ORG directive a must?

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.
Code:
         bss
         org      SCRATCHPAD      ; SCRATCHPAD = $FF400000
_inptr            dw      0
_bsptr            dw      0      ; BASIC storage pointer
         org      SCRATCHPAD + $AFF8
__brk_stack      dw      0

      data
      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.

Author:  Garth [ Thu Jan 24, 2019 1:29 am ]
Post subject:  Re: Is the ORG directive a must?

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 http://wilsonminesco.com/StructureMacros/, and longer examples starting about 60% of the way down the page at http://wilsonminesco.com/multitask/index.html, and more explanation of how the insides work at http://wilsonminesco.com/stacks/pgmstruc.html . A .lst (list) file output after the assembler runs would show hundreds of ORG occurrences if you have macro expansion turned on.

Author:  quadrant [ Thu Jan 24, 2019 2:05 am ]
Post subject:  Re: Is the ORG directive a must?

Ah, I see. Thank you!

Author:  cjs [ Sun May 07, 2023 3:06 am ]
Post subject:  Re: Is the ORG directive a must?

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/