Download as pdf or txt
Download as pdf or txt
You are on page 1of 104

CHRISTOPHER LAMPTON

.
BOSTON
PUBLIC
LIBRARY
COMPUTER LANGUAGES
“Will you shut up and listen?!!”
CHRISTOPHER LAMPTON

COMPUTER
LANGUAGES

A Computer-Awareness
First Book

Consulting Editor
Melvin Berger

Franklin Watts
New York I London I Toronto
Sydney 11983

WEST ROXBURY
Photographs courtesy of Rothco Cartoons: opp. title p. (Seeman);
Apple Computer, Inc.: opp. p. 1 and p. 54; IBM: p. 2; New York
Public Library Picture Collection: pp. 6 (right and top left) and 32;
Texas Instruments, Inc.: pp. 6 (bottom left) and 69; Sidney Harris: pp. 9
and 19; Timex Computer Corp.: p. 15; Control Data Corp.: pp. 16 and 28
Wang Laboratories: p. 20; Tandy Corp./Radio Shack: pp. 21, 50, and 74;
Hewlett-Packard Co.: p. 43; Commodore Business machines: p. 56;

Library of Congress Cataloging in Publication Data

Lampton, Christopher.
Computer languages.

(A Computer-awareness first book)


Includes index.
Summary: Introduces computer programming languages
including FORTRAN, COBOL, BASIC, PASCAL, and LOGO.
1. Programming languages (Electronic computers)—
Juvenile literature. [1. Programming languages
(Electronic computers) 2. Computers] I. Title.
II. Series.
QA76.7.L35 1983 001.64'24 83-6493
ISBN 0-531-04638-9

Copyright ® 1983 by Christopher Lampton


All rights reserved
Printed in the United States of America
5 4 3 2 1
CONTENTS

Chapter One
The Language of the Machine 1

Chapter Two
Putting it in Words—Assembly Language 13

Chapter Three
FORTRAN and the High-Level Languages 27

Chapter Four
COBOL—A Language for Business 42

Chapter Five
BASIC—A Language for Beginners 52

Chapter Six
PASCAL—The Language of Structured Programming 61

Chapter Seven
LOGO—The Language of the Turtle 67

Final Note 73

Glossary 76

Index 81
Digitized by the Internet Archive
in 2018 with funding from
Kahle/Austin Foundation

https://archive.org/details/computerlanguageOOIamp
COMPUTER LANGUAGES
imm))
CHAPTER ONE

THE LANGUAGE OF
THE MACHINE

How do you talk to a computer?


Well, the short answer is that you can't "talk" to it at all. It
won't be able to hear you (unless it's equipped with special voice-
recognition equipment). Still, for a computer to be useful to us,
we must have some way of communicating with it, interacting
with it, telling it what we want it to do. So if the spoken word
will not suffice, the written word must—and there are several
ways of transmitting written messages to a computer. One of
them is through a keyboard, like that on a typewriter. Anyone
who has ever seen a home computer knows what such a keyboard
looks like. To communicate with the computer, you simply sit in
front of the keyboard, type your message on the keys, and wait
for the computer to respond.
There are other ways of communicating with a computer.
Some large computers can "read" pieces of paper called punch
cards, which have holes poked in them according to a special
code. Most of us have seen these peculiar holes punched in forms
we are sometimes required to fill out. These holes contain mes¬
sages that a computer can be equipped to understand.
Once we have a means of communicating with a computer,
however, we still have to know what to say. A simple "Hello.
How are you doing?" would seem sufficient, but to a computer

[1]
Some large computers can “read” the holes
punched in this IBM punch card.
this would most likely be gibberish. A computer can be "taught"
to recognize this and other simple phrases, but by and large the
complexities of human languages are quite beyond these amaz¬
ingly feebleminded machines.
No. In order to speak to a computer, we must address it in its
own language, or in a language that can be translated into the
computer's language. English is much too complicated and will
not do, except in highly simplified form.
What is the computer's own language? Numbers, mostly.
And, because the computer is, as we said, surprisingly feeble¬
minded, it can recognize only two numbers—zero and one. Yet it
can do a great deal more with these two numbers than you might
guess.
The numbers zero and one are popular with computers
because they are so easy to represent. Ordinarily, we represent
the numbers zero and one like this: "0" and "1." However, we
can also represent them by writing the words "zero" and "one,"
as we did earlier in this paragraph. Can you think of other
ways?
Well, consider a light switch. It can be in either of two pos¬
sible positions—on or off. Let us say that a switch in the "on"
position represents the number one, and a switch in the "off"
position represents the number zero. Why would we say this? For
no particular reason, except that it can be said and is easy to
understand—so easy that even a computer can understand it. The
nice thing about a light switch is that it is either on or off; it can't
be in between. (Of course, some lights are controlled by devices
called rheostats, which allow us to make the light dimmer or
brighter, as desired, but that's not the kind of switch we're talk¬
ing about.)
Inside a computer, there are millions of tiny switches. These
switches are not exactly the same as the ones that control the
lights in your home. You can't reach inside the computer with
your hand and turn them on or off. The effect, however, is simi-

[3]
lar. Each switch can be in only one of two possible positions. And
the two positions these switches take can be represented by the
numbers zero and one.
When computer technicians “teach" a computer language,
they set a series of electronic switches into an on or off position.
In effect, they are putting zeros and ones into the computer. On
early computers—and with some modern computers—this is
done with a panel of actual switches, much like light switches,
that can be switched on or off depending on whether we want to
put a zero or a one into the computer. Generally, these zeros and
ones tell the computer how to behave.
Usually, when we teach a computer to understand language,
it is because we want it to follow a set of instructions that will
cause it to perform a specific task. Such a set of instructions is
called a program, and the person who writes the instructions is
called a programmer.
How do these instructions work? Well, for a moment let's
think of the inside of the computer as being like a railroad track.
Imagine that you are the engineer of a freight train. Ahead of
you, the track forks off in two different directions. At the fork in
the track is a switch (not exactly like the switch we use to turn on
a light, but similar in operation). If the switch is in one position,
your train will take the left track. If the switch is in the other
position, your train will take the right track.
Suppose further that you are unable to stop your train to get
out and throw the switch. You are forced to go in the direction
that the switch is pointing when you come upon it. And no soon¬
er do you go through this fork than you come to another, with a
similar switch determining the direction your train will take.
Beyond that is another fork, and beyond that is another. By the
time you reach your destination, whatever that may turn out to
be, you will have passed hundreds of such switches.
Assume now that, along each path that you didn't take, there
was a similar succession of forks and switches. Each of these

[4)
switches was thrown in one direction or another. If any switch
along your path had been thrown in a different direction than it
actually was, you would have gone off in one of these other
directions and ended up at a different destination. In fact, your
precise destination was determined by the positions of all the
switches that you passed. If there had been only one fork along
your path, you could have ended up at either of two different
destinations, depending on how that switch was set. If there had
been eight forks along your path, you could have ended up at any
of 256 (two multiplied by itself eight times) different destina¬
tions, depending on how each switch had been set. If there had
been sixteen switches, you could have ended up at any of 65,536
different destinations. With hundreds of different switches, you
can only begin to imagine the number of possible destinations.
So it is with a computer. Whenever we put a bit of informa¬
tion—a zero or a one, say—into a computer, the eventual desti¬
nation of that bit depends on the positions of hundreds of thou¬
sands of different switches. This gives us the ability to control
what goes on inside the computer. Suppose, for instance, that we
wish to place a small white dot on a video screen, the TV-like
monitor that accompanies the computer. Suppose further that, in
order to do this, we must put a certain switch into the “on" posi¬
tion. Since this position represents the number one (as we agreed
earlier), we must first put a one into the computer and direct it
toward that switch. In order to direct it toward that switch, how¬
ever, we must first set the proper switches on all the "tracks"
leading to that switch. And to do this, we must put a whole series
of zeros and ones into the computer to prepare the way.
Fortunately, when we program a computer, we do not have
to think in terms of "throwing switches." We do not even have to
think in terms of the specific path that our instructions take
through the machine. This is because other programmers have
taken those zeros and ones that throw the switches within the
computer and broken them up into so-called operation codes—op-

15]
Very early computers were electromechanical devices that
required large panels of complex wiring and manual
switching. Right: the control panel from the early IBM
model 402 accounting machine. Left: the Mark I computer,
built in 1944 and containing 3,300 relays, all triggered
by mechanical devices. Below: today’s computers use
microprocessors, such as this one that has been magnified
many times. So-called computer chips are about the size
of a fingernail and are much more powerful than the
room-sized computer components of the early days.
codes for short. As the name implies, these op-codes (which
usually come in chunks of eight or sixteen zeros and ones) per¬
form specific operations inside the computer. For instance, a giv¬
en op-code might move a number from one place to another
inside the computer. Or it might add one number to another and
store the result in a specific place inside the computer.
When we store a number inside the computer, we say that
we are putting that number in the computer's memory. Comput¬
ers don't have quite the same kind of memory that human beings
do, but they do have blocks of switches that can be used to store
numbers, if those numbers are represented as zeros and ones.
This makes it seem as though the computer is "memorizing" the
numbers.
Programming with op-codes is called programming in
machine language. Machine language is the only language that
machines called computers actually "understand."
Of course, the computer doesn't understand the instructions
in the way that a human being would, any more than it can
"think" the way a human being does. It only "understands" in
the sense that it will make an appropriate response to the instruc¬
tion. And it will make exactly the same response each time—
which is why computers do some jobs (those involving a lot of
repetition) better than human beings do, and other jobs (those
involving creative thinking and imagination) not as well as
human beings do.
Here is a short program in machine language:

00111110
00000011
00000110
00000100
10000000
00110010
01100110
01111110
[7]
Different brands of computers use different sets of op-codes, so
these instructions might mean one thing to one computer and
something completely different—or nothing at all—to another.
To certain small computers, however, these op-codes mean to add
the numbers four and seven and store the result in the comput¬
er's memory.
We don't have to represent op-codes as zeros and ones. There
are several alternatives, which we will discuss later. First, how¬
ever, let's look briefly at a new way to count.
Ordinarily, when we count, we use ten different digits: 0, 1,
2, 3, 4, 5, 6, 7, 8, and 9. All of the numbers we ordinarily count
with are made up of these ten digits. This is the decimal number¬
ing system. The word decimal comes from a Latin word meaning
"ten."
There is a special numbering system, however, called the
binary numbering system, that uses only the digits 0 and 1. The
word binary comes from a Latin word meaning "two." When you
count in binary, you start with the number 0 or 1, just as you do
in decimal. But the next number after 1 is 10. This is equivalent to
the digit 2 in the decimal numbering system. (When we pro¬
nounce the binary number 10, we do not call it "ten." This would
confuse it with the decimal number ten. We simply say "one-oh"
or "one-zero.") The binary number after 10 is 11, and the number
after 11 is 100. The binary numbers 11 and 100 are equivalent to
the decimal numbers 3 and 4. And so on. (It is not necessary for
you to learn how to count in binary, so long as you keep in mind
that binary is essentially a system for counting that can be used in
place of the decimal system.)
Because the op-codes that we studied a moment ago are made
up entirely of zeros and ones, we can look at them as binary num¬
bers. This is important, because for every binary number, there is
a decimal number that means precisely the same thing. This
means that a programmer does not necessarily have to write a
program as a series of zeros and ones. Instead, he or she can write

[8]
“It was bound to happen—they’re
beginning to think like binary computers.’’
it in decimal numbers. The computer itself can translate these
numbers into binary.
This introduces a very important idea, one that will reappear
many times in this book. It is possible for a programmer to write a
program in one form and have the computer itself (actually,
another program inside the computer) translate that program
into another form. Thus, the programmer can write the program
in a form that he or she finds comfortable, and the computer can
translate it into a form that it can work with.
In decimal numbers, the machine-language program above
would look like this:

62
3
6
4
128
50
102
126

You can see that it would be easier to write a program in decimal


