Page 1 of 1

Is there a way to specify memory address for C programming?

Posted: Fri Aug 12, 2022 8:37 am
by Aloha6502
Like the ORG (or *=) in assembly, is there anything similar for C, to specify that from this point on, start compiling opcodes at this address, until it is specified again later?

(I am of course experimenting with C for 65xx)

Re: Is there a way to specify memory address for C programmi

Posted: Fri Aug 12, 2022 9:24 am
by BigEd
If you’re using cc65 then it’s the linker which puts code at addresses - check your machine description or linker control script or command line.

Re: Is there a way to specify memory address for C programmi

Posted: Fri Aug 12, 2022 3:47 pm
by barrym95838
There definitely is a method to bind a hard-coded address to an ISR or an I/O port using a somewhat convoluted cast, but I haven't done so in over 30 years, and the details elude me at the moment (the applicable program listings are stashed somewhere in my attic).

Re: Is there a way to specify memory address for C programmi

Posted: Fri Aug 12, 2022 5:30 pm
by CountChocula
It might be helpful if you could tell us a bit more about what you're trying to do.

One possible solution is to use a trampoline written in a separate assembler file, where you can specify the origin, that simply jumps to the address of your C function; that way, you can just rely on the the linker to figure out where your code should go.

So if you have this code in, say, myfunc.c:

Code: Select all

void __fastcall__ myFunction()
You could write something like this (from memory and untested, but hopefully you get the gist) in myfunc_trampoline.s:

Code: Select all

.proc _myFunctionTramponline:
  .org $C000
  jmp _myFunction
.endproc
The problem, however, is that defining the origin of any piece of code doesn't mean that it will be automatically relocated there when loaded from media; it just makes the compiler generate code that is pinned to a specific location for the purpose of calculating absolute addresses. It is then your responsibility (or your OS's, if it supports dynamic loading) to place it in a particular location in memory.

Hope this helps!