MZ-800 course Chapter 2
|2. BASIC-800 ( 1Z016 / 5Z-009 )|
There are quite some differences between tape BASIC ( 1Z016 ) and QDBASIC
( 5Z-009 ), that is why we will make a distinction between the two languages
and we will, if necessary, mention that a particular program runs either
under tape BASIC or QD-BASIC. We will sometimes give the alterations
needed for a program to run under the other BASIC version.
|2.1 Using BASIC-800 optimally|
Let us first explain how you can see the number of free BYTES available:
PRINT SIZE <cr>
<cr> means that you must press the CR-key ( do not type <cr> literally ). You will see this many times in this book, so you might as well get used to it now.
When you begin programming, you have to start on a certain line number,
for example: line 10. You can also start at line 10000. You might think
that the number 10000 will use more memory than the number 10 because
there are three more digits in it, this not NOT the case. Both numbers
use the same amount of memory: 2 BYTES. So you do not have to start
at a low
Then we come to the program itself. You want to type something. A program usually starts with a couple of REM lines, with, for example, the name and address of the author in them. Usually the REM instruction itself is not used, but the ( = inverted comma ) sign. In the first place it looks better and secondly it SEEMS to use less memory. This is not the case however. The word REM uses just 1 BYTE, while the ( ) sign uses two. A warning: Do not think each character needs at least one byte of memory, because that is not the case. All BASIC instructions are converted to 1 code and therefore they only need 1 BYTE of memory. Numbers however, take much more memory, but this is discussed later on.
After the REM-lines a number of variables are usually initialized. You can save huge amounts of memory in this part of your program. When you want to initialize a variable, you can do that for example by typing A = 10. What you do not know is that the number 10 uses 6 BYTES of memory. Below are some examples on how to save memory. The first example is always the memory-waisting example. If you do not believe us, just look at your free memory by typing PRINT SIZE each time.
Note: The pi-sign ( π ) you will sometimes see, you can make on your SHARP by pressing SHIFT and 0.
In the third example you can see that the values of the variables can be stored with DATA. If you have a lot of DATA lines, you can also store it in an OBJ-file and load it in high memory. How all of that works, is too complicated to explain in this chapter. However, in the last part of this chapter there are two small programs that show us how to LOAD and SAVE, but that does not have much to do with what is stated here.
Curious people who still want to know how loading from and saving to such an OBJ-file is done and own the program PICTURESHOW, should examine that program very carefully, because that program uses this method.
By the way: you can order the program PICTURESHOW from us, or it will be available very soon. You can order it by calling us ( phone numbers in APPENDIX C ). (Note: will be available for download soon )
That brings us to some things that can occur anywhere in the program. First we discuss the redundant use of the GOTO instruction. Here are two examples:
The first example is still the memory-waisting example. A commonly used memory-waisting reading routine for the keyboard can also be replaced with a shorter and memory-efficient line. An example:
10 A=0 20 CURSOR 0,0:PRINT A:S=STICK(0):IF S=3 AND A<100 THEN A=A+1 30 IF S=7 AND A>0 THEN A=A-1 40 GOTO 20
can be replaced with: 10 A=0 20 CURSOR 0,0:PRINT A:S=STICK(0): A=A+(1 AND S=3 AND A<100)-(1 AND S=7 AND A>0):GOTO 20
Of course you can replace A=0 with A=π-π and CURSOR 0,0 with CURSOR π-π,π-π, but this has been discussed already.
You might have noticed that the example above contains another memory efficient trick. In case you missed it, here is the solution:
10 IF STICK(0)=3 THEN END
This requires more memory than:
Some more memory-efficient solutions:
Requires more memory than:
Requires more memory than:
10 A=10 :B=25 :C=B-A
This way you can multiply, divide, etc. If you use at most three variables this scheme will still be memory-efficient. The last item is this series is the concept of recursive programming. If you understand this concept, you will also understand why each GOSUB should be followed by a RETURN, otherwise we will run out of memory. What is recursive programming exactly?
First something is executed, after which we retrace our steps in the opposite order. You can compare it to a car which drives 100 meters forward and then drives 100 meters backwards, leaving the car in the starting position.
Below we shall point out how to use and how not to use GOSUB.
The correct example:
The wrong example:
Recursive programming This example using recursion will show it is essential to use as many RETURNs as GOSUBs. A little note: The tree of Pythagoras is also built recursively.
10 CLS:A=1 20 GOSUB 30 30 CURSOR 0,0:PRINT"NUMBER OF FREE BYTES :";SIZE:WAIT 50: A=A+1:IF A<51 THEN 20 40 IF A=51 THEN WAIT 2000 50 IF A<101 THEN RETURN
In this program as many Returns are executed as GOSUBs. First you will see the amount of free memory decrease, because the GOSUBs are not answered with a RETURN. Later all Returns are executed and the amount of free memory will increase again.
After discussing memory efficiency, we will continue with a couple of actions which will simplify typing in your programs, which in term will speed up the process of programming.
As mentioned before, each program starts with a certain line number. To save you the work of typing in those line numbers, the AUTO instruction can be used. By using this instruction, the line numbers will appear on your screen automatically. There is one disadvantage using AUTO. The line numbers increase linear. A few AUTO examples:
AUTO 1000,100 <cr>
AUTO 50,1 <cr>
Hopefully this has clarified the usage of AUTO. More information can
be found in the manual.
|Some tips to speed up typing:|