than in binary. There are fewer digits to keep track of, and each
digit has a more distinctive look to it than all of those zeros and
ones. The programmer can memorize the op-codes more easily
and is less likely to make a typing error.
However, there are drawbacks to using the decimal number¬
ing system for programming. These drawbacks are technical in
nature and difficult to describe. Suffice it to say that program¬
mers find it awkward to write programs strictly in decimal num¬
bers. Furthermore, it is difficult to convert numbers from decimal
to binary, and the programs that tell the computer how to do this
tend to be both long and slow.

[10]
Fortunately, there are other numbering systems besides the
binary and decimal. For instance, there is octal (from a Latin word
meaning "eight"). The octal numbering system uses eight digits,
from 0 to 7. And there is the hexadecimal system, which uses a full
sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. (Notice
that in order to supply all sixteen digits, hexadecimal uses not
only the ordinary decimal digits but the first six letters of the
alphabet.)
The advantage of using the octal or the hexadecimal system
is that translating a number from one of these to binary, or vice
versa, is very easy. Programmers can do it in their heads. And
most of the awkwardness that accompanies programming in
decimal does not accompany octal or hexadecimal program¬
ming.
In hexadecimal, our machine-language program would look
like this:

3E
03
06
04
80
32
66
7E

Because these hexadecimal op-codes are short and distinctive,


they are much easier to remember and to use. The task of trans¬
lating these numbers into binary op-codes is left to the computer,
which is (as we saw earlier) good at repetitive jobs.
By the mid to late 1940s, most computer programming was
done in either octal or hexadecimal. Even so, programmers were
dissatisfied. Octal or hexadecimal numbers were easier to use

[11]
than strings of zeros and ones, but it still required extensive plan¬
ning on the part of the programmer to assemble a series of op¬
codes into a program. And though octal or hexadecimal op-codes
were easier to remember than binary op-codes, they were still far
from unforgettable. It would be so much simpler if the op-codes
were in a form that actually bore an obvious resemblance to the
computer operations they were responsible for. It would be nice,
for instance, if the programmer could simply say "ADD" rather
than writing the hexadecimal op-codes for addition. Further¬
more, it would be nice if the programmer could turn some of the
more repetitious tasks of programming over to the computer,
which is so good at performing repetitious tasks.
In the next chapter, we will see how both of these goals were
accomplished with the introduction of assembly language.

[12]
CHAPTER TWO

PUTTING IT IN
WORDS—ASSEMBLY
LANGUAGE

If a computer could translate op-codes written in hexadecimal or


octal, couldn't it also perform a similar translation on op-codes
written as words? The programs required to tell the computer
how to do this would be very complex, but think of the ways in
which we would benefit. We could write op-codes as words—
words that would actually describe the computer operation the
op-code performed—and the computer could translate these
words into binary. Programmers would no longer be forced to
memorize long lists of hexadecimal or octal op-codes. Instead,
they could simply memorize words, which would be a great deal
easier.
The programs that were developed to perform this transla¬
tion were called assemblers, because they "assembled" a binary
computer program out of a series of words. The words them¬
selves were called mnemonics (pronounced nih-mon-iks), from a
Greek word meaning "memory," because they were easy to mem¬
orize. A program written in mnemonics was said to be in assembly
language.
An assembly-language version of the program in the last
chapter would look like this:

LD A,3
LD B,4
[13]
ADD A,B
LD (32358),A

This program is written in the mnemonics understood by the Z80


microprocessor, the tiny "computer-on-a-chip" found at the
heart of several popular personal computers, including the Radio
Shack TRS-80 and the Timex Sinclair 1000. The first line of this
program tells the computer to load (LD) the number 3 into the
computer's memory in a special place—a storage cell, if you
will—called "A." The second line tells it to load the number 4
into a second storage cell called "B." Line 3 tells it to add the
number in storage cell B and the number in storage cell A. All the
arithmetic in this particular computer takes place in storage cell
A. In fact, we can think of "A" as standing for "arithmetic" or
"addition," if that makes it easier to remember, though actually it
stands for "accumulator," which is a programming term meaning
"the place where arithmetic is performed." The final instruction
tells the computer to take the result of the last operation (which is
still stored in cell A) and put it in the cell numbered 32358. (Most
computers contain many thousands of memory cells. Only a few
such cells are privileged enough to have letter names. The rest
are simply numbered.)
Does all of this seem like a rather complicated way of adding
two numbers together? Well, it is. Assembly language made pro¬
gramming easier than it had been, but it still didn't make it easy.
(Don't worry, though; there are much easier forms of program¬
ming that we will discuss in later chapters.) Still, an assembler is
more than just a translation program. It also performs a number
of simple but time-consuming tasks that would otherwise be the
responsibility of the programmer.
For instance, the assembler introduced the important con¬
cept of labels, a concept that will pop up in most of the computer
languages we will study. To see how labels work, though, we

[14]
The Timex Sinclair 1000
first have to understand how a program is stored in a computer's
memory.
********

We saw a moment ago that a computer contains special cells in


which numbers can be stored. Because these cells allow the com¬
puter to “remember" these numbers and retrieve them later, we
call them memory cells. An average microcomputer will have any¬
where from roughly 16,000 of these cells to more than 256,000 of
them! The size of the number that can be squeezed into an indi¬
vidual cell depends on the size of the cell itself. Most microcom¬
puters have memory cells that can hold an eight-digit binary
number; that is, each cell can hold a string of eight zeros and
ones. The largest binary number than can go into an eight-digit
memory cell is 11111111, which is the equivalent of the decimal
number 255. Thus, any one of these memory cells can hold any
decimal number from 0 to 255.
All memory cells are numbered (except, as we said earlier,
for the few that are assigned identifying letters). We call these
numbers addresses, because they help us find specific memory
cells when we need them, just as the addresses on houses help us
find specific houses when we need them.
We can imagine these memory cells as being lined up inside
the computer on a long wire, something like this:

Memory Cells Addresses


00111110 — 24062
00000011 — 24063
00000110 — 24064
00000100 — 24065
10000000 — 24066
00110010 — 24067
01101110 — 24068
01111110 — 24069

[17]
The wire (which is actually a whole series of wires, or, in many
cases, a printed circuit) allows us to communicate with these
memory cells, so we can put numbers into them or take numbers
out of them. The assembly-language program we looked at ear¬
lier put a number into a memory cell with the instruction LD
(32358),A. This instruction moved the number that was stored in
the special-purpose memory cell A into the memory cell with the
address 32358.
What do the numbers inside these memory cells mean? Well,
there are a number of things they could mean, depending on the
situation. They might just be numbers. For instance, a program¬
mer may have stored these numbers in memory cells so that the
computer could perform arithmetic on them at some later time.
Or perhaps they are the result of arithmetic operations that the
computer has already performed.
On the other hand, these numbers might represent letters.
How can numbers represent letters? Through the use of special
codes, such as the ASCII code. (ASCII stands for American Stan¬
dard Code for Information Interchange and is usually pro¬
nounced "askey.") This code assigns numbers to represent letters
and other symbols such as numerals and punctuation marks. The
ASCII code for the capital letter "A," for instance, is 65. (The
lower-case letter "a," on the other hand, is represented by the
number 97.) The ASCII code for a period (//.,/) is 46. The code for
the number "3" is 51. And so on. If we wanted to store text—
words, sentences, paragraphs, etc.—in a computer, we would
probably use the ASCII code. Of course, most computers automat¬
ically translate letters typed on their keyboards into ASCII code
numbers, so the programmer usually doesn't have to worry about
this. ASCII-code memory storage is especially important in com¬
puter programs called word processors, which turn computers into
fancy typewriters. (This book, for instance, was originally typed
on a computer programmed as a word processor.)
Finally, the numbers that we've stored in our series of mem-

118]
“The problem seems to be
in the memory bank.”
rj*iSita^-J-Stttt•tffltl*WiIt»±#’> 1 35 !*r>!>
v><ftoi.ctttt f - 9 mmvt - t t?i'ttt«~> <«■
•dial 967#-»--3-?®87-7s
*Rft¥^’>tl!8iti,tfr.*|.i®e>r#8! 000£jJ •
v'clS^eA-MT-fj X99 i ll
, tc.MtaMMr£*‘tt'* t

f^\ |P^, |P^

This Wang word processor can be used to create,


edit, and print documents in Chinese. The
standard ten-key pad can reproduce some 10,000
Chinese ideograms, or symbolic characters.
Computers with word-processing programs
are usually attached to “letter-quality” printers,
such as this one from Radio Shack.
ory cells may be binary op-codes. This is probably the most
important use of a computer's memory. We use it to store pro¬
grams. And if you look back at the sequence of memory cells on
our chart, you will see that the numbers they contain are the
same as the op-codes in the machine-language program we intro¬
duced in the last chapter.
The way that we program a computer, in fact, is to store a
sequence of op-codes in a sequence of memory cells. Generally,
these op-codes are in order according to the way in which we
wish the computer to perform (or execute) the operations they
represent. That is, the instruction that we wish the computer to
perform first is in the first memory cell, the second instruction is
in the second memory cell, and so on. (It should be noted that
many op-codes take up more than one memory cell.)
There is a special set of instructions, however, that we call
jump, or branching, instructions. The mnemonic for a jump instruc¬
tion might be JP or JMP or even JUMP. (Or it might be BRA, for
"branch." Different computers use different mnemonics.) A jump
instruction says to the computer: "Jump to the memory cell with
address such-and-such, and perform the instruction that you find
there and all of the instructions in the following cells, until you
come to another jump instruction." In other words, the jump
instruction allows the program to jump around in memory, per¬
forming a few instructions here and a few instructions there,
rather than executing them in the order in which they are stored.
It may even instruct the computer to jump backward and perform
once again instructions that it performed earlier. (In fact, this is
one of the most common uses of jump instructions.)
A jump instruction might be written like this:

JP 18776

This tells the computer to jump (JP) to the memory cell with
address 18776 and start performing the instructions that begin

[22]
there. The computer will continue to perform those instructions
until it encounters the next jump instruction.
An average assembly-language program will contain many
jump instructions. Each of these will direct the computer to pro¬
ceed to a specific memory address and perform the instructions
beginning there. Obviously, this requires that the programmer
keep track of a large number of memory addresses. When the
programmer reaches the portion of a program that requires a
jump, he or she must be prepared to specify the memory address
to which the program is supposed to jump.
There is a problem with this, however. The programmer may
not know that address. In fact, he or she most likely does not
know it. When writing a program in assembly language, the deci¬
sion as to where in the computer's memory the program will be
stored is usually left until the last minute. Even then, all the pro¬
grammer need do is specify the memory address where the com¬
puter should store the first instruction. The computer then fig¬
ures out where to store the rest. The programmer may never
know which memory addresses hold which instructions.
How, then, does the programmer write a jump instruction?
How can you tell the computer to jump to a specific memory cell
if you don't know which memory cell you want it to jump to?
The answer is labels. When writing an assembly-language
program, the computer allows the programmer to give names, or
labels, to specific instructions. We can then refer to those instruc¬
tions by name rather than by memory address. When it comes
time for the computer to assemble the program into machine lan¬
guage, it translates these labels into addresses.
We can, for instance, refer to an instruction as FRED or
RUMPELSTILTSKIN, if that helps us remember it. More likely,
we would give it a name that actually describes what the instruc¬
tion does. For instance, a program might contain a series of
instructions for converting decimal numbers into binary num¬
bers. We might use the label DECBIN for these instructions.

[23]
When we want the computer to jump to the part of the program
that performs this task, we would say JP DECBIN, that is, jump to
the memory cell that contains the series of instructions that we
have labeled DECBIN and perform those instructions. In a pro¬
gram for a video arcade game, we might have a series of instruc¬
tions that draws a picture of a spaceship on the video screen. We
could call this series of instructions SHIPDRAW. Then, whenev¬
er we wanted the computer to execute these instructions, we
would write JP SHIPDRAW. Of course, these labels would be
meaningless to the computer when it comes time to execute the
binary version of the program, so the assembler program must
translate all these labels into the actual addresses where the
labeled instructions reside.
Labels are one of the reasons that assembly language is easier
to use than machine language. No longer does the programmer
have to keep lists of memory addresses to be plugged into jump
instructions—lists that can be compiled only after a program is
completely written, since any revisions to the program may cause
these addresses to change. The computer can do this monoton¬
ous, repetitive work itself.
Let's take a look at an example of an assembly-language pro¬
gram. Don't worry if it seems difficult to understand. You won't
need to remember how any of it works. Rather, the program is
designed to give you the flavor of assembly language, not teach
you how to use it. Later, we will learn much easier ways of writ¬
ing a program. Seeing an assembly-language program may help
you appreciate how truly easy these other methods are.
Following is an assembly-language program that tells the
computer how to add together and store the sum of a series of
numbers that were previously stored in the computer's memo¬
ry:

