Cameron Hayne‘s excellent BASIC compiler pretty well solves the need to learn to program in Z80 machine code. Once you have a program tested and working well in BASIC, compilation is a snap. Well! Almost a snap. There are still some challenges and a few problems to be solved and lessons to be learned. Here are some of the tricks I have picked up that you may find useful.
1. If there are any embedded attributes in a line which is being acted upon by OPEN#2,”p” (ie, to direct the normal screen output to the 2040 printer) then all may not work well even though it was OK in BASIC. Take the following example:
1000 PRINT#1;AT 0,0;INVERSE 1;
"Press ENTER to stop the
printer"
In BASIC at the end of the line the attribute INVERSE 1 automatically resets to INVERSE 0. In Timachine compiled code, this did not happen and it was found necessary to add INVERSE 0 at the end of the line. Then the printer copy was correct while before (without the INVERSE 0), the printer copy was in INVERSE characters from that point on.
2. If you use the Larken DOS and you have menus on your disks and if you use POKE 23658,8 to set the input to receive upper case letters, then remember to reset 23658 to 0 before loading a compiled program that expects lower case inputs. Otherwise, the compiled program will hang up.
3. Saving your program variables is easy, too! Look at this example:
When compilation is completed, the printer shows:
M/C: 7218 BYTES
+ 13374 BYTES FOR M/C VARIABLES
(BASIC WAS 6904 BYTES)
SAVE "m/c"CODE 53682,7218
LOAD "m/c"CODE 40308
When the m/c is loaded back to the correct address (40308 here), RANDOMIZE USR ‘address’ to start the program running. This program would now be 20592 bytes long (13374+7218); the 13374 bytes are reserved for the variables where data can be input. When the data is entered, it is then only necessary to save the code as follows:
SAVE "name" CODE 40308,20592
or the equivalent using LDOS.
4. Timachine does not support ON ERR but it can still be used in a loader or menu program which calls the compiled code. Just before the call (say, RANDOMIZE USR 40101), insert ON ERR GO TO 9000 or whatever line you choose and then add the following:
9000 ON ERR RESET
9010 PAUSE 30
9020 ON ERR GO TO 9000
9030 ON ERR CONTINUE
At the program start line, insert an ON ERR RESET and any other likely normalizers (eg, CLOSE#2 if your code was in an OPEN#2,”p” mode).
5. Finally, using the LIST and LINE compiler directives, it is possible to alter some of the code after the BASIC program has been compiled. For example, you find that the line number in a GO TO statement is wrong and you want to change it and you don’t want to recompile the thing.
Providing you had used LINE when compiling your BASIC and as long as you had made a printer copy, you could consult the copy and find the address of the line containing the GO TO statement. Let’s say it is 44117; while you are at it, get the address of the end of that line, say 45321.
Break the start address into its LSB and MSB components (use RANDOMIZE start addr. and PEEK 23670 and 23671). Write these down. Examine the bytes from the start address of the compiled line to the end and look for the LSB and MSB values. To do this, you can use a loop: FOR I=44117 TO 45321: PRINT I, PEEK I: NEXT I. Examine the values; when you have found the addresses containing the LSB and MSB, you can POKE the new LSB and MSB of the address of the revised line number you want after the GO TO.
That may sound complicated, but it really takes longer to explain than to do.
Another example: Supposing after entering a mountain of data into a big array [eg,r$(1000, 13)],your compiled code has a glitch in it and has to be recompiled. What! Enter all that data again! Maybe not. Use REM ! LIST to locate the start address of the program variables. Say it is 47526 and end of the program variable area is at 60899. Using your glitched compiled program save the data from 47526 to 60899; load in your newly compiled BASIC and then load back your data.
Bob Mitchell Nov 1986