Lattice C
Programming: General · Sinclair QL · 1 players
Metacomco
MIA Missing in action. This is known to have existed, and no copy has ever been found. If you own one, the archives would like to hear from you.
Discussion
Nobody has started a thread about this yet.
In the magazines 1
ZX Computing #28Aug 1986 · p.28
David Nowotnik
PROGRAMMING IN C If you're bored with Basic but not yet up to machine code, there are alternatives. David Nowotnik looks at the increasingly popular 'C' language. Once the basics of BASIC have been mastered, then the Programming enthusiast often looks for a fresh challenge. One of the main motivations or this is for speed. For fast action games, BASIC is frustratingly slow. This sluggishness is a result of the fact that BASIC is a translated language. In other words, a BASIC program exists in memory in a form which is relatively easy tor us to understand, and this program is then translated by the routines in ROM to machine code, in run-time (while the program is in operation). It would be better for this translation process to be carried out before the program is in operation, simply to save time. Doing the translation this way is called compilation, and the result is a machine code program (or something close to machine code) which runs very much faster than the original translated program. COMPILERS You can buy BASIC compilers tor both Spectrum and QL computers, which convert your BASIC programs to machine code. However, these compilers are never fully compatible with all aspects of BASIC, so, more often than not, alterations have to be made to BASIC programs to allow them to be compiled and to run smoothly. Furthermore, BASIC was not designed to be compiled, and the machine code produced by BASIC compilers can still be rather slow and wasteful of space. Learning machine code (or assembly language) to achieve fast machine code programs is a daunting task for amateur programmers. For those with neither the time or patience to devote to mastering, the complexity of machine code, there are 'high level' languages which are designed to be compiled. Over the relatively short history of microcomputers, the fashionable alternative to BASIC has changed a few times. Pascal was the favoured language in the '70s but gave way to FORTH in the early 1980s. Now, top of the charts is the language of 'C'. The attraction of 'C' is that it adopts a structured style similar to Pascal, and allows close interaction with the machine operating system. 'C' is machine language orientated, compiling to fast and space efficient machine code, yet it is relatively simple to learn and to write in 'C'. 'C' originated in 1972 as the systems language of the UNIX operating system, designed by Bell Laboratories. Like many other languages, 'C' developed because of deficiencies in other languages: 'C' was based upon BCPL and 'B'. The former is still used, but the latter appears to have faded into obscurity. LURE OF THE 'C' The attraction of 'C' is its size. It has very few keywords, so it is easy to learn. Routines can be small, but very powerful. Many of the operations adopt a shorthand which save on typing, and enhance readability (eg. ++i is equivalent to LET i=i-1 in BASIC). Its structured style will be very familiar to SuperBASIC programmers on the QL. Programs are built up in a modular fashion using routines, called functions, which can be independent of each other. Each function can be written separately and tested independently before building into a program. By tradition there are no functions built in to the 'C' compiler; all compilers are supplied with a 'standard' library of functions and routines are 'borrowed' from the library in compiling a program. Like most libraries, new functions can be added by the user, or the supplier. This is also a feature of SuperBASIC, but 'C' has the advantage that all functions remain in the external library (rather than occupying ROM or RAM space), and are included only when required by a program. 'C' is not without certain disadvantages. Being small and powerful, it can be very difficult to debug. A particular problem lies in the multiple use of operators such as *,+,=, etc For example '=' and '==' mean quite different things, but a simple typing error could enter the wrong operator, which might take some time to spot and correct. It is beyond the scope of this article to provide a detailed description of 'C'. Due to its popularity, there are a number of books available which provide a tutorial on the language. The 'C' programmer's 'bible' is "The C Programming Language" by 'C' designers W. Kernigham and D. M. Richie, which sets the standard for the language. Good lower price options are "C at a Glance" by Adam Denning, and "Practical C" by Mark Harrison; both cost £7.95. However, to give a flavour of the language, fig. 1, contains a listing a a simple 'C' program, with its equivalent in BASIC. The BASIC listing was designed to run on both QL and Spectrum, and is a simple test of speed. The 'C' listing was typed into two 'C' compilers, one for the QL and one of the Spectrum, to determine how much faster the compiled program will run compared with the BASIC versions. More on that test later... Notice in the 'C' listing, there is one defined function called 'main'. This function appears in all 'C' programs, and is the starting point for all programs (other functions can be called from 'main'). The body of the function is contained within 'braces' (curly brackets); braces are also used to group together blocks of statements which combine logically together, eg, the do...while block. Variables can be defined as local (as in SuperBASIC) to the function, and these are normally defined at the beginning of a function. In the example, the keyword 'int' defines the variables a, b and c as integer. The mathematical expressions used in fig. 1, are very similar in both languages The 'printf' command is a standard function of formatted output to the screen. HISOFT C Hisoft £25 Hisoft's compiler costs £25 and consists of a tape cassette with the compiler on one side, the 'C' library on side 2, and two manuals. The main manual, 78 pages in length, provides instructions on how to use the compiler and text editor, with a reference section giving differences of Hisoft's implementation of 'C' to the standard of Kernigham and Richie. There is also a chapter explaining error messages. The second booklet explains upgrades of version 1.3 (the second issued version of this compiler) and responds to deficiencies reported by users in the original version. Once the compiler is loaded, the user is given the option to save the compiler to microdrive. The library routines on side two can also be transferred to microdrive once in the editor routine. The compiler is very simple to use, and Hisoft's package is ideal if you are learning to program in 'C'. Pressing 'EDIT' takes you into the text editor. When typing in a program, the text editor assigns each line a number. Line numbers are not part of the language of 'C', but they are used to simplify text editing. Once a program or routine is complete, compilation is simply a matter of returning to the compiler (pressing 'c'), then typing 'include'. The compiler appears to operate, unusually, in a single pass. Many of the standard 'C' routines are built into the compiler so there's often no need to access the library. If the compiler finds any errors, it stops to present an error number, line number, and short error message, of course, the actual error may not be on the line indicated (and the compiler did sometimes gets its line numbers mixed up), but with very few key presses the programmer can return to the text editor, edit the offending line, then back to the compiler for another try. Once all bugs are removed, the programmer indicates 'end of file', and the compiler asks if you want to run the program, and if the reply is yes, the compiled program is executed. In this mode, the compiler, text editor, text file and compiled code all exist in RAM at the same time, so the amount of space for 'C' programs is rather limited. The object code generated in this manner cannot be saved independently; to use a 'C' program in another session, the text file must be saved to tape or cartridge, then compiler and text file loaded at the next session, then the source file re-compiled and run. Alternatively, use of the 'translate' command in the source file causes the compiled code with run time routines to be dumped to tape, for use independent of the compiler. Compiled code can only be used when loaded to a start address of 25200, leaving just enough space for a short BASIC boot program. A feature very useful for beginners is the library; as it is in source code, the library can be loaded into the text editor and many lessons learnt from examining the routines. User routines can be added to the library, and Hisoft promise to add routines periodically. Library routines are added to the user programs with the 'include' command. A special variant of this command, '?include?' searches the library, and only includes routines which are required by the user program. Hisoft's compiler offers integer arithmetic only, but otherwise it is an excellent, easy-to-use package, ideally suited to both beginner and computer professional. Before moving onto the QL compiler, it's worth making the point that neither package offers any tutorial on 'C'. Both manuals make frequent reference to Kernigham and Richie's book. This is a must for professionals, although beginners may find one of the cheaper books, mentioned earlier, easier to use. LATTICE C Metacomco £99 At £99, the Metacomco Lattice 'C' compiler, called the 'QL C Development Kit', is much more up-market; not only in price but in the extra facilities on offer. It is a professional package for professional users. In its smart case, this package provides a plug-in ROM, software on three cartridges, and a 248 page manual. The ROM is the system's security mechanism, providing a few extra keywords essential to the operation of the system, and allowing the cartridges to be backed-up freely. The compiler uses Metacomco's highly-rated full-screen text editor, with a wide-ranging set of immediate and extended commands to make program development very easy. Once complete the source file is saved to disc or microdrive with a '_C' suffix, required for recognition by the compiler. Compilation is carried out in a two pass process followed by use of the GST 'Linker' (included in the package) to add library and run-time routines; all three steps are required to produce code suitable for running with the QL's EXEC OR EXEC_W commands. First pass compilation is initiated with the new keyword 'LC1'. Several arguments can be added to this keyword to provide a very wide range of options for the compiler, including adjusting workspace, producing list files, and setting the stack reserved space. The compiler, loaded from the first microdrive cartridge, displays any warning or error messages, automatically halting compilation with fatal errors. In a similar way, 'LC2' initiates the second pass loading the compiler routine from the second cartridge. Again, many compile time options are permitted. The second pass compiler signs off with a message providing information on code size, and the sizes of blocks for initialised and uninitialised data. Having called Metacomco's 'C' compiler a professional system, it must be said that it really needs a professional QL system for ease of use. With a standard QL, with no added memory it can take over 10 minutes to compile even a very short routine, such as shown in fig. 1. With 'only' 128K of RAM, the compiler needs to use part of the screen display as workspace, producing some interesting coloured screen displays. Most of that 10 minutes is devoted microdrive access, and the longest operation is using the linker. With added memory and discs total compilation time and linking can be less than a minute. A lot of space on the microdrives provided with 'QL C' is devoted to library routines, and a comprehensive set of Unix, mathematical, and QDOS functions are available to the user. When including any library routines, all the library is incorporated, giving rise to very large files, even with short programs (the object code compiled from the program in example in fig. 1, occupies 29 sectors on microdrive). Machine code functions can be included, provided they follow the standard explained in the manual. The compiler closely follows the Kernigham and Richie standard with full floating point arithmetic; and it also supports Macros. The compiler was designed by Lattice, so code written on the QL should be compatible with other Lattice compilers for 68000 and 8086/88 micros. 'QL C' may be expensive by home computing standards, but it is well worth the investment for those taking up 'C' programming seriously. SPEED TEST So what of the operating speed, mentioned at the beginning? Here are the results a the test programs in fig. 1. The Spectrum took 164 seconds to work its way through the BASIC version; In 'C' the program ran in just 9 seconds. The corresponding times for the QL are 82 an 7.5 seconds. Quite a saving! As a footnote to all those tempted to take a look at 'C', it is worth bearing in mind that many established software houses now program almost entirely in 'C', and it is predicted by some that 'C' will eventually take over from BASIC as the standard for microcomputers.
C Programming - 11 Fig 1. Simple programs to test operating speeds a) In BASIC 10 REMark A simple test of 20 REMark operating speed 30 REMark 40 LET a=0; LET b=0; LET c=0 50 REMark 60 PRINT "s" 70 REMark 80 LET a=a+1 90 LET b=5*a/10 100 LET c=a and b 110 IF a<8000 THEN GO TO 80 120 REMark 130 PRINT a 140 PRINT b 150 PRINT c b) IN 'C' /* A simple test of operating speed */ main () { int a,b,c; a=b=c=0; printf("s/n"); do { ++a; b=5*a/10; c=a && b; } while (a<8000); printf ("%d %d %d",a,b,c); }
Reviews from people here
Sign in to write about this, or make an account. It takes a minute and lets you correct the catalogue too.
No year, language, credit, description, tag recorded.
Sign in to correct or add to this.
Hold the rights to this and want it taken down?