Writing More Efficiently

I confess, I am a ZX81/TS1000 user still. A real survivor. Although I have promised myself to program only (or mainly) on the ZX81/TS1000 in machine language, BASIC still is my main language on this computer. The amazing thing is that there are still things to discover in BASIC programming for the ZX81/TS1000.

The writing of a sorting routine for the ZX81 usually means the bubble sort or at least the flagged bubble sort as given in the book by Morse, et al., “THE ESSENTIAL GUIDE TO TIMEX/SINCLAIR HOME COMPUTERS”, a ZX81 and TS2068 BASIC programmer’s touchstone for comforting solutions. Well, Byte magazine in their April 1991 issue published an improved version of the flagged bubble sort and with a little adaption (from True BASIC to ZX81 BASIC), it was possible to test it and find that it did indeed run faster than the flagged bubble sort. I suspect it is even faster on much bigger arrays than I tested its sorting on.

Another thing that has helped in the saving of RAM in ZX81/TS1000 programs is the use of flags as in the flagged bubble sort, as either 0 or not 0 (that is 1) as the two possibilities, like so:

instead of:

 100 IF FLG=0 THEN GOTO 1000
110 IF FLG=1 THEN GOTO 2000

use:

 100 IF NOT FLG THEN GOTO 1000
110 IF FLG THEN GOTO 2000

These operators can be combined with others in compound conditions as in:

120 If PG AND X>10 THEN GOTO 5000.

Speaking of conditions after IF, another discovery of a year or so ago was that the ZX81/TS1000 can take:

200 IF X=5 THEN IF A=2 OR A=4 THEN GOTO 700

This is equivalent to something that the ZX81 BASIC does not seem to like to accept:

200 IF X=5 AND (A=2 OR A=4) THEN GOTO 700,

One of the problems with the ZX81/TS1000 and most BASIC interpreter other than that of the QL, (SuperBASIC) is that if you put a loop deep in a program, after a lot of other statements, then it executes more slowly. However, a simple POKE and some fiddling with its position in the program listing will allow you to get a GOTO equivalent that is almost as fast as a GOTO when used at the beginning of a ZX81/TS1000 BASIC program.

There is little more to explain than that, but the POKE in effect POKES the 1st byte of the NEXT-LINE ZX81/TS1000 system variable. It is a local GOTO, that can go as far away as about 255 bytes in the program listing only, but since all fast loops can’t be put at the start of your BASIC program, this will help speed up the ones that can’t be given a high enough priority to be put there. Maybe a sort of search routine might fall into that category, or simply a variable array initialization routine. One variable initialization routine that saves RAM is:

instead of:

8800 LET B$="--------------------------------"
8810 LET X$="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
8820 LET Y$="________________________________"

use:

8800 LET B$"--"
8810 LET X$="XX"
8820 LET Y$="__"
8830 FOR N=1 TO 4
8840 LET B$=B$+B$
8850 LET X$=X$+X$
8860 LET Y$=Y$+Y$
8870 NEXT N

Initializing the graphic strings is done by starting them at a value and then doubling their length. This runs infinitely quicker than the following:

8840 LET B$=B$+”-” through a loop 32 times! 0/0.

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