Software Review: FP Compiler

Author(s): Wes Brzozowski
Publication: LISTing Newsletter
Date: May - June 1985
Pages: 5

SOFTWARE REVIEW: FP COMPILER
FOR: 48K SPECTRUM
FROM: SOFTEK, UK
PRICE: UNDER £20.00
FUCTION: CONVERTS BASIC INTO MACHINE CODE

Most computer owners know that their machines run something called machine code, which to many is a deep dark mystery. Lots of folks are perfectly content for it to remain so, and that’s all right. Still, machine code can be mighty fast, and those who program only in BASIC may feel just a twinge of envy toward the machine code types. Surely there must be some short cut that can allow everyone to tap a bit of that power…

If you find yourself nodding, you’ll be pleased to know that a class of programs, called compilers, will do just that, by translating a more familiar language into machine code. In this case, the FP Compiler, by Softek, will turn most any command in Spectrum BASIC into its machine code equivalent, and then goes on to add extra commands that Sinclair “forgot”. Spectrum? Sinclair? Well, yes, this program does require a Spectrum emulator to run, but it’s one more good reason to join the ranks of the “emulator people.”

Some machine code programmers are less than enthusiastic about compilers, which tend to use a “cookbook” approach to their translations, and so seldom produce efficient code. Yet, even the most hardened MC users will find use for this product, because this is a floating point compiler. That is, it uses a special five byte format to represent numbers, which may span over the entire range of values that they do in Sinclair BASIC.

The fixed point number system in the Z-80 instruction set only allows integers, ranging from 32767 to -32768, or 0 to 65535, and most machine code programmers have resigned themselves to this. This is not because Sinclair’s floating point calculator software is hard to use; it’s not. Unfortunately, very little has been written about the calculator and the new “instruction set” it simulates.

As a whole, then, this program will allow even the newest BASIC programmers to “soup up” their favorite projects. Machine code programmers will be able to patch floating point portions into their code. The truly “hard core” can, with the aid of a disassembler, Ian Logan‘s Spectrum ROM Disassembly, and perhaps one of those floating point articles, compile simple examples to help them learn to use the floating point calculator. There’s something for everyone!

So much for what it does. How well can it do it? Most BASIC compilers can only handle a subset of BASIC, and compilers for the Timex & Sinclair machines have been particularly limited. In contrast, Softek’s creation is very complete, although there are a few restrictions. Number and string arrays may have only one dimension. No expressions may be put in DATA statements. INPUT has some very minor limitations. LOAD, SAVE, and VERIFY only work with the CODE extension. GOTO, GOSUB, and RESTORE must be followed by a number, not an expression. Microdrive commands aren’t supported. There are a few other things, but all in all, the compiler’s restrictions are really not too hard to work around.

Faster floating point code is great, but nothing comes for free. If fixed point numbers will do, (as in updating the position of a cursor, for example) calculations can be done in just a few machine code instructions. Doing the same in floating point format, which the compiler demands, involves the use of long and complicated math routines. This means that it will run far slower, and so this compiler is not the best choice for every program. Still, when you need it, it does a fine job. Also, users who simply don’t want to bother with fixed point numbers can still use this compiler to get a significant speed increase, no matter what their programs do.

To give an example of the differences in running speed, some comparison tests were done. The first was a bubble sort, with non-integer numbers, so requiring a floating point program … well, everyone uses a bubble sort for comparison … it’s tradition, that’s why, so there. Using 10 elements, arranged to require the maximum amount of sorting, a straight BASIC program took 2.6 seconds, whereas the compiled version took .83 seconds; a 310 percent speed increase.

A second example, chosen to show the inefficiency of floating point code where it’s not needed, involves filling an 8K block of memory with zeros. A BASIC FOR loop that POKEs each location takes 69.9 seconds. The same program compiled took 18.7 seconds. Not bad. Now, this program can be handled with an integer compiler. One such product, (which runs on a different computer, but with a similar clock speed) produced code that did the task in about 1.5 seconds. Quite a speed increase. A machine code loop, with the same program structure does it in about 0.1 second. (This number was calculated.)

Note that compilers simply follow a recipe to translate a program, line by line. they don’t have the smarts to look at a group of lines, analyze the task, and realize that there’s a simpler way to do it, with the machine code instructions available. The BASIC CLS command uses a neat trick with the LDIR instruction to clear the display file. This same trick will initialize our 8K block in about .05 seconds.

These numbers provide a reasonably good comparison between the speed of BASIC and compiled BASIC. They also compare straight machine code and the occasional lucky short cuts that so brighten a programmer’s day upon their first discovery. Lastly, they show that compilers can really help, but there will always be places where straight machine code is the only way to go. This is not intended to malign compilers. They’ll put extra zip into sluggish BASIC programs, with very little effort, but they can’t do magic.

No review would be complete without a couple of nasties, so I’ll oblige. Most compilers include a “run time package” of subroutines that are used by the compiled code. Many compilers allow this package to be located wherever in memory you desire; the FP Compiler does not. Instead, this package remains imbedded in the compiler, and so the entire compiler must be SAVEd with your compiled code, in order for it to run. The compiler takes up about 6K at the top of memory, and so you’ll have to locate your compiled code somewhere nearby, if you want to SAVE the smallest possible code block. Although this made the compiler easier to write, it wasn’t necessary. It’s unfortunate.

If the above paragraph seems a bit confusing to you, be assured that you needn’t understand it at all to use the compiler. Once you follow a few step by step instructions. everything else will be done for you. Just be warned that if you know any MC programmers who need something to complain about, this will be at the top of the list.

Because the manufacturer retains the copyright on the run time package, it’s usually not possible to market code made with the compiler, without paying royalties. The Softek people have very generously given permission to sell such code, as long as they are given credit for the use of their compiler. While this is commendable, it’s also very surprising. After all, they’re allowing strangers to “give away” a free Softek compiler with every program they sell (remember, the compiler has to be SAVEd with the compiled code.) Then they must advertise that the free compiler is in there! This is not a complaint; merely a confused observation.

If you want to get fancy, it’s perfectly OK to go back and forth between BASIC and your compiled code. This will allow your programs to include microdrive commands, as well as the unsupported types of cassette and INPUT commands. (None of these will be noticeably slower running from BASIC, anyway.) In effect, your compiled code becomes just another BASIC subroutine.

Only subscripted variables are passed between BASIC and the compiled code. If you use the same non-subscripted variable in both BASIC and compiled code, they will be treated as two separate variables. Beginning programmers will find this confusing until they get used to it. Some who’ve worked with other languages will recognize these as “global and local” variables, and will find them useful. The probable reason things are arranged this way is that it takes a long time to scan through the BASIC variables area, and local variables outside this area can be accessed more quickly. So it buys us some speed. It’s both a blessing and a curse, but I think it was the right choice.

In summary, the compiler is very easy to use, compiles almost all BASIC commands, and gives a noticeable speed increase. It’s not intended as a substitute for machine code, but what it does, it does well. My gripes, which I’ve stated, are minor, and many users won’t even notice them, let alone be bothered by them. As such, the program rates a 9 out of 10, and I recommend it highly.

Wes Brzozowski


! Since your 2068 BASIC programs will LOAD and RUN on a Spectrum (unless they’re too long or contain the unsupported commands FREE, STICK, ON ERR OR SOUND), those of you with emulators can compile your BASIC programs for Spectrum mode. The compiler will not work in 2068 mode.

PD.