BASIC is flexible, but sometimes it is necessary to resort to machine language to carry out some tasks at reasonable speed or to perform tasks which cannot be done in BASIC (such as accessing sideways ROMS). The machine language must be stored in a reserved part of memory, and it helps if the routine is relocatable so that its placement does not interfere with other memory requirements.
Most auxiliary routines are short and any internal branching can be handled by relative jumps in the range of +129 to -126 bytes. Unfortunately we do not have relative CALLs, but the enclosed routine demonstrates how this lack can be overcome. It is CPC-specific, but it is probable thai it could be adapted for the PCW which uses the same processor. The Z80 processor implements eight RESTARTS, which are CALLs to locations in low memory starting at address0000. Each restart location consists of eight bytes which can be used by the operating system to carry out its housekeeping tasks. The CPC O/S does not use RST 6, leaving a convenient fixed location which makes it possible to find the address of the current instruction. Each CALL and its one-byte cousin RST leaves a return address on the stack and normally a corresponding RETurn takes this address off the stack and uses it to resume execution al the by le following the calling instruction. In our case the two bytes at the RST 6 location exchange the address which is pointed to by the Stack Pointer with the contents of the HL register [EX (SP), HL] and the' JP (HL)' , returns to the correct address to resume execution. This is effectively a return with a copy of the Program Counter (PC) in HL, and it allows us to jump to a subroutine, where the first three bytes increment the HL register twice to gel past the two-byte instruction 'JR xx', and again exchange the top of the stack with HL. This leaves the correct address for the next RET as well as restoring the original value in HL. The demonstration converts a two-byte binary number into hexadecimal notation and prints it. The length of the conversion routine docs not really warrant implementing it as a subroutine, but it will serve as an example. Execution time and one byte could be saved by replacing the sequence 'CALL TXT_OUTPUT and RET' by 'JP TXT OUTPUT', but the seemingly missing RET could obscurethe program flow. This implementation of relocatable CALLs using relative jumps is efficient, does not corrupt any registers, and should solve most problems. Relocatable CALLs. and jumps outside the relative range, and buffer pointers, can be implemented by sacrificing two register pairs(HL or IX or IY for the current add rcss, and BC or DE for the necessary increment), but complexity increases. This will be shown in RELOC2. TAU
|