Your Spectrum
Issue 1, January 1984 - Basic Compilers & Forth
Home Contents KwikPik
G O - F A S T E R !
Once the initial association with Sinclair Basic has run its course, Spectrum owners soon come to realise its limitations - especially with regard to speed of operation. Steve Mann checks out alternative routes - in particular the 'FP' and 'IS' compilers from Softek, and the Abersoft Forth package.
they'll ask, "machine code will deal with all that and more" - but no one likes a smart-arse, and machine code has a reputation for sending beginners gibbering and whimpering back to the comfort of Basic. It's not that it's amazingly difficult - in fact, in some ways it's extremely simple - but it's not an easy subject to learn from books and early attempts at understanding often prove so off-putting that the whole idea gets quietly shelved in the 'forget it' tray.
There has to be an easier way, and there is - in fact there are several. One way out is to learn Forth - definitely the year's most fashionable language for home computer users - and later on in this article we'll be taking a look at the Abersoft Forth compiler for the Spectrum. Another alternative is to use compiled Basic instead of the built-in interpreted version and, as Softek has released new versions of its Basic compiler, it seems only right and proper to give these the once-over.
So, introductory waffle over, it's time now to move towards the highly laudable aim of making life easy for the user. Let's get down to some specifics ...

Integer and Floating Point from Softek
Purists may look down on Basic compilers as being something of an easy way out, but then who wants to make things difficult? The principle behind a Basic compiler is perfectly straightforward - just type in your Basic program (or load it from tape or Microdrive), hit a couple of keys and, presto, the 707 has become a space shuttle - at least that's the theory.
Of course, nothing is actually that simple. The manual for the Softek compilers claims that compiled programs will run 'up to' 200 times faster (in some circumstances even 500 faster is possible), but the important point to note is that 'up to' is apostrophised (if there is such a word). In practice you will find that the compiled code is rarely that efficient and your programs are unlikely to be any more than 10 times faster. But let's not be ungrateful, a speed increase of 1000 percent is certainly not to be sneezed at!
All Spectrum owners will have noticed that their implementation of, say, Missile Command just doesn't have the speed or the smooth, pixel-by-pixel movement that a true arcade game devotee has come to expect from a program. In fact, Basic is just about okay if the display is kept simple - PRINT AT is actually, relatively, quite fast if the number of objects that need to be moved is kept small - but once the number of moving objects reaches four or five, the movement slows down dramatically.
Sinclair's highly individualistic - some might say bloody-minded - method of laying out the display file ensures that direct POKEs to the screen are more trouble than they're worth, so something different is needed. Another drawback is that anything that is moved a character square at a time is going to
look jerky when compared with the single pixel steps achieved in professional programs.


A recent innovation is the idea of a 'games design' package, Melbourne House has just marketed one of these, as has Quicksilva. But these are necessarily limited - the Quicksilva package, for example, while allowing the designer a fair amount of freedom in manipulation of sprites, insists that the resultant game is of one of four types, described as Space Invader, Asteroids, Scramble or Berserk. If you want a completely new format then, tough, you're out on your own, buster.
Pundits will pontificate patronisingly ... "Where's the problem?"

The other point to note is that there is a trade-off of speed against ease of use - the Softek 'FP' floating-point compiler offers many more features to the user: it will handle almost all Basic commands and functions, including strings and arrays (single dimension only) and a full range of 'real' numbers. The 'IS' integer compiler has many more restrictions on its use - no arrays, fewer commands and integer numbers only, in the range -32767 to 32767 (some functions will store a number in the range 0 to 65536 - for example, PEEK, POKE and USR). Floating point numbers are stored in five bytes, whereas integers take up two only - so the IS compiler produces much quicker code than does the FP version; in some cases operation can be speeded up by using the INT function, but generally the FP compiler will run two to ten times faster and in all but optimum conditions the speed increase will be at the lower end of this range.
In most instances, the 'IS' model is perfectly satisfactory, despite its more limited range of facilities, and the speed factor more than makes up for the extra care that is needed to ensure that the Basic is compilable. The lack of floating point maths need not necessarily be a drawback - integer numbers are often quite sufficient and, indeed, most Forth implementations will not handle floating point numbers anyway.
As a demonstration of the comparative speeds of interpreted and compiled Basic, let's examine a very simple program that plots every point on the screen (Figure 1). A couple of FOR/NEXT loops do the job quite adequately and, as can be seen from the timings, the 'IS' compiled version is fastest by many orders of magnitude. In the segment of this review that looks at Abersoft Forth, I have included a Forth version of this routine and a timing for further comparison.