ORG 4000H
LD HL.6A7FH

[24]
LD A,0
LD B, 10
LOOP LD D,(HL)
ADD A,D
INC HL
DEC B
JP NZ, LOOP
LD (977EH), A

How does this program work? Because it is so complex, we won't


explain it step by step, but let's look at a few of the more impor¬
tant parts.
The word ORG in the first line is short for ORiGin. It tells the
assembler to place the first binary instruction of the program in
the memory cell with the address 4000H. The "H" at the end of
this address indicates that it is in hexadecimal.
The next instruction, LD HL, 6A7FH, tells the computer to
place (load) the hexadecimal number 6A7FH in special memory
cells H and L. Why are we putting the number into two memory
cells instead of one? Because it is too big to fit into a single mem¬
ory cell. In fact, we can envision the H and L memory cells as
forming one big memory cell that we can call HL. This memory
cell can hold sixteen binary digits; therefore, we can use it to hold
any number from 0 to 65,536.
In the next line, we find the instruction LD A,0. This simply
means to load the number 0 into memory cell A. LD B,10 puts the
decimal number 10 into memory cell B.
In the next line, we find an example of a label. This instruc¬
tion is labeled LOOP. We will come back to loops in a moment.
What does INC HL mean? INC is short for "increment." This
instruction tells the computer, "Add 1 to the number in memory
cell HL." You may recall that HL contains the number 6A7FH. If
we add 1 to that number it becomes 6A80H. (This is hexadecimal
arithmetic, remember?)

[25]
DEC means the opposite of INC. DEC is short for "decre¬
ment." It means to subtract 1 from the number in B. The number
in B is 10. After this instruction, the number in B is 9.
The instruction JP NZ, LOOP says to the computer: "If the
result of the last arithmetic operation you performed was not
zero (NZ), then jump (JP) to the instruction labeled LOOP." The
last arithmetic operation was DEC B, which subtracted 1 from the
number in B. The result of that operation was 9, which is defi¬
nitely not zero. Therefore, the program jumps back to the line
labeled LOOP and begins to execute instructions again from that
point. When a program doubles back on its own path like this, we
say that it is in a loop. (That is why this particular instruction is
labeled LOOP; it is the first line of the loop.)
Why are we doing all of this? As we said before, this pro¬
gram takes ten numbers from memory and adds them together.
The way in which it does this is too complicated to explain here
in detail. Suffice it to say that each time the computer executes
the loop, it adds a number to the total, and since it will execute
the loop ten times, it will add a total of ten numbers. The last
instruction, LD (977EH),A, stores the total at hexadecimal memo¬
ry address 977EH.
Even though this program is not easy to explain, it is actually
an extremely simple one. Most assembly-language programs are
a great deal longer and more complicated.
But don't let that scare you. You don't have to learn assem¬
bly-language programming at all. There's an easier way: high-
level languages.

[26]
CHAPTER THREE

FORTRAN AND
THE HIGH-LEVEL
LANGUAGES

It probably started with macros.


What is a macro? When you write a program in assembly
language, the assembler translates each mnemonic instruction
into a single binary instruction. There are problems with this sys¬
tem, however. Sometimes, in order to do an apparently simple
thing, you have to write a long series of instructions, as we saw in
the last chapter. Furthermore, groups of instructions can be high¬
ly repetitive, with the same phrases being used over and over
again in the same order, with only the numbers changing.
What if there were some way that the computer could trans¬
late a single mnemonic instruction into a whole series of binary
instructions? That way, complex but repetitive sequences of
machine-language instructions could be reduced to a single mne¬
monic instruction, which could then be repeated as many times
as necessary. This would free the programmer from the tedious
task of writing out long sequences of instructions again and
again.
This idea led to the development of the macroinstruction, or
“macro" for short. The word macro comes from a Greek word
meaning "large." Thus, a macroinstruction is a "large instruc¬
tion."
How does a macro work? A programmer who knows that he

[27]
or she is going to be using the same sequence of instructions
again and again throughout a program can write this series of
instructions at the beginning of the program and give it a name.
As with labels, the name can be anything the programmer wants
it to be—Tom, Dick, or Mary, for instance. More likely, the name
will describe what the instruction does, as with labels.
If we made a macro out of our instructions for adding
together ten numbers, for example, we might call it ADD10 or
ADDTEN. Then, whenever we wanted to insert this series of
instructions into a machine-language program, we would simply
use the macro name, as though it were a regular mnemonic.
When the assembler translated our program into machine lan¬
guage, it would know that the word ADDTEN, whenever it
occurred in the program, referred to the series of instructions
that we looked at earlier. The chief difference between macro
mnemonics and ordinary mnemonics, in short, is that a macro
mnemonic translates into many binary instructions while an
ordinary mnemonic translates into only one.
Wouldn't it be nice if we could invent a series of macros that
would do just about everything we might want to do in a pro¬
gram, then write an entire program made up of nothing but the
names of those macros? Such a program might look like this:

CLEARSCREEN
WRITE“Please type a number”
GETNUMBER
WRITE‘‘Type another number”
GETNUMBER
ADDNUMBERS
WRITESUM

What do you think this program does? Well, that depends on


how we've defined each of these macros. The first macro, CLEAR¬
SCREEN, might represent a series of instructions that does exact-

[29]
ly that—clears the video screen on the computer, much as an
eraser clears a blackboard. The second instruction, WRITE“Please
type a number,” may be the name of a macro that tells the com¬
puter to put the words Type a number on the video screen. The
third macro, GETNUMBER, might tell the computer to look at the
keyboard and "get" the numbers that are typed there, then store
them in memory. The next two macros are a repetition of the last
two. Then the next macro adds the numbers from the keyboard
together, and the last writes the sum on the video screen.
That's certainly a much simpler program to understand than
our assembly-language program for adding ten numbers
together—and it does so much more! Of course, we would still
have to perform the tedious task of letting the computer know
what each macro means, by writing out the individual instruc¬
tions and giving them a name. What a bore! Wouldn't it be easier
if someone invented a series of all-purpose macros and had them
permanently installed inside the computer, or in some kind of
special program, so that we could use just macros from now
on?
Yes, it would be easier. In fact, it's been done. Nowadays,
most computer programming is done in this manner, with high-
level computer languages.
A high-level computer language is made up of nothing but
macros—or instructions very much like macros—that are trans¬
lated by the computer into entire series of machine-language
instructions. We don't even have to write our own macros. This
has already been done by the programmers who originally
designed the languages. And there are a large number of high-
level languages to choose from, each with a different set of
macros, so we can select the set of macros that is most to our taste
as a programmer. To use these macros, we have only to buy a
program that already contains them and run that program on our
computer. We can then program in that particular language.
Some computers come with a high-level language perma-

[30]
nently programmed into them. Most microcomputers, for
instance, have the high-level language called BASIC already
installed in them.
In a high-level language, incidentally, the instructions are
not usually referred to as mnemonics or even macros. They are
usually called commands or statements.
The first important high-level language to be developed,
and one of the best known even today, is FORTRAN. The name is
short for FORmula TRANslator. As its name implies, the FOR¬
TRAN language was intended for use by scientists and engi¬
neers—people who deal in numbers and formulas.
FORTRAN has been around for more than a quarter of a cen¬
tury, and in that period it has gone through a number of changes.
In fact, there are several different versions of FORTRAN in exis¬
tence. The most popular current version—and the one we will be
talking about in this chapter—is called FORTRAN 77. This is con¬
sidered the standard version of FORTRAN. Having standard ver¬
sions of computer languages enables a program written for one
type of computer to be run on a different type of computer,
assuming that the same language is available for both. This
doesn't hold true for assembly language, however, which is dif¬
ferent on different computers. With assembly language, a pro¬
gram written on one type of computer can be run only on similar
computers. With FORTRAN, a program is "portable"—that is, it
can be run on many different types of computers.
FORTRAN was developed in the mid-1950s by a team of
computer scientists working for the IBM Corporation. The pur¬
pose of the FORTRAN project was to develop a computer lan¬
guage that would allow programming instructions to be written
in the form of English-like commands, which would then be
translated into machine language by the translator program
within the computer.
Whether FORTRAN is truly English-like depends on your
outlook toward such things. Certainly, it is a great deal more

[31]
English-like than assembly language. Let's look at a typical sec¬
tion of a FORTRAN program:

A = 2
10 PRINT *, A
A = A + 2
GOTO 10

Note that the program is written as a series of lines. Each line


contains an instruction to the computer. The first line reads A =
2. The letter A is a variable. All high-level computer languages
use variables. In a sense, variables perform the same role in high-
level language instructions that the special memory cells we
looked at earlier performed in assembly language. The letter A,
in this case, is used to identify a special location in the computer's
memory where we can store numbers. One of the many differ¬
ences between the high-level languages and assembly language,
however, is that in a high-level language such as FORTRAN, we
can give our own names to these memory locations, rather than
using the ones chosen by the computer manufacturer. The
instruction A = 2 tells the computer to store the number 2 in the
location we have called A. From this point on in our program, we
can use the letter A in place of the number 2, because whenever
the computer encounters the variable A, it will check its memory
to see what number is stored in location A. As long as we do not

Engineers use FORTRAN for


many different purposes. Here, a
programmer-engineer is reviewing
part of his FORTRAN program as
he modifies the design of a new
aircraft being built for NASA.

[33]
change the contents of that location, the number stored there
will be 2—and the computer will treat variable A as though it
were the number 2.
The next line reads 10 PRINT *, A. The 10 at the head of the
line is a label. We saw how labels worked in the chapter on
assembly language. Somewhere else in this program we will find
another line that makes reference to this label. We'll study that
line in a moment. The term "PRINT *," is a command to the
computer. It tells the computer to display something on its video
screen. What is the computer supposed to display on its video
screen? Because the "PRINT instruction is followed by the
variable A, this instruction tells the computer to display the num¬
ber stored in location A. The computer checks this location and
finds that we have stored the number 2 there. Thus, it prints the
number 2 on its video screen.
The next line reads A = A + 2. This tells the computer to
perform arithmetic on the number stored at memory location A
and store the result of that arithmetic back at location A. Since
the computer is currently treating the variable A as though it
were the number 2, we can read this line as A = 2 + 2. Since 2
plus 2 is 4, this line stores the number 4 in the memory location
labeled A. Now A will equal the number 4.
The final line reads GOTO 10. Like the jump (JP) command
in our assembly-language program, this tells the computer to "go
to" an earlier portion of the program. Which earlier portion? The
portion labeled "10."
Obeying the GOTO instruction, the computer immediately
returns to line 10 and executes the instruction on that line once
more. As before, this instruction tells the computer to print the
value of the variable A on the video screen. This time, however,
the number stored in location A is 4 rather than 2. For this reason,
the computer prints the number 4 on its screen, directly below
the 2 it printed a fraction of a second earlier. (The computer auto¬
matically moves to the next line on the video screen after execut-

[34]
ing a PRINT instruction, which is why the number 4 is printed
underneath the number 2, rather than next to it.)
The computer then proceeds to execute the instructions fol¬
lowing the PRINT statement yet another time. When it reaches
the instruction A = A + 2, however, it will find that the value of
A has changed. This time there is a 4 stored at location A, so we
can now read this line as A = 4 + 2. Since 4 plus 2 is 6, the
computer stores the number 6 at location A. When the program
loops back to line 10, the computer prints the number 6 on the
screen, directly underneath the 4 and the 2.
How long will the computer continue executing this series
of instructions? Forever, if nothing is done to stop it. Every time
the computer passes through the loop, it will add 2 to the value
stored in memory location A and print the result on the video
screen. The result will be a stream of even numbers on the screen:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, and so on. In fact, there
will be far too many numbers to be printed on the screen at one
time; the old numbers will have to be moved out of the way to
make room for the new ones. (The computer does this automati¬
cally.)
Many computers are equipped with special devices to sense
when the computer is stuck in an infinite loop—that is, a series of
instructions that will continue executing indefinitely. When the
computer senses such a loop, it will stop the program, and so the
loop will end. Other computers are equipped with a special key
on the keyboard that can be pressed when the user wants the
program to stop. With such a key, we could watch the computer
print numbers on the screen until we became bored—which
would happen rather quickly, one suspects—then stop the pro¬
gram and write a new one.
It would be nice, though, if the computer program itself
would end the loop at a specified point. Is there some way to tell
the computer to stop looping when it reaches a certain number
and end the program?

