SPL PROGRAMS

The following demonstration programs can all be compiled into machine-code by the Compiler program to be described, and run on the Atom or BBC Computer. They illustrate some of the features of SPL.

Bubble Sort

The first program performs a bubble sort of the characters in the top half of the screen memory. The sort works by successively comparing pairs of adjacent locations; if two are in the wrong order they are exchanged, and then the smaller one is moved back to its correct position in the locations that have already been sorted.

5 {BUBBLE SORT OF SCREEN}
10 PROC BUBBLE();
15 BEGIN I=0;
20   LOOP:J=I;
25   LOOPA:IF SCREEN[J]>SCREEN[J+1] THEN
28   BEGIN
30     TEMP=SCREEN[J];
31     SCREEN[J]=SCREEN[J+1];
32     SCREEN[J+1)-TEMP;
35     IF J=0 THEN GOTO OK;
40     J=J-1;GOTO LOOPA
42   END;
45   OK:I=I+1+IF I<255 THEN GOTO LOOP
60 END

When the compiled program is executed the characters on the top part of the screen will be sorted into order.

Crawling Snake

The following SPL program moves a snake across the top half of the screen, and demonstrates the use of the language’s shift operators '>>’ and '<<'. The program is only suitable for the Atom, and uses a routine WAIT to make sure that the screen accesses are noise-free. After being compiled it should be executed by linking to the address corresponding to the label ENTER.

  5 {CRAWLING SNAKE}
 10 PROC SNAKE();
 15 BEGIN
 20   PROC CLEAR(K);
 25   {CLEAR SCREEN TO CHARACTER K}
 28   BEGIN
 30     X=0;CLR:SCREEN[X]=K;
 35     X=X+1;IF X<>0 THEN GOTO CLR
 40   END;
 42   PROC WAIT();
 43   {WAIT FOR FLYBACK SYNC}
 44   BEGIN
 45     WA:IF PORT[2]>127 THEN GOTO WA
 46   END;
 47   ENTER:L=0;CLEAR(64);
 48   SCREEN[0]=127;SCREEN[1]=127;
 49   SCREEN[2]=127;SCREEN[3]=127;
 50   RUN:X=0;
 60   LOOP:WAIT();
 70   C=(SCREEN[X]&63)<<2;
 80   SCREEN[X]=C|192+L;L=C>>6;
 90   X=X+1;
 95   IF X<>0 THEN GOTO LOOP ELSE GOTO RUN
100 END

Write Hexadecimal

The following procedure will print a number as two hexadecimal numbers on the screen. On the Atom this routine is pre-defined.

40 proc wrhex(n);
50 begin
60   if n>=160 then wrch(n>>4+55)
70   else wrch(n>>4+48);
80   n=n&15;
90   if n>=10 then n=n+7;
100   wrch(n+48)
110 end;

When compiled and executed the procedure will print the contents of the accumulator in hexadecimal.

Prime Numbers

The following SPL program finds all the prime numbers up to 127, and prints them in hexadecimal:

10 PROC MAIN();
20 BEGIN
30   PROC PRIME(N);
35   {PRINT N IF PRIME}
40   BEGIN D=l;
60     TRY:D=D+1;E=N;
65     IF D<N-1 THEN
68     BEGIN
70	TEST:E=E-D;
75	IF E<>0 THEN
80	IF E<128 THEN GOTO TEST
85         ELSE GOTO TRY
88     END
90     ELSE WRHEX(N)
110   END;
115 {MAIN PROGRAM}
120   ENTER:T=1;
125   ALL:PRIME(T);WRCH(32);
130   IF T<128 THEN
140   BEGIN
150     T=T+1;GOTO ALL
170   END
190 END

On the BBC Computer the definition of the 'wrhex' routine should be inserted at the start of the program between lines 20 and 30. The compiled program, when executed, prints the following sequence:

01 02 03  05  07    0B  0D    11  13    17      1D  1F
25    29  2B    2F      35      3B  3D      43
47  49      4F    53      59	61    65  67    6B
6D    71	7F

It prints a space for every number tested, so the primes can be seen to become more sparse as they get larger.

Greatest Common Divisor

The following function finds the greatest common divisor (GCD) of the numbers A and B using Euclid's algorithm:

5 PROC TEST(); BEGIN
10 PROC GCD(); {GCD OF A,B IN B}
20  BEGIN LOOP!IF A<>B THEN
25    BEGIN
30    IF A<B THEN B=B-A
35 ELSE A=A-B END;
40  RETURN B END;
45 ENTER:A=9; B=12; WRHEX(GCD())
50 END

The routine, once compiled, should be entered at ENTER, when the GCD of 9 and 12, i.e. 3, will be displayed. Again, for the BBC Computer version the 'wrhex' routine should be included since it is not pre-defined.

Multiply Routine

The following test program demonstrates an 8-bit multiply routine written in SPL:

 5 PROC TEST();
 8 BEGIN
10   PROC MULT();
15   {A*B - RESULT IN C}
20   BEGIN C=O;
25     MULL:IF B>0 THEN
28     BEGIN
30	IF B&1=1 THEN C=C+A;
40	A=A<<1;B=B>>1;GOTO MULL
45     END
50   END;
 8 ENTER:A=6;B=19;MULT();WRHEX(C)
60 END

The machine code is executed from the address corresponding to the label ENTER, and should print out '72'; in other words, 6*19=114, or 72 in hexadecimal.

Mastermind

In the following SPL Mastermind program the computer generates a random 4-digit code, which the player must guess. The guess is entered as four decimal digits, and the computer displays the result as two digits: the first digit gives the number of digits correctly guessed in the correct position; the second digit gives the number of correct digits incorrectly placed. When the computer's code is correctly guessed, with a score of '40', the program gives a 'bleep'.

The following sample run shows each of the player's 4-digit guesses followed by the computer's 2-digit reply:

1122 00
3344 10
5566 00
7788 20
9900 10
3780 02
7948 40

The version of the program shown below is for the BBC Computer; on the Atom all the symbols and variables should be in upper case, and lines 40 to 110 can be omitted.

10 {mastermind}
20 proc mastr();
30 begin array my[3],your[3],temp[3];
40 proc wrhex(n);
50 begin
60   if n>=160 then wrch(n>>4+55)
70   else wrch(n>>4+48);
80   n=n&15;
90   if n>=10 then n=n+7;
100   wrch(n+48)
110 end;
120 proc rnd();
130 begin rndy:rndxmndx<<2+rndx+7;
340   if rndx&15>9 then goto rndy;
150   return rndx&15
160 end;
170 proc input();
180 begin n=0;
190   readc:j=rdch();wrch(j);j=j-48;
200   if j>9 than goto readc;
210   your[n]=j;n=n+1;
220   if n<4 then goto readc
230 end;
240 {main program}
250 enter:n=0;
260 myno:my[n]=rnd();n=n+1;
270 if n<4 then goto myno;
280 try:input();n=0;
290 copy:temp[n]=my[n];n=n+1;
300 if n<4 then goto copy;
310 n=0;score=0;
320 bull:if temp[n]=your[n] then
330   begin temp[n]=10;your[n]=11;
340     score=score+16
350   end;
360 n=n+1;if n<4 than goto bull;
370 n=0;
380 cow:m=0;
390 cowx:if temp[n]=your[m] then
400   begin temp[n]=10;your[m]=11;
410	score=score+1
420   end;
430 mm+1;if m<4 then goto cowx;
440 n=n+1;if n<4 then goto cow;
450 wrch(32);wrhex(score);wrch(10);wrch(13);
460 if score<>64 then goto try;
470 wrch(7)
480 end