Code Transfer: ZX81

See all articles from Sinc-Link v9 n6
Author(s): Nick Godwin
Publication: Sinc-Link
Pub Details: Sinc-Link v9 n6 (Volume 9, Issue 6)
Date: November/December 1991
Pages: 25

If you are experimenting with machine-code programming on the ZX-81 the moment will arrive when you want to incorporate a machine-code routine, which you have in one program, into another program. This can present a problem, since machine codes are often long and laborious to enter. The following process, however, enables machine-code to be transferred from one program to another with a minimum of effort, and error-free.

First, RAMTOP must be set at some convenient level. I generally set it at 32000 for this kind of work, which gives 768 bytes to play with, while leaving over 15K for Basic listing, display file, and variables.

The following routine enables RAMtop to be set easily at any desired level:

10 INPUT X
20 LET Y = INT(X/256)
30 LET X = X-256 * Y
40 POKE 16388,X
50 POKE 16389,Y
60 NEW

Run the program, enter the number to set RAMtop, and press NEWLINE (ENTER). The program promptly disappears – Line 60. If you want to check that you have set RAMtop correctly, the following command will print the number you entered:

PRINT PEEK 16388 + 256 * PEEK 16389

Now load the program containing the machine code routine which you want to isolate and transfer. I assume here that the machine code routine is stored in a Rem statement in Line 1, but the program can be easily adapted by adjusting the initial value of J in line 9030. This should be the address just prior to the address at which the machine-code routine commences.

Add the following lines to your listing. I have chosen to number them from from 9000 but that is not important – simply slot them where you can.

9000 FAST
9010 LET RAMTOP = PEEK 16388 + 256
* PEEK 16389
9020 LET X$ = ""
9030 LET J = 16513
9040 LET J = J + 1
9050 IF PEEK J = 118 THEN GOTO 9080
9060 LET X$ = X$ + STR$ PEEK J +
"(inverse space)"
9070 GOTO 9040
9080 FOR J + 1 TO LEN X$
9090 POKE RAMTOP + J, CODE X$(J)
9100 NEXT J
9110 STOP

Run 9000. When that has finished a few seconds later, load the program to which you wish to transfer the machine code routine. Again, I assume that you want to Poke the routine into a Rem statement in Line 1, but this can be changed by altering the initializing value of J at Line 9010 to the address prior to that at which you want the machine code routine to start.

First you must reserve some space in the normal way:

1 REM XXXXXXXXXXXXXXXXXXXXXXX.....

Note that using this routine, it is not critical that you enter the correct number of X’s at the first attempt. If you are short, the program will stop and tell you, so you can add some more. If you enter too many, it is a simple matter to edit them out afterwards.

9000 FAST
9010 LET J = 16513
9020 LET R = PEEK 16388 + 256 *
PEEK 16389
9030 LET X$=""
9040 LET R = R + 1
9050 IF PEEK R = 128 THEN GOTO 9090
9060 IF PEEK R = 0 THEN STOP
9070 LET X$ = X$ + CHR$ PEEK A
9080 GOTO 9040
9090 LET J = J + 1
9095 LET X = VAL X$
9100 IF PEEK J = 118 THEN GOTO 9080
9110 POKE J,X
9120 GOTO 9030
9130 PRINT "INSUFFICIENT SPACE -
EXTEND LINE 1 THEN GOTO 9100"
9140 STOP

RUN 9000, OR GOTO 9000 if you have any data to preserve. When it is done, List; there is your machine-code routines safely lodged in Line 1. If you want to tidy the program, Edit any surplus X’s from the line.

You may be tempted to simplify this program by simply copying the REM statement data as it stands into a literal string and Poking that over RAMtop. However, be warned; that does not always work. Characters that are printed as a ? in the Rem statement are reduced to code 15. Thus although the Line may look identical to the original, it will not, in fact, work as machine code.

This article is authored by Nick Godwin,Eyemouth Berkshire.

From Your Computer magazine June 1982: Retyped by G.F.C.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.