[35]
Sure there is. We can use an IF-THEN statement. With an
IF-THEN statement, our program would look like this:

A = 2
10 PRINT *, A
IF (A = 2000) THEN
END
END IF
A = A + 2
GOTO 10

What the IF-THEN statement does isn't terribly hard to under¬


stand. You'll recall that each time the computer performs this
program loop, the value of A is increased by 2. Eventually, after
the program loops for a thousand times, A will be equal to 2000.
The instruction in the third line checks to see when this happens.
Translated into English, this line tells the computer, "If the value
stored in location A is equal to 2000, then perform all the instruc¬
tions down to the line that reads END IF. Otherwise, skip those
instructions and begin performing the instructions immediately
after the END-IF statement."
The first time the computer executes these instructions, the
value of A will be 2. Thus, the computer will skip past the END IF
statement and execute the instructions following it. However,
once the value of A becomes 2000, the computer will execute
everything between the IF-THEN instruction and the END IF
statement. There is only one instruction between those lines,
however. It reads, simply, END. This tells the computer to stop
executing instructions and end the program. Because it lies
between the IF-THEN statement and the END IF statement, this
instruction will be executed only when A is equal to 2000—that
is, when the special memory location we have labeled A contains
the number 2000. The result is that the computer will print num-

[36]
bers on the video screen only until it reaches the number 2000.
Then it will stop.
One of the nice things about computer languages is that
there is usually more than one way to achieve any particular
result. The same program could also have been written like
this:
DO 10 A = 2, 2000, 2
PRINT *, A
10 CONTINUE

This is an example of what FORTRAN programmers call a “DO


loop." What it does is force the program into a loop without
using any GOTO statements. In order to understand the first line
of this program, we must break it up into several sections. The
first section reads DO 10. This tells the computer to do every¬
thing from that line to the line labeled "10," then return to the
first line and start over again. The second part of the line reads
A = 2. This means the same thing as it did in our earlier version;
it gives a value of 2 to the variable A.
The last part of the line is made up, simply, of the numbers
2000 and 2. In this case, the number 2000 is the limiter (or upper
limit) of the loop. We shall see in a moment what that means. The
number 2 is the increment of the loop. Before we define what
these terms mean, let's see what the loop in this program does.
Actually, it does precisely the same thing as the loop in the
earlier program. The DO statement forces the computer to exe¬
cute all instructions down to the line labeled 10, then return. At
the same time, the statement A = 2 following the DO statement
sets the variable A equal to 2—that is, it stores the number 2 in
location A. Therefore, the first time the statement PRINT *, A is
executed, it prints a 2 on the screen.
When the loop returns to its beginning, however, the state¬
ment A = 2 is ignored. This time, the value of the increment is

[37]
added to the value of A. The increment, in this case, is 2 (as we
saw above). Therefore, 2 is added to A, bringing the value of A to
4. The second time through the loop, then, the number 4 is
printed on the screen.
On the third pass through the loop, the increment is once
again added to A, bringing its value to 6, which is subsequently
printed on the screen. The next time its value is raised to 8, and so
on. How long does this go on? Until the value of A reaches the
limiter value, which you will recall is 2000. Thus, the computer
prints a series of numbers on its screen until the number 2000 is
reached, at which point the DO loop ends, and the program con¬
tinues with the next instruction after the loop. The next instruc¬
tion, in this case, is END, which stops the execution of the pro¬
gram.
In other words, this program does exactly the same thing
that our earlier program did, but it does it in fewer instructions.
No doubt there are yet other ways that this program could have
been written in FORTRAN, but our point has been made.
*****

It's not hard to see the superiority of FORTRAN over assembly


language. Saying A = 2 is much clearer than saying LD A,2. It
looks a lot like ordinary mathematics and therefore makes sense
to the average programmer. The language even includes
English-like phrases such as IF-THEN, which mean exactly what
they say.
Furthermore, FORTRAN makes it a great deal easier to dis¬
play words or numbers on the computer's video screen. In
machine language, using the video display can be very tricky
indeed, because different computers tend to be connected to their
video screens in different ways. Therefore, the machine-
language instructions for using the video screen will vary from
computer to computer. In FORTRAN, on the other hand, you can

[38]
always display numbers or words on the video screen through
the command PRINT *,. (Another command, WRITE, can also be
used to display words or numbers on the screen.) This command
will then be automatically translated into the proper machine-
language commands for displaying characters on the video
screen of your computer.
You can also write FORTRAN commands that will check to
see if the user wants to enter information into the computer by
using the keyboard or some other device. The command to do
this is READ.The READ statement causes the computer to look for
information from the keyboard. The computer will pause when
that command is encountered and wait for a key to be pressed.
Such a program might look like this:

100 PRINT *, “Type 2 numbers”


READ*, X, Y
IF X = 99999 THEN
END
END IF
Z = X + Y
PRINT Z
GOTO 100

The PRINT statement in the first line tells the computer to print
the sentence Type 2 numbers on the screen. Note that the sen¬
tence is in quotes. This tells the computer to print those words
exactly as written. Otherwise, the computer might interpret them
as variables, or as some kind of command.
The READ command in the second line waits for two num¬
bers to be typed on the keyboard. When they are typed, the com¬
puter automatically stores them in the variables X and Y, which
are specified immediately after the READ command. Suppose the
user typed the numbers 3 and 4, in that order. The variable X will

[39]
be set equal to 3, and the variable Y will be set equal to 4. That is,
the number 3 will be stored in memory location X, and the num¬
ber 4 will be stored in memory location Y.
The computer checks to see if the user typed 99999. If so, it
ends the program. This gives the user a way of ending the pro¬
gram simply by typing a prearranged number. Of course, if the
user wants to type the number 99999 for some other reason, he or
she is out of luck.
After the END IF statement, the computer adds the values of
X and Y together and makes the variable Z equal to the result. If X
equals 3 and Y equals 4, then Z now equals 7. The computer
prints this value on the screen, then loops back to the beginning
of the program to get more numbers from the keyboard.
The FORTRAN language is particularly good at arithmetic
and mathematics. In fact, the very concept of variables comes
from mathematics. In algebra, variables with names such as X and
Y are often used to stand for numbers. In the same way, FOR¬
TRAN uses variables in place of numbers—and sometimes in
place of words. Any letter of the alphabet, from A to Z, can be a
FORTRAN variable. Combinations of letters are allowed, too.
Words such as NUMBER or EGGS, or strings of letters such as
MRPHLKT, are all legitimate FORTRAN variables and can be
used by a programmer. Numbers can also be used as part of a
variable name, as long as that name begins with a letter of the
alphabet. As we shall see in forthcoming chapters, other lan¬
guages use variables in much the same way as FORTRAN does.
As we have already seen, in FORTRAN we can perform addi¬
tion with a plus sign ( + ). We can also perform subtraction with a
minus sign ( —), division with a slash ( /), and multiplication
with an asterisk (*). For instance, to set the variable H equal to 12
divided by 4 and then multiplied by 3, we would write H = 12/4*3.
You'll recall that when a program is written in assembly lan¬
guage, it is then translated into machine language by another
program called an assembler. This process is called assembling. On

[40]
the other hand, when a program is written in FORTRAN, it is
translated into machine language by a program called a compiler.
This process, similar to assembling, is called compiling. For this
reason, FORTRAN is referred to as a compiled language. Many oth¬
er high-level languages are also compiled languages, though
there are other ways of translating a high-level program into
machine language, as we shall see in the chapter on BASIC.
You'll recall that FORTRAN proved particularly valuable for
scientists and engineers. However, by the late 1950s, it was
obvious that scientists and engineers weren't the only noncom¬
puter people who needed access to computers. The FORTRAN
language was not everyone's cup of tea. It was not as good at
performing nonmathematical operations as it was at performing
mathematical ones.
Thus, it was inevitable that other high-level computer lan¬
guages would be developed. And so they were.

[41]
CHAPTER FOUR

COBOL—
A LANGUAGE FOR
BUSINESS

Computers are versatile machines. They can be many different


things, from electronic typewriters to arcade games to high¬
speed calculators. The ideal programming language would take
advantage of this versatility. Early versions of FORTRAN, how¬
ever, were lacking in some respects. While they were excellent
for scientific applications, they were not as good for business
applications.
Business puts unusual demands on a computer. While scien¬
tists and engineers might want the computer to perform mathe¬
matical operations—"number-crunching,” as programmers call
it—business applications are light on math but heavy on file¬
keeping.
What is a file? A file is a collection of records and informa¬
tion. Businesses must keep extensive files concerning their prod¬
ucts and their employees. A firm of five hundred employees, for
instance, must keep files on each individual employee. One of
those files might look like this:

NAME: JOHN Q. JONES


ADDRESS: 444 MAIN STREET
APARTMENT 11 IE
ARLINGTON, VA 19999

[42]
Computers have many different applications
in the business world. Many software
programs for businesses are written in COBOL.
POSITION: COMPUTER PROGRAMMER
WEEKLY SALARY: $400
SUPERVISOR: SUSAN SMITH

And so forth.
Five hundred records containing all this information (and
usually a great deal more) can use up a lot of paper. Combine
these with records concerning product inventory and regular
customers and correspondence, and you'll find a lot of file cabi¬
nets filled with paper.
With a computer, this information can be reduced to a series
of electronic impulses and stored on magnetic tape, a magnetic
disk, or punch cards. One of the deficiencies of early FORTRAN,
however, was in file-keeping. It was apparent that a computer
language strong on file-keeping was needed. The programs
developed in this language would, ideally, allow for the creation,
recording, and continual updating of files. In addition, a program
that could use these files to create paychecks or form letters, thus
freeing human beings from having to perform such tedious,
repetitive tasks, was needed.
In 1959 a committee was formed to explore the special prob¬
lems of business computing. In December of that year this com¬
mittee published a report recommending the creation of a lan¬
guage they called COBOL.
Like FORTRAN, COBOL, which stands for COmmon Busi¬
ness Oriented Language, is a compiled language, and COBOL
compilers were soon written for most major computers. The U.S.
government helped the new language along by requiring that all
computers purchased by the government have COBOL compilers
available, thus encouraging computer manufacturers to prepare
such compilers for their machines.
What is the COBOL language like? Well, to illustrate, let's
put together a COBOL program to handle the file that we looked
at earlier in this chapter.

[44]
A COBOL program is divided into four parts, or divisions.
These are called the identification division, the environment division,
the data division, and the procedure division.
The identification division simply gives special information
about the program, more for the sake of any human being who
reads it than for the computer. In fact, this part of the program
exists primarily to be read; it isn't actually compiled into machine
language. The sort of information found in the identification
division might include the name of the program, the name of the
programmer, the purpose of the program, and so on.
A typical identification division might look like this:

IDENTIFICATION DIVISION.
PROGRAM I.D. Employee Roster.
AUTHOR. Josephine Doaks.
REMARKS. This program contains information concerning
the employees of the Acme Corporation.

The first line serves to identify the division of the program. The
second line, the program I.D., gives the name of the program—in
this case. Employee Roster. All information after this is optional;
that is, the computer doesn't care if you put it in or not, but the
programmer may do so at his or her discretion. The author line,
for instance, gives the programmer's name, and the remarks line
can be used to discuss the nature of the program, or, for that
matter, anything else the programmer wants to talk about.
The environment division is where the programmer de¬
scribes the specific equipment that the program will be running
on—the type of computer and printer, for example. If the equip¬
ment that the program will be running on is different from the
equipment it is being written on, then this is mentioned as well.
The environment division varies considerably from computer to
computer.
The data division describes the kind of information the pro-

