Sharp logo

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


2.4 Convenient BASIC-(sub)routines

The last part of this chapter will consist of some convenient and sometimes even funny (sub)routines. Some programs will be explained in more detail, so you will understand the functionality of the program and how to use it best.

5 REM Sort of chase
10 INIT "CRT:M1"
20 XP=0:YP=0
30 A=INT(RND*40):B=INT(RND*24)
40 CURSOR A,B:PRINT "O"
50 CURSOR XP,YP:PRINT CHR$(104);
60 IF XP=A AND YP=B THEN 120
70 WAIT 150
80 CURSOR XP, YP:PRINT " ";
90 IF XP<A THEN XP=XP+1
100 IF YP<B THEN YP=YP+1
110 GOTO 50
120 CURSOR 0,0:PRINT "GOTCHA~"
130 GET A$:IF A$="" THEN 130 ELSE 10

What is happening in this program probably looks familiar. In the well known game PAC-MAN this routine is used, there, the main character is chased by a ghost. In this program nothing is chased, but if the target would move, this is exactly what would happen.

Very little can be said about this program. In lines 90 and 100 the character is sent to its target. If the target would move, two lines must be added in case the character is beneath or at the right of the target. This is a nice exercise for the reader.

5 REM Display the time continuously
10 INIT "CRT:M1"
20 INPUT "WHAT IS THE CURRENT TIME? (hhmmss)";A$
30 IF LEN(A$)<>6 THEN 10
40 TI$=A$
50 CLS
60 CONSOLE 1,24
70 CURSOR 29,0:PRINT "TIME: ";TI$
80 CURSOR INT(RND*39),24:PRINT "*"
90 GOTO 70

Everything in this program actually revolves around the CONSOLE instruction. Before this instruction is discussed, you must know something. The screen consists of 25 rows. The first row corresponds with the number 0 and the last one with 24. This is where the CONSOLE command is based on. The first number following the CONSOLE instruction denotes the first row to which the instruction applies and the second number denotes to the number of rows that will be affected. The CONSOLE command splits off a certain number of rows on which a couple of instructions no longer apply. The area outside the selection is only affected by a few instructions.

Here a a couple of examples:
CONSOLE 1,24 - Row 1 through 24 are affected by all instructions.
CONSOLE 5,10 - Row 5 through 14 are affected by all instructions etc.

For those who still do not fully understand this instruction: Look inside the manual, a good explanation is given for all instructions; as for the CONSOLE instruction.

1 REM Funny way of putting text on the screen.
2 DATA 46,3,46,5,45,5,47,3,47,5,45,3
3 INIT "CRT:M1"
4 PAL 3,14:PAL 1,5
5 FOR A=1 TO 6:READ X,Y:SYMBOL X,Y,"NEPTUNES",3,3:NEXT A
6 SYMBOL [2]46,4,"NEPTUNES",3,3
7 FOR A=0 TO 7:LINE [1]46+A*14,40+A*3,237-A*14,30+A*3:NEXT A
8 GET A$:IF A$="" THEN 8 ELSE CLS:END

This program needs no further explanation, it is just a funny little program.

5 REM Usage of REM-lines.
10 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
20 ’ INSTRUCTIONS ’
30 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
31 ’ This program shows how to ’
32 ’ show instructions in REM-lines ’
33 ’ in the program without showing ’
34 ’ the line-numbers. ’
35 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
50 INIT "CRT:M1"
60 PAL 3,0
70 LIST 10-35
80 BOX 10,10,0,20,199,0
90 PAL 3,15
100 GET A$:IF A$="" THEN 100 ELSE CLS:END

The documentation of this little program is in the program itself, so we will not go into it any further. The program is not very hard to understand.

5 REM BASIC score-routine
10 INIT "CRT:M1"
20 BOX 135,85,185,115
30 CURSOR 17,11:PRINT "SCORE: "
40 SYMBOL 70,130,"SCORE: ",2,2
50 S$="00000":SC=0
60 SYMBOL [0]165,130,S$,2,2
70 S$=STR$(SC):S$=MID$((S$),1,6-LEN(SC$))+SC$
80 CURSOR 17,13:PRINT S$
90 SYMBOL 165,130,S$,2,2
100 WAIT 250
110 SC=SC+10:GOTO 60

In this program the score is kept in a rather complicated way. How it is done exactly is hard to explain, that is why the listing will have to suffice.

10 REM Letter by letter
20 DIM A$(2)
30 A$(1)="This is an example. "
40 A$(2)="(C) by NEPTUNES SOFTWARE."
50 INIT "CTR:M1"
60 FOR A=1 TO 2:FOR B=1 TO LEN(A$(A))
70 CURSOR B-1,A,1:PRINT [2]CHR$(200)
80 WAIT 100:BEEP
90 CURSOR B-1,A-1:PRINT MID$(A$(A),B,1):NEXT:NEXT

This program needs no further explanation.