Both Softek compilers use the same memory addresses, and both take up approximately 6K of RAM. I had hoped to incorporate Softek's Master Key program, which defines the SPACE key as a further SHIFT and which then uses this in conjunction with the numeric keys to give user-definable function keys. Unfortunately Master Key, which worked a treat on my earlier Spectrum model, is another fine program that has fallen by the wayside now that Sinclair has started mucking about with the ULA (see Frontlines, Sinclairwatch). Your author's much-loved Issue 2 Spectrum gave up the ghost a few weeks ago and despite having now got a replacement - also categorised as an Issue 2 - affections for the new model are already strained by the number of programs that now fail to work. Sinclair Research may claim that it's all the fault of software manufacturers who have jumped to conclusions about specifications, but one program that resolutely
10 FOR A=0 TO 175
20 FOR B=0 TO 255
Figure 1. Basic program to plot each point on-screen. Basic takes 6min 3 secs, 'FP' compiler takes 1min 40secs, 'IS' compiler takes 10 secs.

10 FOR A=1 TO 128
20 LET B=(255-A)
30 REM S,87,A,A
40 REM S,88,B,A
50 REM B
60 BEEP 1/50,1
Figure 2. 'Mini-sprites' on the 'FP' compiler.

10 CIRCLE 128,88,50
20 LET A=16384
30 LET X=46000
60 IF A>22528 THEN GOTO 80
70 LET A=A+1: LET X=X+1: GOTO 40
80 CLS: PAUSE 100
90 LET A=16384: LET X=46000
110 POKE A,K
120 LET A=A+1: LET X=X+1
130 IF A>22527 THEN STOP
140 GOTO 100
Figure 3. 'FP' screen dump - this saves the screen above RAMTOP and then downloads the screen. Line 10 is to put the design on-screen (anything will do).

:TEST 176 0 DO
      256 0 DO
      I J PLOT
      LOOP LOOP;
Figure 4. This program plots each point on-screen in Forth - it takes just 20 secs.

 251 0 DO
 125 0 PLOT I 90 DRAW
 125 175 PLOT I 90 DRAW
 15 +LOOP;
Figure 5. Using dictionary words to draw a pattern on-screen.

 7 0 DO
 5000 0 DO LOOP
Figure 6. Using the previous example of Figure 5 to define a new word, the pattern then runs through all the ink colours.
refuses to run on my new machine is Sinclair's own IQ Test! Anyway, Master Key would have been a boon when used in conjunction with the compiler. It would have enabled Basic programs to be compiled at a single keypress, thus obviating the need to remember the somewhat clumsy RAND USR
command needed both to compile and then to run programs. I just hope that Softek produces a revised version for later Issue 2 and Issue 3 machines.
Anyway, even without Master Key, Softek's compilers work well enough. The compiler is first loaded and the user is asked if RAMTOP is okay at 40000 (48K version). RAMTOP may be moved to allow many different short routines to be linked together. After typing in the Basic, all that is needed is to key in RAND USR 59300. The compiler makes two passes through the program, printing on-screen the start and end addresses and detailing any errors. Compilation stops at any mistake and the offending line may be pulled down and edited in usual Basic fashion. Once any errors have been dealt with, all that's needed is to make a note of the start address and use this in another RAND USR call to RUN the program. The Basic program remains in memory if desired, so both interpreted and compiled Basic programs may be run.
Initially, all but the simplest programs will probably refuse to compile when using the 'IS' version - but it's not too difficult to rewrite the offending sections to allow for the limitations (arrays will obviously take some thought - one solution is to POKE the required information into spare memory for later retrieval). Only skimpy, provisional documentation was available to work with while writing this review, but Softek assures us that full manuals will be available very shortly. The 'FP' compiler is much less finicky, although it did seem impossible to use a third parameter in a DRAW statement, despite the manual's claims to the contrary.
One particularly nice feature is the use of special REM statements to give commands and facilities not normally available in Sinclair Basic. These include such features as ON ERROR GOTO; ON n GOTO, where n must be a single-letter variable; and, most useful of all, REM S a,x,y which plots the character whose ASCII code is at PLOT position x,y. This REM statement allows some very smooth pixel-by- pixel movement (see Figure 2) and can be used with user-defined graphics to produce mini-sprites.
There were some slight problems with graphics when using the compilers - the Union Jack program from the Sinclair manual, for example, gained some extraneous black blobs when compiled - but there was certainly nothing that could not be put right after a little thought. Hopefully the final version of the manual will go into a little more detail than the preliminary effort - but even with just this, both Softek compilers proved easy to use and gave some impressive results
It's also nice to see that Softek has dropped its demand for royalty payments on programs that are produced with its compilers - all the company now asks is that the use of a compiler is acknowledged on packages and advertising.

The 'FP' Compiler costs £19.95, the 'IS' Compiler £9.95 and both together the price is £24.95. You can get them from Softek, xxxxx nn, nn/nn xxxxxxxxx xxxxxx, xxxxxx xxn.

