Personal Computer News


Machine Code Tip On The Spectrum

 
Published in Personal Computer News #078

Machine Code Tip On The Spectrum

Readers who program in machine code on the Spectrum may be interested in the following tip. When the USR command is used, both the zero and carry flags are reset to zero and the BC register pair holds the start address of the code to be executed.

The BC register value has an obvious use, that of relocating machine code. It is easy to prove that BC holds the address called by using POKE 25000,201 (i.e. RET) and then PRINT USR 25000. This will print 25000, that is, whatever is in BC is printed.

The reset carry flag can also be useful since it allows the machine code to distinguish between a call to the first byte or a call to the second byte of the program. For example:

LABELOLD SCF
LABELNEW NOP JR C,STARTOLD
STARTNEW etc
  RET
STARTOLD etc
  RET

What will happen is that a call made to the first byte of the above program causes the machine code from STARTOLD to execute, since the carry flag has been set with SCF. When LABELNEW is called, the carry flag is zero on entry and does not become set by the NOP, hence JR,C is ignored and the code from STARTNEW is executed.

This provides a way of cutting down on the number of addresses that need to be remembered. The second routine simply runs from the first address plus one.

T. P. Crispn
Southampton, Hampshire

T. P. Crispn