[All numbers in this article refer to the 4K ROM ZX80; for 8K ROM (ZX81/TS1000), see “Machine Code Programs: Where and How to Load Them” in Syntax Quarterly, Winter 1982.–EO]
While you can store machine code subroutines anywhere in your BASIC program, the best place is 1 REM (& 2 REM, if you have two subroutines).
The first address will always be 16427. The first BASIC program address is 16424 and holds 0 if the program line number is less than 256. The second address, 16425, will hold 1 for line 1. 16426 holds the code for REM, 254, and the first subroutine address will thus be 16427.
Since subroutines use non-character code numbers, displaying MC bytes usually makes the ZX80 crash. 1 REM is the easiest line to keep off-screen since it is at the “top” of your program.
If you store your subroutine below other program lines, adding or editing program lines above it will change subroutine addresses.
Here is an easy method for entering MC subroutines:
1. Reserve some storage space for storing your machine code program. Enter “1 REM AAA” etc., including as many A’s as bytes your subroutine requires. Any character will do; the idea is just to reserve addresses.
2. A buffer against listing line 1 is advisable, so enter 2 REM B. (This will not protect against line 1 showing after hitting HOME or LIST, however.)
3. Now, or after entering the input loop, POKE 16403,2 to bump line 1 off-screen. This trick will also work if you accidently LIST and the screen is “skewed” but not “scrambled”. Move the cursor down a few lines first, though.
4. Enter the input loop:
100 FOR X=16457 TO 16457+(xx-1)
110 INPUT Y
120 POKE X,Y
130 PRINT PEEK X,
140 NEXT X
Where xx is the number of bytes in your machine code.
5. GO TO 100 and enter your codes. Entering from a 4-column written table eases input checking.
6. SAVE on tape in case your subroutine crashes.
7. Enter LET Q=USR(16427) to test your subroutine for a crash. (“Q” can be any variable letter.)
8. Enter the rest of your BASIC program.
Next month: CALLING AND RETURNING