145]
gram will handle. In this section, the programmer must list the
so-called data names that will be used throughout the program. A
data name in COBOL is very much like a variable in FORTRAN.
It is a label that identifies a place in memory where information
will be stored.
The data division for our employee roster program might
look like this:

01 EMPLOYEE-NAME
02 FIRST-NAME PIC A(10)
02 MIDDLE-INITIAL PIC A
02 LAST-NAME PIC A (10)
01 ADDRESS
02 STREET PIC X (20)
02 APARTMENT PIC X (7)
02 CITY PIC A (16)
02 STATE PIC A (2)
02 ZIP-CODE PIC 9 (5)
99 POSITION PIC X (15)
99 SALARY PIC 9 (5) V9 (2)
01 SUPERVISOR
02 FIRST-NAME PIC A (10)
02 MIDDLE-INITIAL PIC A
02 LAST-NAME PIC A (10)

This gives us just about all the information the computer needs to
know concerning the data the program will be handling. Let's
take a closer look and see if we can understand what it says.
Each line contains a single data name. As we just noted, these
identify the places in memory where information will be stored.
The PICTURE statement following the data names tells the com¬
puter how many memory cells to allot to each of these storage
places and what kind of information will be going into the mem¬
ory cells.
The number in front of the data names tells us still more
[46]
about the nature of these storage places in memory. For instance,
the data name EMPLOYEE-NAME in the first line is preceded by
the number 01. This tells us that EMPLOYEE-NAME is a primary
data name—one that can be broken up into smaller data names.
The smaller data names that it can be broken up into are written
directly beneath it, prefixed with the number 02. These are sec¬
ondary data names. The primary data name EMPLOYEE-NAME
is made up of the secondary data names FIRST-NAME, MIDDLE-
INITIAL, and LAST-NAME. Another way to look at this is to say
that the storage area in memory called EMPLOYEE-NAME is
divided up into three smaller storage areas called FIRST-NAME,
MIDDLE-INITIAL, and LAST-NAME.
If we were using this program to store the employee file we
looked at earlier, FIRST-NAME would contain “JOHN," MID¬
DLE-INITIAL would contain "Q," and LAST-NAME would con¬
tain "JONES." The primary data name, EMPLOYEE-NAME,
would therefore contain JOHN Q JONES, because the primary
data name is made up of the contents of the three secondary data
names. Similarly, the primary data name ADDRESS breaks up
into the secondary data names STREET, APARTMENT, CITY,
STATE, and ZIP-CODE.
The next two data names are preceded by the number 99.
This means that they are elementary data names. An elementary
data name cannot be broken up into smaller units.
Most of the data names are followed by a PICTURE state¬
ment, identified by the word PIC. We saw a moment ago that this
tells the computer what the storage compartment in memory is
supposed to look like. The first PICTURE statement reads PIC A
(10). This tells the computer that data name FIRST-NAME will be
filled with ten (10) or less alphabetical characters. (The letter A, in
this case, represents the letters A through Z.) Thus, the computer
opens space in memory for ten letters and calls that space FIRST-
NAME. A FIRST-NAME with more than ten letters will be cut to
ten letters by the computer in order to fit this space. The name
Christopher, for instance, would be trimmed to Christophe—as
[47]
the author of this book well knows. (You may have had the expe¬
rience of filling out forms that give you a limited number of
squares or blanks in which to write the letters of your name.
These forms are designed to be processed by a computer, and the
programs that tell the computer how to do the processing were
quite possibly written in COBOL.)
A little farther down, we see the statement PIC X (20), which
tells the computer that this data name will be made up of twenty
characters that may be letters or numbers. X indicates that the
specific type of character is unknown. Still farther down, after
the data name ZIP-CODE, we see PIC 9 (5). This tells the comput¬
er that the data name will hold five (5) numbers. In this case, 9
stands for the numbers zero to nine.
Finally, after the data name SALARY, we see PIC 9(5)V9(2).
The V stands for an implied decimal point. This PICTURE state¬
ment tells us that the data name will consist of seven numeric
characters, but if the computer should be asked to print out these
characters with a decimal point, the decimal point will go
between the first five and the last two.
The data division of a COBOL program shows us how infor¬
mation will be stored in the computer. But how do we get that
information into and out of the computer? The procedure for
doing this is described, appropriately enough, in the procedure
division.
A short procedure division to allow the user to input data
from the keyboard might look like this:

PROCEDURE DIVISION.
GET-DATA.
ACCEPT FIRST-NAME FROM CONSOLE.
ACCEPT MIDDLE-INITIAL FROM CONSOLE.
ACCEPT LAST-NAME FROM CONSOLE. DISPLAY
“THANK YOU”.
END-PARA.
STOP RUN.
[48]
One of the first things that may strike you about COBOL is how
closely it resembles English. A COBOL program is made up of
paragraphs and sentences, and the sentences even end with peri¬
ods. The line that reads GET-DATA in the above program is a para¬
graph name. It describes what the following instructions are sup¬
posed to accomplish. The paragraph names work much like the
labels in FORTRAN and assembly language. Elsewhere in this
program we could write a line that read GOTO GET-PARA, and
the computer would immediately start executing the instructions
in the paragraph with that name.
The line ACCEPT FIRST-NAME FROM CONSOLE means
exactly what it says. It tells the computer to get information from
the console (keyboard) and store it in the memory location
labeled FIRST-NAME, which was established in the data divi¬
sion. The following lines tell the computer to perform similar
tasks for other data names. The sixth line displays the words
THANK YOU once all the data names are supplied. Then the para¬
graph ends. Since there are no other paragraphs in the program,
the program then terminates.
An actual COBOL program, of course, will be made up of
several paragraphs and a multitude of instructions. Altogether,
those instructions would fill one file of information on a single
employee. This information could then be transferred to magnet¬
ic tape or disk, where it would be stored permanently (or until
changes needed to be made). Then the program could loop back
to allow the same task to be performed for another employee.
Filing isn't the only thing that can be done in COBOL,
though it's probably what the language does best. We can per¬
form arithmetic in COBOL just as in FORTRAN. But the differ¬
ence between COBOL arithmetic and FORTRAN arithmetic is
striking. Suppose, for instance, that we have a variable/data
name called HOURS that stores the information on how many
hours an employee has worked in a week. And suppose we have
another data name called SALARY that stores the hourly salary of
that employee. Suppose further that we wish to calculate how
[49]
This man is developing a COBOL program
for his business operations.
much this employee has earned in a week and store the result in a
data name called WAGES. Obviously, WAGES will be equal to
SALARY multiplied by HOURS. In FORTRAN it would be writ¬
ten like this:

WAGES = HOURS * SALARY

In COBOL, on the other hand, we might write it like this:

MULTIPLY SALARY BY HOURS GIVING WAGES

How different the COBOL version is! These two program lines
illustrate the basic difference between the two languages. The
FORTRAN version is more mathematical looking, while the
COBOL version is more businesslike and also more English-
like.
COBOL is a versatile language. We could also write the
above example like this:

COMPUTE WAGES = HOURS X SALARY

The COMPUTE statement tells the computer that we are going to


explain the problem in a much more mathematical, FORTRAN-
like manner.
Because of its resemblance to English, it is possible for a
beginning programmer to gain a smattering of COBOL very
quickly. However, COBOL is a large and complex language, and
to learn everything it can do can be very time-consuming.
By the early 1960s it had become apparent that there was a
need for a simpler language than either COBOL or FORTRAN,
one that could be learned by those who needed a nodding
acquaintance with computer programming but who did not
intend to pursue that knowledge very far. Thus, the BASIC lan¬
guage was conceived—as we shall see in the next chapter.

[51]
CHAPTER FIVE

BASIC—
A LANGUAGE FOR
BEGINNERS

As the 1960s dawned, the computer age was already in full


swing. The machines had invaded laboratories, offices—and uni¬
versities.
This made sense. If the future was to be dominated by com¬
puters, then university students needed to learn how to use
them. For instance, students in the “hard" sciences—physics,
astronomy, mathematics, and so on—needed computers for
important calculations and for assessing the results of experi¬
ments. Students in the "soft" sciences, such as sociology and psy¬
chology, needed computers to analyze the results of statistical
studies they had performed.
But many, perhaps most, of these students lacked the time to
learn programming, even in such "simple" languages as FOR¬
TRAN or COBOL. What of these students? How would they
obtain the programs necessary for their assignments if they did
not write them themselves?
This was the problem faced by two professors, Thomas Kurtz
and John Kemeny, at Dartmouth College in the early 1960s. Their
response was to devise yet another computer language, one made
up of only a few simple but versatile commands, with which a
wide variety of programs could be written. They called their lan¬
guage BASIC, short for Beginners All-Purpose Symbolic Instruc¬
tion Code.
[52]
Aside from its simplicity, the chief difference between
BASIC and the earlier high-level computer languages such as
FORTRAN and COBOL was that BASIC was designed to be an
interpreted language, not a compiled one. What's the difference?
You'll recall that a compiled language is translated into machine
language by a program called a compiler. The binary version of
the program is the one that is actually run on the computer. The
original (or source) version of the program—the one written in
the high-level language—is filed away for future reference.
An interpreted language is also translated into binary, as all
high-level languages must be. But the translation takes place
while the program is actually being executed by the computer.
The original source program must be in the computer at the same
time as a machine-language program called an interpreter. The
interpreter examines the BASIC program a command at a time
and translates each command into binary. The computer then
executes the binary version of the command.
There is a disadvantage to this method. Interpreted lan¬
guages such as BASIC are slow. On the average, a program writ¬
ten in BASIC will execute three hundred times slower than a
similar program written in machine language.
If interpreted languages are so slow, then, why bother with
them at all? Because they are interactive. With a compiled pro¬
gram, you must write the program, compile it, and then run the
binary version. Should you find that you've made a mistake, or
that something doesn't work quite the way you'd intended it to
(and few programs work as intended the first time), you must
return to the source program, find the error, correct it, and com¬
pile it again before you can run it.
With BASIC, on the other hand, you just enter your program
and type RUN. The computer executes your program immediate¬
ly, without waiting for compilation. If something goes wrong,
you hit the key on the keyboard marked BREAK and retype the
line with the mistake. You can even test programming ideas in
the "immediate" mode; this means you simply type a command
[53]
and the computer executes it as soon as you finish typing it, even
before you can type RUN. This way, you can see if a command
does what you want it to before you've made it part of your pro¬
gram.
This makes BASIC very easy to learn. A student with no idea
of what programming is all about can sit at a computer terminal
(or a microcomputer) and learn the BASIC language simply by
typing in BASIC commands and seeing what happens. And
should the student write a program that doesn't work, he or she
can make changes easily and painlessly, until it does work.
Because of this interactive ease, the BASIC language caught
on quickly. But a strange thing happened. BASIC grew. From a
language of simple, manageable proportions that could be
learned in a day, BASIC became as large and complex as FOR¬
TRAN (though not quite as large or complex as COBOL). Fortu¬
nately, the core of the language developed by Kemeny and Kurtz
still exists, so an amateur programmer can still learn the rudi¬
ments of BASIC programming in a few hours. But it now takes
much longer to master the entire language.
How did this happen? How did BASIC undergo such wild,
almost uncontrolled growth after it was first introduced?
The answer is simple—microcomputers. In 1971 a company
named Intel introduced the first ''computer on a chip," the Intel
4004 microprocessor, and inadvertently ushered in the age of the
microcomputer. With microcomputers came the need for a lan¬
guage that could be learned quickly and easily by amateur pro¬
grammers. BASIC filled the bill perfectly. Almost every micro¬
computer on the market today—and there are hundreds—comes

The Apple 11 +, like most of


today’s microcomputers, comes
already programmed in BASIC.

