Tandy gram; C - a useful alternative to Basic. (evaluation) Jake Commander.
For the last few months, I've been slaving away on the keyboard of may Model III. The intended fruit of this self-enforced hard labor is the deliverance of myself from the Basic Language. Not that I have anything against Basic: For the last 14 years it has served me like a faithful slave, obeying my every wish, command--and mistake. Whenever it has been too slow, I have resorted to the good old, low level assembly language with which I also feel most comfortable.
No, don't knock it. Basic hasn't become the celebrity it has for nothing. It is easy to learn, looks fairly readable (especially with a liberal sprinkling of remarks), and does the job. So why do I want to deliver myself from it?
Well maybe I'm overstating the situation a little. What I want to do is provide myself with an alternative to the choice of either slow but easily developed programs in Basic, or fast but harder to writer programs in machine code. When I first started programming professionally in 1968, I learned a language called Cleo which was similar in many ways to Cobol. This language was compiled into assembly language in a two-step process. The first step was to translate the Cleo source code into what was called Intercode. Intercode was a language in its own right and was much lower level than Cleo. The second step was to compile the Intercode into assembly language source code. So, starting with a level language, you ended up with an assembly language program.
The Intercode was the fascinating part. It was still a language in its own right but you could really get a feel for how its structure lent it to being compiled into pure assembly language. It was a simple task to learn the ins and outs of this language, and I soon abandoned dear Cleo in favor of my more powerful ally.
Being a machine code programmer at heart, I enjoyed looking through a listing to see how the compiler had turned each line of code into assembler source lines. I had discovered the power of a low level language. Unhappily, shortly after these discoveries, the company I worked for bought a Honeywell 6000 so I said goodbye to Intercode and hello to Basic.
Since that time, 95% of my programming efforts have been divided between machine code and Basic: two extremes--black and white--night and day. That is why I have been so hard at my keyboard; I have rediscovered another extremely powerful ally, the C programming language. Don't buy a Basic compiler, buy a C compiler. Basic was always meant to be interpreted anyway. Come to grips with a language that is as easy to learn as Basic and promotes a more disciplined approach to programming. Be Not Afraid of C
Those of you who are already into C probably knew all along what was coming. But for those of you who didn't, read on. With any luck, I can persuade you that there are alternatives to the either/or approach of Basic versus machine code. Deliverance from (or at least an alternative to) Basic can be yours if you keep an open mind.
For a start, C is not one of those weird languages that are so far removed from English that comprehension is possible only for a robot. (I'll barefacedly accuse APL and Lisp of being in that category.) If the Basic statement XX = 99 feels familiar enough, then the C statement XX = 99, shouldn't represent too large a leap of the intuitive processes. The best, though, is that XX = 99 in C is compiled into something like: LD HL,99 LD (XX),HL --pure assembly language source code, but still close enough to the original C line to retain some meaning even to someone not familiar with assembler.
That is how it is with the C language. Everything is compiled into a set of assembly language statements which perform the equivalent task. You end up with a bona fide machine code program which the computer pieces together instead of the set of actions which the computer would piece together under an interpreter.
In an attempt to convince Basic programmers that they would have an easy time learning C, I'll give a quick rundown of some of the features of the language. For those of you who want the whole works, there is a book called (appropriately enough) The C Programming Language by Brian Kernighan and Denis Ritchie, the designer of C. It is published by Prentice-Hall and is considered the bible of the language. I highly recommend it along with another book from Byte Books (more for beginners) called The C Primer by Les Hancock and Morris Krieger.
Assignment and Arithmetic Operators
If I asked you what the assignment operator was in Basic, you might have to think a bit before you answered "the equal sign." That is because you don't think of the term "assignment operator" when you write XX = 99. You're more likely to think (or at least I do) "XX equals 99" than "99 is assigned to XX." The point I'm making here is, don't be put off by menacing terms like assignment operators; you use operators such as these all the time in Basic without having to know their generic names.
In fact there are many things you come to take for granted about the syntax of Basic. When you are learning or reading about another language, terse descriptive terms look more menacing than the simple objects they describe.
So, in plain terms, the assignment operator in Basic and C is the same--the equal sign. The conditional operators are just about the same too; expressions like XX < = 99 are the same in C as in Basic. One difference here is that the Basic < > for "not equal to" is ! = in C (the exclamation point reads as "not"). In C, XX = !TRUE reads "XX equals not true." Sure, sure, I could have said XX = FALSE but I'm illustrating a point, OK?
Another difference in this area is that you don't use a single equal sign in a test for equality. Inc C, you use a double equal sign as in: if (XX == 1) (read it as "if XX is-equal-to 1"). This shows another thing you take for granted in Basic. That equal sign has a different meaning in XX = 99 and IF XX = 99 but you don't think twice about it. Inc C, they are kept separate. I'll confess that I still forget and occasionally use a single equal sign because of habits learned from Basic.
Arithmetic operators are the same as Basic, the usual signs for plus, minus, times, and divide with a percent sign for the modulus operator. There is no exponentiation operator in C--that is considered a high level function, and C is a low level language.
The Basic Boolean operators (those menacing terms again) are AND, OR, and XOR. In C, they are all single characters--an ampersand, a vertical bar, and a caret. Armed with what we know so far, let's look at the Basic line and equivalent C statement in Figure 1.
The two lines are similar enough to keep me similing, but here are the differences for anybody with a frown. A C statement always ends in a semicolon. There are no line numbers in C so you name the routines instead; in this example I've assumed the function "doit" does the same as the subroutine at line 100 in Basic. (Subroutines in C are referred to as "functions.")
The AND is replaced by &. The = becomes = = and the < > becomes! =. A part from the parentheses to enclose the "if" expression, that's it. The statement written in C will produce a Z80 routine on a Model 4 or a 6809 version on a Color Computer which will perform exactly the same task in machine code on either machine. Useful Functions
The FOR: NEXT loop in Basic has always been one of its most useful features. In C, it is even better. Imagine if in Basic you could say: FOR X=1 TO Y=10 STEP Z=Z+2 What you would be saying here is "start the loop with X equal to 1; end the loop when Y is 10; and add 2 to Z every time around the loop. This is exactly what you can do in C and it is an order of magnitude more powerful than the Basic version. The statement would look like: for (X=1; Y==10; Z=Z+2). Whereas a more typical Basic FOR: NEXT loop counting 1 to 10 would be: for (X=1; X==10; X=X+1).
This is still close enough to Basic to make the learning experience easy. That's my point. If you know Basic, you are already halfway to C. Moreover, if you know Pascal, you are 80% on the way. Like Pascal, C contains a "while" statement (which is even incorporated into some dialects of Basic these days). For those unfamiliar with the "while" statement, Figure 2 offers an example.
This assumes a function called print which presumably not only prints the value of X, but changes the value of Z so the loop can terminate at some point. This example is not all that different from a FOR: NEXT loop, except that there is no initialization or step size expression in the parentheses.
You can break out of any loop in C (including a "while," a "do while," or a "for") by using a break statement. A favorite statement of mine in C is the switch statement. This is a little like the Basic ON...GOTO construction. Imagine you have a variable CH which contains a number between 2 and 4. In Basic you would say: ON CH-1 GOTO 100,200,300 What happens if CH is 1? Or 5? or 100? C is ready with the switch statement like this: switch (CH) * case 2: * one (); break; * case 3: * two (); break; * case 4: * three (); break; * default: gotcha (); *
This statement simply executes whichever case is appropriate according to the switch variable (in this instance, CH). Each case is terminated by a break to drop out of the switch statement once the action is complete. Not only that but now we would catch an errant number 1 or 5 or 100 with that default case. Very easy and very watertight.
That is as far as I aim to go in tempting you with a description of its syntax. I've made a noise about some of the similarities to Basic in order to convince Basic aficionados that programming in C isn't the gigantic mental leap that you might think. There are, of course, some things that are different. But not that many. A look at the C statement words alone shows how much you know already: if, else, while, do, for, switch, case, default, break, continue, return, and goto.
One aspect of the language that has been jealously guarded by its writers since its inception has been its portability. A properly written C program is almost certain to run on a different machine (mainframe, mini, or micro) without a single alteration. A library of C programs is, therefore, more valuable than a Basic library due to myriad versions of polluted Basic floating about.
I hope I have convinced somebody. I convinced myself a while ago, and I am enthusiastic enough about the language to pass some of it on. I'm writing a compiler using C--a task which would be an absolute bear in assembly language but is nearly a joy in C. Available Compilers
There aren't many C compilers around for the TRS-80, but choices to exist. If I can get my hands on any, I'll review them here. For the Color Computer there is the Radio Shack OS-9 version of C at $100 which is a fully implemented compiler. (It is possible to buy cheaper compilers which implement only the most commonly used parts of the language.) Dugger's Growing Systems offers C for 05-9, Flex, and Color Computer DOS at prices between $50 and $75.
For the Model 4, there is PRO-LC from Misosys at $124.95 including an editor and macro assembler. Manx Software Systems also produces C for the Models II, III, 4,12 and 16 with prices ranging from $75 to $2000.
Good grief. I didn't leave any room to say thanks for the letters. Some of them I'll answer in the column if I can stop myself from getting on such a roll in the future. Also, I meant to review ADOS from Spectrosystems and Spectrum DOS from Spectrum Projects. Next month, guys. Honest. That's if I don't get into Fortran.