5 REM Sort
10 INIT "CRT:M1"
20 DIM A$(10)
30 PRINT "ENTER 10 NAMES. (IN CAPITALS)"
40 FOR T=1 TO 10:INPUT A$(T)
50 NEXT T
60 CLS
70 PRINT "THE PROGRAM IS SORTING THE DATA."
80 FOR E-1 TO 10:FOR D=1 TO 10
90 IF A$(E)<A$(D) THEN GOSUB "SORT"
100 NEXT D,E
110 CLS
120 PRINT "THESE ARE THE SORTED NAMES.":PRINT
130 FOR T=1 TO 10:PRINT A$(T):NEXT T
140 GET B$:IF B$="" THEN 140 ELSE CLS:END
150 LABEL "SORT"
160 U$=A$(E):A$(E)=A$(D):A$(D)=U$
170 RETURN

Sorting data is commonly used in programs, that is why we discuss it here. First a short explanation about the operation of the program is in order.

Why only capitals, you might wonder. People who know the character set of the SHARP will know that the capitals are neatly in alphabetical order. This is not the case with the small letters, those are scattered all over the place.

We sort the list according to the character set. The letter that occurs first in the character set will come first. If the letter ’a’ is further on in the character set than the letter ’e’, then ’e’ will precede ’a’.

By using a small piece of machine code, we can change all this, but we are not going to do that just yet.

Sorting data is used in all sorts of files and similar programs. These programs usually do not sort only by name, but also by address, zip code, place name and so on. Those programs will have a more difficult sorting scheme than the one described above. With this program you can get started, but if you want to sort a big file this program is not suitable, you should experiment more with sorting before you can do things like that.

5 REM Saving data.
10 INIT "CRT:M1"
20 DIM A(10)
30 FOR B=1 TO 10
40 PRINT "NUMBER ";B:INPUT A(B)
50 NEXT B
60 PRINT "PRESS REC/PLAY"
70 INP @$D2,A:IF A<>152 THEN 70
80 INIT "CRT:M1"
90 DEFAULT "CMT:"
100 WOPEN #2,"10 NUMBERS"
110 FOR B=1 TO 10
120 PRINT #2,A(B)
130 NEXT B
140 CLOSE #2
150 END

This program writes 10 numbers to a tape. Writing to tape is done with the instruction PRINT #2. This way you can write all sorts of data to tape, even names and things like that. You can also write to other devices, like QD, FD, RS232 and so on. The manual is very clear on this subject.

WOPEN stands for WRITE OPEN, in other words, make sure we can write data. This instruction ( always ) proceeds the PRINT instruction. At the end of the program do not forget to close with CLOSE. Of course you would like to know how to read the written data again. Here is the code to do it:

5 REM Loading data.
10 INIT "CRT:M1"
20 DIM A(10)
30 PRINT "PRESS PLAY"
40 INP @$D2,A:IF A<>152 THEN 40
50 INIT "CRT:M1"
60 DEFAULT "CMT:"
70 ROPEN #2,"10 NUMBERS"
80 FOR B=1 TO 10
90 INPUT #2,A(B)
100 NEXT B
110 CLOSE #2
120 FOR B=1 TO 10:PRINT "NUMBER";B;": ";A(B)
130 NEXT B
140 END

Of course you first have to rewind your tape before running the program.

As you see, this program has much in common with the previous program. The main difference is that instead of WOPEN #2 it says ROPEN #2 and it says INPUT instead of PRINT. ROPEN stands for READ OPEN, in other words, make it possible to read data. INPUT normally stands for input and PRINT for output, this is no exception. So it is all pretty logical. Do not forget to close with CLOSE #2.

There are some more instructions that can be used like EOF and the like, but these instructions are not really relevant at this point. For the time being the two programs above should be more than sufficient. The other instructions are almost never used.

5 REM Tables.
10 INIT "CRT:M1"
20 INPUT "Which table?? ";T
30 FOR A=1 TO 10
40 PRINT USING "##";A;
50 PRINT " x";
60 PRINT T;" = ";
70 PRINT USING "###";A*T
80 NEXT
90 WAIT 3000
100 GOTO 10
110 ’Just try what happens
120 ’if you remove or add
130 ’a # on lines 40 and 70
140 ’when you do the table
150 ’of 100 or 555!!!
10 REM Reversed
50 INIT "CRT:M1"
60 LIST
70 FOR A=0 TO 24:A$="" :DIM B$(40)
80 FOR T=0 TO 39:B$(40-T)=CHR$(PEEK($2000+A*40+T)):NEXT T
90 FOR T=1 TO 40:A$=A$+B$(T):NEXT T
100 BOX [0]0,A*8,319,A*8+7,0
110 SYMBOL 0,A*8,A$,1,1
120 NEXT A
130 GET P$:IF P$="" THEN 130
140 END

As a closure of this chapter these where a few small routines that probably do not need any further explanation.

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