Rather than starting by beating a dead horse, the answer will precede the explanation.
Here are two templates:
1 REM .........1.........2...
......3.........4.........5.....
....6.........7.........8.......
..9.........A....... /\::
2 REM .........1.........2...
......3.........4.........5.....
....6.........7.........8.......
..9.........A.........B.......
/\::
Template #1 provides 112 bytes of machine code space. When more than 112 bytes are needed, template #2 is added. Template #2 provides 122 bytes of machine code space. When still more space is needed, template #2 is copied to line #3 etc., as many times as you like.
Program memory begins at 10509. The following chart shows the memory addresses where the templates are stored. The addresses ***** are especially important.
Your machine code can be POKEd into the text area of any template, with one (but important) restriction.
NOWHERE in your machine code can CHR$(118)=”ENTER” appear! The computer will interpret it as the end of the REM statement, with disastrous results.
FORMAT 1 REM 2 REM 3 REM 4 REM 5 REM ...
MSB LINE # 16509 16627 16755 16883 17011 ...
LSB LINE # 16510 16628 16756 16884 17012 ...
LSB LENGTH-4 16511 16629 16757 16885 17013 ...
MSB LENGTH-4 16512 16630 ***** 16886 ***** ...
CHR$(234)="REM" 16513 16631 16759 16887 17015 ...
START OF TEXT 16514 16632 16760 16888 17016 ...
... ... ... ... ... ...
END OF TEXT 16625 16753 16881 17009 17137 ...
CHR$(118)="ENTER" 16626 16754 16882 17010 17138 ...
TEXT BYTES 112 122 122 122 122
Avoiding CHR$(118) in your machine code is always possible, but here are sneaky ways it can get there without you being aware of it. Either byte of a two byte CALL or JP address might accidentally be CHR$(118). The first sneaky addresses in RAM are: [illegible]6502, 16758, 17014, 17270, etc. by increments of 256 bytes.
Ah hah! The templates hide these under *****, so you do not have to worry about them. Don’t stop worrying yet, though.
There are two more cases which require caution.
The first is another sneaky one, but simple to avoid. Never use JR __,+120 . Use JR __,+121 and add a NOP instead.
The other case is obvious, but harder to fix. LD __,118 and CP 118 cannot be used.
For LD __,118 , use LD __,117 : INC __ . For CP 118 , use CPL : CP 37 : CPL
In brief, never use:
JR __,+120 , LD __,118 , or CP 118 .
The result is perfectly safe machine code which can be edited, added to, and saved to tape. (All right, you experts; you can’t use I/O device (N)=118 , and you better use (HL) when addressing ROM routines.)
HEY! It works! Forget that dead horse.
The wierd doodad at the end of the templates is simply three NOPs followed by JR +6 . It comes in handy when your code runs into the next template.
One last hint–keep a spare copy of both templates.