By Richard Mansfield — originally published in COMPUTE!’s Gazette Volume 1, Issue 1
ISSUE: July 1983
There’s something amazing beneath BASIC. Each month in this column we’ll explore the mysterious, powerful world of machine language. Along the way, we’ll try some fascinating tricks and techniques. Everything will be designed to give you hands-on experience using ready-made, easily typed-in examples. I hope that you’ll soon come to feel that machine language is not much more difficult to learn or to use than BASIC.
From time to time you’ll hear someone say that they could never learn to program in BASIC because they aren’t good at math. If you’ve done any BASIC programming, you know that math has little to do with it. You can choose to write complicated mathematical programs, but few home computerists want to or need to. Instead, most of us write games, practical applications programs, music, or whatever interests us. But not math. BASIC commands such as SIN, TAN, and COS just sit there unused.
Going Below BASIC
Likewise, many BASIC programmers hear about this mysterious machine language and think that it’s only for physicists, engineers, or advanced professional programmers. For example, an author will explain that he or she wrote a game in BASIC, but added machine language in certain places for speed. Somewhere in the same article you might see a “program listing” of the machine language part and be convinced that it’s a lot less understandable than BASIC. After all, machine language uses special three-letter abbreviations instead of the English words of BASIC.
It is somewhat easier to learn the meaning of the STOP command than its machine language equivalent, BRK. But they do pretty much the same thing – they stop a running program – and it’s not that hard to remember that BRK is just shorthand for BREAK. But we’re getting ahead of ourselves. Let’s first try to define what machine language is and also try out some easy, preliminary excursions “below” BASIC. To understand the meaning of machine language we should first take a brief look at how BASIC itself works. Try typing in and RUNning the following:
Here we are PEEKing into the section of your computer’s memory which contains BASIC as a program on a Read Only Memory (ROM) chip – which is never erased. In the VIC, BASIC takes up memory locations 49152 through 57343, and in the 64 it goes from 40960 through 49151. In both cases, BASIC uses up 8K of memory, somewhat more than 8000 bytes. What we’re PEEKing here is a list of the BASIC command words. Also, at the end of that list is another “table” of words which are the computer’s error messages. (Notice that the computer can tell when a word ends and a new one begins because the last letter is capitalized.)
BASIC is just another program – albeit a very large program. But BASIC itself is not written in BASIC. BASIC is a machine language program.
Following The Trail
BASIC is a language that both you and your computer can easily understand. It’s the way you communicate your instructions (your programs) to the VIC or 64. That list of words we just extracted is sometimes called a lookup table and contains the 50 or so commands that you can use to program in BASIC. Let’s see roughly what happens when you communicate with your computer in BASIC. How is the word “Utah” printed on the screen?
First we type in:
10 PRINT “UTAH”
Then, if we announce to the computer that there is a program in memory to RUN (by typing RUN), it will look for the lowest line number and then analyze the meaning of your instructions following the line number. In this case, it would find a match to the instruction “PRINT.” After it located the match, it would then look down a separate list of memory addresses. If PRINT is the sixth word on the “words list,” then the computer would count down to the sixth word on the “addresses list” and would send control of your machine to the address it found. At that address is a machine language program which handles PRINT commands.
Now we can begin to see why programmers want to learn machine language – programs run far more efficiently than they ever could in BASIC. Why? Because BASIC is an “interpreter.” BASIC must interpret each instruction while a program is running. What’s more, a command like PRINT is very general. Not only does BASIC need to find out where (in ROM memory) the PRINT instructions are located, it must then also interpret a number of additional things. What’s the format for the requested PRINTing? Is there a TAB or SPC to deal with? A comma or semicolon?
In our Utah example, there isn’t any special format so BASIC must then decide if this is a request to print a literal string (something inside quotes), a variable, or a number. And so on. Each question must be answered by the computer before it can start putting something on the screen. And all this takes time.
BASIC always has to keep track of the current location of the cursor on the screen. This is how it knows where to put the next item it PRINTs. By the way, you can directly control the location of your cursor by POKEing new numbers into the place that BASIC looks for this information. On both the 64 and the VIC, the line location is held in address 214 and the position on that line is in address 211. You could type: POKE 214, 12 and the cursor would be moved down to the twelfth line. Or try POKE 211,10: PRINT “X”.
We’ve just followed the trail of the PRINT instruction, from lookup through execution. Remember that all of these events are going on while BASIC is executing a program. This approach is tolerably fast for many applications. After all, the computer can fly around asking and answering questions at impressive, electronic speeds.
Yet, because BASIC is all-purpose – it’s the Beginner’s All-purpose Symbolic Instruction Code – it’s never as fast as machine language. Machine language can be hundreds of times faster because you can print UTAH without needing to check for a quote or find a variable somewhere else in memory or whatever. You write a machine language program which is customized, tailored to serve no other function than to put the word UTAH where you want it. In one sense, printing Utah via machine language means that you are constructing a new BASIC command word which might be called PRINTUTAH.
An Experiment In High Velocity
Here’s one final experiment. You’ll see what’s meant by machine language speed and how valuable it is when you’re writing games. It will also illustrate the way that machine language can be combined with a BASIC host program. The machine language part is that series of numbers in the DATA statements. When RUN, these numbers are POKEd into memory to form a short machine language program that will fill your screen with whatever key you press. And it will change the entire screen instantly when you hit a different key.
After the numbers are POKEd into place, the computer executes the machine language program by a SYS command. This is essentially a GOSUB, but the target isn’t a BASIC line number. Rather, it’s the starting address of a machine language routine. When finished, control is returned to BASIC just as if the machine language routine had ended with a RETURN. The VIC version is a little longer to allow the program to work correctly on any memory configuration.
We’ll get into further explanations in the months to come, but you might want to write a short BASIC program to accomplish the same fill-the-screen task. You’ll then know why many people find that learning machine language is well worth their time.