[55]
The Commodore 64 is another popular microcomputer
that comes already programmed in BASIC.
with its own version of BASIC, often with the interpreter built
right into the computer.
The programmers who designed these interpreters, howev¬
er, felt free to add BASIC commands of their own, commands that
were probably not even dreamed of by Kemeny and Kurtz. The
result was that we now have a hodgepodge of different versions,
or dialects, of BASIC, at least one for each brand of computer.
There is an Apple BASIC for Apple computers, a TRS-80 BASIC
for Radio Shack computers, a TI BASIC for Texas Instruments'
computers, an Atari BASIC for Atari computers, and so on. And
some of these BASICS come in several different versions. Radio
Shack offers at least nine versions of their TRS-80 BASIC.
Depending on which model Radio Shack computer you buy,
there is Level I BASIC, Level II BASIC, Disk BASIC, Model II
BASIC, Model III BASIC, Color BASIC, Pocket Computer BASIC,
and so on.
The American National Standards Institute is currently
working on a standard version of advanced (or "extended")
BASIC. (There is currently a standard version of simple, unex¬
tended BASIC, based on the original Kemeny and Kurtz version,
but it is rarely used.) One wonders, though, whether even this
prestigious institute will be able to cut through the tangled web
of BASIC dialects.
Fortunately, however, there are certain points that all ver¬
sions of BASIC have in common, and it is possible to put together
a BASIC program using only the simplest of commands, which
will run on nearly any computer. Here is an example:

10 PRINT “HOW OLD ARE YOU?”


20 INPUT A
30 B = A * 365
40 PRINT “ON YOUR LAST BIRTHDAY YOU WERE “;B;”
DAYS OLD.”
50 GOTO 10

[57]
After our examinations of FORTRAN and COBOL, this program
shouldn't be too hard to understand. In fact, BASIC bears a strong
resemblance to FORTRAN. Notice, first, that every line of the
program begins with a number. These numbers are like the labels
in assembly language and FORTRAN and the paragraph names
in COBOL. When we want to refer to a specific line of the pro¬
gram, we refer to it by its line number.
In Line 50 of the above example, for instance, we see the
familiar GOTO command, directing the computer to return to
Line 10 and begin executing, once again, the instructions it finds
there. The chief difference between using labels in BASIC and
using them in other languages is that in BASIC every line gets a
label—that is, a line number—whether it will be needed or not.
In FORTRAN and most other computer languages, a line is given
a label only if that line will be referred to elsewhere in the pro¬
gram. In BASIC, however, the line numbers are also used for pro¬
gram editing. When the programmer wants to make a change in
("edit") the program, he or she tells the computer the number of
the line to be changed, and the computer allows the programmer
to make changes in that line. This speeds up the editing process,
making BASIC even easier to use.
The rest of this program is easily explained. Line 10 prints
the sentence "How old are you?" on the video screen. Then, with
the INPUT command. Line 20 takes input from the keyboard. If
the input from the keyboard is a number, the variable A is set
equal to that number—that is, the number is stored in the mem¬
ory location called A. In Line 30, the number is multiplied by 365,
and the result is stored in B. If the number that was input was the
user's age, then variable B is now equal to the person's age in
days. In Line 40 the value of variable B is printed on the screen as
part of the sentence. Line 50 loops the computer back to do it
again.
Though a program that tells you how old you are in days
may not sound very useful, essentially the same commands can

[58]
be used to calculate the payments on a mortgage or the statistical
results of a large survey.
BASIC is also good at handling strings. A string, in computer
talk, is a group of characters such as letters, numbers, or other
written symbols. The sentence you are reading now is a string.
Variables can be used to store strings. However, a special kind of
variable is required—string variables. A string variable looks like
an ordinary variable followed by a dollar sign (“$“). A$, ZP$, and
WORDS are all legitimate string variables. We use a string vari¬
able almost like an ordinary, numeric variable, except that we
can't perform arithmetic on it.
For instance:

10 A$ = “HOW ARE YOU DOING, ”


20 B$ = “?”
30 PRINT “WHAT IS YOUR NAME?”
40 INPUT C$
50 PRINT A$;C$;B$

In the first two lines of this program, the variables A$ and B$ are
set equal to the sentence, “How are you doing," and the symbol
//?,“ respectively. Line 30 asks, “What is your name?" Line 40
accepts input from the keyboard—presumably the user's name—
and stores that input in the string variable C$. Then Line 50
prints all of the string variables in a single sentence, held
together by semicolons. (The semicolons are not printed on the
screen. Rather, they tell the computer that the value of these vari¬
ables are to be printed directly after one another on the screen,
rather than on separate lines.) If C$ is equal to “Michael," say, the
final line of the program will print “How are you doing,
Michael?"
BASIC's main strength is the ease with which it can be
learned and used. As we saw above, it is slow—though, by
human standards, it is still blindingly fast for most applications.

[59]
And there are those who complain that it encourages sloppy pro¬
gramming.
There is some justice in this complaint. Because it is so easy to
use, a programmer may tend to start writing a program in BASIC
before thinking about what he or she is doing and then have to
patch it together until it works correctly, thus creating a jerry-
built structure.
Further, while BASIC programs are easy to write, they are
not easy to read. A programmer who writes a complex program in
BASIC may find, a month after the fact, that he or she doesn't
understand it. This could cause problems if changes are needed.
And, if the programmer who wants to make the changes isn't
even the programmer who originally wrote the program
. . . well, the situation may be hopeless.
Out of the desire for programs that could be easily read and
understood, and for a language that did not encourage sloppy
programming, came the idea of structured programming. And the
most structured programming language of all is PASCAL.

[60]
CHAPTER SIX

DACrAI _

THE LANGUAGE OF
STRUCTURED
PROGRAMMING

Those who consider BASIC a sloppy language place much of the


blame on the GOTO statement. Almost every computer language
has a GOTO statement, or its equivalent, even assembly lan¬
guage. But BASIC is more dependent on it than most. The GOTO
statement, for instance, can, and often must, be used to create a
program loop—and loops are extremely important in program¬
ming. While most languages offer a number of alternative ways
to create loops, BASIC offers only one other—the FOR-NEXT
statement, which is similar to the DO loop in FORTRAN.
The BASIC GOTO statement, therefore, is often quite useful
and even necessary, but it can also be abused. A sloppy program¬
mer who doesn't plan programs carefully in advance may fill
those programs with GOTOs, bouncing the computer from pro¬
gram line to program line, like a Ping-Pong ball. Such a program
is very difficult to read or locate mistakes in.
Suppose we were setting out to design a computer language
that programmers could use to write easy-to-read programs. Fur¬
thermore, suppose we wanted our language to discourage sloppy
programming and encourage programmers to plan their pro¬
grams very carefully in advance. How would we go about this?
The idea hit upon by the designers of PASCAL was to devel-

[61]
op a language without a GOTO statement. A language without a
GOTO statement? Some programmers must have been amazed
that such a thing would even be attempted. The GOTO statement
is central to so many programming concepts—not just to loops
but also to conditional branching, where the computer is told IF
(such and such statement is true) THEN GOTO (this line) ELSE
GOTO (the other line). How could programmers write condition¬
al statements without it?
The answer was, not easily. But it could be done. And the
effort was often quite worthwhile.
The PASCAL language is not as easy to use as BASIC or even
FORTRAN. It's not supposed to be. PASCAL forces the program¬
mer to do a great deal of planning with pen or pencil before even
touching the computer—which is exactly what its designers
intended.
Surprisingly, though, programs can often be developed in
PASCAL faster than in other languages, including BASIC. This is
because PASCAL does not encourage mistakes. A PASCAL pro¬
gram may take longer to write initially, but once written it
doesn't take as long to get into shape. In BASIC, a programmer
may spend more time getting the "bugs" out of the program than
he or she spent writing it in the first place. In fact, this is usually
the case.
PASCAL programs are also fairly easy to read, if written
intelligently. For instance, the language encourages program¬
mers to use variable names that describe what they stand for, and
to give descriptive names to different sections of the program.
PASCAL is a lot like COBOL in this regard.
Where PASCAL really shines, though, is in loops. Because
PASCAL has no GOTO statement, it was necessary to find other
ways to create program loops. PASCAL features not one, not two,
but three different kinds of loops: the FOR-DO loop, the WHILE-
DO loop, and the REPEAT-UNTIL loop.

[62]
Here's an example of a REPEAT-UNTIL loop:

REPEAT
WRITE (TYPE A NUMBER FROM 1 TO 10’);
WRITE (TYPE 0 TO STOP’);
READ (NUMBER);
WRITE (THE NUMBER YOU TYPED IS’ : NUMBER);
UNTIL NUMBER = 0;

This simple loop takes numbers from the keyboard and displays
them on the video screen. The WRITE statement in PASCAL is
the equivalent of PRINT in BASIC and FORTRAN. (There is a
WRITE statement in FORTRAN as well, though we have not
examined it in this book.) Like the identically named statement
in FORTRAN, READ waits for the user to type a number on the
keyboard and then stores that number in a variable—in this case,
NUMBER. This sequence of instructions will continue looping
over and over until the user types a zero. Why? Because the entire
sequence is encased inside an instruction that reads, in its entire¬
ty, REPEAT UNTIL NUMBER = 0. And that's exactly what will
happen. The sequence of instructions will continue repeating
until the user types a zero and NUMBER becomes equal to 0.
Notice that all the statements within the loop are indented
about five spaces. This isn't required by the computer, but it
makes the program easier to read, and therefore it is encouraged
by those who designed the language. The indentation makes it
obvious which statements are part of the loop and which are not.
In a large program, this can be very important.
Also notice that PASCAL, under normal circumstances, uses
neither line numbers nor labels. Why should it? Without a GOTO
statement, they aren't needed.
The above program could also have been written as a
WHILE-DO loop, like this:

[63]
WHILE NUMBER <> 0 DO
BEGIN
(Body of loop)
END;

This statement tells the computer to keep "doing" the loop as


