C and the 68000
Kathy Yakal, Assistant Features Editor
A programming language written almost 15 years ago for a specific purpose--to develop an operating system for a mainframe computer--has been getting a lot of attention in the microcomputer community lately. C, a language described by many as simple, elegant, and powerful, is especially well-suited for programming the 68000 chip housed in the Commodore Amiga, Atari ST, and Apple Macintosh.
C is a beguiling language. In structure and difficulty, it falls somewhere between machine language and a higher level language such as BASIC or Pascal. C was developed in 1972 by Dennis Ritchie, who wrote it specifically to design the UNIX operating system running on the PDP-11 (a mainframe computer). Though some mainframe and minicomputer programmers have chosen to use it in the years since, it's enjoying a renewed popularity with the advent of the new 68000-based machines: the Atari ST, the Apple Macintosh, and the Commodore Amiga.
C is the language of choice for the Amiga and the ST, say many programmers, for four main reasons. First, its basic command structure is quite concise, but can be extended by individual programmers for specific functions. Second, its relative closeness to actual machine language gives the programmer tremendous power. Third, there is a harmony between the 68000 microprocessor and the C language; 68000 machine language itself supports C constructs, thanks in part to the similarities between the PDP-11 and the 68000. Finally, and perhaps most important for the home computer market, programs written in C on one 68000-based machine can be more easily and quickly transported to another computer than can programs written in many other languages.
Programmers consider several factors when deciding what language to use. Of course, everyone has favorites based on personal experience, but the physical capabilities of individual computers and the type of application being written necessarily create some restrictions.
Just as some computers are designed to support specific languages, some languages have been developed to support specific applications. You could accomplish almost anything in almost any language, but there is significant variability between languages in the efficiency of program writing and excecuting. Some languages are simply more appropriate than others for specific jobs.
One of the first high-level languages, FORTRAN, was written in 1954, and is geared especially for scientific formula translation. COBOL, developed around the same time, best supports business applications. BASIC and Pascal were intended to be teaching languages, sometimes making them unwieldy for particular applications.
C, written to develop an entire operating system, is less application- and machine-specific, and is amenable to various kinds of programs. Its skeletal architecture allows programmers--once they've learned the sparse command structure--to add their own routines, commands, and input/output functions called libraries (standard C libraries are also available commercially). It's really like the construction of an onion: You have a tiny seed in the center and layer upon layer covering it up. And the fact that input/output is extrinsic to the C language makes for greater portability.
So even though the language itself is small, you can end up with very large programs if you don't economize on your use of libraries, warns Tom Hospelhorn, senior analyst at Mindscape Software. "Sometimes you have very small C programs compiling to what seems to be a very large object" he says. "But what that's usually caused by is you've included a standard library of input/output functions with your object code even though your program may not actually use those." So if you're serious about keeping your finished C programs small, you want to avoid the automatic inclusion of a standard library. You can exclude any unused functions.
A Sample of CHere is an example program which prints the numbers from one to ten and describes each number as odd or even. Program 1 is written in Amiga BASIC. By comparing it to Program 2, the C version, you can get a sense of some of the differences between the two languages. For one thing, makes liberal use of braces---{ }--to group statements into a compound statement, or block, which the language treats as a single statement. Functions are called by supplying the name of the function, with arguments in parentheses--the entire program is a function called main(). Variables are declared before use, and comments are framed with /* and */ characters. Program 3 is C in a somewhat more condensed, but less easily visualized format. The IF-ELSE construct has been collapsed into one line using the conditional operator (? :) to designate the alternative results of the test. This brief sample, however, can't do justice to the qualities which make C an increasingly popular language. There are several excellent texts on C for beginners, available in most bookstores, which will give you a sense of the language's flexibility, power, and efficiency. Program 1:REM This is a demo program written in AmigaBASIC PRINT "The numbers from 1 to 10:" FOR count = 1 TO 10 PRINT count;" "; IF (count AND 1) = 1 THEN PRINT "odd" ELSE PRINT "even" NEXT count Program 2:/* This is a simple demonstration of a C prqgram, written for the Lattice C compiler on the Amiga computer */ main() { int count; printf("The numbers from ito 10:\n"); for (count = l;count <= 10;count++) { printf(" %d ",count); if (count & 1 == 1) printf("odd\n"); else printf("even\n"); } } Program 3:/* A more compact C version of the demo program */ main() { int count; printf("The numbers from 1 to 10:\n"); for (count = 0;++count <= 10; printf(" %d %s\n"",count,count & 1 ? "odd" : "even")); } |
C's affinity with machine language gives the user greater programming power, but can also create big problems for novice programmers. Some programmers suggest that C is best not attempted by people unfamiliar with machine language. At the very least, a disciplined, structured approach to C programming is highly recommended.
"If you haven't done a lot of assembly language first, going from Pascal or BASIC to C is going to be more of a challenge because it is getting closer to the machine," says Jeff Steinwedel, engineering manager at Activision Software. "It's like going from BASIC to assembly language. You have to know a lot more about what's going on internally. The language system is going to be less help in bailing you out of a situation or preventing you from making bugs in the first place. But if you've done a lot of assembly language programming, the transition to C will be fairly straightforward."
One of the characteristics shared by C and machine language is the ability to manipulate memory directly (in C, pointers are used). In higher level languages, memory access is often indirect. And C's heavy reliance on pointers is very powerful in general, says Hospelhorn.
But because you tend to do a lot of things with pointers, and because there's no error checking on pointers to see what they point to, you can wind up creating problems for yourself if you're not a disciplined programmer, he says. "It's possible to damage your program, to set memory locations that you didn't intend to if you're not well aware of where your pointers are pointing. That can make debugging sort of tedious. It's certainly possible for a programmer to generate very difficult errors--more so than some other languages that do a better job of checking things for you."
C, then, isn't a free lunch. What C gives you is a tremendous amount of power and flexibility, a feeling of liberation, he says, because most of the things you can think of, you can do in some way. But with that liberation comes a certain amount of responsibility. "It's always best to plan things out before forging ahead, but it might be a little more true in C."
And there's a big payoff after a C program is successfully completed and debugged: Translating that program to run on another 68000-based computer is easier than transporting a program from, say, an Apple II to an Atari 800. Part of the reason for this, of course, lies in the memory limitations of the eight-bit machines. "When you're in a very constrained machine without a lot of memory, like the Apple II, you tend to do a lot more in assembly language because you don't have the space," says Steinwedel. "With an Atari 1040 ST with a megabyte of memory, it probably isn't going to hurt you.
"But the real advantage is to be able to produce the code quickly and rapidly transport it from one environment to another. Something running in GEM on an IBM can move very quickly to the ST."
There's one additional reason for the popularity of C on the 68000-based machines, It's often mentioned almost as an afterthought, but it's significant. There are currently many good C compilers available. Other languages aren't as well-supported at this time with efficient, tested compilers. Lattice (Glen Ellyn, IL) and Manx Software Systems (Shrewsbury, NJ) publish C compilers that are enjoying popularity with 68000-based computer programmers, as do several other software publishers. There are also versions of interpreted C available, for programmers who want to debug more easily. Some use interpreters to write the programs and then, after all the problems are ironed out, run the source code through a compiler for the greatest runtime efficiency.
Although the friendly user interfaces of the Apple Macintosh, Atari ST, and Commodore Amiga have enticed nontechnical consumers to purchase them as applications machines, sales of languages and how-to books and other programming utilities indicate that many buyers are planning to write their own programs. The special benefits of C on 68000 machines recommend it to experienced machine language programmers. Likewise, people who have worked with higher level languages and want to move a bit closer to the inner workings of the computer might want to sample this special way of communicating with their machines. A decade and a half after its inception, the C programming language is approaching a new level of popularity.