×

Search this site

For your Information. When using the search box on this page you will be directed to an external search engine (freefind.com) which may contain adverts not affiliated to MisterSpectrum.com. If you do not wish to continue please click on the 'X' in the right hand corner of this pop-up.



A selection of routines for the ZX80 which simulate ZX Spectrum keywords


ZX80 BASIC





Overview

A suite of routines for converting ZX Spectrum statements to work on the ZX80.


FOR..NEXT loop with STEP

A few examples of simulating a ZX Spectrum FOR..NEXT loop with a STEP on a ZX80. The STEP can handle negative values.

10 LET START=42
Where the loop counter starts. A nice round 42.
20 LET END=-20
Where the loop counter ends. How about -20.
30 LET STEP=-2
Now the STEP. -2 will step the counter backwards by 2 each cycle of the loop.
40 FOR F=START TO END
FOR loop starts here.
[section to be repeated goes here]
Your code goes there.
60 LET F=F+STEP-1
STEP less 1 is added to the loop count (F).
70 IF STEP<1 THEN IF ABS(F)>END THEN GO TO 100
If loop is stepping backwards check if we have reached the end and exit FOR..NEXT loop is necessary. This line is not required when stepping forward.
80 NEXT F
Continue loop.
100 STOP
Loop complete.


The above code can be reduced when used within a program. The START, END and STEP variables can be replaced with constants. An example in pseudo-code follows, again if the step value is positive the line beginning IF α<1 may be removed.


FOR F=m TO n
FOR loop starts here. F is the loop counter. m is the start. n is the end.
[section to be repeated goes here]
[section to be repeated goes here]
LET F=F+α-1
α is the STEP value
IF α<1 THEN IF ABS(m)>n THEN GO TO exit_loop
The same values of α, m and n follow the IF statements. exit_loop will be a line number after NEXT.
NEXT F
Continue loop.
exit_loop STOP
Loop complete.


BIN

This short routine will take a string (B$) of binary digits and convert it to a decimal numeric variable (BIN). Only zeros and ones should be used in B$ as there is no error checking.

10 INPUT B$
Entry point for B$.
20 LET BIN=0
BIN variable stores working out for digits we have passed from B$.
30 IF B$="" THEN GO TO 100
If B$ is null then we have no more binary digits left.
40 LET BIN=2*(BIN+CODE(B$)-28)
Add the next digit (adjusted for the ZX80's character set) to BIN then multiply by two to move up one binary place.
50 LET B$=TL$(B$)
Discard the character we have just converted.
60 GO TO 30
Repeat.
100 LET BIN=BIN/2
The last calculation has moved the calculation one binary place to many to the left. This division will adjust the result.
110 PRINT BIN
Print result (BIN).


LEN

This routine will simulate the LEN function. It requires an extra string variable (b$) as a copy the original string (a$). The length of the string a$ is returned in variable c.
10 INPUT A$
Entry point for A$
20 LET B$=A$
Make a copy of A$
30 LET C=0
Set up C as a count variable
40 IF B$="" THEN GO TO 100
If B$ and hence A$ is null then exit routine
50 LET B$=TL$(B$)
Discard first character
60 LET C=C+1
Add 1 to count
70 GO TO 40
Loop round until we have no characters left
100 PRINT A$;" IS ";C;" CHARACTERS LONG."
Length of A$ is now C and A$ is left intact


SGN

A very simple one line routine to replicate the SGN function (signum) of the ZX Spectrum. X is the variable to be tested. Then result is passed to the variable SGN.

LET SGN=(-1 AND X<0)+(1 AND X>0)
At least one side of this equation will always be zero. When X is true the number to the left of AND is the result. When X is false the result is 0. Result is passed to the variable SGN.


SQR

This is a simple brute force method of finding a square root. It works by adding one to a count (SQR) squaring it, then comparing it with the value we wish to find to root of, in this case X. When the difference between the to is zero then the SQR variable is the root. When the difference is less than zero the root lies between two integers.

10 INPUT X
X is then number we want to find the square root of.
20 LET SQR=0
The SQR variable is where the working out is held. Ultimately this will be the result.
30 LET D=X-(SQR**2)
Square the number we have so far and subtract from our original number. D is the difference.
40 IF NOT (D>0) THEN GO TO 100
If D≤0 (the difference between X and SQR**2) then we have found a root so exit loop.
50 LET SQR=SQR+1
Keep adding to SQR.
60 GO TO 30
Repeat.
100 IF D=0 THEN PRINT "SQUARE ROOT IS ";SQR
Show exact result.
110 IF D<0 THEN PRINT "ROOT LIES BETWEEN ";SQR-1;" AND ";SQR
Since ZX80 is integer only we cannot show decimal places. This is the best we can do.


TAB

This subroutine will move the PRINT cursor to a column specified by the variable I. To use set I to the column desired with LET I=n. Call the subroutine either with GO SUB 1040 to print at least one space or with GO SUB 1050. On return use PRINT α$ where α$ is any character or string to be printed.
This subroutine was originally first printed in the ZX80 Operating Manual.
1040 PRINT " ";
Print one space without performing a newline.
1050 IF I+PEEK(16420)=33 THEN RETURN
We've printed enough spaces so return.
1060 IF I = 0 THEN IF PEEK(16420)<2 THEN RETURN
We're at the end of the line so return
1070 GO TO 1040
Go and print another space.


VAL

A simple VAL routine to pass a string of numbers to a numeric variable. This routine will not work with negative numbers.

10 INPUT A$
Entry point for A$.
20 LET VAL=0
Set VAL to 0.
30 LET B$=A$
Make a copy of A$ and store in B$.
40 LET VAL=VAL+CODE(B$)-28
Extract a number from the leftmost character in B$.
50 LET B$=TL$(B$)
Now discard leftmost character in B$.
60 IF B$="" THEN GO TO 100
If we have no characters left then exit routine.
70 LET VAL=VAL*10
Move the numbers we have so far one decimal place to the left.
80 GO TO 40
Keep going until we have no more characters.
90 PRINT VAL
Print result (VAL variable)


Simple string slicing - α$(x TO)


This routine reproduces one of the ZX Spectrum's string slicing capabilities. α$(x TO) will slice a string from the xth character to the last character in a string discarding any characters before x.

10 LET A$="HARMLESS"
A$ is the string to be sliced.
20 INPUT X
X is the xth character where we will slice from.
30 IF X=1 THEN GO TO 70
X is 1 so we are not going to slice anything.
40 FOR F=1 TO X-1
Set up a loop to count up to one less than x. (We don't want to chop off the xth, only up to the one before.
50 LET A$=TL$(A$)
Slice off a character.
60 NEXT F
Keep slicing until one less than x.
70 PRINT A$
Tada! A$ is sliced.




Last Revision : 22/02/2023