ZX Computing
1st August 1983
Author: David Bolton
Publisher: HiSoft
Machine: Spectrum 48K
Published in ZX Computing #8
Spectrum Pascal
How would you like to write programs in a language very much like Basic and then run them at machine code speeds? It is now a very practical suggestion with the release of a full Pascal compiler for the 48K ZX Spectrum by the British company Hi-Soft.
Before I look to the Spectrum versin, let me give you a little history of Pascal. It is a very young language, born in 1969 when a leading computer scientist called Wirth decided that ALGOL, the then leading academic language, was getting too complex. He designed Pascal specifically to teach good programming practices. It is now available in many forms on disc-based machines, but there are very few implementations on cassette-driven micros.
Pascal is very similar to Basic in many ways, certainly much closer than languages like Forth, Lisp, assembly or Pilot. It is generally a much better language.
This version of Pascal is a very high quality product comprising some 20K of machine code for the compiler, editor and run-time package. It completely compiles a Pascal program into Z80 machne code which runs very fast indeed. Try filling an array of 7,000 elements in half a second, or summing up the numbers one to 20,000 in seven! Obviously, one could write faster programs using an assembler, but try assembling A = LOG(SIN(B/180 * 22/7)-10.45)-8/2.2234!
When the compiler package is loaded, it starts in the editor, allowing the programs to be typed in, or loaded from tape. Hi-Soft have been very cunning with their editor as it completely bypasses the Spectrum's single-key entry system. Programs are entered line by line just like Basic, with a line number at the start of each line:
10 PROGRAM DAVE;
20 VAR I,J:INTEGER;
30 BEGIN
40 J:=0;FOR I:=1 TO 100 DO
50 J:=J+1;
60 WRITELN('J = ', J:5)
70 END.
The line numbers are at the start of each line for purposes of the editor only; they have no bearing on the program. Commands are included to do the equivalent (in Basic) of AUTO, DELETE and RENUMBER as well as searching and moving. There is also quite comprehensive commands involved with line editing which can do find, insert, replace and delete on one line.
Overall, the editor is not as good as a screen editor, but it is the next best thing and for anyone sed to Sinclair editing it is a significant improvement.
The editor only changes the 'source code', i.e. the program as it looks above. Once a program has been compiled into machine code (or object code as it is known) it can only be changed by changing the source code and re-compiling.
Source code can be saved onto tape and it's quick to do so because Hi-Soft usees tokens (just like Basic) to represent reserved words, e.g. PROGRAM and INTEGER only occupy one byte each in internal storage. When source code is loaded into RAM it appends onto the end of any existing code; this makes the creation and use of libraries much more convenient.
One problem with the editor is that there is no keyboard beeping. I tried unsuccessfully to get it to work, even writing a short program to POKE 23609 with 255, but to no avail. As soon as I exited from Pascal to Basic, the beeping started working again.
Compilation is the process which converts source code to object code. There are two ways of doing this in Hi-Soft Pascal; the first is for debugging purposes, and keeps both source code and object code in RAM at the same time to speed up the "Edit-Compile-Test" cycle. When testing is complete, the specially compiled object code can be dumped out to tape so that it will automatically load and run as a standalone program.
If a syntax error is discovered during compilation then the choice is given of stopping and going into the editor at the faulty line, or listing other errors. Pascal is typical of compiled languages in that one error early on can generate lots later.
Compiling is a very quick process, but it will be slowed down if the compiler provides a listing at the same time. The Spectrum 'write character' routines are not terribly fast, so if the compiler listing is disabled then the compilation takes place in a short period of time and instantaneously for small programs.
There are seven compiler options which determine whether or not certain checks will be incorporated into the machine code. They check such things as overflow in integer addition/subtraction, or checking if array references go out of bounds. The idea of these checks is to include them while testing and debugging and then remove them in the finished program. Some of the other options are used to control compiler listing or printing.
Tape and I/O facilities are included for the program to store variables on tape and read them back in. It is non-standard for Pascal but is very powerful as just one statement can write out or read in any variable from one character up to a very large array.
The I/O ports used on the Spectrum can be read and written to by use of the INP function and the OUT procedure.
A statement similar to INP, (OUT(X,C) is used to output a character byte C to port X.
Statements and functions are included to allow reading from and writing to RAM. Again, non-standard for Pascal, these are taken from Basic and are PEEK and POKE. Both are much more versatile than their Basic equivalents as they can work with single characters up to entire arrays. For example, POKE (#A000,'DAVID BOLTON') will deposit twelve characters in RAM from A000 onwards.
As well as the statements described above, there are a couple of features which are useful for the machine code programmer.
INLINE is a statement which permits machine code to be directly embedded into the Pascal program. It is followed by a list of decimal or hexadecimal integers which represent the machine code.
USER is a function which is similar to BASIC's USR, and permits calls to machine code routines external to the program. For example, with the Spectrum, there are a lot of useful ROM calls which can be incorporated into programs.
Note that addresses can be also given in hexadecimal form; this is a more useful representation than decimal for integers, because any integer higher than 32767 must be given in negative form.
This is a very impressive product, and certainly one that could be of benefit to any Spectrum programmer once he/she has mastered Basic and wants to advance.
The compiler is fast as its job and produces very quick machine code, and together with the ability to have machine code incorporated in the program should mean that there are no tasks which are better done in assembler than Pascal.
Naturally there are some negative aspects, nothing's perfect, but I consider them fairly minor.
Apart from machine code speed, integer and real arithmetic, better data structuring, recursive procedures and functions, there is also formatted output where you can specify how numbers are written out, with rounding.
For the price of £25, you get the complete compiler/editor and a 60 page reference manual. This is not a teaching manual, so if you are unfamiliar wth Pascal I would recommend purchase of a good book. The manual mentions a few at the back.