long as the variable number is not equal to ("<>") zero. This is
the same thing the last loop told the computer but expressed in a
different manner. The BEGIN and END statements (not required
in the REPEAT-UNTIL loop) tell the computer where the loop
begins and ends.
Another area in which PASCAL excels is called procedures.
What is a procedure? In a sense, a procedure is a chance for the
programmer to actually rewrite the computer language. Many
languages have procedures (though BASIC doesn't), but in PAS¬
CAL, because it lacks the GOTO statement, they are more neces¬
sary than in most.
Suppose there is a particular sequence of commands that you
will be using more than once in your program. For instance, sup¬
pose you were writing a program that ran too fast, and you
wanted to slow it down at certain times during its execution. You
might slow it down with a delay loop, like this:

DELAY := 1;
REPEAT
DELAY := DELAY + 1;
UNTIL DELAY = 10000;

This sequence of instructions, in the form of a REPEAT-UNTIL


loop, keeps adding 1 to the value of the variable DELAY until
DELAY is equal to 10000. (Notice that PASCAL sets variables
equal to numbers with the symbol ":=" rather than the plain
equals sign [" = "]. The equals sign in PASCAL is still used in

[64]
IF-THEN statements, among others.) This loop serves to slow the
program; it has no other use.
Do you have to write a delay loop at every point in your
program where you wish to slow the action down? No. Doing so
would be a great waste of time and space. In PASCAL, you can
make this delay loop into a procedure, like this:

PROCEDURE SLOWDOWN;
BEGIN
DELAY := 1;
REPEAT
DELAY := DELAY +1;
UNTIL DELAY = 10000;
END;

The first line announces that what follows between the BEGIN
and END statements is a procedure with the name SLOWDOWN.
When we want to use this procedure, we "call" it like this:

WRITE (‘LET US PAUSE FOR A FEW SECONDS’);


SLOWDOWN;
WRITE (‘HERE WE GO AGAIN!’);

When we defined the procedure, we gave it the name SLOW¬


DOWN. To call the procedure, we simply refer to it by name, as
though it were a command. Procedures are a little bit like labels,
but only a little bit. In a sense, a procedure is a kind of macro¬
macro—a macroinstruction made up of macroinstructions.
If procedures are used correctly, they contribute to the gen¬
eral PASCAL goal of making programs more readable. The trick
is in the name. If the procedure is given a name that describes
what it does, as when we referred to our delay procedure as
SLOWDOWN, then its purpose should be obvious. If the name is

[65]
clear enough, the reader won't even have to look over the proce¬
dure definition to figure out what it does.
PASCAL is a fascinating language, but it is not an ideal one
for beginning computer users, unless those users are interested
in getting into advanced, serious programming applications very
quickly. Still, it would be nice, wouldn't it, if there were a lan¬
guage that incorporated some of the advanced ideas found in lan¬
guages like PASCAL (and not in BASIC), but that was designed to
help newcomers learn programming.
In fact, there is. It's called LOGO, and we'll take a look at it in
the next chapter.

[66]
CHAPTER SEVEN

LOGO—
THE LANGUAGE OF
THE TURTLE

The computer language LOGO was developed by a team of edu¬


cators at Massachusetts Institute of Technology in the early to
mid-1970s, under the direction of Professor Seymour Papert. The
intent of the MIT team wasn't to invent a language that would
make programming easier or do tricks other languages couldn't;
rather, the point of LOGO was to make learning—many kinds of
learning—fun. LOGO was designed to help students explore
computers, mathematics, geometry, even the universe itself.
Obviously, LOGO is not a typical computer language. In fact,
it is a language that should be explored and played with, not
learned by drill and practice, as you might learn COBOL.
The most interesting thing about LOGO concerns its graph¬
ics capabilities. In computer talk, the term graphics refers to pic¬
tures on a computer's video screen. Most computers have the
ability to produce some sort of graphics, ranging from simple
black-and-white squares on business-oriented computers to de¬
tailed color images on arcade games and Hollywood special-
effects computers (the kind used for the movie Tron, for
instance). Graphics capabilities usually vary from computer to
computer, not language to language.
LOGO is a graphics-oriented language. That fact alone makes
it unusual. The kind of graphics that LOGO is oriented toward is
turtle graphics.
[67]
What are turtle graphics? A turtle, in LOGO, is a tiny triangle
of light that moves about the computer's video screen in response
to programmed commands. According to the LOGO mythology,
the turtle carries with it a small pen. If the programmer so
desires, the turtle can rest this pen against the "surface" over
which it is traveling. Thus it leaves a visible trail of ink in its
path, a trail seen by the user as a line across the video screen. In
this way, the turtle can be used to draw pictures.
The commands that move the turtle are simple. FORWARD
causes the turtle to move in the direction in which it is pointed.
BACK causes it to move backward. LEFT and RIGHT cause it to
turn. We can tell it how far we wish it to travel by specifying the
distance in turtle units (a very small amount). FORWARD 25
would cause the turtle to move forward twenty-five turtle units.
We can tell it how much we wish it to turn by specifying a num¬
ber of degrees. (A degree is a portion of a circle. A complete,
circular turn will consist of 360 degrees.) RIGHT 90 would cause
the turtle to make a ninety-degree (quarter) turn to the right.
If we want the turtle to draw a line, we tell it to lower the
point of its pen. This is done with the command PENDOWN.
When we want it to stop drawing, we use the command PENUP
(or END to end the program).
The following program instructions would cause the turtle
to draw a square:

PENDOWN
REPEAT 4 [FORWARD 25 RIGHT 90]

The instructions inside the brackets tell the turtle to move for¬
ward twenty-five turtle steps, then turn to the right ninety

LOGO is a language
for fun and learning.
[68]
degrees. Because we gave the computer the PENDOWN instruc¬
tion, the turtle will draw a line. The REPEAT 4 instruction, out¬
side the brackets, causes the turtle to repeat the instructions
inside the brackets four times. Each time it draws a line, then, it
will make a quarter turn and draw another line—until it has
drawn a complete square.
When we studied PASCAL, we saw how we could design
custom computer commands using procedures. We can also use
procedures in LOGO. Suppose, for instance, we wish to define a
procedure that will draw a square on the screen. We can use the
same sequence of commands we just examined:

TO DRAWSQUARE
PENDOWN
REPEAT 4 [FORWARD 25 RIGHT 90]
PENUP

This creates a procedure called DRAWSQUARE. The TO is all we


need to indicate that this is a procedure. To call—that is, to use—
this procedure elsewhere in the program, we refer to it by name,
just as in PASCAL:

PRINT [WE ARE NOW GOING TO DRAW A SQUARE]


DRAWSQUARE

It is even possible to use variables in a procedure, thus allowing


us to change the procedure each time we use it. The DRAW-
SQUARE procedure, for instance, could use the variable SIZE,
indicating how large we want the square to be. The procedure
would then look like this:

TO DRAWSQUARE: SIZE
PENDOWN
REPEAT 4 [FORWARD: SIZE RIGHT 90]
PENUP
[70]
As you can see, all variables used in the procedure have to be
declared immediately after the name of the procedure. Note that
the turtle would now move forward the number of units speci¬
fied by the variable SIZE. By changing the value of SIZE before
we call the DRAWSQUARE procedure, we can change the size of
the square. Such a procedure is called like this:

PRINT [WE ARE NOW GOING TO DRAW ANOTHER


SQUARE]
DRAWSQUARE: SIZE

Notice that, when we call the procedure, we must also declare the
variable name by writing it immediately after the procedure
name. This is called "passing a variable to the procedure." If we
forget to do this, the value of the variable will get "lost" between
the main program and the procedure.
In the LOGO language, we can even write a procedure that
calls itself. What does this mean? Let's take a look at POLYSPI, a
procedure that has proved very popular with LOGO fans:

TO POLYSPI: LENGTH: ANGLE: CHANGE


FORWARD: LENGTH
RIGHT: ANGLE
POLYSPI: DISTANCE: ANGLE + CHANGE: CHANGE
END

We pass three variables to this procedure—length, angle, and


change. First, the turtle is told to move forward the number of
turtle steps specified by the variable LENGTH. Then it turns
right the number of degrees specified by the variable ANGLE.
Then the procedure does something very peculiar—it calls itself.
It specifies its own name as a command.
When a procedure calls itself—that is, uses its own name as
part of its definition— a programming technique called recursion
is being used. Not all computer languages allow recursion, but
[71]
LOGO does. What happens when the POLYSPI procedure calls
itself? To find out, we must follow the turtle step by step through
the procedure.
Suppose the value of LENGTH is 20, ANGLE 60, and
CHANGE 3? The turtle will move forward twenty turtle units,
turn sixty degrees, and—what? At that point, procedure POLY¬
SPI calls itself, so the turtle can only repeat the procedure all over
again, starting from the beginning. However, note that when
POLYSPI calls itself, it alters the value of the variable DISTANCE
by adding CHANGE to it. Thus, the turtle behaves somewhat dif¬
ferently the next time POLYSPI is executed. The result is that,
while the turtle starts out to draw a five-sided shape (just as our
DRAWSQUARE procedure caused it to draw a four-sided shape),
it ends up drawing a spiral instead.
If we change the value of the variable CHANGE, we will see
other kinds of spirals being drawn. This can be very beautiful to
watch. Of course, because the procedure keeps calling itself, it
never ends—until we tell the computer to stop executing the pro¬
gram. It doesn't really matter that our POLYSPI procedure does
nothing more than create polyspirals. The patterns are an end in
themselves.
That's what LOGO is about—exploring patterns and ideas. In
addition to turtle graphics, some versions of LOGO also feature
"sprites"—tiny figures of varying shapes and sizes that can be
made to race hither and yon about the video screen, thus allow¬
ing the programmer to create animated computer "movies."
This discussion barely begins to cover the capabilities of the
LOGO language. If you know a little about computer program¬
ming (or even if you know a lot), you might want to locate a
computer that runs LOGO. Perhaps you can find one at your
school.

[72]
FINAL NOTE

The computer languages presented in this book are not the only
ones in existence—not by a long shot—but they are probably the
best-known and most popular ones. It would be difficult, in fact,
to estimate how many computer languages now exist. Many of
them are used only for special applications rather than for gen¬
eral computing.
The language LISP, for instance, is used primarily by
researchers in the field of artificial intelligence, the science that
attempts to program computers to behave—in some ways, at
least—like human beings. The name LISP is short for LISt Pro¬
cessor; the language LOGO (which we studied in the last chapter)
was based on LISP.
Mathematicians might find themselves drawn to APL, a lan¬
guage that features many mathematical symbols and virtually no
recognizable words at all! In fact, APL (which stands for A Pro¬
gramming Language) uses such a wide variety of symbols that
most of them are not even available on the standard computer
keyboard, and so special keyboards have had to be designed for
use in conjunction with this language.
Those programmers interested in large-scale computer appli¬
cations with many files (like those we studied in the COBOL
chapter) may want to investigate PL1. In the spirit of APL

[73]
(though somewhat less modestly so), the name PL1 stands for
Programming Language 1. (No PL2 has been developed yet.)
Teachers who need to develop educational programs on
short notice often turn to PILOT, an easy-to-learn and easy-to-
apply programming language designed for use in schools. Pro¬
grammers who desire a language with the flexibility of assembly
language and some of the ease of high-level languages may
explore FORTH, a unique hybrid of the two.
New languages are being developed all the time. Recently,
the Xerox Corporation made public a new language called
SMALLTALK. And the U.S. Air Force just released the final spec¬
ifications of a computer language called ADA. (ADA was named
for the Countess Ada Lovelace, daughter of the poet Byron and
companion of Charles Babbage, inventor of the digital computer.
Legend has it that Ada was history's first computer programmer.)
There are even new variations on old languages.
Some programmers believe that the ultimate computer lan¬
guage will be English (or French or Russian or whatever lan¬
guage the programmer normally speaks). Someday, computers
may have the ability to converse (by keyboard and video screen,
anyway) with programmers in natural language. We don't know
when such "fifth generation" computers will appear, if ever, but
when and if they do become available, the question "How do you
speak to a computer?" will no longer require an entire book to
answer.

[75]
GLOSSARY

Address. The number that represents the location of a specific


memory cell within the computer's memory.
ASCII code. The American Standard Code for Information Inter¬
change. The ASCII code assigns numbers to represent letters,
numerals, and other symbols. These numbers can then be
used to store those symbols in a computer's memory.
Assembler. A computer program for translating assembly-lan¬
guage programs into machine language.
Assembly language. A computer language made up of written
instructions called mnemonics, each of which is translated
directly into a single machine-language op-code by a pro¬
gram called an assembler.
BASIC. Beginners All-Purpose Symbolic Instruction Code. A com¬
puter language designed to be easy to learn and use. The
most popular language for microcomputer users and proba¬
bly the most widely known computer language in the
world.
Binary. A system of numbering that uses only two counting
digits: 0 and 1. Binary is the numbering system used by dig¬
ital computers.
COBOL. COmmon Business Oriented Language. A computer pro-

176]
gramming language designed for business applications, such
as the storage and processing of large files of information.
Compiled language. A high-level computer language for producing
programs written in high-level instructions (usually En¬
glish-like or mathematical words and symbols) that can then
be translated into machine-language programs by a compil¬
er. FORTRAN and COBOL are compiled languages.
Compiler. A program that translates high-level language pro¬
grams into machine-language programs.
Data name. A COBOL variable. Data names are used in COBOL
programs to refer to the location in memory where certain
pieces of information have been stored.
Decimal. A system of numbering that uses ten counting digits: 0,
1, 2, 3, 4, 5, 6, 7, 8, and 9. Decimal is the system we normally
use for counting.
Delay loop. A program loop, the sole purpose of which is to take
up time during the execution of a program.
Execute. Perform. Refers to a computer's response to commands
given it by an operator.
File. An organized collection of data used in a computer program.
Files can be stored on magnetic disks, magnetic tape, or even
on punch cards for long-term or temporary use.
FORTRAN. FORmula TRANslator. The first significant high-
level computer language. FORTRAN was designed chiefly
for use by scientists and engineers.
Graphics. Pictures on the video screen of a computer.
Hexadecimal. A system of numbering that uses sixteen counting
digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Hexa¬
decimal is popular with computer programmers because it is
easily converted into binary (and vice versa).
High-level language. A computer language made up of English-
like or mathematical words that are translated by the com¬
puter into machine language. FORTRAN, COBOL, BASIC,
PASCAL, and LOGO are all high-level languages.

[77]
Input. Information put into the computer by the user or the pro¬
grammer. Input can enter the computer from the keyboard,
the disk drive, a modem (which connects the computer to a
telephone or to another computer), or any of several other
input channels.
Interpreted language. A high-level language for producing pro¬
grams written in high-level instructions that can then be
translated into machine language, all this while the program
is actually executing.
Interpreter. A program that translates high-level languages into
machine language while a program is actually being exe¬
cuted.
Jump instructions. Instructions in assembly language that cause the
execution of a program to jump to a new location in the com¬
puter's memory. Also called a branching instruction. In high-
level languages, jump instructions are usually called GOTOs
and cause the execution of the program to jump to a different
line of the program, rather than to a specific location in
memory.
Label. A name given to a portion of a program. The label can then
be used elsewhere in the program to refer to that portion,
usually for a jump or GOTO instruction.
LOGO. A computer language designed primarily for use by stu¬
dents. LOGO is based on the earlier computer language
LISP.
Loop. A program structure in which the computer executes a
sequence of instructions more than once.
Machine language. The only language actually "understood" by
the computer. Machine language is made up of a sequence of
zeros and ones, broken up into shorter sequences called op¬
codes, each of which gives a single instruction to the com¬
puter. Programs written in all other computer languages
must be translated into machine language before they can be
used on a computer.
Macroinstruction. Usually abbreviated to macro. An assembly-lan-
[78]
guage instruction defined by the programmer as being made
up of a number of briefer instructions. When an assembler
translates an assembly-language program into machine lan¬
guage, it substitutes a sequence of shorter instructions for
the macro.
Memory. The portion of a computer designed to store information
in the form of binary numbers.
Memory cells. Places within the computer where numbers can be
stored and later retrieved for use.
Mnemonic. An assembly-language instruction. The word comes
from a Greek word meaning "memory," because assembly-
language mnemonics are intended to be easier to remember
than the machine-language op-codes they represent.
Octal. A system of numbering that uses eight counting digits: 0,1,
2, 3, 4, 5, 6, and 7. Octal is sometimes used by computer pro¬
grammers because of the ease with which it can be translated
into binary.
Operation codes. Op-codes for short. Instructions in machine lan¬
guage. An op-code is usually made up of a fixed number of
binary digits (zeros and ones).
Output. Information moving from within the computer to outside
the computer. Output is generally channeled through a
device such as a video screen or a printer.
PASCAL. A high-level computer programming language de¬
signed on the principles of structured programming.
Procedure. A high-level computer instruction defined by the pro¬
grammer.
Program. A sequence of instructions given to a computer, detail¬
ing how the computer is to perform a desired task.
Programming. The act of writing computer programs. A person
who writes such programs is called a programmer.
Source program. A program written in a high-level language or
assembly language, before being translated into machine
language by a compiler or an assembler.
Structured programming. A set of programming techniques de-
[79]
signed to make programs easier to read and less vulnerable
to programmer errors.
Turtle. A tiny, movable triangle on a computer's video screen that
can be used to draw lines and patterns. Turtles were origi¬
nally only used in the language LOGO, though they are now
available in certain versions of other computer languages.
Variable. A symbol used in a computer program to represent the
location in memory where a piece of information is stored.
The programmer can then use that symbol in place of the
stored information. For instance, if the symbol B is used to
stand for the place in memory where the number 2 is stored,
then the expression "B + 5" would add the number 2 to the
number 5. If another number, such as 7, were later stored at
the location represented by B, the expression "B + 5" would
then add the number 7 to the number 5.

[80]
INDEX

Italicized page numbers Assembly language, 13-26


indicate photographs. Atari BASIC, 57

Accumulator (A), 14 Babbage, Charles, 75


ADA, high-level computer language, BASIC (Beginners All-Purpose Sym¬
75 bolic Instruction Code), high-level
Addresses, 17 computer language, 31, 52-60, 61-
American National Standards Insti¬ 62
tute, 57 Binary (2-digit) numbering system,
American Standard Code for Infor¬ 8-10, 17
mation Interchange (ASCII code), Branching instruction, 22-23.
18 See also GOTO statement, BASIC
APL (A Programming Language), Business uses of computers, 42-51
high-level computer language, 73
Apple BASIC, 57 COBOL (Common Business Oriented
Apple II+, 54 Language), high-level computer
Applications of computers language, 42-51
business, 42-51 Commands, 31
engineering, 31, 32, 40 Commodore 64, 56
graphics, 24, 67-72 Compiled language, 41, 44
science, 31, 40 Compiler, translating program, 40
social science, 52 Computer capabilities
writing, 18, 20, 21 arithmetical and mathematical,
Arcade games, video, 24, 67 31, 40
Arithmetical capabilities of comput¬ data handling, 42-52
ers, 31-40 graphic, 24, 67-72
Assemblers, translation programs, 13 Computer chips, 6
Computerized record-keeping, 44 FOR-DO loop, PASCAL, 62
Computer languages, high-level Form letters, computerized, 44
ADA, 75 FOR-NEXT statement, BASIC, 61
APL, 73 FORTH, high-level computer lan¬
BASIC, 31, 52-60, 61 guage, 75
COBOL, 42-51 FORTRAN (Formula Translator),
FORTH, 75 high-level computer language, 31-
FORTRAN, 31-41 41
LISP, 73 Functions of computers
LOGO, 67-72 arithmetical and mathematical,
PASCAL, 61-66 31, 40
PILOT, 75 data handling, 42-52
PL1, 73-75 graphic, 24, 67, 72
SMALLTALK, 75
See also Assembly language; Ma¬ Games, video arcade, 24, 67
chine language GOTO statement, BASIC, 61-62
Customer records, computerized, 44 Graphics, use of computers in, 24,
67-72
Dartmouth College, 52 Growth of high-level computer lan¬
Data division, COBOL, 45-46 guages, 31, 44, 52, 55-57, 60, 61-62,
Data-handling capabilities of com¬ 67, 75
puters, 42-52
Data name, 46-47 Hexadecimal (16-digit) numbering
Decimal (10-digit) numbering sys¬ system, 11-12
tem, 8, 10 High-level computer languages
Delay loop, 64-65 ADA, 75
Development of high-level computer APL, 73
languages, 31, 44, 52, 55-57, 60, BASIC, 31, 52-60, 61
61-62, 67, 75 COBOL, 42-51
Disk, magnetic, 44 FORTH, 75
DO loop, 37-38 LISP, 73
LOGO, 67-72
Electronic switching, 3-7 PASCAL, 61-66
Employee records, computerized, 44 PILOT, 75
END IF statement, 36 PL1, 73-75
Engineering uses of computers, 31, SMALLTALK, 75
32, 40 See also Assembly language; Ma¬
Environment division, COBOL, 45 chine language

File-keeping, 42-44 Identification division, COBOL, 45

[82]
IF-THEN statement, 36 Machine language, 1-12
Increment, 37 Macroinstruction (macro), 27-29
Infinite loop, 35 Magnetic disk, 44
Input, 58 Magnetic tape, 44
Intel 4004 microprocessor, 55 Mark I computer, 6
International Business Machine Cor¬ Massachusetts Institute of Technolo¬
poration (IBM), 31 gy, 67
IBM 402 accounting machine, 6; Mathematical capabilities of comput¬
punch card, 2 ers, 31-40
Interpreted language, 53 Memory, 7, 22
Interpreter, translating program, 53 Memory cells, 17
Inventories, computerized, 44 Microcomputers, 55
Microprocessors, 6
Jumping instruction, 22-23 Mnemonics, 13

Kemeny, John, 52 NASA (National Aeronautics and


Keyboard transmission, 1 Space Administration), 32
Kurtz, Thomas, 52 Numbering systems
binary (2-digit), 8-10, 17
Labels, 14, 23-24, 58 decimal (10-digit), 8, 10
Language hexadecimal (16-digit), 11-12
assembly, 13-26 octal (8-digit), 11-12
machine, 1-12.
See also High-level computer lan¬ Octal (8-digit) numbering system,
guages 11-12
Limiter, 37 Operation codes (Op-codes), 5-12
LISP (List Processor), high-level com¬
puter language, 73 Papert, Seymour, 67
LOGO, high-level computer lan¬ PASCAL, high-level computer lan¬
guage, 67-72 guage, 61-66
Loop, 25, 26 Paychecks, computerized, 44
delay, 64-65 PILOT, high-level computer lan¬
DO, 37-38 guage, 75
FOR-DO, 62 PL1 (Programming Language 1),
FOR-NEXT, 61 high-level computer language, 73-
GOTO, 61-62 75
infinite, 35 Printer, Radio Shack, 21
REPEAT-UNTIL, 62-63 Procedure division, COBOL, 45
WHILE-DO, 62, 63-64 Procedures, 64-66, 70-71
Lovelace, Countess Ada, 75 Program, computer, 4

[83]
Programmer, 4 Translation of programs by comput¬
Programming, 4-12 er, 10, 13-26
structured, 61-66 Transmission of messages to comput¬
Program storage, 22 er, 1
Proliferation of computer languages, TRS-80, Radio Shack, 14, 57
31, 44, 52, 55-57, 60, 61-62, 67, 75 TRS-80 BASIC, 57
Punch cards, 1, 2, 44 Turtle, 68
Turtle graphics, 67-72
Radio Shack TRS-80, 14, 57 Turtle units, 68
printer, 21
Record-keeping, 42-44 U.S. Air Force, 75
Recursion, 71-72 Uses of computers
REPEAT-UNTIL loop; PASCAL, 62- business, 42-51
63 engineering, 31, 32, 40
Repetitive jobs, 7 graphics, 24, 67-72
science, 31, 40
Sciences, uses of computers in, 31, 40 social science, 52
SMALLTALK, high-level computer writing, 18, 20, 21
language, 75
Social sciences, uses of computers in, Variables, 33, 59, 70-72
52 Video arcade games, 24, 67
Special-effects computers, 67
Statements, 31 Wang word processor, 20
Statistics, use of computers in analy¬ WHILE-DO loop, PASCAL, 62,
sis of, 52 63-64
String variables, 59 Word processors, 18, 20, 21
Structured programming, 61-66 Writers, use of computers by, 18, 20,
21
Tape, magnetic, 44
Texas Instruments, 57 Xerox Corporation, 75
TI BASIC, 57
Times Sinclair 1000, 14, 15 Z80 microprocessor, 14

[84]
ABOUT THE AUTHOR

Christopher Lampton is a science writer and a self-taught com¬


puter programmer. He is proficient in a number of languages and
is authoring for Watts all the upcoming Computer Literacy Skills
books. Chris has developed a number of his own game programs
and is an admitted "computer junkie."
For Watts, Chris has also authored several popular First
Books, including Dinosaurs and the Age of Reptiles and Space
Sciences (a Reference First Book). He lives in Maryland.
;omputerlanguageOOIamp
computerlanguageOOIamp

compiiterlanguageGOIamp

Boston Public Library

WEST ROXBURY
BRANCH LIBRARY

O A 76 » 7
L35
1983

84013024-76 WR

The Date Due Card in the pocket indi¬


cates the date on or before which this
book should be returned to the Library.
Please do not remove cards from this
pocket.
For a thorough understanding of computers
and computer science, be sure you have
all the titles in Franklin Watts’ new
Computer-Awareness First Book series.
Titles just published include:

CAREERS IN THE COMPUTER INDUSTRY


COMPUTER LANGUAGES
COMPUTERS IN OUR WORLD, TODAY AND TOMORROW
DATA PROCESSING
INVENT YOUR OWN COMPUTER GAMES
PROGRAMMING IN BASIC

And coming soon:

/Computer graphics
W ROBOTS
THE SCIENCE OF ARTIFICIAL INTELLIGENCE
WORD PROCESSING

Also coming soon are the Watts’


Computer Literacy Skills books,
which will teach popular programming
languages to beginning programmers.
-r-i Titles include:

BASIC FOR BEGINNERS


COBOL FOR BEGINNERS
FORTRAN FOR BEGINNERS
PASCAL FOR BEGINNERS

You might also like