CODINGLISTINGS ★ RELOCATING Z80 CODE (THE AMSTRAD USER) ★

Relocating Z80 Code, Part 1Relocating Z80 Code, Part 2

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

★ PUBLISHER: The Amstrad User (Australia) (Australia)
★ ANNÉE: 1988
★ AUTHOR: Petr Lukes
 

★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...

Lien(s):
» Coding Src's » Fourier Synthesis (Your Computer)
» Coding Src's » Grafiksequenz - Animationssystem (CPC Amstrad International)
» Coding Src's » Triangles (The Amstrad User)
» Coding Src's » Die SCROLL-Routine von ''Energy Ball'' (Schneider Magazin)
» Coding Src's » Miniorgany (Bajtek)
» Coding Src's » Bouquet (Amstar)
Je participe au site:

» Vous avez remarqué une erreur dans ce texte ?
» Aidez-nous à améliorer cette page : en nous contactant via le forum ou par email.

CPCrulez[Content Management System] v8.7-desktop/c
Page créée en 492 millisecondes et consultée 3377 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.