|★ APPLICATIONS ★ PROGRAMMATION ★ Program of the Month — Amstrad Basic Compiler ★|
|Amstrad Basic Compiler||Applications Programmation|
This Basic compiler enables programs written in Amstrad Basic to be compiled directly into machine code. This invariably results in faster and more compact programs.
Over 50 Basic keywords are supported by the compiler (including those dealing with sound and graphics), but there are some restrictions on the use of variables and expressions.
To use the compiler, type in the listing exactly as shown (except that all REM statements may be omitted if desired) and save it to tape or disk. (If saving to disk on the CPC464, be sure to save it as an ASCII file — otherwise you will not be able to merge it later.) Next, load or type in the program you wish to compile, and test it thoroughly under Basic.
When you are satisfied with the program's performance, you should then save it in case you wish to modify it at a later date. You must now renumber it so that the last line number is less than 1000, and ensure that the last line of the program terminates with an END statement.
Now merge in the compiler itself (if you don't still have it in memory) and run it using RUN 1000; this may take a few minutes if your program is very long, but the compiler will keep you informed of its progress at each stage. If any errors are reported, you should correct the offending line and re-compile from the beginning.
When your program has been successfully compiled, you may save it to tape or disk as a binary file (using the addresses generated at the end of the compilation) or run it immediately by CALLing the start address.
The accepted commands, together with any limitations, are shown below:
The following Basic functions are also supported:
Where appropriate, statements may also include #n, (where n is a number between 0 and 7) to specify a particular screen window.
Only simple integer variables are supported, and these should consist of a single letter (A . . Z) or two identical letters (AA . . ZZ). Lower-case letters may be used if desired, but the compiler makes no distinction between these and capitals. Any numbers used must be integers between -32768 and +32767.
All the standard arithmetic and relational operators may be used (with the exception of a), but note that all expressions are evaluated from left to right, with no precedence of operators. This system may take some getting used to — especially if you are familiar with the method used by Basic — and care must be taken in formulating expressions to ensure that you achieve the desired result. For example, the expression A = 2 + 4*3 will yield a result of 14 in Basic, as it is evaluated as 2 + (4 * 3) (because * has a higher priority than + ), but when compiled, the result would be 18, as by evaluating from left to right, the expression becomes A = (2 + 4) * 3.
To obtain the same result in Basic, the expression must be re-written as A = 2 + 4(4*3) or A = 4*3 + 2.
Expressions of this kind will probably be easy to cope with, but it is all too easy to be misled by relational expressions such as IF A > B + 3 which will not be compiled as IF A > (B + 3), which is the case with Basic, but would yield the nonsensical result of IF(A > B) + 3.
As a general rule, expressions of this type should have the right-hand side of the inequality enclosed in parentheses.
Note also that where a minus occurs in an expression, it must be enclosed in brackets if it occurs immediately after another arithmetic or relational operator. Thus the statement PRINT -2*3 will correctly evaluate to -6, but the statement PRINT 3 * -2 should be written as PRINT 3 * (-2).
As an example of the speed improvement which can be obtained when using the compiler, try out the following one-line Basic program:
100 MODE 0 : WHILE b < 400 : WHILE a < 640 : PLOT a,b : a = a+4 : WEND : a =0 : b = b + 2 : WEND : END
This program fills up the screen by plotting every individual point in the current pen colour. When run under Basic, it takes over three minutes to run, while the compiled version takes less than 20 seconds!
Australian Personal Computer