Beebug


Acornsoft C

Author: Ray Hughes
Publisher: Acornsoft
Machine: BBC B/B+/Master 128

 
Published in Beebug #55

'C' is a highly regarded computer language, but until now no full implementation has been available for the Beeb. Ray Hughes has been putting Acornsoft's new version to the test.

Acornsoft C (Acornsoft)

For years they said that it couldn't be done. People argued that it was not possible to write a full implementation of the 'C' language for the 6502 processor. But suddenly three separate implementations have been announced, all for the BBC micro:

  1. Acornsoft C
  2. Beebugsoft C
  3. Brasscourt C

In this review I shall concentrate on Acornsoft's version, making occasional comparisons to Beebugsoft's, which, like the former, implements a full Kernighan and Ritchie standard. The Brasscourt version was not available at the time of writing.

Some Preliminaries

I want to begin, however, with a few words about 'C' and related matters. 'C' is a language in vogue at the moment. Languages come and go, but for the moment 'C' is "in". A major reason for this is that it compiles well, producing reasonably efficient run-time code. This is extremely useful for professional programmers and software houses. Instead of writing applications directly in machine code for a given host processor, they can be written in 'C', and compiled to produce a stand-alone application package in machine code. Programming time is less, because it is easier to debug 'C' than it would be to debug machine code. And to make a version for a different processor, you need only to recompile the source code on a suitable compiler for the new processor.

The portability of 'C' is greatly increased by the way that the language is organised. For while 'C' is a fully structured language with FOR loops, DO loops and CASE statements, it contains no code for handling input/output functions such as printing and file handling, or even string handling. All these functions are provided by machine-specific disc-based library routines. These are linked in to the object code during the compiling process. The same 'C' source code program can therefore work equally on an IBM mainframe, a Cray 2 or a BBC micro - they just use different libraries.

Compilers And Interpreters

Programs in high level languages like Basic, 'C' or Pascal must be translated into a series of machine code instruction before they can be executed by a computer's processor. The difference between interpreters and compilers is that interpreters perform this translation one instruction at a time, executing the code that has just been translated, before moving on to translate the next bit. Compilers, on the other hand, translate the whole of a high level language program into a complete machine code program (called the object code). This object code may then be run directly.

Compiled languages generally run a lot faster than interpreted ones, but suffer the inconvenience that you need to compile a program before it can be run - though you only need to compile any given version once, providing that it is bug free.

Acornsoft 'C'

Acornsoft's 'C' is no different in this respect, and comes with a set of BBC specific library routines on disc. Both 5.25 inch (DFS format) and 3.5 inch (ADFS format) discs are supplied in the pack, which also contains a very competent 250 page manual, a keystrip (for the editor) and a shortlist of known bugs. What is important to note about Acornsoft's product, however, is that it will not work across the full range of Acorn micros. If you want to run it on a straight model B, you will also need a 6502 second processor. Master and Compact users, with all that extra RAM, have no need of a second processor though. The Beebugsoft version, by contrast, will work on all machine configurations, including a bare B.

Whichever version of 'C' you are using, there are four major stages to the process of programming. First you must write your program using a text editor, and save the result to disc as an ASCII file. You then compile this text source into intermediate code. You then link this code into what Acorn call object code. Finally this object code may now be executed.

In the case of both Acornsoft's 'C' and Beebug's, this final code is not full stand-alone object code. To run it, you must have 'C' installed in the machine. It is to be hoped that someone will produce a 'converter' program which will convert the so-called 'object code' to fully standalone code. But even if this does not happen, there is still the major advantage that versions of 'C' for other micros - including the Archimedes - will be able to generate standalone code from source code created in 'C' on the BBC micro or Master.

Setting Up

Let us look in a little more detail at the various processes involved in using Acornsoft's 'C'. Before you can do any programming at all, there is a setting up and configuring process to be performed. How this is accomplished depends on which machine configuration that you are using. The Master and Compact version of the software, for example, requires you to run a special file which loads up all four banks of sideways RAM. You must then execute a hard Break to initialise the code, and can then type:

*C

to invoke the language. To signify that you are within 'C', you will then get the following prompt:

C>

and can proceed to use any one of a number of commands.

