cj7hawk
Veteran Member
So I'm looking to ask about consensus or even what people think should happen when assembling code for one location, and writing it to another place in memory.
I'm just updating my assembler, and presently I use the ORG to define where the PC should be, and OFFSET that can be added to PC which defines where in memory it gets written.
So far, so good, so if I did,
.ORG $0100
.OFFSET $0100
HERE:
JP HERE
I would see the following code generated for memory location $0200 as
OK, so far, so good I hope - You can see from the above, in Z80, that the code thinks it's at $0100 and it assembles at $0200
ie, the HEX for the output would be:
:03020000C3000135
And the value for HERE would be $0100 and the disassembly would show JMP $0100
However while that's easy to understand, I want to add a third directive, "TARGET" to calculate what the offset should be, without forcing the programmer to use a macro or calculation each time. This is because code that will be relocated doesn't occur at nice segments. It can start anywhere and needs to be relocated to a specific location. So using "TARGET" is easier than using something like "OFFSET $0200-PCORG ; Subtract the current PC from the desired location to assemble to ).
Here's the problem I'm looking for help with.
Do I change OFFSET or the PC (ORG) with the directive?
ORG (Origin) generally means where we're writing in memory, so one line of thinking says to achieve the above, I should do
.ORG $0200
.TARGET $0100
The benefit of this way, is that ORG maintains it's primary meaning, and the target for jumps, would be automatically offset so that code compiled at the currently selected memory location can be relocated to the target location later and will work correctly there.
However ORG is so strongly connected to assembly programming as where the Program Counter is looking to be, it might make more sense to do something like;
.ORG $0100
.TARGET $0200
Ie, Target is now where it should assemble... Org is where the PC is, and target now refers to the pre-relocated memory location - ie, where we want to actually assemble to, while ORG is where it will end up post-copy-and-relocation once it executes.
The internal variables are the same in both cases, and the .HEX file generated would be the same in both cases. IE,
:03020000C3000135
The question then is which way makes more sense to assembler programmers?
Way 1.
.TARGET means where the code will be *relocated to" and follows what the Program Counter would be expected to think.
.ORG means where the code will be initially assembled to, and likely where it will be loaded to from the disk.
or
Way 2.
.ORG means where the code will be *relocated to" and follows what the Program Counter would be expected to think.
.TARGET means where the code will be initially assembled to, and likely where it will be loaded to from the disk.
Thanks for that - looking for opinions, preferences, the way you would naturally think it should work, or how some other assembler handles the same situation.
All input appreciated.
David
I'm just updating my assembler, and presently I use the ORG to define where the PC should be, and OFFSET that can be added to PC which defines where in memory it gets written.
So far, so good, so if I did,
.ORG $0100
.OFFSET $0100
HERE:
JP HERE
I would see the following code generated for memory location $0200 as
0200 HERE:
0200 C30001 JP HERE
0203
OK, so far, so good I hope - You can see from the above, in Z80, that the code thinks it's at $0100 and it assembles at $0200
ie, the HEX for the output would be:
:03020000C3000135
And the value for HERE would be $0100 and the disassembly would show JMP $0100
However while that's easy to understand, I want to add a third directive, "TARGET" to calculate what the offset should be, without forcing the programmer to use a macro or calculation each time. This is because code that will be relocated doesn't occur at nice segments. It can start anywhere and needs to be relocated to a specific location. So using "TARGET" is easier than using something like "OFFSET $0200-PCORG ; Subtract the current PC from the desired location to assemble to ).
Here's the problem I'm looking for help with.
Do I change OFFSET or the PC (ORG) with the directive?
ORG (Origin) generally means where we're writing in memory, so one line of thinking says to achieve the above, I should do
.ORG $0200
.TARGET $0100
The benefit of this way, is that ORG maintains it's primary meaning, and the target for jumps, would be automatically offset so that code compiled at the currently selected memory location can be relocated to the target location later and will work correctly there.
However ORG is so strongly connected to assembly programming as where the Program Counter is looking to be, it might make more sense to do something like;
.ORG $0100
.TARGET $0200
Ie, Target is now where it should assemble... Org is where the PC is, and target now refers to the pre-relocated memory location - ie, where we want to actually assemble to, while ORG is where it will end up post-copy-and-relocation once it executes.
The internal variables are the same in both cases, and the .HEX file generated would be the same in both cases. IE,
:03020000C3000135
The question then is which way makes more sense to assembler programmers?
Way 1.
.TARGET means where the code will be *relocated to" and follows what the Program Counter would be expected to think.
.ORG means where the code will be initially assembled to, and likely where it will be loaded to from the disk.
or
Way 2.
.ORG means where the code will be *relocated to" and follows what the Program Counter would be expected to think.
.TARGET means where the code will be initially assembled to, and likely where it will be loaded to from the disk.
Thanks for that - looking for opinions, preferences, the way you would naturally think it should work, or how some other assembler handles the same situation.
All input appreciated.
David