Some Assembly Required
by Robert Peck
Last issue we explored some of the advantages of machine-language programming, most notably its speed. This issue I'll introduce you to hexadecimal notation, a number system, like binary (Base 2) or decimal (Base 10) that is the heart of machine language. Don't worry, Basic programmers, I'll stick to vocabulary that you may already know.
Despite the mystery which seems to shroud machine language, it is simply a series of numeric codes that tells a computer what job to perform and how. Unlike Basic, which is an interpretive language, machine language is understood and acted upon directly by the computer.
When you want the computer to do something for you, you have to tell it how to do the job. Consider sending a robot to the corner store for a loaf of bread.
Your robot's first question might be: "How is that Job to be done?" You might respond "Drive to the store, get the bread and come back." But that's not enough data for a computer. Where's the car? How does he turn it on? What is bread and how do you pay for it? For that matter, what is a store?
You won't get away with simple instructions, unless your computer can infer from them all of the smaller steps that even the simplest financial transaction requires.
Think of these simple instructions as something called a "higher-level language," an interpretive language, much the way you and I communicate. But the computer can think only in small steps. These steps are the machine codes upon which the programmer builds to perform a higher level task. Given enough of these short instructions, the computer appears to "interpret" your most general wishes. All of this interpreting takes place in the central processing unit.
This central processing unit (CPU) can only understand numbers. These numbers may be used to represent letters of the alphabet, special characters, or other numbers.
Let's imagine the computer is a post office. Then let's think about the central processor as a mail clerk. This clerk is very efficient when he is told exactly what to do, and when to do it. But he's not very aggressive; he does only what he's told.
In front of him, is a set of mailboxes. His job is to sort the mail data into the correct box.
The clerk has his instructions written on a reference card. These instructions tell him, by number, exactly what to do. Here's what his card says:
1. Pick up a new envelope.
2. Read the name.
3. Read the address.
4. Read the zip code.
5. Compare the address on the envelope to those addresses on the mailboxes.
6. If the address is a match, then put it in the box.
If the clerk learns his job well, soon he'll know just what to do by the numbers. In other words, if he understands the numbers instantly, there is no need for him to read-and-interpret each of the sentences in order to get his job done. That's the difference in speed between an interpreter (Basic) and performing tasks by the numbers (as in a machine language program).
Let's carry the analogy one step further. If you were the postmaster and wanted to alter your clerk's list of duties you could rely on the same numbers. You might have to look up what the numbers represent in a list of your own, but this process is just like "assembling" a program. You only have to do it once, and your clerk (the central processor) can do the tasks at top speed each time he reads the list, even if it's in a different order.
The Importance of Hex
As you can see, numbers are important to the computer and to you as a programmer. In this column numbers will be written in hexadecimal. By using hexadecimal, you can express a value from zero to 255 in only two characters. The "two characters" are important, because the computer itself only understands binary.
Though binary may appear rather limited, given enough digits you can represent an infinite range of numbers. Here are the possible two-digit combinations:
Digit A Digit B
0 0 1 1 |
0 1 0 1 |
combination 00 combination 01 combination 10 combination 11 |
Each digit you add to the group doubles the different combinations of number sequences you can form. Three digits offer eight combinations; four digits offer 16 possible combinations. If there are eight digits, then 256 possible combinations from 0 0 0 0 0 0 0 0 (value 0) to 1 1 1 1 1 1 1 1 (value 255) can be formed. Your Atari is an eight-bit machine, which means it can read a data line eight digits long.
Let's see how that last combination of binary ones forms the decimal 255.
In the binary system, the first digit represents a zero or a one and every digit thereafter represents a power of two. Reading from right to left, the second digit, represents two raised to the first power, or two. The combination 1 1 in binary equals 2 + 1, or three, in decimal. Remember, you translate to decimal only those digits in the "on" condition. The eight-digit combination in our example represents "on" conditions for every power of two, up to 2(7) plus one.
1 1 1 1 1 1 1 1
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255.
Since all values are considered "on," we find the sum of all values in the first row by adding them together. Therefore, 128+64+32+16+8+4+2+1 = 255. While binary is rational to the computer, it's awkward for the rest of us. The number 130, for example is represented by the binary combination 10000010 and the number 80 by the binary combination 01010000.
|
Instead of using all of those binary digits, hexadecimal numbers can be used as a shorter way of showing the same number. Hex bites off four binary digits (bits) at a time and represents their value with a hexadecimal value (Base 16 number system).
The hexadecimal system represents the first 16 decimal units by using 10 decimal digits (0-9), and the first six letters of the alphabet (A-F). Table 1 shows decimal (Base 10), binary (Base 2) and hexadecimal (Base 16) conversions.
Let's convert binary:
0 1 0 0 ------- 4 bits |
1.1 1 0 ------ 4 bits |
to hexadecimal. Hex treats each four-digit group separately. In other words, the left four digits are represented by possible decimal values one-to eight; so too are the right-most four bits.
8421 0100 4 |
8421 1110 14 |
According to the conversion table, decimal 4 = hexadecimal 4, and decimal 14 = hexadecimal E. So binary 01001110 = hex 4E. In decimal this represents 4x16 or 64, plus 14 or the decimal 78.
So this is the number system you will most often see in my column. It will be used because it is closer to the number system the computer uses than the decimal numbers we use normally.
Next time we'll continue the computer/clerk comparison and see some of the ways the computer keeps track of the job it is supposed to be doing.
Robert Peck is a regular contributor to Hi-Res.