Deluxe Loader Program
This installment gives you simple loader program that will make it very easy for you to enter machine code in decimal notation. But first, let’s have a look at another “fun” program.
It is assumed by now that you’re familiar with the loader program that we have been using, so only the machine code listing is included, in Figure 6-1. And what does this program do?
You’ll remember, going back to the renumbering program, that you probably would have to change the destinations of all the GOTO and GOSUB instructions, if you wanted the new program to work. Well, this machine code routine will help you, by printing out the numbers of all the lines that include a GOTO or a GOSUB command. Just run the machine code program before you renumber, and you’ll know where to look for potential trouble spots. This machine code routine should be used in conjunction with the BASIC mini-program in Figure 6-2.
Or, you can use this program to search for other one-byte entries, such as REM or LPRINT (and LLIST). This last application can be especially useful if you want to avoid inadvertently sending the computer into an orgy of paper consumption.
16514 42 E LD HL,
16515 161 C (16545)
16516 64 RND
16517 62 Y LD A,
16518 236 GOTO 236
16519 35 7 INC HL
16520 190 D CP(HL)
16521 40 C JR Z,
16522 18 > 18
16523 60 W INC A
16524 190 D CP(HL)
16525 40 C JR Z,
16526 14 : 14
16527 62 Y LD A,
16528 117 ? 117
16529 60 W INC A
16530 190 D CP(HL)
16531 32 4 JR NZ,
16532 240 LIST -16
16533 35 7 INC HL
16534 190 D CP(HL)
16535 200 COS RET Z
16536 70 ? LD B,(HL)
16537 35 7 INC HL
16538 78 ? LD C,(HL)
16539 24 / JR
16540 232 CONT -24
16541 34 6 LD(16545),
16542 161 C HL
16543 64 RND
16544 201 TAN RET
16545 161 C (DATA)
16546 64 RND (DATA)
FIG. 6-1 THE 1 REM STATEMENT
5000 POKE 16545,161
5010 POKE 16546,64
5010 REM THE CONTENTS OF THE ABOVE TWO BYTES WILL CHANGE WHEN THE PROGRAM IS RUN
5020 LET A=USR 16514
5030 IF A>9999 THEN STOP
5040 PRINT A
5050 GOTO 5020
FIG 6-2. THE OPERATING PROGRAM (TO BE USED IN CONJUNCTION WITH THE 1 REM STATEMENT OF FIGURE 6-1)
And now back to lessons. If your memory is severely limited, you can leave out everything before line 100 (except line 1, of course), once you have learned the procedures they describe.
I almost decided to leave out line 109–not to save the few bytes involved, but because having the line in there might encourage bad habits. Its purpose, as you can probably figure out, is to make sure that the machine code routine ends with RETURN, even if you forget to put it in. I strongly recommend that you include the RETURN in the program proper, and regard line 109 as a safety net, to help you only in case of emergency. Don’t worry about the RETURN being in there twice. The computer, obeying the first one, will never even know that the second one is there.
The danger, of course, is that you’ll come to depend on the safety net, and you may find yourself performing some day in another arena (to pursue the metaphor) where they don’t have a net. My decision was to include line 109, and then to write my programs as if it wasn’t there. This is much like the proverbial worrier who wears both a belt and suspenders. You can do as you like on this point.
After you’ve got the loader program working, SAVE it.
Let me say a few words about the SAVE routine. The lines following 9990 are designed to squeeze out the “excess water” from the program before you SAVE it, to economize on tape and time. (This does not help with the eprom upgrade. The screen will not collapse.-ed.) This technique will work on any program, but it’s more impressive on short ones.
Here’s the way it works. Get your tape all ready to go (but don’t start the recorder), then GOTO 9990. The computer will stop at line 9995. Now make sure that the LOAD link between your recorder and the computer is disconnected (to reduce the danger of feedback), start the recorder going in RECORD mode, and enter CONTINUE into the computer (pressing ENTER, of course).
The tape can then be loaded in the usual manner, and the program will identify itself when loading is complete.
If you want to know why this SAVE-saver works, you’ll have to understand that the ZX81 has a parameter called RAMTOP, which is established by bytes 16388/16389. The value at 16389 is tested every time that you clear your screen. If that value is 4Dh or less, then the display file is deleted from memory and is therefore not saved with your program.
Now let’s put in that mini-program that we know so well. Change line 100 to read:
100 LET P$="6.1.14.0.201."
Remember to include the period after each byte, including the last one. Otherwise, the computer won’t know where one byte leaves off and the next one begins.
You can proofread the program before you RUN it. And then, for a final check, when you RUN the program, you’ll see the relevant byte addresses and their contents lined up for a final inspection before you cross over into USR land.
RUN the program, and then PRINT USR 16514, if the program passes muster at proofreading time. You should get 256, as before.
Now, try the other test program. Change line 100 to:
100 LET P$="6.0.14.1.201."
and check the result of PRINT USR 16514 (after proofreading, of course).
Just out of curiosity, try a program with no instructions. Except RETURN, of course. ALWAYS include RETURN. Put in LET P$=”201.” Then RUN and PRINT USR 16514. So now you know what BC does if you don’t give it any instructions. It just takes the value of the starting byte–in this case, 16514.
Now you have seen that the loader not only facilitates proofreading, but it also makes editing easier. Exchanging the values of data bytes 1 and 0 was just a simple matter of editing line 100. No need to POKE, no need to retype the entire program. Correcting a typing error would have been just as easy. This capability really helps when you find that you have inadvertently left out a byte. Under the “hunt and POKE” system, you’d have to locate the error, add another null, and then rePOKE the offending area–and possibly the entire program following the error.
It’s not such a big deal when the entire program consists of 5 or 6 bytes, but you can imagine what it would be like to retype a few dozen or more bytes (and then proofread them again) just to get rid of one typographical error.
FIG 6-3. THE LOADER PROGRAM
1>REM .................
10 PRINT ,,"LIST CODE IN DECIMAL AS:"
15 PRINT "100 LET P$=""6.1.14.0.X.X.X.201."""
20 PRINT ,,"REMEMBER TO PUT ""."" AFTER EACH"
25 PRINT "BYTE, INCLUDING THE LAST ONE"
30 PRINT ,,"MORE CODE CAN BE ADDED AS:"
35 PRINT "101,102, ETC. LET P$=P$+"" ... """
40 PRINT ,,"[illegible] NOW MAKE SURE THAT THE 1 REM"
45 PRINT "LINE CONTAINS AT LEAST AS MANY"
50 PRINT "NULLS AS YOU HAVE BYTES IN THE"
55 PRINT "PROGRAM"
60 PRINT ,," THEN RUN THE PROGRAM"
100 STOP
109 LET P$=P$+"201."
110 CLS
120 FOR F=0 TO LEN P$
130 IF P$="" THEN GO TO 220
140 LET C$=""
150 LET C$=C$+P$(1)
160 LET P$=P$(2 TO )
180 IF CODE P$<>27 THEN GO TO 150
190 POKE 16514+F,VAL C$
200 LET P$=P$(2 TO )
210 NEXT F
220 FOR M=16514 TO 16513+F
230 PRINT M;" ";PEEK M
240 NEXT M
250 STOP
8000 FOR F=26708 TO 26715
8010 PRINT F,PEEK F
8020 NEXT F
8030 STOP
9990 REM SAVE
9991 LET A=PEEK 16388
9992 LET B=PEEK 16389
9993 POKE 16388,PEEK 16400
9994 POKE 16389,PEEK 16401
9995 STOP
9996 SAVE "LD"
9997 POKE 16388,A
9998 POKE 16389,B
9999 PRINT "DECIMAL LOADER"
Any error, however small, is very likely to make the program crash. As I’ve mentioned before, machine code programs are very unforgiving of mistakes! If your program is very long, you can distribute P$ among lines 2 to 108, to bring the number of bytes per line down to manageable size.
And now we’re finally ready to start playing that violin. Well, almost…
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.