MAKING GOLDEN LISTINGS FROM THE LEADEN BASIC
Speed and smoothness of action is what everyone attempts to achieve in programs. John Gilbert reports on compilers.
IT IS the dream of a Basic programmer to have a slow-running, jerky, Basic program turned instantly into machine code. That is possible using a compiler which will take a Basic listing and translate it into a form of machine code. The benefits are that the program instructions are less open to prying eyes and piracy. It also means that the programs will run faster.
Anyone who has some idea of how Sinclair machines work will appreciate that neither the ZX-81 nor Spectrum can read Basic code directly. They have to use a program called an interpreter which is stored in the ROM. That program translates Basic into a code which the machine will understand.
When you write a machine code program the need for the interpreter is removed. The interpretation of Basic takes up a good deal of time, so when no interpretation is needed the program will run faster. A compiler generates code which dispenses with the need for the interpreter.
Unfortunately none of the compilers on the market will translate all Basic instructions into machine code. The usual level of translation ranges from 90 percent to 95 percent.
Softek was the first company to produce a compiler and that was for the 48K Spectrum. Its main claim to fame was that you could speed graphics displays in programs. That it did very well and in so doing provided a facility which had not been seen on the Spectrum.
Like other companies which produce compilers, Softek upgraded its design of the compiler whenever it could. The company is in the process of launching two new compilers, one of which operates using integer Basic; the other is a great step forward in Sinclair computer compiler design, using floating point numeric representation.
An integer compiler allows the use of numbers only in the format 3, 56, 9. They will now allow you to use decimal points with numbers in a program. In some cases that proves to be a problem. For instance, you may want to perform some mathematical operations, such as division, which will leave you with a decimal number. The restriction in the compiler means that you will have to use INT to make sure that the computer can handle the numeric representation.
A floating-point compiler is a great step forward as it will allow you to use decimal notation when performing mathematical operations.
That will allow the programmer to use most of the commands in Sinclair Basic. Softek claims that it will compile up to 99 percent of Basic which, compared to other compilers, is very good. Unfortunately the floating point compiler is slower than the new Softek integer compiler. The difference in speed is not too noticeable but it is best to use the new integer compiler for graphics in games and the floating point compiler for the accuracy in calculations is needed by scientists and teachers. With the two products Softek has taken control of the market.
The second company to put a compiler on the market was PSS. It was called MCODER, of which versions for both the ZX-81 and Spectrum were developed. MCODER has since been upgraded and MCODER II is available. Both versions of the compiler are similar.
The MCODER is fairly representative of what is on the market and it is worth looking at the type of commands it will allow the Basic programmer to use. Unlike the early compilers, it can use numeric and string variables as well as arrays.
The restriction to their use, of course, is that only integer numbers can be stored in them. Another restriction, which is not too great for most applications, is that the range of numbers which it can use if from -32768 to +32767. Again that is as a result of the way in which the machine stores numbers.
Arrays have to be one-dimensional and you cannot use string arrays with the MCODER. That is unfortunate but you can circumvent it by using ordinary string variables.
The input instructions include both INKEY$ and The usual INPUT statements. For input in a program the READ, DATA and RESTORE instructions are available. The compiler will handle characters only within strings which have a code less than 32 and more than 164. That means you cannot put Basic keywords into strings.
By allowing the range of codes indicated, the compiler can handle user-defined graphics, with codes ranging from 144 to 164. You can define your shapes in the usual way, using decimal notation with USR or the BIN numbers.
Other display statements include DRAW and CIRCLE, with PLOT and OVER also included in the catalogue of legal Basic statements. PRINT and PRINT AT are also available, although AT is not included in the documentation. The TAB function is not available but can be replaced easily with PRINT AT.
Several program flow statements are also included in the compiler. They include GOTO and GOSUB. The IF... THEN statement can also be obtained.
The FOR... NEXT loop structure can be used in the same way as in Basic but one restriction on it is that you cannot use a loop number which is greater than 32767. That also applies to other instructions in the compiler subset, including IF... THEN and RND.
Even with all those restrictions you can overcome the problems and write almost any Basic application program.
All the compilers on the market stress ease of use and that is true. For instance, there are three ways of operating the MCODER. The first is to load the compiler and enter your program by hand. After it is finished you can run the Basic to see if everything is working and there are no bugs. After that a quick machine code call will compile the Basic, which can then be run by a RAND USR 40000. The code can be moved round in memory if necessary but 40000 is the default location for the start of your routine.
You can load a Basic program from tape if you have one already developed and do not want to re-type it. To do so you must LOAD the Basic in the normal way after loading the compiler.
The third type of compilation allows you to translate two Basic programs into machine code in the same session. To do so, you will have to move RAMTOP and stack one program on top of the other. The two programs can then be called by separate USR instructions.
When you have told the computer to compile, one of two things can happen. You can either get an error code or you will get the OK signal.
If an error has been detected in your program, the MCODER will return your program listing with an 'S' beside the line which could not compile. It will then allow you to change that line and re-compile the code.
If you have had no difficulty you can invoke your program using USR to see that it runs faster than in Basic. You can then SAVE it to tape by following the easy-to-read but not over-long instructions.
The effect of the compiler on the code is more marked in some instructions than others. The CIRCLE and DRAW commands are not much faster than in Basic, although if you draw sufficient circles you should see the speed compared to Basic.
The real effects of the compiled code will be noticed when you use FOR... NEXT loops and the other structural statements which affect the flow of a program. The speed in some cases is unbelievable when you think that the code used to be in Basic.
A compiler is a boon to anyone who can write Basic well but cannot master machine code. Unfortunately, the compilers in the Sinclair market and elsewhere incorporate some of the manufacturers' code into the code of a program when it is compiled.
That means that some of the manufacturer's program would be in your program and as a result you could no make a profit from your program without giving credit, and perhaps a royalty, to the software house which created the compiler.
Companies which produce compilers for more expensive computers, such as the Apple II, charge a hiring or rental fee for those utilities. In that way they receive money for any program which contains some of their compiler routines.
Unfortunately things are not so simple in the Sinclair market. It would be uneconomical to charge for compilers in that way, since mass production would mean keeping track of thousands of programs.
The initial stand was to insist that it would market programs which it thought were good and where the compiler was used. In no other cases could companies use the compiler without consultations.
At the time that was a difficult but necessary move, as there was, and still is, no clarification of the law of copyright regarding software. Softek has since softened its objections to the use of its compiler for profit and asks that companies which produce games using it credit Softek on the program and cassette insert.
Compilers are useful instruments both to individuals and to software companies but the legal difficulties about their use still exists. Compilers become more complex and, as a result, companies which produce them will want to protect their interests from the usual type of piracy and the use of a compiler without consulting manufacturers.
It will need a change in the law to clarify the position but until then compilers will be of most use to individuals who do not want to take the step of learning to program in machine code. It is an unfortunate restriction of use which will need to be investigated in the near future.
Compilers are useful to the individual. Although they do not provide a complete translation from Basic to machine code they will still help anyone who does not want an assembler or to learn assembly code.
It may also provide an incentive to learn machine code programming techniques because of the speed at which it will make a former Basic program work. In the next few months we could see a 100 percent Basic compiler but until then we will have to work with what we have.
Softek. 12-13 Henrietta Street, London WC2
PSS. 452 Stoney Stanton Road. Coventry CV6 5DG.