Once you have reached this stage, the next job is to create a program source code using the editor - so type:

EDIT

Second processor users must use *EDIT to load in the editor, so overwriting the compiler, because the second processor is a bit tight on RAM! And Master users are advised to *UNPLUG the Master's resident editor because of a command name conflict.

The Editor

This looks and behaves almost identically to the Master's Editor, except that for some reason I could not get the Change Mode key to work, and it uses a virtually identical keystrip. It is a nice piece of software, and is very easy to use. Once you have typed in your program it should be saved to disc in the C directory. You then use Shift-f4 to take you directly back to the C> prompt. Second processor users need to perform a *TC to reinstall the compiler. In the Beebug 'C' package, by contrast, there is no built-in editor; and users are recommended to use View, Wordwise, the Master Editor or som other word processor for the purpose.

The Compiler

To compile your program, just type:

COMPILE filename

at the C> prompt. Acornsoft's compiler operates in three passes, performing the following tasks:

1st pass: Preprocessing
2nd pass: Syntax check & code generation
3rd pass: Post processing

If this all proceeds without error, the newly created intermediate code is automatically saved away in the L directory, and you may proceed to link your program.

The Linker

It is at this stage that the final code is produced, and any required library files are automatically appended to the program. User-created library functions may also be appended at this point. At the end of this process you should have on disc in directory O a file which may be directly executed. To run it, simply type the filename (without the directory) at the prompt. If it doesn't run correctly, you must, as with all compiled systems, go all the way back to the editor to correct it; and then repeat the succeeding stages until you get it right.

Performance Tests

To check out the performance of Acornsoft's 'C' we have run it against two PCW benchmarks (converted to 'C' from Basic), and given comparative figures for the rival Beebugsoft package. To give you an idea of what a 'C' program looks like, we have included the listing for INTMATH. As you can see from the results, the compile time for the Beebug version is considerably quicker than Acornsoft's, though on INTMATH, Acornsoft's run time is a good bit better. These figures seem to be reasonably representative of the two packages, over other code on which we have tested them.

PCW BENCHMARK 1 (INTMATH)

/* program intmath */  | comment
#include <h.stdio>     | library file
main ()                | program start
{
int i,x,y;             | integer variables
i = 0;
x = 0;
y = 9;
puts ("start");        | basic print
while(i < 1000)        | main loop
{
x = x +(y * y - y) / y;
i++;                   | increment i
}
printf("Finish %d\n",x);  | formatted
}                         | print

You will also notice that the Beebugsoft code is much shorter in length than Acornsoft's. This is because the linker in the Acornsoft package includes all library routines, even though only a small subset may be required. Acornsoft do supply some subsets of their library to reduce this overhead; and of course in those situations where you need large parts of the library, the code lengths of the Acornsoft and the Beebug source code will be more comparable.

It should be said that neither the Acornsoft nor the Beebug compilers produce particularly fast code. Generally speaking, while the object code from both compilers executes faster than BBC Basic, their speed is much closer to Basic than it is to normal (hand produced) 6502 machine code. Just for the sake of comparison, we give times for INTMATH and REALMATH for the Borland Turbo C running on an Amstrad PC, which note, uses a 16 bit processor.

Benchmarks

  Compile Time Link Time Run Time Size (bytes)
INTMATH:
Acornsoft 40.4 36.8 13 8876
Beebugsoft 8.6 9.0 25 230
REALMATH:
Acornsoft 421 41.5 49 8885
Beebugsoft 8.7 8.8 49 238

The benchtests were performed on a standard Master 128 fitted with DFS v2.29 and 5.25" floppy disc. All times are in seconds.

Borland C Times

  Run Time Size (Bytes)
INTMATH 0.04 5484
REALMATH 3.70 19288

Conclusion

Acornsoft have produced a creditable implementation of 'C'. It follows the full Kernighan and Ritchie standard and is easy to operate, if a little slow to compile. The documentation is first rate, though be warned that the guide supplied is a guide to the product and not to the language. To use this package, you will need an additional text on 'C'.

The recommended book for both tutorial and reference is of course: The C Programming Language by B. Kernighan and D. Ritchie Prentice Hall, £22.75 (members).

Ray Hughes