Abersoft helps trash Basic speed limits
The Forth language was originally developed by Charles Moore in the 1960s as a language for controlling radio telescopes. Since then it's been used for a wide variety of applications, including arcade video games. Unlike Basic, which is usually ROM-based, Forth is generally supplied on disk and, with its sudden surge in popularity over the past year or so, now on cassette too.
The newcomer to the language will notice two major differences between Forth and Basic - Forth uses Reverse Polish Notation (ie. Basic's PRINT 2+2 is replaced by 2 2 + . ), and makes great use of the stack for virtually every operation.
There are now many versions of Forth available for the Spectrum - Abersoft's implementation is basically fig-FORTH (the modishly lower case 'fig' referring to the Forth Interest Group) with extensions to handle the machine-specific graphics and sound commands. Forth was originally designed for use on disk-based systems, and the Abersoft version emulates disk operation by setting aside an 11K chunk of memory and using this as a 'pretend' disk. Although 11K does not sound like an incredible amount of memory, the compactness of Forth allows some very complex programs to be developed.
The version looked at here has no provision for Microdrives, but it would be relatively simple to implement these, giving in effect a true (although slow) disk-based system. The 11K of 'RAM-disk' is organised into pages of 1K, each containing 16 lines of 64 characters. This is hardly ideal for the Spectrum's 32-character display but is a Forth standard and, in fact, gives rise to no real problems in use.


For those who are totally unfamiliar with the language, it should be said that Forth is used by defining words, using the basic building-blocks of the standard words in the dictionary. Each new word is treated as a new dictionary entry, and further words may be defined by using these. Thus, eventually, a complex program may be defined and run by the typing of a single word.
The stack assumes paramount
importance, being used for almost every operation - even to the extent of passing values from word to word and doing the same job as a Basic variable. It is in fact a surprisingly easy language to get to grips with, once the user has grasped Reverse Polish. Words may be defined direct from the keyboard - in which case the definitions go straight into the dictionary; or by using the editor - in which case they are not compiled immediately but wait until the blocks of text from the editor are LOADed.
For most of the machine-specific extensions, Abersoft has stuck to the familiar Basic names, but Reverse Polish means that parameters go before, rather than after, the keyword - the Basic PLOT x,y becomes, for example, x y PLOT (note the absence of a comma). In some cases the familiar words have a slightly different meaning - the Basic DRAW, which uses relative coordinates (DRAW 50,50 would mean draw to a point 50 pixels horizontally and 50 pixels vertically away from the last plotted position) is replaced by absolute coordinates, with 50 50 DRAW meaning draw a line from the last point plotted to the screen coordinates 50,50. The CIRCLE routine in ROM could be called from Forth. The Basic BEEP command is omitted completely, which is a pity. However, the facility exists to insert machine code routines and so the CIRCLE becomes the Forth BLEEP, used in the format n1 n2 BLEEP to produce a tone of duration n1 and pitch n2. User- defined graphics are fully supported, and the manual contains a routine to define any character by using the word UDG.
In addition to the Spectrum graphics and sound extensions, Abersoft Forth supports the Pascal-like CASE structure for handling multiple decisions, and the word FREE which - surprise, surprise! - tells you how many bytes
remain free for use.
The Abersoft documentation makes no claims about the use of Forth, instead concentrating on the Spectrum-specific extensions and giving a glossary - with a few omissions - of the words in the dictionary. Unfortunately the glossary contains some misprints, but as the novice user will need a proper Forth instruction book, these should not be too troublesome. It's a good idea to supplement the Abersoft manual, and whatever book is decided on for teaching purposes, with the fig-Forth installation documentation; this costs a fiver and is well worth shelling-out for (the contact address is given at the end of this article).
All in all, then, Abersoft Forth is highly recommended for the beginner who wishes to learn more about this fascinating language. With an increase in speed of up to 5000 percent over Basic, Forth provides a good alternative to machine code for fast-running programs and speedy graphics - and its use in arcade machines testifies to its success as a language for the programming of all-action games. It's impossible to give more than a rough idea of the language's capabilities in this short article - but hopefully, at least, your curiosity will provoke you into investigating Forth on your Spectrum.
Abersoft Forth costs £14.95 and is available from Abersoft, n xxxx xxxxxxx, xxx xxxxxx, xxxxx xxnn nxx.


The Complete Forth - Alan Winfield (Sigma Technical Press)
Discover Forth - Thom Hogan
Forth Programming - Leo Scanlon
Forth installation documentation is available from Forth Interest Group UK, nn xx xxxxxx xxxxxxx, xxxxxxxxxx xxxxx xxxxx, xxxxxx xn.

"I agree it's quite remarkable that we can run the whole of BL on a Sinclair Spectrum - but do we really need 17,340 people to operate it?"
Home Contents KwikPik