Sharp logo

MZ-800 course Chapter 2 
2. BASIC-800 ( 1Z016 / 5Z-009 )


This chapter is about the BASIC-800 language. Amongst other things, we will tell you how this BASIC can be used optimally and which instructions you will not find in the manual, but are usable anyway. You will also find small programs that will add new instructions to BASIC. An explanation of the new instruction will be given. How these programs work exactly will be discussed in another chapter. At the end of this chapter you will find some very interesting BASIC- (sub)routines you can use in your own programs. You will also find some documentation about these routines.

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


We all know we only got a limited number of BYTES of free memory to put our program in. With the information we will give, you will be able to make maximum use of your free memory.

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
line number if you do not want to.

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.

Example 1:
10 B=0 can be replaced with:
10 B=π - π:REM pi - pi

Example 2:
10 B=10 :C=10 can be replaced with:
10 B=10 :C=B

Example 3:
10 A=10 :B=15 :C=14 :D=2 :E=90 can be replaced with:
10 DATA 10,15,24,2,90 :READ A,B,C,D,E

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:

Example 1:
100 IF A=10 THEN GOTO 10 can be replaced with:
100 IF A=10 THEN 10

Example 2:
100 IF B=10 THEN GOTO 100 ELSE GOTO 100 can be replaced with:
100 IF B=10 THEN 100 ELSE 100

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
20 IF STICK(0)=5 THEN END

This requires more memory than:
10 S=STICK(0) :IF S=3 THEN END
20 IF S=5 THEN END

Some more memory-efficient solutions:

Example 1:
10 S=0
20 S=S+1 :IF S=50 THEN CLS :END
30 GOTO 20

Requires more memory than:
10 S=0
20 S=S+1 :IF S<50 THEN 20 ELSE CLS :END

Example 2:
Another example with variables.
10 A=10 :B=25 :C=15

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:
10 GOSUB 20 :GOTO 10
20 CURSOR 0,0 :PRINTSIZE :RETURN

The wrong example:
10 GOSUB 20
20 CURSOR 0,0 :PRINTSIZE :GOTO 10

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 <cr>
10
20
30
etc. The line numbers increase with jumps of 10.

AUTO 1000,100 <cr>
1000
1100
120
etc. The lines start at 1000, and increase with jumps of 100.

AUTO 50,1 <cr>
50
51
52
etc. The lines start at 50, and increase with jumps of 1.

Hopefully this has clarified the usage of AUTO. More information can be found in the manual.

Some tips to speed up typing:

  • Instead of PRINT you can use ?. When you ask for the listing, it will display PRINT as usual.
  • You can abbreviate almost every instruction. In APPENDIX C all BASIC instructions with their abbreviations can be found. If you enter an abbreviation and ask for the listing, the original instruction will be displayed.
  • You could redefine a function key for commonly used instructions. Just try this:

    DEFKEY(3)="DATA" <cr> and press the function-key F3.

Previous page
Next page
Contents


Go to the top of this page Home

last updated July 8, 2004
Arjan Habing, Mark de Rover, Jeroen F. J. Laros, sharpmz@sharpmz.org