Copyright (C) 1983
DOCUMENTATION
PART I
Apart from its entertaining value, this program was meant to be an exercise in learning about the actual aspects, functions, and limitations of the Sinclair ZX81 microcomputer and its version of the BASIC language. It also provided a tool for finding if, given the technology of this machine, quality software could be produced and, if indeed so, how would someone do it.
First of all, a word of caution to the reader, about what lies ahead, is in order. Some of the concepts presented in this paper, which pertain to programming habits and techniques, were borrowed from works by other parties. Careful attempts in this work have been taken to clearly credit their ideas to each of them. However, the integration of these ideas into a concise whole, as well as any derivations of new ideas therefrom, it is only this writer who does accept full credit and responsibility for this work.
The game, in itself, is no novelty. There are other more exciting games that can be played. But considering what follows, an investment in this one might be more worthwhile. There are many programs available today that are fun to play, but few share with its owner an explanation about what was learned when they were written.
As a novice I must say that I was oblivious to the most basic aspects of writing a program using this machine: space and time. Both of these elements are usually, but not always, directly proportional. The less available programmable memory used, the less time it should take for a program to either execute or transfer from cassette to the computer. The load time for this program is 4.17 minutes, without any special hardware assistance. It is saved, self-runs, and executes entirely in FAST mode. The actual RAM occupied by the program is 10535 bytes. How this figure was determined reveals a relevant procedure about programming with this machine.
Both the ZX81 and TS1000 User Manual fail to discuss how a given amount of RAM programmable memory can be measured in order to determine the total amount of it available, used, and remaining for programming.
It is well known that a byte is a memory location that, when active, is holding either blank space, numerical or alphanumerical characters. Given, as an example, a machine with 16K of RAM that is turned on and is not exhibiting a program, plus the fact that 1K is equal to 1024 bytes, we would expect that its total available programmable memory is (16 x 1024) 16384 bytes. Yet, the display file constantly occupies 1K of memory, even when it is providing nothing but a blank screen with a cursor (for this piece of information I am grateful to Memotech Corporation, 7550 W. Yale Ave., Suite 200, Denver, Colorado, 80227). Hence, a blank screen actually represents 1024 bytes of memory occupied by character spaces.
If the programmable area of the screen consists of only 23 lines and 32 columns, then there are only (23 x 32) 736 programmable memory locations (i.e., only 736 programmable bytes) available in the display file. The other (1024 – 736) 288 bytes in the display file are found starting with the first line after the cursor line in the work space area and consists of (288/32) 9 lines, each having 32 columns. If, then, we subtract 288 from 16384 we obtain the total available bytes —16096— that can be used before the machine’s entire programmable memory becomes full.
To determine the amount of programmable memory used by a program, the following expression should be placed as the last line of a program and executed with a GOTO from the immediate mode:
9999 Print Peek (16404) + Peek (16405)*256-16597;"____ BYTES"
(In part, for this expression, I am grateful to Ms Toni Baker, Mastering Machine Code On Your ZX81, Reston Publishing Co., Inc., 1981, page 30). The figure 16597 was determined by taking the starting address of the program area (16509) then adding to it the bytes occupied by this expression (66) and 22 more bytes occupied by another expression (described below) that reveals the amount of programmable memory remaining.
In determining how many bytes of programmable memory remain during, or after, a program is being, or was, composed, the following expression should be placed as the first line of a program and executed from the immediate mode by the statement “Print USR 16514”:
1 REM E0RNDLN ACS : FAST AT 5 ▬ GOSUB PI FAST AT TAN
(For this expression I am grateful to Dr. Ian Logan, “Bytes Remaining—8K,” SYNTAX, Vol. 2, No. 12, Dec. 1981, page 5). With this expression I obtained 5559 bytes of memory remaining after finishing my program. My memory used (10535), plus my memory remaining (5559), equals 16094. If, however, we add 2 bytes occupied by the cursor sign, then, we have the exact sum (16096) of the total available programmable memory for a 16K machine that was mentioned earlier.
Another pertinent phenomenon I realized about both time and space, while writing this program, was about how the ZX81 uses its available memory to store and manipulate data and the effect of same on processing time.
My primary concern while writing my program was whether I could fit it all within 16K of RAM. I strived to make my routines as concise and as frugal as possible with respect to the utilization of memory. Consequently, my programming impetus was to make a program that was primarily a space maximizer: “Do all that you have to do, with what you have, in the least amount of space that you can.”
It was the inadvertent pursuit of this maxim and the fact that I happened to be reading Chapter 3 of Mr. Randle Hurley‘s book, The Sinclair ZX81: Programming For Real Applications (The MacMillan Press, Ltd., 1982, 164 pages), that made me become aware of the fact that less space utilization does not always result in less processing time required for a program or routine to execute properly.
It would be redundant (and beyond the scope of this work), to reiterate the fine explication that Mr. Hurley provides in his book, as proof, of why the ZX81 sacrifices processing time at the expense of storing and manipulating more accurate data than, say, its forebear the ZX80. It suffices to say that the ZX81 pays for these new capabilities in terms of more time and less speed during processing.
It should, therefore, be a priority of ZX81/TS1000 programmers, who use BASIC, to accomplish their programming objectives by using instruction-code techniques that minimize memory consumption while simultaneously maximizing (or at least not minimizing) processing speed.
In his book, Mr. Hurley identifies five important techniques that were helpful toward obtaining an optimum memory-cost-to-processing-speed (MC/PS) ratio when I wrote my program. Though I did use most of these techniques in writing my program, I cannot honestly claim that each part of my program represents the most optimal achievement of said ratio. Nonetheless, they were employed enough to make somewhat of a positive effect on processing speed and memory savings.
First, there is an advantage in using numerical variable names versus using numerical constants within code expressions. The savings in memory costs can be as much as 40% while still maintaining a faster processing speed. It is for these reasons that all of my subroutines, within the program, are called via a respective variable name. This technique also helps to easily access a particular routine in the program for purposes of editing or debugging.
Second, one of the most important aspects about my program is that it requires and makes plenty of use of For/Next loops. How these loops are structured can have a significant effect on the MC/PS ratio. How they can be made to optimize this ratio is a matter of determining the instruction-code placed inside this type of loop structure, which will use the least amount of memory to accomplish the necessary accurate manipulation of data with the least amount of loop repetitions. Specific examples illustrating this principle can be found in Mr. Hurley’s book on page 36.
Third, another benefit regarding how to preserve processing speed while saving memory is in how values are calculated with For/Next loops containing different arithmetic functions. For example, values determined by power functions tend to take longer to compute than if either multiplication or addition were used instead (Ibid, page 36).
Fourth, a substantial optimization of the MC/PS ratio results by initializing numerical variable name constants, in the immediate mode, versus listing them within the programming area. For both memory costs and execution time are augmented when these variables unnecessarily occupy space, both in the instruction area and the variable store. If entered via the immediate mode they will still be saved on tape; so, why list them in the instruction area again?
Finally, Mr. Hurley’s book briefly mentions that where a subroutine is located in a program can affect the MC/PS ratio (Ibid, page 37). Routines (that differ only by the size of their line numbers) at the beginning of a program use less memory, but are slower in executing; whereas, those further down the program execute faster but use more memory.
From this observation I deduced that it would be prudent to locate my subroutines, as often as it was practicable, below and as near to the place that they have to be called from in order to achieve an optimal MC/PS ratio. This technique can best be implemented, given a program that is relatively as multi-functional as is mine, by using what I call: “drivers.” (For this term, but not the meaning given to it below, I am grateful to Messrs. Frank L. Friedman/ Elliot B. Koffman, Problem Solving And Structured Programming in Fortram, Addison-Wesley Publishing Co., Inc., 2nd. Edition, 1981, page 299).
A driver is similar to a “main program” (to learn about the concept of a main program, see the Chapter on Subroutines in the User Manual). A driver calls one or more subroutines (or another driver(s)) within an overall program in a certain order, depending upon how its author wants them to be executed. Yet a driver differs from a main program in that is actually a subroutine that may be called by a main program (e.g., see the driver routine GAME in my program, which is called from line 119 and consists of lines 7001-7011).
There should be only one main program within an overall program, but there can be many drivers within an overall program. The unique value of a driver is that it allows a subroutine or another driver, far below in the overall program, to be nearer to the actual place from where it is being called within the overall program. And this, indeed, contributes to a better MC/PS ratio.
The procedural rules derived from structuring (as efficiently as possible) the main program (see lines 112-120), drivers and subroutines (see Declarations below for exact line numbers) within my overall program are, for convenience sake, referred to as “Rules of Top-Down Design,” and can be summarized as follows:
1. A main program is placed immediately after the program’s name and any commands/initializations, if any,that appear at the beginning of the instruction area.
2. A main program begins with either a subroutine or a driver call, and it ends with a GOTO statement.
3. A main program calls either drivers or subroutines; it best calls itself with a GOTO statement.
4. A driver or subroutine are located best when they are below and nearest to where they are being called.
5. A driver or subroutine are located, in an overall program, in the order that it is first called by a main program or another driver.
6. If a driver and main program both call the same driver or subroutine, then, this same driver or subroutine is located in the overall program in the order first called by the main program.
7. A driver calls either one or more drivers or subroutines.
8. A driver or subroutine should contain at least one RETURN statement.
9. A driver or subroutine best calls itself by using a GOTO statement.
10. A subroutine, to be considered as a subroutine, must not contain either a driver or another subroutine.
11. Both a driver and subroutine are best called by a GOSUB statement, unless they each call itself.
12. Always use the smallest line number possible when writing each line of a program.
So far I have been elucidating some of the procedural techniques used in writing and structuring my program. This presentation however, would be incomplete without an explanation of what each particular section (from top to bottom) of the overall program does and why it was written. It is toward this end that I dedicate the rest of this work.