Relocating Z80 binaries is one of those difficulties I’d assumed could only be f...

@jhlagado@defcon.social

Relocating Z80 binaries is one of those difficulties I’d assumed could only be fixed by reassembly or a linker. I’ve been working on bringing CP/M to the TEC-1G and, while researching CP/M executable formats, discovered somewhat belatedly that relocation had a simple and relatively elegant solution.

Digital Research’s PRL, or Page Relocatable, format was used by MP/M and CP/M 3. You can produce it with an ordinary assembler and load the resulting program wherever there’s room on a 256-byte page boundary.

A normal CP/M COM program loads at 0100h. Its calls, jumps and data references contain absolute addresses, so copying it elsewhere leaves those references pointing to the old location. The Z80 has no segment registers to shift the address space along with the code.

PRL takes advantage of a useful restriction: moving by multiples of 256 leaves each internal address’s low byte unchanged. Only the high byte needs adjusting. A routine at 0109h becomes 8009h when its program moves from 0100h to 8000h. Add 7Fh to the high byte and leave 09h alone.

To identify those bytes, assemble the source at 0000h and again at 0100h, then compare the images. Ordinary internal address high bytes increase by one. Fixed addresses such as CP/M’s BDOS entry at 0005h stay unchanged. A compact bitmap marks the adjustments, so this route needs no separate linker. Calculated address expressions need care because they may require more than that simple adjustment.

The loader patches the marked bytes and discards the relocation information. For TEC-OS, that means loading resident programs into available space below the BDOS without rebuilding each for its eventual address. The relocation loop takes just 26 bytes of Z80 code.

I think this deserves to be much better known among Z80 programmers. I’d have saved myself some work if I’d heard this news from the early 1980s earlier.

semantic-scroll.com/content/20

#Z80 #CPM #RetroComputing