4 ms·
ORG directives were really just hints. There was no operating system and no linker/loader on Apple II: The developer had to "somehow" manage to tran
by CamperBob2 8y ago
ORG directives were really just hints.
There was no operating system and no
linker/loader on Apple II: The developer
had to "somehow" manage to transfer the
instructions from floppy disc to the
intended location.
Well, no. .ORG directives had nothing to do with any of that. They were (and are) used to tell the assembler how to fix up absolute references such as JMPs, JSRs, and loads/stores to variables defined in the code.
As he points out, when building complex programs on the Apple II, you had to keep track of where to load modules yourself, precisely because there was no metadata in the object file. Nothing prevented you from loading a module compiled at one ORG at a different address... well, nothing except the fact that it would probably crash.
- joezydeco 8y agoYou could write 6502 code to be completely relocatable, but at a cost of space and a more complex assembler.
- tinus_hn 8y agoThe 6502 can only do small +128/-127 bytes relative jumps so that would be pretty complex indeed.
- pvg 8y agoSelf-modifying code was not that unusual and that's what Apple's own DOS did to relocate itself.
- boomlinde 8y agoTypically you'd patch the relocatable routine before inserting it at the desired memory location, simply overwriting the location-dependent instructions like jsr and absolute jmp with new operands. This isn't very complex, especially if the relocator doesn't need to be position independent itself. There are some factors that can simplify relocation: only allow relocation to page boundaries (i.e. $xx00 addresses) and possibly keeping the routines shorter than a page. For example, here's a single page, page boundary constrained relocator for a routine with two patch points: ; x = page relocate_routine stx rel_sta+2 ; patch stx patch1+2 stx patch2+2 ldy #0 copy lda routine_template,y rel_sta: sta $0000,y iny bne copy rts ; some routine assembled for any origin $xx00 routine_template bne continue patch1 jmp done continue ... patch2 jsr print ... done rts print ... rts
- tinus_hn 8y agoIf the relocator is not position independent then the solution is not completely relocateble. I don’t even know how you would get access to the program counter without trashing some memory.
- boomlinde 8y agoNot completely relocatable, but in practice this means that you have some memory reserved at a specific point in memory for the relocator and the template. The relocator can be made position independent by using indirect stores. You never need direct access to the program counter for this, but it gets a bit more complex. You don't need to read the program counter directly for this, but I think the simplest way of doing that is the jsr instruction, which puts the PC (plus some fixed offset) on the stack. The idea is that instead of using a "template" and referring directly to that, you load the relocatable code where you want it, load that location in some zero page vector and in the relocator you do indirect+relative stores based off of that vector: lda page ; the page that the routine was loaded to sta $ff lda #0 sta $fe jsr relocate ; wherever you've loaded the relocator ... ; vector $fe-$ff = routine location, $fe must be 0 relocate lda $ff ; load the lsbyte of the offset of the first patch in the routine into y ; in reality, would be calculated at the point of assembling the routine template ldy #<2+patch1-routine_start sta ($fe),y ldy #<2+patch2-routine_start sta ($fe),y rts As you can see, the relocate routine is now completely position independent. The relocated routine has the same constraints (has to be loaded to the beginning of a page). We use the y register to load the offset within a page and $ff as the page counter. It's also not much more complex to write the relocator for routines spanning multiple pages, but it comes with a bunch more considerations that I think would take much more code to demonstrate (e.g. cross page jumps and calls).
- tinus_hn 8y agoThe problem is that the only way to read the program counter is through jsr or an interrupt and these both can’t do relative jumps. If you start with that information and you allow patching you can of course relocate the code on the fly, but I wouldn’t really call that position independent code generation by a compiler. It’s more like you’re implementing your own linker.
- boomlinde 8y agoI think that you are misinterpreting that statement. The .ORG directive is indeed somewhat of a hint to the assembler (or rather, obviously, a directive) telling it which memory location it should assemble the code for. It is a hint in the sense that it doesn't necessarily reflect the actual location of the code at run-time, because it is separate from linking.