The Easy Guide To Your Commodore 64 (1983)
The Easy Guide To Your Commodore 64 (1983)
The Easy Guide To Your Commodore 64 (1983)
GUIDE TO YOUR
COMMODORE 64
THE EASY
GUIDE TO YOUR
COMMODORE 64®
JOSEPH KASCMER
Every effort has been made to supply complete and accurate information. However,
SYBEX assumes no responsibility for its use, nor for any infringements of patents or other
rights of third parties which would result. Manufacturers reserve the right to change speci
fications at any time without notice.
Copyright ©1983 SYBEX Inc. World Rights reserved. No part of this publication may
be stored in a retrieval system, transmitted, or reproduced in any way, including but not
limited to photocopy, photograph, magnetic or other record, without the prior agreement
and written permission of the publisher.
Acknowledgements ix
Preface x
2 VIDEO PRINTING 10
Screen Control 10
Video Printing 13
Usingjudgement 29
Simulations 31
PROGRAM CONTROL 35
APPENDICES 120
Index 128
IX
ACKNOWLEDGEMENTS
Thanks to James Compton for his editing and to those in the editorial
and production departments at Sybex who were active in turning my
manuscript into this book.
Special thanks to Heidi Miller for preparing the index and to Bill Mlotok
for bringing his technical knowledge, classroom experience, and good sense
to a valuable review of the book's content.
Also thanks to Paul Losness of PC Computers in El Cerrito, CA, for
providing information.
The poetry on page 11 is by Philip James Bailey, from his Festus. The
verse on pages 12-13 is from Robert W. Service's "The Law of the
Yukon."
PREFACE
Because you can use a computer in different ways, you can approach
this book from more than one direction:
Chapter 1 is a brief introduction telling where the controls are, how to
plug your computer system together, and what buttons to push and not to
push. No matter what you plan to use your computer for, a run through
this chapter will put you at ease with the box in front of you.
If you plan to use only prerecorded programs and wish to avoid writing
computer directions yourself, you can skip ahead to Chapter 7 if the pro
grams are on cassette tape, or to Chapter 8 if they are on disks. And if
you'd like to make full use of the programs on disks, read the second part
of Chapter 9. Since the Commodore 64 isn't yet as well supplied with
commercial software as some older personal computers, you may not
want to limit yourself to these chapters.
If you'd like to write your own directions to the computer, you should
read Chapters 2,3, 4, 5 and 6, and also the first part of Chapter 9.
The most optional chapter of the book is Chapter 10. Ifyou'd like more
XI
Editor's Note
To get the most out of this book, you should read it with your computer
in front of you, set up and connected to a video monitor or TV set as
described in Chapter 1. As each new command, group of commands, or
sample program is presented, type it in exactly as printed to see the results on
your screen. Be sure to type in each character that you see: letters, num
bers, punctuation marks, and spaces. (In typeset program lines, the space
equivalent to one stroke of the space bar on your keyboard will vary
slightly, but it will always be approximately equal to the space between
words in this sentence.) The spaces are not absolutely essential; any pro
gram will run without them, but you will find the screen display much
easier to read if you include them.
After you type in a sample program, and before you run it, check what
you have typed against the book. If you find you have made a mistake, go
back and correct it. (You'll see how to do that in Chapter 2.) The pro
grams have all been carefully tested, and will run as described if entered
correctly.
Finally, you will see that the Commodore 64's command words (or
"reserved" words) have been set in boldface type. Boldfacing is simply a
typographical convention, meant to call your attention to these words and
help you to remember them. You will not see this effect on your screen,
and you need not try to reproduce it.
MASTER AND SERVANT MEET
Facing a computer for the first time, people often feel powerless over
the machine—and for a good reason. After all, when it comes to com
puters, knowledge is power, and we all start off ignorant.
A worker of incredible speed and unimaginable precision, the computer
is nonetheless a completely obedient servant. It only needs directions, stated
in terms it was built to recognize. You can command the Commodore 64
with a vocabulary no larger than that understood by a highly-trained
canine. A computer following your commands can choose among alterna
tives, organize information, search for facts and patterns, and control
devices. It can be ordered to begin any duty in response to a single word or
by the triggering of a single event.
No computer is all-powerful or all-knowing, and there are limits to the
operation of any machine. But those limits arise from the design and
speed of the computer and the size of its memory.
Although you can gain control of the Commodore 64 in a short time, to
do so, you must first learn to become its master. Through this book you'll
find a clear way to do just that.
and computer display. Figure 1.3 shows the system connected for com
puter use only.
The final adjustments include setting your television tuner to either
channel 3 or channel 4, whichever one is not used for broadcast in your
area. Also set the channel on the computer. If channel 4 is unused, slide
the small, recessed switch at the back of the computer (next to the metal
television jack) toward the jack. If it's channel 3, slide it away from the
jack.
Now that the connection between computer and television is complete,
you can use the computer by setting the switch on the box to COM
PUTER. To use the television for tv reception, slide the switch to TELE
VISION.
If you use a video monitor, a different cable—one ending in a five-pin
plug at the computer end and a phono plug at the monitor end—connects
to a flat, circular video jack at the back of the computer, next to the televi
sion jack, as shown in Figure 1.4.
Once connected to a television or monitor, the computer before you is
part of a fully operational system that can display on the screen whatever
you type.
All you need to command the computer sits before you. The keyboard
is laid out like a typewriter's, but has a few extra keys that give you more
control than is possible with a typewriter.
Before turning on power to the computer, consider a simple, comforting
fact: Nothing you can do from the keyboard will damage the computer.
With some of the commands you'll find in this book, you can change the
screen display, the contents of the computer's active memory, or even the
way the computer works, but when you turn the power to the Commo
dore 64 off and then on again, you wipe dean the changes you've made.
The computer then greets you in readiness, all previous keyboard manip
ulations forgotten.
The single most useful bit of advice while you're learning to operate the
computer from the keyboard is this: If something interests you, try it. The
worst that can happen will be the loss of some command, information or
calculation. In the earliest stages, while you're experimenting, such a loss
is part of learning to control the beast. No matter what happens, the com
puter will emerge from human keyboard error or curiosity completely
undamaged.
When you switch on first the monitor or television, then the computer,
printing will appear on the screen. Across the top, if all is connected and
working properly, three lines will be printed as light letters on a dark
screen, as in Figure 1.5.
The Cursor
At the upper left of the screen you will see the word READY, and below
that a character-sized flashing square; these are signals from the computer
REAOV.
Figure 1,5: Display on the screen after the computer isfirst turned on.
THE EASY GUIDE TO YOUR COMMODORE 64
that it's ready to read whatever you type from the keyboard. The flashing
square marks where the next character from the keyboard will appear on
the screen, and is known as the cursor.
The Commodore 64 is designed to display immediately what's typed
from the keyboard, and will print light blue letters on a dark blue screen as
you type them. The word READY is a signal from the computer that it's
prepared to accept a command. But whether you type one of the scores of
commands the Commodore 64 can act on or a line of poetry, your typing
will appear immediately on the screen. And as you type, the computer
waits for a signal from you that will tell it to act on the command you've
just typed.
character, and pressing the C = key and a graphics symbol key will pro
duce the character shown on the left of that key, as it did before.
You switch out of this lowercase-and-uppercase mode and back to the
uppercase-and-graphics mode by pressing SHIFT and C = again. In
fact, each time you press the SHIFT and C = keys together you switch the
computer from one display mode to the other. Any typing already on the
screen will also change when the computer is switched from one display to
the other. Together, the SHIFT and C = keys act as your switch to control
the display from the computer.
The SHIFT LOCK key can be set to either of two positions. When
pressed down and allowed to click into a lowered position, its effect is the
same as holding down the SHIFT key. Pressed again, the SHIFT LOCK
key will click into a raised position, and there is no such effect.
Two other keys allow you to control the display further. The key in the
upper-right corner of the keyboard marked INST DEL serves two func
tions. It can insert or delete. When pressed by itself, it directs the cursor to
erase and close up one space to the left of the cursor. When pressed while
the SHIFT key is held down, it directs the computer to open a space to the
left of the cursor and move the cursor there, so another character can be
8 THE EASY GUIDE TO YOUR COMMODORE 64
typed. If you hold down this key in either mode of operation, it will repeat
the effect until you release it.
The key marked CLR/HOME, next to it, works in two ways also. You
can use it to direct the computer to send the cursor "home," to
the upper-left corner. Or, pressing SHIFT and that key together
(SHIFT-CLR/HOME), you can direct the computer to clear the screen
and send the cursor up to that corner.
RETURN Revisited
all of the keys. Pressing any key simply provides a signal to the computer,
which responds to that signal by printing a character or taking some pre
planned action. All the keys are shown in Figure 1.6.
Whether you are using prerecorded commands (from tapes or disks, as
explained in Chapters 7 and 8) or direct commands, you ultimately con
trol the Commodore 64 through the keyboard. The keyboard serves as
reins and bridle to the computer and any devices attached to it.
SCREEN CONTROL
screen of all printing and to move the flashing cursor to the upper left cor
ner. When given this command, the computer presents a blank screen,
regardless of what was typed there before. If you wax poetic, for instance,
and type onto the screen something like the lines in Figure 2.1, you can
remove what you've typed at once by doing the following:
and everything that follows on the screen will be printed in what is called
SENO Mf
STRONG
VIDEO PRINTING
The Commodore 64, like any computer worth its circuits, can do more
than display your keystrokes on a video screen. In fact, much of the Com
modore 64's computing power lies in the built-in operations it works on
furiously at your command, without giving so much as a clue to what it's
doing. The simplest example can be seen with elementary arithmetic,
which the Commodore 64 works on automatically and unseen when so
14 THE EASY GUIDE TO YOUR COMMODORE 64
-2*4
you won't see the result, just a PSYNTAX ERROR reply and another
READY prompt. To see the results of that multiplication, you must add a
command.
PRINT-2*4
As used here, the PRINT command directs the computer to display the
result of the multiplication that follows it. After sending this command by
pressing the RETURN key, you'd see the value — 8 on the screen and a
new READY prompt below that, signalling the computer's readiness for
another command, as shown in Figure 2.4.
You can put the PRINT command into service for any combination of
arithmetic commands according to the rules of algebra, to operate your
sophisticated Commodore 64 as a simple calculator, with entries like this
one:
PRINT 9+ 8-7*6/5
PRINT (9 + 8-7)*6/5
for a result that will appear, below the PRINT statement, as 12.
But the PRINT command's usefulness goes beyond simply displaying
mathematical manipulations (all of which are listed in Appendix A).
VIDEO PRINTING 15
The computer's response after you press the RETURN key will be the
words,
So glad to be here.
You can even slip a calculation into a printed sentence, like this:
PRINT 20 "or" 30 "is more than I'm willing to pay. But" 10+5
"sounds right."
To do this you insert the words TAKE A within the second set of quotes.
You can do this by placing the cursor over the letter L in the word LOOK,
and there inserting seven spaces, for the words TAKE A and the blank
space between them. To insert the spaces, you just press the SHIFT and
INST/DEL keys seven times together.
If you press the RETURN key now, you'll see a changed display result:
You can use such an approach to send any command from the screen
into the computer for action. If the command statement takes up two lines
on the screen, you can send it by positioning the cursor anywhere on
either of the lines, and then pressing the RETURN key.
18 THE EASY GUIDE TO YOUR COMMODORE 64
More Commands.
You'll notice, when you press the RETURN key after typing a num
bered statement like this, that the computer replies with a flashing cursor,
without acting on the commands. Actually, the statement is sent into stor
age in the computer's memory, where it is kept on file by number. Here,
20 THE EASY GUIDE TO YOUR COMMODORE 64
the commands will be held as line number 10 until you type the command
that tells the Commodore 64 to proceed with them. That command is:
RUN
which directs the computer to translate and act on the numbered state
ments in its memory, in numeric order. As the only numbered line of
commands in memory in this case, line 10 will be acted on command-by-
command. The computer automatically holds numbered commands in
memory, even after acting on them, so you can tell the computer to carry
out the commands of line 10 as many times as you like by sending the
command RUN each time.
Although commands may reside unseen in the computer's memory,
you can display them on the screen with a simple command. If you send
the command
LIST
the computer will respond by printing the entire contents of its program
memory—in this case, line 10.
You can send another set of commands for the computer to act on after
line 10 by labeling the statement with a higher number, like this:
The computer now has two lines in its memory and, when sent the
command:
RUN
will start with the lower-numbered statement, 10, translate and carry out
each command, and then translate and carry out the commands of state
ment 20.
You can direct the computer, with a series of numbered statements, to
switch among different screen printing displays to fashion a result like that
produced by these additional statements:
If, after sending these statements to the computer, you give the command
RUN, you'll see a display of four statements appearing one after another
with a final result like Figure 3.1.
Statement lines 10, 20, 30, and 40 make up a program that can direct
PLANS AND PROGRAMS 21
REAOV.
the computer, each time you issue the RUN command, to produce the
same printing display. Change the statements in the program and you
change its results. For instance, the reverse-video row that declares:
will be absent from the screen during a program run if you erase line 20,
in which you ordered its printing. To erase a line from the computer's pro
gram memory, you can simply type the line number and press the
RETURN key:
20
Figure 3.2: A program listing after one line has been deleted.
The computer follows this simple rule in handling program lines: The
contents of a newly-sent numbered line replace the previous contents of
the same number. You can add a new statement as line 20 like this:
is printed.
The computer carries out numbered statements sequentially, according
to any numbering scheme you devise (of positive, whole numbers up to
63,999). It will follow a program beginning at any number moving up the
sequence of numbers, regardless of gaps in numbering between them.
Numbering by tens is a handy approach to programming that leaves
room between statements to add any program lines that occur to you as
afterthoughts or refinements.
The computer retains numbered statement lines in its program mem
ory until you switch off the power, or remove particular lines by typing
PLANS AND PROGRAMS 23
their line numbers, or command the computer to erase them all. You can
direct this erasure by sending the simple command:
NEW
The letter A substitutes for a number in the TAB command here. If you
sent the RUN command to the computer for this two-line program with
out first assigning a value to A, it would follow built-in directions and
replace the letter A with the value zero.
10 A = 12
When you RUN this three-line program, the result will be the words
"HOW DOES THIS LOOK?" printed beginning at column 12.
Having so named a number used in the program, you can change the
operation of the program without changing the line that directs that oper
ation. Change the value of A in line 10, like this:
10 A = 20
and the computer will print at column 20. Used in this way, the letter A
acts as a variable symbol that can stand for whatever number you specify
with an equal sign.
You can use a variable in as many commands in your program as you
want. If you add the command:
to the program, the computer will respond when you RUN the program
by printing
COLUMN 20
Then it will print the question starting at column 20 below that line.
Again, if you change the value of A and then give the command RUN,
the computer will position the printing accordingly, by column number:
10 A = 5
NUMERICAL PREDICTIONS
The computer can act on more than one symbolic variable in a pro
gram, if you include an assignment statement for each. This capability
gives you enormous power in writing programs to analyze or predict vari
ous situations. If you can describe a situation with numbers, you can
direct the computer to simulate the situation with any or all of its aspects
changed. You can, for instance, compare the results of different salary
offers, or the filling of reservoirs behind different dams.
A prediction of the filling of a dam or your bank account could be made
by forming a numerical model of the factors known to be at work. Then,
sending values for these factors to the computer with assignment state
ments, you can see what the model predicts as a result. You can easily
predict the amount of water in a reservoir or the amount of money in your
account after, say, nine months, if you know the amount already there and
the average monthly rate at which more accumulates. You can direct the
computer to print these values and then to print the calculation of the total
amount nine months later with the following program. (Be sure to send
the NEW command to clear the last program from memory before typing
this one.) In it, the original amount is symbolized by the letter A, and the
monthly rate of accumulation by the letter B:
NEW
30 PRINT "(press SHIFT and CLR/HOME)"
40 PRINT "STARTING WITH" A
50 PRINT "AND GAINING" B "EACH MONTH"
60 PRINT "YOU'LL HAVE" A + (B*9) "AFTER 9 MONTHS."
If you command the computer to RUN this program now, it will auto
matically substitute zero for the variables A and B. But if you assign val
ues for the amount present and the rate of accumulation, the computer
will display those values and, through the directions in program line 60,
print a prediction of the future amount. You can give values to the varia
bles with the program lines:
10 A = 2200
20 B = 355
The computer will run the complete program with 2200 as the starting
amount (gallons of water, or dollars) and 355 as the monthly increase, to
produce the display shown in Figure 3.3.
26 THE EASY GUIDE TO YOUR COMMODORE 64
25 C = 15
When you run the program through the computer now, it will display the
original amount of 2200, and predict an increase of 355 each month, over
a period of 15 months.
You can direct the computer to substitute one variable letter for
another, or for an "expression," consisting of variables and operators. In
COMING INTO POWER 27
this program if you wanted a shorthand way of naming the final amount
predicted, you could rename that amount, so far represented by the value
of A + (B X C), in a separate statement, calling it F:
27 F = A + (B*C)
Now that the final value has a simple name, you can order the calculation
and display of the difference between the original and final values, A and
F, with this simple statement:
You could then streamline the program's appearance a bit with a substitu
tion in line 60:
You can again change the details of this model by changing the values in
lines 10, 20, and 25.
Although we have used only whole numbers so far, the computer is also
designed to work with decimal numbers. It can take values stated in fine
detail—for example, A = 2200.35. It can also handle fractions if they're
treated as divisions. The value for 10 and 3/4 months, for example, would
be stated as C = 10 4- 3/4.
COMMANDS IN GROUPS
10 A = 220
20 B = 355
25 C = 15
can be grouped under a single program line number, if you prefer. If com
mands are separated from each other by colons, the computer will act on
28 THE EASY GUIDE TO YOUR COMMODORE 64
each command in a grouped set one at a time, as though each were on its
own numbered line. You could replace those three lines with a single line,
like this:
10 A = 2200:B = 355:C = 15
And then erase the two now-redundant lines by sending empty line num
bers with the RETURN key:
20
25
The new line number 10, holding a group of commands, then replaces the
earlier lines 10, 20, and 25. The computer will hold the same information
in its memory and carry out the program commands exactly the same
way. You can group as many commands in a single program line as will fit
in the 80 characters of space the computer recognizes for command
entries.
Although both methods of grouping commands produce the same
results when a program is run, you may prefer one to the other. Within a
program written with one command to a statement line, you can more
easily find problems that call for rewriting or alteration—the accidental
misspelling of a command leading to a " ?SYNTAX ERROR" reply dur
ing a program run, for example. On the other hand, grouping several
commands to a line number may be an easier way to compose a program
from multiple command statements. Either way, the choice is yours.
USING JUDGEMENT
Like a vigilant sentry, you can watch over your computer, deciding
when you would like certain commands repeated, or values changed, or
when you'd like to interrupt a program run. But that means you're
attending to the computer rather than having it attend to you.
Fortunately, such vigilance isn't necessary. Your decisions can be
reflected within a program, so that when the computer encounters specific
conditions you've described, it will take the action you've directed. The
Commodore 64 can examine a numeric comparison (that is, a statement
that one value is equal or not equal to, or greater or less than, another
value), and "evaluate" it as either true or false. If the statement is true, the
instruction that immediately follows will be executed. If not, the computer
will skip over that instruction and proceed to the next numbered line.
The command that allows this decision-making is the IF-THEN state
ment, which takes much the same form as the "if... then" conditional
statement in everyday English: "If it's raining now, then go by car." Ifthe
statement is true, then take the action named. (If not, then don't.) The
computer command looks like this:
10 5:B = 8:C =
30 THE EASY GUIDE TO YOUR COMMODORE 64
20 T=A + B + C
You can then arrange for the computer to display a statement if the sum is
100, with the command:
which will instruct the computer to print the statement only when the con
dition T = 100 occurs.
When this program is run through the Commodore 64, the quotation
will be printed. If line 10 is changed so that the values add to something
else, the computer will skip past THEN and its PRINT command, in this
case to the program's end.
On encountering an IF-THEN statement, the computer follows built-
in directions to evaluate the condition stated to the right of the word IE
When that condition is true, the computer follows the command to the
right of the word THEN. When it is false, the computer skips to the next
program line.
You can direct the computer in this decision-making process through
any mathematical relation it is built to understand. The following change
to the program, for instance, tests the numbers of guests arriving in
groups at a gathering:
Here, the test is not whether T is equal to a particular value, but whether it
is greater than the value.
When it encounters a number after the word THEN, the computer
translates the number as a direction to carry on with the program from the
commands in that line number. Thus, besides instantly following a com
mand in the IF-THEN statement, the computer can be sent to another
part of the program by line number, as in the following program:
NEW
10 A=20:B = 15:C = 9
20 T= A + B + C
30 PRINT "(press SHIFT and CLR/HOME) WITH GROUPS
OF"A"AND"B"AND"C"THE ROOM WILL HAVE'T
"PEOPLE IN IT"
40 IF T> 75 THEN 100
50 PRINT "(press CURSOR UP/DOWN) NO PROBLEM
ACCOMMODATING THAT MANY "
60 END
DECISION-MAKING BY COMPUTER 31
Using the values assigned in line 10, the computer will print the quota
tions in lines 30 and 50, then stop carrying out commands on reaching the
END statement. It will skip past the command that would redirect it to
line 100 from line 40: THEN 100. If you change line 10 so that the sum
exceeds 75:
10A=19:B = 23:C = 37
and run the program again, the computer will carry out the command
after THEN and skip past lines 50 and 60 to carry out the PRINT state
ment of line 100.
To direct the Commodore 64 to a line number from an IF-THEN
statement, you can also include the optional command word GOTO. The
following revision of line 40:
produces the same results as its predecessor, but may be a little easier to
read and understand.
In directing the computer forward through a program according to val
ues it encounters, or directing it to some immediate command within an
IF-THEN statement, you can change the way the computer executes a
program. By directing the computer backward to an earlier line number
from an IF-THEN statement, you can make it repeat earlier commands,
with variations if you like.
SIMULATIONS
size, the number of guests at the simulation's beginning, and the rate of
new arrivals:
NEW
10 C = 200:G = 20:R = 6
The computer will prepare the screen and display the available room from
these commands:
In line 30, the variable N will serve as a counter for the number of trips
through which the program has directed the computer. You make N a
counter and keep G, the number of guests, up to date with these
commands:
40 N = N + 1:G = G + R
The crucial command is the one that directs the computer either to con
tinue or to stop considering trips:
50 IFG<CTHEN30
This line instructs the computer to either return to the sequence at line 30
(if G is less than C) or continue to the final line, which signals you that a
limit has been reached:
When you run it, this program will direct the computer to repeat the
quotation in line 30 with different values until the room's limit is reached.
Figure 4.1 shows the steps that the program run takes.
First, the computer stores the values for C, G, and R (stated in line 10)
in its memory, then clears the screen and positions the cursor for the
PRINT statement in line 30. When that statement is first encountered,
the value of N has been automatically set at zero. This represents the con
dition of the room before the first trip of newcomers:
60
AFTER 21 TRIPS PRESENT & ROOM FOR
54
AFTER 22 TRIPS PRESENT £ ROOM FOR
48
AFTER 23 TRIPS PRESENT & ROOM FOR
42
AFTER 24 TRIPS PRESENT & ROOM FOR
36
AFTER 25 TRIPS PRESENT & ROOM FOR
30
AFTER 26 PRESENT & ROOM FOR
24
AFTER 27 TRIPS PRESENT & ROOM FOR
18
AFTER 28 TRIPS PRESENT & ROOM FOR
12
AFTER 29 TRIPS PRESENT & ROOM FOR
6
tEAOV.
again on line 30, the computer displays the values of N and G after that
first trip:
Blind to its poor grammar, the computer goes on to consider the second
trip at line 40, and repeats the process. It prints the same statement at each
trip until the pass at which the number of guests reaches the capacity,
when N is 30. On encountering the IF-THEN command at that point,
the computer advances to line 60, printing the message that the room
would be filled after that trip.
You can add a handy feature to this program by directing it to display
the conditions you stated in line 10:
70 LIST 10
This command will print line 10, as in Figure 4.2. You can now retype the
line to reflect a larger room, C, a different number of guests already
present, G, or a different rate of arrival, R.
Under the direction of an IF-THEN command sending it backward,
the computer conjures its repetitive powers. Acting under other com
mands, the computer can be sent in any direction to yet other commands,
34 THE EASY GUIDE TO YOUR COMMODORE 64
54
AFTER 22 PRESENT & ROOM FORl
43
AFTER 23 TRIPS PRESENT & ROOM FOR
42
AFTER 24 TRIPS PRESENT & ROOM FOR
36
AFTER 25 PRESEMT & ROOM FOR
30
AFTER 26 PRESENT & ROOM FOR
24
AFTER 27 TRIPS PRESENT & ROOM FOR
18
AFTER 29 PRESEMT & ROOM FOR
12
TRIPS PRESENT & ROOM FOR
EAOY.
Figure 4.2: A simulation program that displays the conditions which directed it.
If you put together many programs, you'll find handy a command that
marks program lines without affecting the computer's operation. That
command is REM, which stands for "remark." You can appreciate its
usefulness by considering the following graphics program, which directs
36 THE EASY GUIDE TO YOUR COMMODORE 64
the drawing of a checkered pattern like that shown in Figure 5.1. The pat
tern is made by first drawing vertical lines in white in even-numbered
columns (in program lines 20 through 60), and then slicing through them
with horizontal lines of black (in lines 70 through 120). Notice that line 70
uses the CLR/HOME key without SHIFT, to send the cursor "home"
without clearing the screen.
NEW
10 PRINT "(press SHIFT and CLR/HOME)"
20 A = 0
30 PRINT TAB(A) "(press C= and +)(press SHIFT and CURSOR
UP/DOWN)"
40 A = A + 2:IFA<25THEN30
50 PRINT
60 B = B + 1 :IFB<20THEN20
70 PRINT "(press CLR/HOME)"
80 M = 0
90 PRINT TAB(M) "(press SPACE BAR)(press SHIFT and CURSOR
UP/DOWN)"
100 M = M + 1:IFM<25THEN90
110 PRINT "(press CURSOR UP/DOWN)"
120 V = V + 2:IFV<20THEN80
S H S S
S S S S S
S S il; S S S S S
£ $ $ s a $ s $ a * s a
&&*&*$»$as* a
S S S S :j $ S ?: S S * S
and also before the statements that produce the horizontal lines:
A program marked in this way will be easier to identify and alter, later.
REM statements are subject to the same 80-character length limit as other
program statements; line 65 is as long as a REM line can be.
While this program is running, you might decide you'd like a slightly
different pattern. To stop the program in mid-stride, you can use any of
three commands. The planned way is to include a command to that effect
within the program itself. The program command that will interrupt a
run is STOP. You can strategically include it to stop the program run after
the vertical lines have been drawn by program statements 20 through 60,
like this:
65 STOP
38 THE EASY GUIDE TO YOUR COMMODORE 64
If you run the program with this additional line, the computer will stop
following program commands on encountering the statement, and will
inform you that a forced break in the run has been ordered, with the reply:
BREAK IN 65
The STOP command differs from the END command only in that the
computer replies with a message. STOP can be paraphrased as: "Print
BREAK IN and this line number, and wait for directions from the
keyboard."
An advantage of the STOP command is that you can, at your conve
nience, command the computer to proceed with the program run, by
sending the command:
CONT
120 V = V + 2:IFV<18THEN80
PROGRAM CONTROL 39
Of course, you can keep revising until the pattern is one you like. Perhaps
this revision of program lines 80 and 100 will add the finishing touch:
80 M = 5
100 M = M + 2:IFM<20THEN90
These three changes will produce a program that displays the pattern of
lines and circles shown in Figure 5.2 when run.
Figure 5.2: The result ofrevising the program that produced Figure 5.1.
40 THE EASY GUIDE TO YOUR COMMODORE 64
We've seen some commands that direct the Commodore 64 outside its
usual course through higher line numbers in a program; there are still
more. One, GOTO, is even simpler in action than the IF-THEN com
mand. We've already used it in a version of that command, IF-THEN-
GOTO. You can also use the GOTO command by itself, to direct the
computer back to some earlier statement in a program, again and again,
creating an unending bop of computer action. Ideal for repetitious tasks,
this sort of loop can be stopped only with one of the three program-
stopping methods described in the previous section.
The following program will, if you let it run, command the computer to
produce an "endless" multiplication and division table. (Actually, you
would eventually reach a number greater than the Commodore 64 can
handle, and get an OVERFLOW ERROR message.) In line 10 the
number to be multiplied and divided is set at five.
NEW
10 A = 5
20 PRINT "(press SHIFT and HOME) -THE NUMBER"
A" "
30 PRINT "N" TAB(5) "MULTIPLIED BY N" TAB(22) "DIVIDED BY N"
40 PRINT
50 N = N + 1
60 PRINT N TAB(10) A* N TAB(25) A/N
70 GOTO 40
The final command, in line 70, directs the repetition of the program
from line 40 on. Without that GOTO direction, the program would sim
ply run three rows of printing—the heading from line 20, the subheading
from line 30 and the first pass at the counter number N (when it's raised
from zero to one), the multiplication of A X N and division of A/N from
line 60. When the computer finishes with the commands of line 60 and
PROGRAM CONTROL 41
which directs the computer to evaluate A/N, compare it with the built-in
ranges and then to proceed with the appropriate program line—100, 200,
or 300—according to that value. In this case, you can put PRINT state
ments starting at program lines 100, 200, and 300 to alert you to the val
ues produced by earlier lines of the program and A/N. If you had no
interest in values less than two, for instance, you could give the following
42 THE EASY GUIDE TO YOUR COMMODORE 64
which will print "VALUE BELOW TWO" in the A/N column, and then
stop the computer with the message "BREAK IN 110."
Likewise, if you wanted the computer to continue the program run
and merely signal you when the value A/N slipped below four and then
again below three, you could add statements starting at 300 and 200,
respectively:
and
You may want to repeat a useful set of commands, perhaps not endlessly
PROGRAM CONTROL 43
(as done by the GOTO statement), nor conditionally (as done by the
IF-THEN statement), but simply for a specific number of times. The
Commodore 64 will respond to a pair of commands that do this. One
counts off the repetitions, the other directs the computer back to the first
command. These commands are FOR and NEXT. You can slip them in,
one before a stretch of statements you'd like repeated, the other after.
Here's a program to which you can add this pair of commands in a
variety of ways to produce a variety of displays. It directs the computer to
draw an arrow shape.
NEW
10 PRINT "(press SHIFT and CLR/HOME)"
20 X = 5
30 PRINT TAB(X)" (press SHIFT and M)"
40 PRINT TAB(X)" (press SHIFT and Z)"
50 PRINT TAB(X)" (press SHIFT and N)"
Through the value you type for X as a column number in line 20, you can
locate the arrow drawn by the directions in lines 30, 40 and 50 in any hori
zontal position. Now, to draw the arrow several times at different places,
the computer needs directions to repeat lines 30, 40, and 50 for different
values of X, so that the same pattern is plotted from differing starting
points.
You can give these directions with the first statement of the counting
pair. In this statement you state a starting value, an ending value, and
(optionally) the size of the jumps the computer should make in going from
one value to the other. In this graphics program, you can specify a column
number represented by X, to be changed at each pass so that the arrow
will be drawn at each of several places along a single row, spaced 8
columns apart, with the statement:
25 FOR X = 1 TO 30 STEP 8
The other member of this command pair will automatically direct the
computer back to the program line on which the FOR-TO-STEP com
mand is found:
55 NEXTX
To direct the computer to put each subsequent arrow on the same hori
zontal line as the one previously drawn, you can add a statement that
sends the cursor back up to the line on which it began. Such a statement
would take the form:
44 THE EASY GUIDE TO YOUR COMMODORE 64
When you run this program, all commands located between the FOR-
TO-STEP and NEXT statements at lines 25 and 55 will be repeated until
the limit set after the word TO is reached, as in Figure 5.3.
In this case, lines 30, 40, and 50 (the drawing commands), and line 27
(the row-positioning command), are repeated. The computer will begin
drawing at each of four locations along a single row at the top of the
screen. The first arrow starts from the position set by a TAB of 1, and the
last at a TAB of 25. The value of X = 1 assigned in the FOR-TO-STEP
statement replaces the initial one from line 20, and each successive value
of X replaces the one before it.
Using the FOR-TO-STEP, NEXT command pair, now you have a
program that draws a row of arrows. With smother pair of FOR-TO-
STEP, NEXT commands ranging over values of Y, you can expand the
program to draw a similar group of arrows at more rows, and to fill the
screen with them. To repeat the first row of arrows, the second pair of
commands should bracket the statements that draw the row, including the
FOR-TO-STEP and NEXT pair for the values ofX. Thus, the arrows at
And to direct the computer to repeat the drawing at four descending rows,
you can add another pair of repeating commands, like this:
22 FORY = 1TO4
60 NEXTY
Figure 5.4: Filling the screen with arrows by altering the program ofFigure 5.3.
46 THE EASY GUIDE TO YOUR COMMODORE 64
Since the effect of line 20 (which assigned a value to X) has been can
celled by line 25, you can drop that statement as excess baggage:
20
(If you LIST the program now, you'll see the nested pairs of FOR-TO-
STEP and NEXT commands as they appear in Figure 5.5.) The first
NEXT statement, at line 55, directs the computer to take action on line
25, where the value of X is changed.
The computer can actually distinguish one NEXT statement's direc
tions from another's even if you don't label each NEXT statement with
the variable from its FOR-TO-STEP line; lines 55 and 60 could also have
been written as:
55 NEXT
60 NEXT
10 PRINT'LT
22 FOR V=l TO 4
24 PRlNTlKJttUi11
25 FOR X=iTO 30 STEP 8
27 print'ott^'
30 print tabcx)11
*0 print tab(x)11
50 print ta8(x)"
55 NEXT X
60 NEXT V
READV.
To position the blank printing back on the same row on which the arrow
was drawn, you can add a cursor-positioning statement before it, like this:
When you run the program now, the results will be as before, except that
immediately after each arrow appears it will disappear and the next arrow
will be drawn. The result will be an arrow racing from left to right across
one row after another.
Another clever use of the FOR/NEXT loop is worth noting, as it adds a
delay, which can be useful in any program in which you would like to
allow time for someone to view the program's effects. You might have
already noticed that the computer, incomprehensibly fast though it is,
does take a perceptible time to do things. The more complex or compli
cated the task, the longer it takes. Knowing this, you can use such a delay
to your advantage, by controlling it. You can add a loop containing no
real active command. And though there may be no command to repeat in
such a loop, the computer will go dutifully through it, taking time to trans
late and process the FOR-TO-STEP and NEXT commands.
To slow the animation program, then, you can add an empty loop
(known as a "timing loop") after the blank-space-positioning line, like
this:
Once again, the STEP part of the command is optional—if you leave it
off, as is done here, the computer automatically takes steps of 1.
In this program, the computer pauses briefly before each erasure as it
runs senselessly through its empty loop. This type of FOR-TO, NEXT
statement can be inserted anywhere in a program to slow or freeze
advancing action.
By judiciously removing statements, you can sometimes create effects
quite different from the original program. For instance, by removing the
Y FOR-TO, NEXT loop and the accompanying positioning command
from this program, you can create a display that shows an arrow descend
ing diagonally across the screen. Removing lines 22, 27,and 60, like this:
22
27
60
NEW
10 A$ = "WORDS WITHOUT THOUGHTS NEVER TO
HEAVEN GO."
You can then use the string variable, A$, in place of the string itself when
you give subsequent commands:
20 PRINT A$
Encountering line 20 when this brief program is run, the computer prints
Shakespeare's words on the screen, just as it would print a number
assigned to a numeric variable.
Adding two more strings to this program, you can prepare the com
puter to combine them, by assigning them to string variables:
12 B$ = "WILLIAM SHAKESPEARE"
14 C$ = "HAMLET, ACT III, SCENE 3"
To see these strings printed in combination, you can retype line 20 as:
20 PRINT A$ + B$ + C$
When the program is run, the computer will respond with the display
shown in Figure 6.1.
You can extract a numeric value from a string with a command that
counts the number of characters, including spaces, within it. Such a
command can be useful in any program that manipulates strings. The
command that directs the computer to count characters takes the form
LEN( ). In a program, you can assign a string length value to a numeric
variable with an instruction like this new version of line 20:
20 A = LEN(A$)
This line can be paraphrased as "Set the value of the numeric variable A
equal to the number of characters in the string A$." It directs the com
puter to substitute the number of characters in string A$ wherever it
encounters the symbol A in a program run. You can then use this value in
another statement, like this:
and also add a statement to display the quotation itself after spacing a
couple of rows:
40 PRINT: PRINT:PRINT A$
40 D$ = LEFT$(A$>5)
You could then display the new string with the command:
50 PRINT D$
When the program is run it produces the display shown in Figure 6.2.
You can create new strings of increasing length (up to the full length of
A$) out of the original string, by including the LEFTS command in a
FOR loop with a counter variable:
35 FORI = 1TOA
40 D$ = LEFT$(A$,I)
60 NEXT I
the first character of the string and is one character longer than its prede
cessor. When the last cycle is reached (when I = A), D$ will be assigned
the value of LEFT$(A$,A), which is the full length of A$. D$ will be the
same as A$, and the last line printed will be the entire quote, as shown in
Figure 6.3.
By adding another statement, you can cite the source of the quotation:
70 PRINT C$
You can make this program into a test of recognition by slowing printing
with an empty FOR-TO-NEXT loop, like this:
Now, with any quotation assigned to A$, you can run the program, stop
ping it from the keyboard with a RUN STOP keystroke command and
advancing it with the CONT command.
The second string-slicing command, RIGHT$, directs the computer to
count off and slice sections of strings from the right. Our current program
will print sections of A$ counted from the last letter of the string, if the
HANDLING WORDS AND INFORMATION 55
Ml 1HUU1 1 HUUtin lo
WORDS UITHOUT THOUGHTS NE
WORDS WITHOUT THOUGHTS NEU
WORDS WITHOUT THOUGHTS NEUE
WORDS WITHOUT THOUGHTS NEUER
WORDS WITHOUT THOUGHTS NEUER
WORDS WITHOUT THOUGHTS NEUER T
WORDS WITHOUT THOUGHTS NEUER TO
WORDS WITHOUT THOUGHTS NEUER TO
WORDS WITHOUT THOUGHTS NEUER TO H
WORDS WITHOUT THOUGHTS NEUER TO HE
WORDS WITHOUT THOUGHTS NEUER TO HEA
WORDS WITHOUT THOUGHTS NEUER TO HEAU
WORDS WITHOUT THOUGHTS NEUER TO HEAUE
WORDS WITHOUT THOUGHTS NEUER HEAUEN
WORDS WITHOUT THOUGHTS NEUER HEAUEN
WORDS WITHOUT THOUGHTS NEUER TO HEAUEN &
IREAOY
Figure 6.3: The effect ofa program that generates increasingly longer strings
from the LEFTS command.
40 D$ = RIGHT$(A$,I)
Driven by the same FOR loop, the computer will now print each line one
character longer than its predecessor, but it will start from the right quote
mark, as shown in Figure 6.4.
The third string-slicing command, MID$, directs the computer to snip
a section from the center of a string. To use this command, you include the
name of the string from which a section is to be excised, the leftmost char
acter's position (as counted from the left side of the string), and the num
ber of characters (to be counted and cut to the right). The command takes
the form MID$(A$,s,n), where s is the starting point, and n is the number
of characters in the section. If, for instance, you were to cut the eight-letter
word THOUGHTS, which begins sixteen spaces from the left quote,
from the quotation of line 10, the excising command would be
MID$(A$,16,8). If you insert this command in place of the other string-
cutters, again at line 40, the computer will produce another list of strings,
56 THE EASY GUIDE TO YOUR COMMODORE 64
Figure 6.4: The effect ofa program that generates increasingly longer strings
from the RIGHTS command.
this time growing outward from the middle of the original string, A$, in
both directions.
To use the MID$ command to produce strings growing symmetrically
from the middle of the original string, however, requires a twist of
arithmetic. You can control the length of the string with the variable I,
which is given value by the FOR-TO-NEXT statement, as it was in the
LEFT$ and RIGHTS commands. The computer finds the middle of the
string A$ by dividing its total length (as found in line 20: A = LEN(A$))
in half: by A/2. The computer finds half the number of characters, I, to be
printed at each pass, by 1/2. Half of the characters will be printed from the
right side, and half from the left of the center. Since the leftmost character
of a string is numbered 1, the left boundary can be specified as A/2 - 1/2
-I- 1. Thus, you can direct the computer with the MID$ command to pro
duce strings from the middle of the original quote, A$, with the following
command at line 40:
REAOV.
Figure 6.5: The effect ofa program that generates increasingly longer strings
from the MID$ command.
INSIDE INFORMATION
Like versatile actors within a play, variable symbols for numbers and
characters can play many changing roles within a program. The equal
sign directs these variables into their respective roles of the moment. But
the Commodore 64 recognizes another way of assigning values to varia
bles, which is somewhat the way a stage company's actors are given their
assignments—together. You can assign values to several variables at once
with a pair of commands, DATA and READ, in which you list the values
to be sent to the computer, and the variables to which they are assigned,
respectively. Any number of values can be sent to the computer with the
command that handles the information list, DATA. You can use it to send
the values of a string and two numbers, CHOKEBERRY, 15, and 30,
with the following command:
NEW
10 DATACHOKEBERRY,15,30
58 THE EASY GUIDE TO YOUR COMMODORE 64
Then use the command that makes the assignments from the list ofvalues,
READ, to assign those values to the variables N$, A, and B, respectively,
with this command:
20 READN$,A,B
You can now use these assignments, which are the equivalent of the com
mands:
N$ = CHOKEBERRY
A = 15
B = 30
Now add lines 10, 20, and 30 to a set of commands in which one value, B,
is used to control a PRINT display:
The above lines direct the computer to draw a horizontal bar of a length
corresponding to the value of B given in the DATA statement of line 10.
Running this program as it is, you'll see the heading:
10 DATA CHOKEBERRY,15>30,VETCH,25,14,BEARDWORT,17,27,
CHICKWEED,9,21, THYME.11,25
But if you run the program now, the computer will respond as before,
printing only the information about the chokeberry, since it acts on the
READ statement only once. To direct it through the READ statement
enough times to substitute a variable for each three-piece set of informa
tion in the DATA statement, you can add a FOR-TO, NEXT pair of
commands to repeat lines 20 through 70:
17 FORI = 1TO5
80 NEXT I
25 PRINT
When you run this program, the computer clears the screen and moves
the cursor to the top left corner. It then assigns to the variables N$, A, and
B the first three values in the DATA statement, CHOKEBERRY, 15,
and 30, respectively. It next prints the statement from line 30, and then
follows the directions for drawing the horizontal bar. After running
through the empty FOR-TO, NEXT loop in line 75, the computer is next
sent back from line 80 to the READ statement at line 20, which assigns to
the variables N$, A and B the next three values in the DATA statement
(VETCH,25,33) and repeats the printing, plotting, and pausing. The
NEXT command in line 80 once again sends the computer back through
60 THE EASY GUIDE TO YOUR COMMODORE 64
READY
Figure 6.6: Bar graph produced by using READ and DATA statements in a
FOR loop.
the READ statement and another set of values. This process continues
until the final run through the FOR-TO, NEXT loop, when the last val
ues of the DATA statement are assigned and the display of Figure 6.6 is
completed.
Besides making new assignments for variables, the DATA, READ and
FOR-TO, NEXT program structures can be used to put a different
name, tagged by a number, on each value in a DATA statement. With a
FOR-TO, NEXT loop you can direct the computer to number each vari
able at passes through the loop. To name the variables so that each now
matches a single value, you can change line 20 to read:
20 READN$(I),A(I),B(I)
which will number the variables. On the first pass through the loop, when
1=1, the three variables N$(l), A(l), and B(l) will be assigned to
CHOKEBERRY, 15, and 30. On the second pass, when I = 2, the three
variables N$(2), A(2), and B(2) will be named to represent VETCH, 25,
and 33. The computer repeats this process until the last pass, when N$(5),
A(5), and B(5) are named and assigned.
You'll now have seven sets of second-named, or subscripted, variables.
HANDLING WORDS AND INFORMATION 61
You can direct the computer to bring them into a program statement by
number, with these lines:
The resulting program will produce the same results as its unsub-
scripted predecessor. But the advantage is that you can direct the com
puter to use these numerically-named variables in various ways,
according to numbers calculated or assigned in other program statements.
You can, for instance, direct the tabular printing of the plants' names and
performances over time. Now the subscripted variables, A(I) and B(I),
come in handy in a combination of PRINT and FOR-TO, NEXT state
ments, which will display all the information:
85 PRINT
90 FORI = 1TO5
100 PRINT N$(I)"GREW TO " B(l) "AFTER " A(l) "MONTHS "
110 NEXTI
The program will now direct first the printing and bar graphs as it did
before, then the printing of a list of the plants' performances, as shown in
Figure 6.7.
If you have a use for variables with more than a single numeric tag
each, you can add more tags. The variables N$(I,J), A(IJ), and B(IJ)
could be named by wrapping another FOR-TO, NEXT loop, one that
counts with the variable J, around the I loops. The numbers for J could
stand for the watering interval of each plant, for example, and could range
from 1 to 3. You can use several loops in this way to instruct the computer
to name a series of variables in the form N$(I,J,K,L,M), in which
I would be a code number for the plant, J might be its watering period,
K the amount, L the amount of fertilizer, and M the time allowed, for
example.
Each loop of FOR-TO, NEXT statements that you use to assign values
to subscripted variable names multiplies the number of variables. Each
DATA statement ought to contain as many values as requested by the
accompanying READ statement. When the computer encounters a
READ command asking for more information than is provided by a
DATA command, it will stop the program and print the message:
Figure 6.7: Another use ofthe DATA and READ valuesfrom the program that
produced Figure 6.6.
DIM N$(50)
HANDLING WORDSAND INFORMATION 63
For example, if you're considering fifty people who can each work one
of three different shifts, and you wish to reserve space for all possible
arrangements of names, times, and performances, the variables resulting
could take any of the forms between N$(l,l), A(l,l), B(l,l) and
N$(50,3), A(50,3), B(50,3). You can use the DIM command to save
space for all of these variables and values by sending the directions:
Whenever you switch on the computer, even if you don't touch the key
board, the Commodore 64 secretly busies itself, keeping time. An inter
nal, electronic dock begins running the instant power is switched on, and
runs as long as the power is uninterrupted.
The time, in the form of a six-digit number representing hours, min
utes and seconds, can be read by directing the computer to display its
value. That value is stored, and continually updated, under the name
TIME$. To check it at any moment you can give the command:
PRINT TIME$
The dollar sign in TIME$ indicates that the computer stores this value,
though numeric, as a string variable, like a word. You can turn the com
puter into a displaying clock with this short program:
NEW
10 PRINT"(pressCLR/HOME)n
20 PRINT TIMES
30 GOT010
which, when run, will show the seconds ticking by at the top of the screen.
64 THE EASY GUIDE TO YOUR COMMODORE 64
TIME$ = "120000"
OUTSIDE CONTACT
You can add a command to a program so that the computer will stop,
take the information you have for it, and then proceed with the run using
what you provided. That command is INPUT. So commanded, the com
puter can interact with a person at the keyboard or with devices to which it
is connected. The INPUT command directs the computer to "prime
itself" by setting aside one or more variables, and then "siphon" the val
ues typed at the keyboard. It then proceeds with the program, substituting
those values as though it were responding to an assignment command or a
pair of DATA, READ commands.
Using a simple form of the INPUT command, you can direct the com
puter to tell you that it's waiting for information, and then take what you
type in (with a press of the RETURN key) and include it in the program.
HANDLING WORDS AND INFORMATION 65
The command takes the form of a simple request for a variable's value.
You can use it to direct, for example, an instant doubling of whatever
number you send from the keyboard in this short program:
NEW
10 INPUT A
20 PRINT A*
10 INPUT A$
20 PRINT A$ + A$ + A$
10 INPUT A$, A
20 PRINT TAB(A)A$
the computer will then print the words FROM THE KEYBOARD
beginning at column 20.
You can use the INPUT command like the DATA and READ com
mands, typing values after the question mark (as after a DATA com
mand) and setting variables for assignment after the INPUT command
(as after a READ command). There is a difference, however: using an
INPUT statement, you can send new values during each program run
without changing a line of the program.
66 THE EASY GUIDE TO YOUR COMMODORE 64
When you run a program like the above, the computer is directed to take
a value (or in this case a string of characters) from the keyboard, and then
compare that value with another and act according to the comparison.
HANDLING WORDS AND INFORMATION 67
The INPUT commcind takes the string sent from the keyboard and
assigns it to A$. The IF-THEN command compares it against the origi
nal string. If there is no match, the next command, line 30, directs a
rebuff, "I DON'T TALK WITH STRANGERS", and sends the com
puter to act on the INPUT statement at line 10, which again asks,
"WHAT'S THE CODE PLEASE ?"
When the reply sent from the keyboard (and assigned to A$) makes the
IF condition true, the computer is sent by the THEN commcind to line
40, to PRINT the computer's inner secrets, after which the program
ends. When reply after reply from the keyboard doesn't match the condi
tion of the IF-THEN statement, however, the computer is trapped in the
loop of lines 10 through 30 through 10, printing the rebuff and requesting
the password over and over. The Commodore 64 is particularly stubborn
with INPUT statements. Most other programs can be stopped with the
RUN STOP command. Not so with programs waiting for a reply to an
INPUT command. To stop such a program, you must use the firmer
RUN STOP-RESTORE command, which directs the computer to stop
the program run and retain the program in its memory.
You determine the computer's actions, of course, when you write a pro
gram, but you can also make the action depend on the result of a numeric
calculation or on some other value given to the processor through some
external device. There is another command, born of the same need for
information as the INPUT command, that directs the computer in a similar,
but terser, manner: GET. With it, you can command the Commodore 64
to act on a key pressed at the keyboard, without waiting for the RETURN
key. It directs immediate substitutions and can order values for several
variables at once, but it prints no message in quotes, or question mark.
The main limitation of this command is that it takes only one character—
number or string—for each variable.
You can use the command in a program of its own, like this:
NEW
5 PRINT "(press SHIFT and CLR/HOME)"
10 GETA$
20 PRINT "(press CLR/HOME)" A$
30 GOT010
68 THE EASY GUIDE TO YOUR COMMODORE 64
When this short program is run through the Commodore 64, the
computer assigns the first key pressed to the variable A$, then moves the
cursor to the upper-left corner of the screen, and prints the character of
whichever key is pressed. If more than a single key is pressed, as in this
reply:
TO ERR IS HUMAN
the computer will take the first letter typed, T, and display it. Then,
because of the GOTO command at line 30, the computer will return to
lines 10 and 20 to read and display each letter as it's typed, one at a time.
The letters will replace each other in the corner immediately, since the
computer reads much faster than any person can type. In fact, to use
GET, you will always have to put it into a loop to make it repeat contin
ually. Unlike INPUT, a single GET command will not wait long enough
for you to input even a single character. To see this for yourself, delete line
30 and try the program again.
The features that make the GET command either useful or bother
some, depending on your point of view, are two: it directs the computer to
ignore anything more than the variable it's set to assign, and it acts with
out waiting for the RETURN key to be pressed.
NEW
10 GETA$:IFA$=""THEN10
20 PRINT A$TAB(5)ASC(A$)
30 GOT010
When you run this program, the character and the code number of each
key you press will be printed on the screen as you press it.
HANDLING WORDS AND INFORMATION 69
Line 10 directs the computer to search for the next key pressed. If none
is pressed during the instant it takes to perform a single GET operation,
then A$ has no value (A$ = "") and the computer is sent back to search
again. When a key is pressed, A$ is given a value and the computer moves
to line 20, where it's directed to print the key pressed and then, spacing
over, the numeric code. Line 30 restarts the process.
As this program runs, you'll find that each keystroke combination
(except the RUN STOP and RESTORE keys, which actually stop the
operation of the program) produces a code number. In fact, four keys will
show a code number each, but no value. These four, marked f1 f2, f3 f4,
f5 f6, and f7 f8, are known asjunction keys, and they were put on the key
board not to affect the screen display, but to serve as programmable
switches you can use to signal the computer while keeping the other, dis
play keys free for use.
If you press fl, you'll see that it corresponds to the code 133, and that
SHIFT-fl (which you can think of as f2) corresponds to 137. Knowing
this, you can use any ofthe function keys as a trigger for some action while
a program is running. By adding the following statements to the program
above, you can direct the computer to break out of the loop and take
smother action:
25 IFASC(A$) = 133THEN40
40 PRINT "(press SHIFT and CLR/HOME)" TIME$
You can use other keystrokes in similar ways. For example, to make
further use of the function key you can add:
27 IFASC(A$) = 137THEN50
50 PRINT "NOW YOU'VE DONE IT!"
60 GOTO 50
NEW
10 X = PEEK(54297):Y = PEEK(54298)
5 PRINT"(pressCLR/HOME)"
20 PRINT TAB(10)XTAB(20)Y
30 GOT010
When you run this program, the paddles feed in values through the
PEEK commands. Program line 20 directs the computer to print the
value of each paddle's position near the top of the screen. Incidentally, val
ues below 100 will appear in three digits, as 990, 980, etc., on the screen.
You can add a statement to erase the previous value and so keep only
the current value displayed, like this:
The assignment commands of line 20 scale the values taken from the
paddles to the dimensions of the screen. The loop formed by the com
mands of line 30 carries out a spacing function according to the value of
one of the paddles.
USING PROGRAMS WITH CASSETTE TAPES
People travel across country by both airplane and bus. One way is
faster and costs more; the other is less convenient and slower, but is rela
tively inexpensive. The same sort of choice is available when it comes to
storing and using programs and information files, both pre-packaged and
your own. A disk system will move programs and information more
quickly; a cassette tape system more slowly but at a much lower cost.
You can put your programs on common cassette recording tape
through a Commodore cassette recorder. This is a specially-wired tape
recorder (called a DATASSETTE ™) that plugs into the back of the Com
modore 64, as shown in Figure 7.1. By operating it according to the
instructions that appear on the screen whenever you use the recorder, you
can store anything that resides in the computer's program memory on
inexpensive cassette tapes.
The Commodore 64 recognizes simple commands for transferring pro
grams to and from cassette tape. What's required on your part is a bit of
button-punching on the recorder as on-screen directions guide you.
You can use the cassette tape in the same way as if you were recording
speech or music. To make full use of a cassette tape, make sure
the recording-protect notches at the edge of the cassette case are covered
or blocked, and that the tape has been rewound to the beginning, but
shows the dull brown tape through the window at the business-end of the
cassette.
Slide the cassette into the plastic tracks of the recorder's lid. Then close
74 THE EASY GUIDE TO YOUR COMMODORE 64
Figure 7.1: The connection between the computer and the cassette recorder.
the lid and you're ready to use the tape for program storage. The power to
drive the recorder comes from the computer itself. You can use the
counter to keep track of where your programs are by making a note of
program and number, just as you might on any tape recorder. This is a
good idea, since the tape system of storage won't automatically make a
directory of programs contained on the tape. The programs will instead
be treated much like a series of short songs or speeches recorded one after
another through a microphone.
The procedure for recording a program is fairly direct. If you have a
program in the computer's memory that you want to save under the
name ENCHANTMENT, for example, you can give the command:
SAVE"ENCHANTMENT"
Ifyou then press those two keys on the recorder together, the computer will
quickly respond with:
OK
SAVING ENCHANTMENT
The screen will be blanked, the tape recorder's motor will turn, and a red
light on the recorder will light. When the computer has finished recording
the program, it will return the display screen and present a READY
prompt and a flashing cursor. At this point, unless you want to save
another program on tape, you can press the STOP key on the recorder.
The program now resides in two places—on the tape and in the com
puter's memory.
ECONOMY-CLASS STORAGE—TAPES 75
You can bring a program from tape into computer memory just as sim
ply. The computer will search an entire tape for that program by name
and will load it when it's found. You can load the program ENCHANT
MENT back into the computer from any point on the tape before the
location of the program by giving the command:
LOAD"ENCHANTMENT"
OK
SEARCHING FOR ENCHANTMENT
but it will continue its search for ENCHANTMENT. If, for instance,
programs named AGGRAVATION, CHALLENGE, and OBSTA
CLES were recorded on the tape before it, the computer would blank the
screen as it searched, and list each program it encountered, until it found
the one you asked for. You would then press the C = key to actually load
the program into memory. Incidentally, each time the computer stops the
tape at a program in its search, you can load that program instead of the
one you sent it off searching for, by pressing the C = key.
If you let the computer finish the search, it will eventually load
ENCHANTMENT when it finds it. You would see this display finally:
FOUND AGGRAVATION
FOUND CHALLENGE
FOUND OBSTACLES
FOUND ENCHANTMENT
LOADING
READY
That is the computer's way of telling you it encountered, but passed over,
three other programs before locating the program ENCHANTMENT
and loading it into its memory.
Incidentally, it's best not to "overlap" program names. The computer
76 THE EASY GUIDE TO YOUR COMMODORE 64
will stop at any program name that contains, within its first few charac
ters, the name of the program being searched for. This would happen if
you saved an additional program on the same tape, this one under the
name ENCHANT, and then, after rewinding the tape to its beginning,
commanded the computer to LOAD "ENCHANT". Finding the letters
ENCHANT in the program name ENCHANTMENT, the computer
would stop and load ENCHANTMENT.
You can use the searching ability of the Commodore 64 to generate a
directory of the programs on a tape. By directing the computer to search
for a program you know it won't find, you will put it through its searching
and printing routine. If you send a command like
LOAD "ZZZ"
for a tape that has no such program, the computer will pause and print on
screen the name of each program it encounters before reaching the end of
the tape.
You can use this program in the following way. First, send the command:
RUN
ECONOMY-CLASS STORAGE— TAPES 77
NAME OF FILE?
If you then type in a name of your choosing, say WORMS, and press
RETURN, the computer will prompt you to ready the tape recorder:
Then, after you've done so, the screen will go blank, the recorder's red
light will glow, and the tape will turn. At this point the computer is direct
ing the name WORMS onto the tape as a file name. When this has hap
pened, the screen will reappear with printing and the new lines:
OK
The question mark is a prompt from the computer for the first item in the
file. If you type an item, like NIGHT CRAWLER, and then press the
RETURN key, the computer will prompt you for the next item with
another question mark:
If you type another item, say SILKWORM, and send it with the
RETURN key, the question-mark prompt will again appear. This
program can accept 100 items. (Line 110 created an "array" of 100 vari
ables for answers.) You can send item after item until you've put in the
number you want. At that point, you just reply to the question-mark
prompt with CLOSE FILE, and the computer will encode the entire list
of items on the tape.
The screen will go blank, the red light on the recorder will come on, and
the tape will turn. When the last item has been loaded, the tape will stop and
the READY prompt will appear on the screen. Your list will be on the tape.
You can put any number of files on the tape this way, one after the
other. Of course, encoded as they are, these files are of little use to you.
But with another program to direct the tape system, you can pull the items
out of a file and display them on the screen. The following program will
retrieve a file by name from tape and list each item on the screen.
250 PRINTITAB(5)A$(I)
260 1 = 1 + 1
270 IF ST> 64 THEN 240
280 CLOSE 1
NAME OF FILE?
Once you type the name of the file you're looking for, and press
RETURN, the computer will give you directions to operate the recorder:
After you do that, the screen will go blank and the tape will turn until the
file name and the items have been found. If you run the program when
the tape is wound to its beginning and the file is at the end of the tape, the
computer will search over each name until it finds the name you
requested. (If the name is contained within a longer file name, the com
puter will retrieve that file instead of the one you requested. For example,
the file HAPPENCHANCE, if encountered first, would be retrieved if
the file HAPPEN was requested.) Finally the tape will stop, and the com
puter will display the number assigned to each item in the list, and the
item itself, as it prints a list on the screen.
You can use these programs separately to store and gather files with
tape, and you can incorporate them into your own programs, as long as
your programs assign variables to items in the form A$(I).
Soon after its appearance, a gallant and effective Pony Express yielded
its service of the growing frontier to the faster and technologically superior
telegraph. Likewise, in many computer applications, the tape storage sys
tem we discussed in Chapter 7 is giving way to a new system. A technolog
ical improvement over the tape system of storage, a disk system offers a
speed of access that can be important when frequent exchanges of pro
grams and information must be made between computer and storage
medium.
A MARRIAGE OF MACHINES
A disk system operates under the electrical and logical control of the
Commodore 64 as an extension of the computer's own memory, a ware
house of programs and information. Each element of this system provides
more than twice the memory capacity of the Commodore 64, which is 64
kilobytes. With a disk storage system, you can put aside and use again
your own programs and other programs already written for the Commo
dore 64.
More complex than the cassette tape system, the disk storage system
works through a box about the size of the computer that contains its own
circuitry and small memory. Working under the computer's direction,
this disk drive magnetically encodes programs and information onto the
thin plastic circles known as disks, each contained loosely within a square
plastic envelope.
These disks are coated with a magnetically-sensitive material and
80 THE EASY GUIDE TO YOUR COMMODORE 64
DISKDRIVE
POWER CORD
CABLEPLL
First, a word about how to treat disks. Like paper, disks come in two
forms, blank and written on. You can copy recorded disks, write on blank
parts of recorded disks, and write over, or erase, parts of recorded disks.
You can also protect the contents of a disk, as you might put written
paper in a binder so that it won't be unexpectedly written on. The square
envelope around the disk protects the magnetic writing from erasure or
alteration in the drive if that envelope is completely sealed around all
edges and lacks a cut-out notch, called a write-enable notch. If such a notch
is exposed on an envelope, as illustrated in Figure 8.3, the disk drive will
be switched out of a reading-only mode of operation. You can then direct
the computer to record your typing on the disk.
You can "freeze" all the programs and information on a disk bearing
such a notch, by covering that notch with a sturdy piece of foil tape, which
is usually provided with blank disks. When the mechanisms of the disk
drive sense that this space is covered, the drive will be switched to the read
only mode.
As their nearly total enclosure suggests, disks are fairly fragile. Even
when a disk has endured some mishap with no visible damage, its
magnetically-sensitive surface may have been damaged. Magnetic fields,
extremes of temperature (those outside our comfort range), bending or
folding, and contamination by dirt, dust, skin oils, and the like are all haz
ards. Information and programs recorded on the disk surface in invisible
magnetic patterns can be cripplingly altered by an imperceptible scratch
or contamination. Hidden in its envelope, nearly all of the disk is pro
tected from contamination at any one time; you can handle the envelope
without concern. But the drive head reads where the disk surface itself
peeks through an oval slot, and through that slot can pass dust and con
taminants as well.
You can easily avoid common disk problems by taking two simple pre
cautions: Always remove disks from the drive before turning off the disk
drive or the computer; and always mark labels for disks before you stick
them onto the disk envelopes.
The disk and its plastic envelope are stored in a paper jacket, which
doesn't quite cover the envelope. This paper sleeve shields the otherwise
exposed disk surface at the oval cutout when it's out of the drive.
The magnetic head in the disk drive, recording and reading along a
short track, makes contact with the disk through the envelope slot while
the disk itself is spinning inside the envelope. Because the disk envelope is
square and two-sided, there are eight possible ways to put it into the drive.
Only one way will position the disk correctly.
The drive head is in the rear half of the drive, so the slotted end of the
disk envelope goes in first. One side of the disk envelope is unlabeled and
relatively unfinished-looking; the other side is labeled and smooth. If this
labeled side faces the little door at the front of the drive, the magnetic side
of the disk will face the drive head.
Thus, you insert the disk into an empty, opened drive box like this: slot
in first, labeled side facing the door. Then all you do is slide the disk gently
all the way in, until it catches in the recess. You can then push down on the
drive door until it catches to close over the center of the opening.
You remove the disk by pushing the door until it's pulled up by spring
action. The disk itself will slide halfway out of the drive box under the
same spring action.
Inserting a disk in a drive box and closing the door physically completes
the computer-disk system. But unless it's given the special commands that
direct the disk drive, the system is about as useful as an illiterate librarian.
Even when it is physically and electrically connected with a disk system,
the computer must still be told what to do with this apparatus.
The computer can respond to your disk commands, as well as to the
BASIC programming commands built into it. Disk directions automati
cally compile a listing of programs and file information as they are put
onto the disk. The TEST/DEMO disk, supplied with Commodore disk
84 THE EASY GUIDE TO YOUR COMMODORE 64
drives, holds several programs already, though it's not nearly filled. We'll
use the TEST/DEMO disk to get started. You can command a listing of
the programs on that (or any other) disk, and the computer will respond
with a display showing the name given to the disk and a listing of its pro
grams and files. Each is preceded by a number indicating the amount of
space it occupies on the disk.
You can direct the computer to display this list with the command:
LOAD "$", 8
The computer will respond with a message on the screen, and the disk
drive's red light will glow as a signal that the drive is in operation. You can
also take this glowing red light as a reminder not to remove the disk at this
time.
If the computer-and-disk-drive system doesn't recognize the disk com
mand sent to it, the red light on the drive will flash, as a signal that the last
disk command hasn't been carried out. The system will subsequently
accept any command it recognizes and then extinguish the light after per
forming the command.
If you've sent the command above, the message you'll see is:
SEARCHING FOR $
The dollar sign, $, is a shorthand name for the directory. Once the direc
tory is found, the computer will reply with the message:
LOADING
That means the computer is copying information from the disk (in this
case, the directory listing) into its own memory. When the computer has
finished copying, it will display the familiar signal:
READY
You can then direct the computer to print on-screen the directory just
loaded, with the command:
LIST
The screen will fill with a list like that shown in Figure 8.4.
A directory listing can have more items than will fit on a single screen.
HIGH SPEED STORAGE—DISKS 85
LIST
The Commodore 64 will display the entire listing by "scrolling" the first
items of the list up and off the screen to make room for later items printed
after the first screenful. Once it has displayed the final item of the direc
tory, the computer signals its readiness for your next command with the
READY prompt. You can stop the listing at any point with the RUN
STOP keypress command.
You control the disk drive through the computer with commands given
in the same form as the LOAD command above. The computer will rec
ognize these commands if they each follow the pattern: first the com
mand, then the name of the program in quotes, then a comma, then the
number 8, which opens a path to the drive connected to the computer.
These commands increase in complexity as you order more complex
tasks. However, one of the programs on the TEST/DEMO disk can sim
plify the commands you give to operate the disk drive. After you've
directed the computer to load and then run this program, the Commo
dore 64 will respond to either those rather cumbersome disk commands
it's built to act on, or another, shorthand set of symbolic commands. The
directions for these shorthand commands are stored in an area of the com
puter memory apart from the usual program memory, so you can use
them over and over without interfering with the programs or files you put
in the computer.
86 THE EASY GUIDE TO YOUR COMMODORE 64
You'll find this program listed in the diectory under the name C-64
WEDGE. The C-64 WEDGE program will control the disk drive so that
a set of directions corresponding to these shorthand commands is stored in
the "reserved" part of the computer's memory. These directions will
respond to your shorthand disk commands, and will remain in memory
even after the command NEW erases whatever program you've put into
memory.
With the TEST/DEMO disk in the drive, you can put the C-64
WEDGE program into effect by directing the computer to load it:
Once the computer has completed its search and loaded the program into
memory, you'll see the familiar READY prompt. You can then direct the
computer to run the program, and put the directions for its shorthand
commands aside in memory, by commanding:
RUN
As the program runs you'll see a three-line display of title and credits, after
which the familiar READY prompt appears as a signal that disk direc
tions for the shorthand commands are available in memory.
The computer will now respond to either set of commands. The longer,
more cumbersome commands are always available from the computer.
The shorthand C-64 WEDGE commands are there whenever you load
and run that program from the TEST/DEMO disk.
Since you may be using either type of command, both are shown in the
explanations that follow, although you may prefer to use the WEDGE
commands for convenience. The longer commands are shown here in
italics as alternatives.
You can now load and list the directory with a single command:
@$
LOAD "$",8
LIST
You can run the first program of the TEST/DEMO disk, a display pro
gram called HOW TO USE, with the WEDGE and program commands:
/HOW TO USE
RUN
LOAD. You could get the same results with the standard disk commands:
t HOW TO USE
The computer will then skip a line and signal you with the statement:
Then smother line will tell you the computer is bringing the program into
its memory:
LOADING
After this, the computer will immediately begin carrying out the com
mands of the program. In this case, it displays the screen titled DISK
ETTE INSTRUCTIONS.
The / , @ , and t commands are all you need to use prerecorded disks
operating under the directions of the TEST/DEMO WEDGE
commands.
You can record programs and information on any disk that has a recog
nized magnetic pattern etched onto it. A disk in the drive without this pat
tern is as useless to you using the computer as a bare desk to a writer with a
88 THE EASY GUIDE TO YOUR COMMODORE 64
pen. With them, the disk is like a blank sheet oflined paper, lying ready on
the desktop.
You can direct this magnetic etching of a disk with a single command
from the WEDGE program. In the process, you will erase any informa
tion already on the disk, and put on it a sectioned pattern, in which pro
grams and information can be stored. At the same time, the disk will be
given a name. The computer-disk system also looks for a two-character
code on each disk as it is first scanned. You can provide this code within
the same simple command.
To prepare a disk this way, giving it the name THE FIRST, and the
code Al, you can send the following command:
@N:THE FIRST.A1
or
OPEN 15,8,15
then
PRINT #15,"N0:THEFIRST,A1"
CLOSE 15,8,15
The computer will take control of the disk drive, and you'll hear the sound
of magnetic formatting (like fabric tearing) for several moments. When all
is quiet again and the red disk drive light goes off, the disk has been "ini
tialized," and is at your service. The newly-prepared disk can hold pro
gram and information files, just as the TEST/DEMO disk can. You can
selectively put useful programs from another Commodore 64 disk (like
the TEST/DEMO disk) onto any initialized diskette.
To record a program on a disk, you first type it into the computer. Any
program will do—the Hamlet quotation from Chapter 6, or one of your
own invention. You can type in a program you'd like to store on disk, just
as you would before running it, or you can load a program from another
disk or from a cassette tape. You can, of course, run the program to see
that the computer does what you want with it, and make any necessary
changes. When you've got the program as you like it in memory, every
thing is ready for the disk command that will preserve it outside the
computer.
All that remains is for you to name the program. The disk directions tell
the computer to handle names up to 16 characters long. If you want to call
your program COLD STORAGE, for example, you can direct the com
puter to store it under that name by the command:
HIGH SPEED STORAGE—DISKS 89
♦- COLD STORAGE
or
The disk drive will make its fabric-tearing sounds. When the commo
tion stops and the red light goes off, the program exists in two places.
Magnetically encoded, it's on the disk. And as before, it's in the com
puter's program memory. It hasn't moved, it's been copied.
If you type LIST, the program statements will appear on the screen.
You can type NEW to erase the program memory and still run the pro
gram, this time from the disk, with the command:
t COLD STORAGE
or
then
RUN
It will then be freshly loaded into the program memory of the com
puter. You can remove the disk, turn off the computer, send the disk
round-trip to Chicago if you like, and—on its return—put that disk in the
drive box, turn on the computer and type:
t COLD STORAGE
or
then
RUN
@$
or
LOAD "$",8
90 THE EASY GUIDE TO YOUR COMMODORE 64
then
LIST
You can change the name of a program with another disk command.
Directed by this command, the computer looks for the program under the
first name listed, then substitutes for it the second name.
To change the name of the program COLD STORAGE to STOW-
AV\WY, for example, you can send the command:
or
OPEN 15,8,15
then
@S:STOWAWAY
or
OPEN 15,8,15
then
The computer, under disk directions, will operate the drive to find the
program, erase it entirely, and remove its name from the directory.
You can move programs individually from one disk to another using
the commands that load (/), save (*-), and delete (@S:). First you load
the program from one disk into the computer's memory, then save it by
sending it from the program memory to another disk, and finally delete it
from the disk on which it originated.
To move a program like STOWWVi\Y from one disk to another using a
HIGH SPEED STORAGE—DISKS 91
one-drive system, you can first bring it into the computer's program
memory:
/STOWAWAY
then remove its disk from the drive, insert a second disk—one equipped
with its own magnetic etching—and send the command:
^STOWAWAY
then reinsert the original disk and have it erased with the command:
@S:STOWAWAY
Through the save, load, delete, and rename disk commands, you can
juggle and reshape programs on disks quite freely. Any mag
netically-prepared disk can hold program and information files, just as the
TEST/DEMO disk can. You can selectively put useful programs from
another Commodore 64 disk (like the TEST/DEMO disk) on any initial
ized diskette.
You could copy a program like SEQUENTIAL FILE, for instance,
with the command:
/SEQUENTIAL FILE
or
- SEQUENTIAL FILE
and the computer would duplicate that program on the disk you had ini
tialized.
Using this procedure, you can duplicate selectively and move programs
around your diskettes as you find useful. In particular, you can copy the
information- and file-handling program of SEQUENTIAL FILE onto a
disk that will be used for a special purpose, like holding information files.
Just as several disks can serve you as a reservoir of programs and infor
mation, several disk drives expand the number of pathways for items
moving in and out of the computer memory. With two drives, for
92 THE EASY GUIDE TO YOUR COMMODORE 64
instance, you can load programs from, or store them on, the disks in
either drive.
To connect a second drive to the system, simply take the six-pronged
cord that comes with the drive, and plug one end into the socket over the
fuse holder on the second drive; then plug the other end into the socket
remaining on the drive you've already connected directly to the computer.
The electrical connection will then run from the computer directly to one
drive, and from that drive to the second drive. The power cord from the
second drive connects to an outlet in the same way as for the first drive.
The computer can now communicate with either drive. If you turn on
the computer and either one of the drives, leaving the other turned off, the
computer will carry out the commands described above to operate the
drive which is powered.
If you switched on both drives and gave a command, like that to load
the directory, the computer would be stymied. Without instructions for
which of the two disk drives to search for the directory, it may arbitrarily
choose one or the other, or it may lock up, giving you the message that it's
searching for the directory, $, when in fact it's not controlling either drive.
In that case, you'd have lost control of the computer by sending it off to
make a decision for which it hadn't been programmed.
The key to avoiding this problem is to realize that built-in directions tell
the computer that each drive is identified by the same number. This
device number is automatically set at 8, and you can see it in a command
like LOAD "$",8. Any drive connected to the computer, directly or
through another drive, is identified by this number automatically. But the
computer can be directed to label any drive with a number of your own
choosing through commands that re-label each drive. These commands
are number-laden and abstruse, and involve setting up command chan
nels and files, but you don't need to understand the details of how they
work to use them.
To change the device number by which a connected disk drive will be
called from the computer, here's what you can do. First, switch off each
drive except the one you wish to renumber.
Send the following statement (with the RETURN key, of course):
OPEN 15,8,15
Then send this statement, if you want the disk drive to be numbered 9, for
example:
If you wish to renumber another drive, switch it on and repeat the com
mands above, substituting smother number for 9 in the last two sets of
parentheses. If, for instance, you wish to label another drive as 22, you
can turn it on and follow this sequence:
OPEN 15,8,15
PRINT#15,"M-W"CHR$(119)CHR$(0)CHR$(2)CHR$(22 + 32)
CHR$(22 + 64)
The device number you assign to each disk drive will then be recognized
by the computer until it is again changed, or until power is switched off.
Since the Wedge commands assume a drive numbered 8, they will be
effective only for the drive automatically given that number, or for one
assigned that number. If numbers 8 and 9 are assigned to two drives, for
example, you can use the long form of the disk commands to control each
of the drives, and the Wedge commands to control drive 8.
You could copy a program from one disk in drive 8, for instance, to
another in drive 9 with a sequence of commands like this. First type
Programs are useful enough, but let's face it, what you really want the
computer to do is handle information—data, facts, figures, names,
addresses, times, places, quantities, descriptions.
Any information that can be typed can be put on a disk. You can store it
under a name on the directory listing and bring it out through the com
puter to the screen. You can direct the computer to work on it, manipulat
ing it according to program statements.
Like files in a cabinet, information on a disk is at your disposal to pull
out for reading, or to use in programs in place of DATA and INPUT
statements. However, the computer needs more detailed directions for
putting information that's not organized as a program on disk, and for
pulling that information back into its memory.
One of the programs on the TEST/DEMO disk handles information
files on disk. It has two functional parts. One part directs the computer to
put keyboard information on an initialized disk; the other part directs it to
read information from a disk.
More elaborate programs, which direct the computer in complex
94 THE EASY GUIDE TO YOUR COMMODORE 64
t SEQUENTIAL FILE
or
the computer clears the screen and prints descriptions and directions for
the program's use. At this point you can ignore the first couple oflines and
follow the directions after them, ENTER A WORD, COMMA, NUM
BER. If you want to enter, as first on the list, the name AMBROSE and
the number 1, you can then type after the prompt A$,B:
AMBROSES
and then send it to the computer by pressing the RETURN key. You can
then send another name and number similarly:
BRET.357
MARK.2
If you'd like the list to contain just these three items, you can signal the
HIGH SPEED STORAGE—DISKS 95
END
sent by a press of the RETURN key. A pair of question marks will appear
as a prompt. If you press the RETURN key again, the computer, acting
under the SEQUENTIAL FILE program's commands, will take it as a
signal that you've finished the file. Following the directions from the sec
ond part of this program, the Commodore 64 will print the line, READ
SEQTEST FILE. At this point, SEQTEST FILE is the name the pro
gram has given to the list of items you've typed. Finally, the computer will
print on-screen the list of items and numbers.
You'll find, after running the SEQUENTIAL FILE program, that the
computer has added the file to the end of the directory listing under the
name SEQTEST FILE. The file listing appears as:
1 "SEQTESTFILE" SEQ
The file information has been copied item-by-item into the computer
from the disk, as though in response to keyboard typing after an INPUT
statement, and is still present on the disk.
A program can have a rather uncertain existence. It can be run repeat
edly, performing the same function each time, or altered to fit different
occasions. The clever and creative way to use your computer is not to
write a new program for each new project, but rather to adapt and build
from programs and parts of programs that have proven their usefulness.
How much easier it is to start with groups of commands whose effects are
known than to begin each project facing a blank screen!
You can direct the Commodore 64 to piece together programs (or parts
of programs) you find particularly useful or interesting. Program parts
can be added within the computer's memory. You can do this even if you
understand little more than the connections among programs or program
parts. To create your own style of programming, you can call on ready-
made, often-used statement sequences. Using such an approach, you can
take sequences you're confident of, and splice them into a program you
design to take advantage of each part.
For example, to display a name at the bottom center of the screen, you
can direct the computer with this two-line program:
NEW
10 DATA BONAPARTE THE AMBITIOUS
1000 READ A$: PRINT "(press SHIFT and CLR/HOME)"A$
BUILDING AND REBUILDING PROGRAMS 97
You can use the screen-clearing, positioning and printing features of line
1000 again, by directing the computer to carry out the commands of that
line for other names (i.e., other values of A$). The pair of commands,
GOSUB and RETURN, that direct the computer to line 1000 and back
can be added to the program, with these statements:
20 GOSUB 1000
1010 RETURN
A third statement, END, will stop the computer from acting on line 1000
again. It can be placed anywhere before line 1000, like this:
100 END
At each place where it appears, the GOSUB 1000 command directs the
computer to act on line 1000 and any lines that follow it. The RETURN
statement directs it back to the main sequence of DATA and GOSUB
98 THE EASY GUIDE TO YOUR COMMODORE 64
statements. If you add an empty loop to line 1000, to delay the change of
displays:
and then run this program, you'll see the name given in each DATA state
ment printed at the top left corner of the screen, one after another.
You can direct the computer to act on any number of command
groups—known as subroutines—in any order. You can add a graphing
function to this program with another group of commands:
This group of commands will direct the computer to READ values from a
DATA statement, and then, by a little arithmetic manipulation of X and
Y values, to plot those values.
You can now make further use of the GOSUB command. If you
expand the DATA statements in lines 10, 20 and 30, you can put the com
mands starting at line 2000 to work on those values:
When run, this program will direct the computer first to print the name,
then to plot the corresponding set of numbers.
BUILDING AND REBUILDING PROGRAMS 99
You can direct the computer to draw the grid, just after it reads the values
100 THE EASY GUIDE TO YOUR COMMODORE 64
When you run this program, first the screen will clear and the name
"BONAPARTE THE AMBITIOUS" will appear at the top. Then the
computer will draw grid lines. Next it will plot the values from the first
DATA statement, as shown in Figure 9.1. The screen will clear again, and
the process will begin for the values from the next DATA statement. It will
continue until each set of values from the three DATA statements has been
handled, and the computer stops at the last display.
The computer acts on GOSUB and RETURN commands in pairs. If
it encounters a RETURN statement without having first encountered a
GOSUB statement, it will follow its built-in directions to signal you with
an error message:
This problem would result from the computer's usual progress through
higher line numbers to a high-numbered subroutine. You can avoid it by
putting an END or STOP statement after the main sequence but before
the first of the subroutine statements. In this case, we've put an END at
line 100.
Like the GOTO command, GOSUB directs the computer to the line
number listed in the command. You can use a variation of the GOSUB-
RETURN command pair to direct the computer, in a conditional way, in
and out of the main sequence of statements. Just as an ON-GOTO com
mand directs the computer to depart from the normal sequence on certain
numeric conditions, so an ON-GOSUB command directs the computer
to particular subroutines. You can use this command to direct the com
puter to one of several command groups, from which it will later be sent
back to resume the main sequence again.
The ON-GOSUB statement can be used in the plotting program, for
instance, to selectively draw parts of the grid pattern against which the
information will be graphed. One group of commands, like those begin
ning at line 3000, can direct the computer to draw vertical lines. Another
group, like those beginning at line 4000, can direct the drawing of hori
zontal lines.
By changing program line 1007 to a statement whose directions to the
computer are conditional you can, according to the choice made during
each program run, either use or pass over the subroutines beginning at
3000 or 4000, or a new one at line 5000. This subroutine produces the full
grid, by sending the computer to draw vertical, then horizontal, lines.
The full grid could be drawn through these statements:
This directs the computer like the ON-GOTO command, comparing the
value of the variable with the ON-GOSUB command's built-in ranges of
value: one or greater but less than two, and so on. You can instruct the
computer to request the value of D, by adding a prompt and an INPUT
statement, like these:
Running the program, you'll see the INPUT request at the top of the
screen. If a choice in the range 1, 2, or 3 is sent, the computer will first
draw the graph lines according to choice, then plot the values from the
DATA statement. If a choice outside the range is typed, the computer will
102 THE EASY GUIDE TO YOUR COMMODORE 64
simply move to the next program line and no graph lines will be printed;
the DATA values will be plotted on an unlined screen.
You can build a library of useful subroutines, giving each of them high
line numbers to avoid interference with a lower-numbered main
sequence. In fact, your main program can sometimes be nothing more
than a series of requests for subroutines, along with, perhaps, some
INPUT or DATA sequences. In assembling such a program, the only ele
ments that must match are variables that are common to the subroutines
and are acted on in each subroutine. You can be sure of this match by
checking to see that a given variable name stands for the same quantity
wherever it is used. The main sequence of such a program could be as
simple as:
10 GOSUB1000
20 GOSUB2000
30 GOSUB3000
100 END
The final statement, END, stops the computer from running on to the
subroutines that follow. Following this structure, you can save yourself
work by taking advantage of subroutines already written by you or some
one else, to build programs giving you greater control over your com
puter. You can concentrate on arranging existing elements, rather than on
writing detailed program commands.
Not all useful programs are created in the plodding process of adding
one command after another. You can borrow from existing programs,
alter them to suit your needs, and so make use of the original program
ming effort that went into those programs. By borrowing a disk
program—for example, the SEQUENTIAL FILE program of the
TEXT/DEMO disk—you can use its disk-handling abilities in programs
of your own.
The SEQUENTIAL FILE program, which actually performs two dis
tinct file-handling tasks, making information files on disks and then read
ing them, can be tailored into two separate parts for use in your programs.
Many of its statements do nothing more than describe the program func
tions, and can be removed. By elimination of those commands that you
don't need, you can trim a bit of excess from the original.
BUILDING AND REBUILDING PROGRAMS 103
You need not understand all the details of the program to make use
of it and its parts. For instance, if you look at the entire listing of
SEQUENTIAL FILE, a screenful at a time, you can see that the pro
gram is divided into four parts, each separated by the notes of a group of
REM statements.
The first group of REM statements, numbered 1 through 6, describes
the function of the program that follows, AN EXAMPLE READ &
WRITE A SEQUENTIAL DATA FILE. The statements that follow
that group of remarks, lines 10 through 95 (shown in Figure 9.2), hold
commands common to both functions of the program.
The next group of remarks, numbered 100 through 105, describes
rather tersely the part of the program that directs the formation of a disk
file, WRITE SEQTEST FILE. Those statements occupy lines 110
through 160. (See Figure 9.3.)
The remarks from lines 200 through 205 describe the statements, lines
206 through 420, that direct the reading into the computer of information
from the file, SEQ TEST FILE, produced by the first half of the pro
gram, READ SEQTEST FILE. (See Figure 9.4.)
Finally, the notes in lines 1000 to 1006 describe a sequence of contin
gency commands in a subroutine, statements 1010 through 1060, that will
1 REM
2 »EM * EXAMPLE
3 REM * READ & WRITE
4 REM * A SEQUENTIAL
5 REM * DATA FILE
6 REM aw************
18 PRINT'^ifclMJINITI
28 0IMA$<25>
38 0IMB<25)
49 Q£ML,
68 GQSUB it)&8
78 CR$=CHR$(I3)
88 PRINT
98 URITE
PKINT1i L!URIT SEQ TEST FILE1
95 PRINT
Figure 9.2: The statements ofthe SEQUENTIAL FILE program that prepare
the computer to handle afile.
104 THE EASY GUIDE TO YOUR COMMODORE 64
140 PRIHTt*2.A$ll,IISTR$(B)CR$;
145 G0SU8 1&80
150 GOTO 120
160 CLOSE 2
Figure 9.3: The statements ofthe SEQUENTIAL FILE program that send
information to a diskfile.
REM
REM *
REM » READ SEQ
REM * TEST FILE
REM »
REM ***
PRINT
PRINT" READ SEQ TEST FILE"
PRINT
0PEH2,8.2"8:SEQ TEST FILE ,S,R'
2,
RS=ST
60SU8 1889
PRINTrt$<I).B(I)
IFR S = 64 TI4EN 388
IF RSO8 THEN 438
1 11
1
GOTO 228
CLOSE 2
REAOV
display a code for problems that might develop from unrecognized com
mands between the computer and disk drive, READ THE ERROR
CHANNEL. (See Figure 9.5.)
To cut out that part of the program that creates the file in the first place,
you really only need the lines up to 160, and the subroutine from line 1010
on. You can begin to tailor this program to your file-making needs by
deleting the program lines between 200 and 410. You do that, you recall,
by sending each line number with the RETURN key:
200
201
400
410
113 HISia
1850 CLOSE 2
1860 END
REflOY.
Figure 9.5: The statements ofthe SEQUENTIAL FILE program that act on
a diskproblem.
PRINT statement of line 10, and the remarks and spacing of lines 95
through 105. (Keep in mind, however, that you or another user might
later appreciate having the REM lines preserved.)
The program that remains after this trimming will direct the computer
to form a disk file through your interaction. If you run it more than once
to create different files, however, it will give each set of data the same
name, SEQTEST FILE. By altering two statements, you can modify the
program to assign a name of your choosing to each file you create with this
program.
At line 110 is the statement that names the file to be put on disk. You
can make it more general by simply changing the part that names the file
to a variable. To do this requires the kind of addition that you can perform
on word strings using a plus sign. Using the variable F$ to represent the
file name, you can replace line 110 with the statement:
Now the program directs the computer to give each file the name assigned
to the variable F$. You can make this assignment when the program is
run, if you include a statement that asks you for a file name. An INPUT
BUILDING AND REBUILDING PROGRAMS 107
Now the computer holds a versatile program that can create disk file
after disk file in response to your entries, and can assign to each a different
name. Using this program you can create many files, each of which will
be listed by name on the directory of the disk it is stored on.
The resulting program you'll see on sending the command LIST (as
shown in Figure 9.6), is trimmed and to the point. It will direct the com
puter to ask you for the name of a file you wish to produce, and then,
through the prompts of its INPUT statement, will collect and finally send
to the disk drive the elements you supply from the keyboard.
You can save this program as it is, by putting it on a disk on which
you'd like to store files. You might put it there under the name MAKE
FILE, for instance:
^■MAKEFILE
or
On the other hand, you can include this program in a larger program, if
you like, or make some further adjustments to it.
As it was orginally written, the file-handing program reserves space for
only 26 entries. Lines 20 and 30 make this allocation:
20 DIMA$(25)
30 DIMB(25)
You can extend the number of elements to be put in the files you create, by
modifying those statements to reserve more space in computer memory. If
you wanted to reserve room for 100 items, for instance, you could simply
send new statements to reserve space:
20 DIMA$(100)
30 DIM B( 100)
20 0itfA$(25)
38 0IK8C25)
48 OPENiS.a.IS/'IO"
68 G0SU8 1000
70 CRS=CHR>(13)
140 PRIHTtt2,A$",llSTR$(B)CR$;
58 GOTO 128
188 CLOSE 2
178 EHD
1880 REM mxx
1018 INPUTHI
10
Lines 70 through 206, which direct file formation, can also be dropped:
70
80
206
1001
1002
1003
1004
1005
1006
As it stands now the program will direct the computer to find and read
into memory the items from a file on disk, named SEQTEST FILE. By
changing two statements, you can tailor the program to ask you for the file
name, and then to find that file by name. The line that directs the com
puter to search the file for SEQTEST FILE, line 210, can be changed so
as to direct it to search out a file of any name, stored under the variable
F$, like this:
You can direct the computer to ask you for a file name, with the statement:
The program now in memory (Figure 9.7) will search a disk for the file
110 THE EASY GUIDE TO YOUR COMMODORE 64
20 DIM ASCIOG)
30 DIM B(IGO)
49 0PEN15c8.i5,M[0M
60 G0SU8 1000
207 IHPUT"NAME OF FILE TO 8E FOUNO "
210 OPEN 2.8.2Jllfl:ll*F$ + ",S,R11
215 G0SU8 10(30
220 INPUTtt2,A$<I),B(I)
224 RS=ST
225 G0SU8 1000
230 PRIHTA$(I).B(I)
240 IFR S=64 TlHEH 300
RErtOV
that you name in response to the computer's query, and then print out
each element of the file as directed by line 230:
PRINT A$(I),B(I)
You can use this program to search for and list files, or you can further
alter it to use those elements in different ways. You can add any statements
you desire, so long as they take into account the form of the variables in
the INPUT and allied statements. Conversely, you can change these vari
ables to match your program if you like.
This approach to program tailoring can be used whenever you want the
computer to act on information it receives from a disk file. By eliminating
statements you find to be superfluous, you can streamline and adapt any
prerecorded program the computer will list.
To the person who has gained facility in using programs and com
mands, the computer is not the unmanageable contraption it often first
appeared. Even though by learning to control the Commodore 64 you can
gain an understanding of the logic behind the machine, many of its possi
bilities lie buried in its memory and design. There are ways these secrets
can be uncovered; you can go further into the computer without finding
yourself elbow-deep in microchips and tangled wires. More commands
will lead you into the maze of number codes that direct the computer. An
assortment of extra circuitry will make the Commodore 64 into something
more than the machine it was built to be. And a look inside will reveal the
guts of a personal computer undreamed ofjust a few years ago.
The computer's translator acts as a bridge between you and the processor,
which directs the flow of information through the computer's circuits.
These circuits are etched on microchips, like the one shown in Figure
10.1. The processor and the information reservoirs (the "memory chips")
sit quite apart from each other in the computer. The computer's memory
has been organized so that each piece of information is stored according to
a number, which represents the location of that data. Although the pro
cessor may change the information in a memory location, that numbered
location continues to exist, regardless of the value contained in it—a fixed
address with changing tenants.
THE INSIDE STORY 113
'■■ '■■'■~W;-"~fmPl
ft:,,
Sti
jRgwne i0. 2: <4n encased microchip ofthe type used in all microcomputers.
POKE 53281,0
This POKE command translates as: "Seek out memory location number
53281 and store the value of 0 at that address."
You can likewise direct the processor to set the border of the display to
black. The processor looks for directions for this part of the display at
memory address number 53280. You can use the command:
POKE 53280,0
SYSA65E
SYS statement, you send the computer to an address in the middle of one of
these sequences of directions, the results may be mangled. But after gaining
some familiarity with these locations, you can avoid such surprises.
Comparisons
Logic (Boolean)
Other
ASC (" "): Gives a code number, called the ASCII code, for the
first character of the string of characters.
Back-up: Not directions given to a truck driver, but a copy kept in case
the item in use should fail. A back-up copy of a disk can be kept, or a back
up copy of a file or program can be kept in another form or on the same
disk.
Bit: Not the past tense of bite, but the smallest piece of information the
computer can handle. A bit always has a value of either 0 or 1.
Boot a Disk: Not an act of aggravation, but the act of sending directions
that operate a disk system into the computer from a disk in a drive. (You
boot the disk before sending disk commands to the computer.)
like a reservoir collecting a river's waters faster than it lets them through
flood gates.
Byte: The basic package of information sent through the computer cir
cuits, in which each of eight bits has a value of either 0 or 1. Each memory
location in the Commodore 64 holds one byte.
Crash: In a program run, what happens when the computer stops act
ing on program commands suddenly, in an unplanned way, usually
because of directions the computer can't follow.
Default ("dear Brutus, lies not in our stars but in ourselves"): That
value chosen automatically by the computer when a choice is not made by
the computer user.
Enter: To send data or instructions into the computer, usually from the
keyboard, and followed by a subsequent press of the RETURN key.
Housekeeping: Not dusting and mopping, but the chores done by the
computer user, or the computer itself, to operate a system or a program.
I/O: The flow of data or commands to and from the computer, from
Input/Output.
Low-level Language: Not locker room talk, but a set of commands that
direct the computer in relatively unsophisticated ways, requiring several
commands to direct a complex task.
Microprocessor: The electronic circuitry chip that controls the rest of the
computer. The 6510 chip is the Commodore 64 computer's micropro
cessor.
Mini-Floppy Diskette: The formal name for the type of disk, 5 V* inches
across and flexible, used with the Commodore 64 and most other personal
computers.
Pixel: Not a mincing dwarf, but the smallest dot a computer can control
on a video screen.
Serial: One after another, as in the way data or command impulses can
be sent from a computer through a single wire. (Contrast with parallel.)
Spreadsheet: Not bed linen hanging to dry, but a visual system of orga
nizing and calculating data, often business or financial, in rows and
columns. Several such systems are available as programs on disks.
Utility: Not the gas company, but a program that directs the computer
in performing some internal task, like moving data about its memory.
DATA, 57-62
Databanks, 118
Adding information to programs, Decimal code, 114
57-63 Decision making, automating, 29-31
Adding program parts, 96-102 Delay (time), 48-49
Adding strings, 51-52 DIM, 62-63
Altering disk programs, 102-111 Disks, 79
Animation, 48 formatting of, 88
Arithmetic, 13-14 initializing of, 88
Assignment commands, 23-24 inserting, 83
Automating decision making, 29-31 prerecorded, 82
programs, altering of, 102-111
BASIC, 113 TEST/DEMO, 84, 85
Blank statement, 21 vulnerability of, 82-83
Disk commands, 83-84
Catalog, 90 C-64 WEDGE, 86
Cassette tapes, 73-78 %,87
Changing modes, 7 t,87
Chips, microcircuitry, 116 Delete (@S:), 90
Clearing the screen, 10-11 Save, 89
Clock, 63 Diskdrive, 79-81
Commands Disk program, erasing of, 90
assignment, 23 Disk system, 79-81
conditional, 29 Drawing arrows, 43-49
direct, 9 Drive head, magnetic, 83
display
comma, 18 Electronic combination lock, 69-71
semicolon, 18 Empty loop, 54
keypress, 1-5 END, 97
machine language, 115 Erasing commands, 21, 23
prerecorded, 9 Erasing a disk program, 90
series of, 27-28
Conditional command, 29 File
IF-THEN, 29 sequential, 94-95, 102
Connecting a second drive, 92 File, cont.
Connections, computer to television, text, 94
2-4 Formatting a disk, 88
Counting string lengths, 52-53 FOR-NEXT loop, 43-49
CP/M, 118 nested, 46
operating system, 118
C-64 WEDGE disk commands, 86 GET, 67-68
129
Tab,16,18
TEST/DEMO disk, 84, 85-87
Text files, 94-95
Time delay, 48-49
Translator, 8
The SYBEX Library
INTRODUCTION TO COMPUTERS
DON'T (or How to Care for Your Computer)
by Rodnay Zaks 214 pp., 100 illustr., Ref. 0-065
The correct way to handle and care for all elements of a computer system,
including what to do when something doesn't work.
U.S.A.
SYBEX, Inc. FRANCE GERMANY
2344 Sixth Street SYBEX SYBEX-VERLAG
Berkeley, 4 Place Felix-Eboue Heyestr. 22
California 94710 75583 Paris Cedex 12 4000 Diisseldorf 12
Tel: (800)227-2346 France West Germany
(415)848-8233 Tel: 1/347-30-20 Tel: (0211)287066
Telex: 336311 Telex: 211801 Telex: 08 588163