Adding zip to BASIC. (computer programming; faster BASIC programs)
by Larry Cotton
I get lots of requests for programming tips on ways to use BASIC with many applications, ranging from games to databases. A typical question might be, "How do I write a fast subroutine for doing searches for a given name and address in BASIC?" Another might be, "How do I make the aliens move faster while monitoring the joystick port, keeping score, and moving background scenery?"
The answer to these questions is simple: If you want to do it fast, forget BASIC. Any program can be written in BASIC (assuming it will fit the computer's memory), but you might drop off to sleep waiting for something to happen.
Many articles have been written on maximizing BASIC's speed, and you can get some improvement using these techniques. However, none but the shortest, most elementary database programs should be written in BASIC. Any program that is more sophisticated is best written in some other programming language--preferably machine language (ML). To learn more about those programming techniques, consult Jim Butterfield's "Machine Language" column elsewhere in Gazette.
As for games, some can easily be written in pure BASIC, especially those that don't require blinding speed. Some examples would be wordsearch, spelling, math-drill, and even simulated board games. These types of games don't require much speed, and the user wouldn't notice if the computer slowed a little during execution.
Actually, BASIC and ML can be used together. One way is to use a BASIC program as an ML loader. Then a SYS command puts you into machine language to stay.
The other way is to incorporate a speedy routine within a relatively slow BASIC program. Here's an example of the latter.
Suppose you're writing a pick-a-card-any-card game. You need to shuffle a deck of 52 cards quickly. By generating a nonrepeating list of 52 numbers, you could assign the numbers to an array of all the cards. The following program is one way to generate those numbers in BASIC.
BASIC RND
10 PRINT"{CLR}{DOWN}PRESS
ANY KEY TO RANDOMIZE 52
NUMBERS 20 PRINT"{DOWN} WITH OUT
REPEATS. 30 GETA$:IFA$=""THEN 30 40 PRINTCHR$(147) 50 Q=RND(-TI/101) 60 C=52:DIMRN(C) 70 FORX=1TOC 80 RN(X)=INT (C*RND(1))+1 90 FORT=XTO1STEP-1:
IFRN(X)=RN(T-1) THEN80 100 NEXT 110 PRINTRN(X), 120 NEXT 130 PRINT"{DOWN} I'M SURE
YOU DON'T WANT A REPEAT!
Now, let's try doing the same thing using machine langage. (Don't worry, Jim Butterfield. Your column is safe!)
ML RND
10 Q=RND(-TI/101):
PRINTCHR$(147) 20 FORT=49152TO49221:
READD:POKET,D: NEXT 30 POKE54286,255:
POKE54287,255: POKE54290,
128: REM SET UP VOICE 3 40 CB=49480 50 A=52:REM RANDOMIZES
FROM 1 TO A WITHOUT
REPEATS; MAX. VALUE OF A
IS 255. 60 POKE49222,A 70 PRINT"{DOWN} PRESS ANY
KEY TO RANDOMIZE" A "NUMBERS 80 PRINT"{DOWN} WITH OUT REPEATS. 90 GETA$:IFA$=""THEN90 100 PRINTCHR$(147): SYS49152 110 FORT=CB+1TOCB+A:
PRINT(PEEK(T)),: NEXT 120 PRINT:PRINT:PRINT"AGAIN?
(Y=YES, N=NO) 130 GETA$: IFA$<>"Y" THENIFA
$<> "N"THENEND 150 GOTO100 1000 DATA 172,70,192, 69,0,153,
72,193,136,208,250, 173,
70,192,170,160, 0,153,72 1010 DATA 192,200,240,11,202,
138,208,246,173,70, 192,
170,76,17,192,173,70,192,
141 1020 DATA 71,192,173,27,212,
170,189,72,192,172,70,
192,217,72,193,240,241,
136,208 1030 DATA 248,172,71,192,153,
72,193,206,71,192,208,
227,96
Run both programs and observe the difference in how long it takes to generate 52 nonrepeating numbers. Allow plenty of time in the BASIC version, especially for the last several numbers.
To use embedded ML subroutines in a BASIC program, just SYS to the routine (see line 100 in ML RND). After the numbers are generated, they appear in memory registers 49481 through 49532 (for 52 numbers).
Here's an invitation to you programmers. I'd like to see your own versions of both BASIC and ML no-repeat randomizing programs. Please send them to me in care of COMPUTE's Gazette, 324 West Wendover Avenue, Suite 200, Greensboro, North Carolina 27408. If you keep them small enough to print on one page of the magazine, I'll publish the best examples in a future column.