Commodore
64 BASIC V2
Commands
CONT (Continue)
This command is used to restart the execution of a program which has been
stopped by either using the <STOP> key (but not <STOP> & <RESTORE>),
a STOP statement, or an END statement within the program. The program will
restart at the exact place from where it left off.
CONT will not work if you have changed or added lines to the program,
or if the program halted due to an error, or if you caused an error before
trying to restart the program. In these cases you will get a CAN'T CONTINUE
ERROR.
LIST
The LIST command allows you to look at lines of a BASIC program in memory.
You can ask for the entire program to be displayed, or only certain line
numbers.
LIST Shows entire program
LIST 10- Shows only from line 10 until end
LIST 10 Shows only line 10
LIST -10 Shows lines from beginning until 10
LIST 10-20 Shows line from 10 to 20, inclusive
LOAD
This command is used to transfer a program from tape or disk into memory
so the program can be used. If you just type LOAD and hit <RETURN>,
the first program found on the cassette unit will be placed in memory.
The command may be followed by a program name enclosed within quotes. The
name may then be followed by a comma and a number or numeric variable,
which acts as a device number to indicate where the program is coming from.
If no device number is given, the COMMODORE 64 assumes device #1, which
is the cassette unit. The other device commonly used with the LOAD command
is the disk device, which is device #8.
LOAD Reads in the next program on tape
LOAD "HELLO" Searches tape for program called HELLO, and loads program if found
LOAD A$ Looks for program whose name is in the variable A$
LOAD "HELLO",8 Looks for program called HELLO on the disk drive
LOAD "*",8 Looks for first program on disk
A secondary address of 1 must be specified if you want to load a
machine code program without relocating it.
LOAD "M/C PROGRAM",1,1 Loads machine code from tape without relocating
NEW
This command erases the entire program in memory, and also clears out any
variables that may have been used. Unless the program was SAVEd, it is
lost. BE CAREFUL WHEN YOU USE THIS COMMAND.
The NEW command can also be used as a BASIC program statement. When
the program reaches this line, the program is erased. This is useful if
you want to leave everything neat when the program is done.
RUN
This command causes execution of a program, once the program is loaded
into memory. If there is no line number following RUN, the computer will
start with the lowest line number. If a line number is designated, the
program will start executing from the specified line.
RUN Starts program at lowest line number
RUN 100 Starts execution at line 100
RUN X UNDEFINED STATEMENT ERROR. You must always specify
an actual line number, not a variable representation
SAVE
This command will store the program currently in memory on cassette or
disk. If you just type SAVE and <RETURN>, the program will be SAVEd
on cassette. The computer has no way of knowing if there is a program already
on that tape, so be careful with your tapes or you may erase a valuable
program.
If you type SAVE followed by a name in quotes or a string variable,
the computer will give the program that name, so it can be more easily
located and retrieved in the future. The name may also be followed by a
device number.
After the device number, there can be a comma and a second number,
either 0 or 1. If the second number is 1, the COMMODORE 64 will put an
END-OF-TAPE marker after your program. This signals the computer not to
look any further on the tape if you were to give an additional LOAD command.
If you try to LOAD a program and the computer finds one of these markers,
you will get a FILE NOT FOUND ERROR.
SAVE Stores program to tape without name
SAVE "HELLO" Stores on tape with name HELLO
SAVE A$ Stores on tape with name A$
SAVE "HELLO",8 Stores on disk with name HELLO
SAVE "HELLO",1,1 Stores on tape with name HELLO and reloads in same
position of memory
SAVE "HELLO",1,2 Stores on tape with name HELLO and follows program
with END-OF-TAPE marker
SAVE "HELLO",1,3 As above but reloads in same position of memory
VERIFY
This command causes the computer to check the program on disk or
tape against the one in memory. This is proof that the program is actually
SAVEd, in case the tape or disk is bad, or something went wrong during
the SAVE. VERIFY without anything after the command causes the COMMODORE
64 to check the next program on tape, regardless of name, against the program
in memory.
VERIFY followed by a program name, or a string variable, will search
for that program and then check. Device numbers can also be included with
the verify command.
VERIFY Checks the next program on tape
VERIFY "HELLO" Searches for HELLO, checks against memory
VERIFY "HELLO",8 Searches for HELLO on disk, then checks
To check if a program is already on a tape, just VERIFY and the
computer will say which program it has found (if any).
STATEMENTS
CLOSE
This command completes and closes any files used by OPEN statements. The
number following CLOSE is the file number to be closed.
CLOSE 2 Only file #2 is closed
CLR
This command will erase any variables in memory, but leaves the program
itself intact. This command is automatically executed when a RUN command
is given.
CMD
CMD sends the output which normally would go to the screen (i.e., PRINT
statements, LISTs, but not POKEs onto the screen) to another device instead.
This could be a printer, or a data file on disk. This device or file must
be OPENed first. The CMD command must be followed by a number or numeric
variable referring to the file.
OPEN 1,4 OPENs device #4, which is the printer
CMD 1 All normal output now goes to printer
LIST The program listing now goes to the printer, not the screen
To send output back to the screen, CLOSE the file with CLOSE 1.
DATA
This statement is followed by a list of items to be used by READ statements.
Items may be numeric values or text strings, and items are separated by
commas. String items need not be inside quote marks unless they contain
space, colon, or comma. If two commas have nothing between them, the value
will be READ as a zero for a number, or an empty string.
DATA 12, 14.5, "HELLO, MOM", 3.14, PART1
DEF FN
This command allows you to define a complex calculation as a function with
a short name. In the case of a long formula that is used many times within
the program, this can save time and space.
This function name will be FN and any legal variable name (1 or 2 characters
long). First you must define the function using the statement DEF followed
by the function name. Following the name is a set of parentheses enclosing
a numeric variable. Then follows the actual formula
that you want to define, with the variable in the proper spot. You
can then "call" the formula, substituting any number for the variable.
10 DEF FN A(X) = 12 * ( 34.75 - X / .3 )
20 PRINT FN A(7)
For this example, the result would be 137.
DIM
When you use more than 11 elements of an array, you must execute a DIMstatement
for the array. Keep in mind that the whole array takes up room in memory,
so don't create an array much larger than you'll need. To figure the number
of variables created with DIM, multiply the total number of elements plus
one in each dimension of the array.
10 DIM A$(40), B7(15), CC%(4,4,4)
41 elements 16 elements 125 elements
You can dimension more than one array in a DIM statement. However, be careful
not to dimension an array more than once.
END
When a program encounters an END statement, the program halts, as
if it ran out of lines. You may use CONT to restart the program.
FOR ... TO ... STEP
This statement works with the NEXT statement to repeat a section
of the program a set number of times. The format is:
FOR (Var. Name)=(Start of Count) TO (End of Count) STEP (Count By)
The loop variable will be added to or subtracted from during the program.
Without any STEP specified, STEP is assumed to be 1. The start count and
end count are the limits to the value of the loop variable.
10 FOR L = 1 TO 10 STEP .1
20 PRINT L
30 NEXT L
The end of the loop value may be followed by the word STEP and another
number or variable. In this case, the value following STEP is added each
time instead of 1. This allows you to count backwards, or by fractions.
GET
The GET statement allows you to get data from the keyboard, one character
at a time. When GET is executed, the character that is typed is assigned
to the variable. If no character is typed, then a null (empty) character
is assigned.
GET is followed by a variable name, usually a string variable. If a
numeric variable was used and a nonnumeric key depressed, the program would
halt with an error message. The GET statement may be placed into a loop,
checking for any empty result. This loop will continue until a key is hit.
10 GET A$: IF A$ = "" THEN 10
GET#
The GET# statement is used with a previously OPENed device or file, to
input one character at a time from that device or file.
GET#1,A$
This would input one character from a data file.
GOSUB
This statement is similar to GOTO, except the computer remembers which
program line it last executed before the GOSUB. When a line with a RETURN
statement is encountered, the program jumps back to the statement immediately
following the GOSUB. This is useful if there is a routine in your program
that occurs in several parts of the program. Instead of typing the routine
over and over, execute GOSUBs each time the routine is needed.
20 GOSUB 800
GOTO or GO TO
When a statement with the GOTO command is reached, the next line
to be executed will be the one with the line number following the word
GOTO.
IF ... THEN
IF ... THEN lets the computer analyze a situation and take two possible
courses of action, depending on the outcome. If the expression is true,
the statement following THEN is executed. This may be any BASIC statement.
If the expression is false, the program goes directly to the next line.
The expression being evaluated may be a variable or formula, in which
case it is considered true if nonzero, and false if zero. In most cases,
there is an expression involving relational operators (=, <, >, <=,
>=, <>, AND, OR, NOT).
10 IF X > 10 THEN END
INPUT
The INPUT statement allows the program to get data from the user, assigning
that data to a variable. The program will stop, print a question mark (?)
on the screen, and wait for the user to type in the answer and hit <RETURN>.
INPUT is followed by a variable name, or a list of variable names,
separated by commas. A message may be placed within quote marks, before
the list of variable names to be INPUT. If more than one variable is to
be INPUT, they must be separated by commas when typed.
10 INPUT "PLEASE ENTER YOUR FIRST NAME";A$
20 PRINT "ENTER YOUR CODE NUMBER";: INPUT B
INPUT#
INPUT# is similar to INPUT, but takes data from a previously OPENed file.
10 INPUT#1, A
LET
LET is hardly ever used in programs, since it is optional, but the statement
is the heart of all BASIC programs. The variable name which is to be assigned
the result of a calculation is on the left side of the equal sign, and
the formula on the right.
10 LET A = 5
20 LET D$ = "HELLO"
NEXT
NEXT is always used in conjunction with the FOR statement. When the program
reaches a NEXT statement, it checks the FOR statement to see if
the limit of the loop has been reached. If the loop is not finished,
the loop variable is increased by the specified STEP value. If the loop
is
finished, execution proceeds with the statement following NEXT.
NEXT may be followed by a variable name, or list of variable names,
separated by commas. If there are no names listed, the last loop started
is the one being completed. If variables are given, they are completed
in order from left to right.
10 FOR X = 1 TO 100: NEXT
ON
This command turns the GOTO and GOSUB commands into special versions of
the IF statement. ON is followed by a formula, which is evaluated. If the
result of the calculation is one, the first line on the list is executed;
if the result is 2, the second line is executed, and so on. If the result
is 0, negative, or larger than the list of numbers, the next line executed
will be the statement following the ON statement.
10 INPUT X
20 ON X GOTO 10,20,30,40,50
OPEN
Then OPEN statement allows the COMMODORE 64 to access devices such as the
disk for data, a printer, or even the screen. OPEN is followed by a number
(0-255), to which all following statements will refer. There is usually
a second number after the first, which is the device number. The device
numbers are:
0 Screen
1 Cassette
4 Printer
8 Disk
Following the device number may be a third number, separated again by
a a comma, which is the secondary address. In the case of the disk, the
number refers to the buffer, or channel, number. In the printer, the secondary
address controls features like expanded printing. See the Commodore 64
Programmer's Reference Manual for
more details.
OPEN 1,0 OPENs the SCREEN as a device
OPEN 2,8,8,"D" OPENs the disk for reading, file to be searched for is D
OPEN 3,4 OPENs the printer
OPEN 4,8,15 OPENs the data channel on the disk
Also see: CLOSE, CMD, GET#, INPUT#, and PRINT#, system variable
ST, and
Appendix B.
POKE
POKE is always followed by two numbers, or formulas. The first location
is a memory location; the second number is a decimal value from 0 to 255,
which will be placed in the memory location, replacing any previously
stored value.
10 POKE 53281,0
20 S = 4096 * 13
30 POKE S + 29,8
PRINT
The PRINT statement is the first one most people learn to use, but there
are a number of variations to be aware of. PRINT can be followed by:
Text String with quotes
Variable names
Functions
Punctuation marks
Punctuation marks are used to help format the data on the screen. The
comma divides the screen into four columns, while the semicolon suppresses
all spacing. Either mark can be the last symbol on a line. This results
in the next thing PRINTed acting as if it were a continuation of the same
PRINT statement.
10 PRINT "HELLO"
20 PRINT "HELLO", A$
30 PRINT A + B
40 PRINT J;
50 PRINT A,B,C,D
Also see: POS, SPC and TAB functions.
PRINT#
There are a few differences between this statement and PRINT. PRINT# is
followed by a number, which refers to the device or data file previously
OPENed. This number is followed by a comma and a list to be printed. The
comma and semicolon have the same effect as they do in PRINT. Please note
that some devices may not work with TAB and SPC.
100 PRINT#1, "DATA VALUES"; A%, B1, C$
READ
READ is used to assign information from DATA statements to variables, so
the information may be put to use. Care must be taken to avoid READing
strings where READ is expecting a number, which will give a TYPE MISMATCH
ERROR.
REM (Remark)
REMark is a note to whomever is reading a LIST of the program. It may explain
a section of the program, or give additional instructions. REM statements
in no way affect the operation of the program, except to add to its length.
REM may be followed by any text.
RESTORE
When executed in a program, the pointer to which an item in a DATA statement
will be READ next is reset to the first item in the list. This gives you
the ability to re-READ the information. RESTORE stands by itself on a line.
RETURN
This statement is always used in conjunction with GOSUB. When the program
encounters a RETURN, it will go to the statement immediately following
the GOSUB command. If no GOSUB was previously issued, a RETURN WITHOUT
GOSUB ERROR will occur.
STOP
This statement will halt program execution. The message, BREAK IN xxx will
be displayed, where xxx is the line number containing STOP. The
program may be restarted by using the CONT command. STOP is normally
used in debugging a program.
SYS
SYS is followed by a decimal number or numeric value in the range 0-65535.
The program will then begin executing the machine language program starting
at that memory location. This is similar to the USR function, but does
not allow parameter passing.
WAIT
WAIT is used to halt the program until the contents of a memory location
changes in a specific way. WAIT is followed by a memory location (X) and
up to two variables. The format is:
WAIT X,Y,Z
The contents of the memory location are first exclusive-ORed with the
third number, if present, and then logically ANDed with the second number.
If the result is zero, the program goes back to that memory location and
checks again. When the result is nonzero, the program continues with the
next statement.
NUMERIC FUNCTIONS
ABS(X) (absolute value)
ABS returns the absolute value of the number, without its sign (+ or -).
The answer is always positive.
ATN(X) (arctangent)
Returns the angle, measured in radians, whose tangent is X.
COS(X) (cosine)
Returns the value of the cosine of X, where X is an angle measured in radians.
EXP(X)
Returns the value of the mathematical constant e (2.71828183) raised to
the power of X.
FN xx(X)
Returns the value of the user-defined function xx created in a DEF FN xx(X)
statement.
INT(X)
Returns the truncated value of X, that is, with all the decimal places
to the right of the decimal point removed. The result will always be less
than, or equal to, X. Thus, any negative numbers with decimal places will
become the integer less than their current value.
LOG(X) (logarithm)
Will return the natural log of X. The natural log to the base e (see EXP(X)).
To convert to log base 10, simply divide by LOG(10).
PEEK(X)
Used to find out contents of memory location X, in the range 0-65535, giving
a result from 0-255. PEEK is often used in conjunction
with the POKE statement.
RND(X) (random number)
RND(X) returns a random number in the range 0-1. The first random number
should be generated by the formula RND(-TI) to start things off
differently every time. After this, X should be a 1 or any positive
number. If X is zero, the result will be the same random number as the
last one.
A negative value for X will reseed the generator. The use of the same
negative number for X will result in the same sequence of "random"
numbers. The formula for generating a number between X and Y is:
N = RND(1) * (Y-X) + X
where,
Y is the upper limit,
X is the lower range of numbers desired.
SGN(X) (sign)
This function returns the sign (positive, negative, or zero) of X. The
result will be +1 if positive, 0 if zero, and -1 if negative.
SIN(X) (sine)
SIN(X) is the trigonometric sine function. The result will be the
sine of X, where X is an angle in radians.
SQR(X) (square root)
This function will return the square root of X, where X is a positive number
or 0. If X is negative, an ILLEGAL QUANTITY ERROR results.
TAN(X) (tangent)
The result will be the tangent of X, where X is an angle in radians.
USR(X)
When this function is used, the program jumps to a machine language program
whose starting point is contained in memory locations. The parameter X
is passed to the machine language program, which will return another value
back to the BASIC program. Refer to the Commodore 64 Programmer's Reference
Manual for more details on this function and machine language programming.
STRING FUNCTIONS
ASC(X$)
This function will return the ASCII code of the first character of X$.
CHR$(X)
This is the opposite of ASC, and returns a string character whose ASCII
code is X.
LEFT$(X$,X)
Returns a string containing the leftmost X characters of X$.
LEN(X$)
Returned will be the number of characters (including spaces and other symbols)
in the string X$.
MID$(X$,S,X)
This will return a string containing X characters starting from the Sth
character in X$.
RIGHT$(X$,X)
Returns the rightmost X characters in X$.
STR$(X)
This will return a string which is identical to the PRINTed version of
X.
VAL(X$)
This function converts X$ into a number, and is essentially the inverse
operation from STR$. The string is examined from the leftmost character
to the right, for as many characters as are in recognizable number
format.
10 X = VAL("123.456") X = 123.456
10 X = VAL("12A13B") X = 12
10 X = VAL("RIU017") X = 0
10 X = VAL("-1.23.45.67") X = -1.23
OTHER FUNCTIONS
FRE(X)
This function returns the number of unused bytes available in memory, regardless
of the value of X. Note that FRE(X) will read out in negative numbers if
the number of unused bytes is over 32K.
POS(X)
This function returns the number of the column (0-39) at which the next
PRINT statement will begin on the screen. X may have any value and is not
used.
SPC(X)
This is used in a PRINT statement to skip X spaces forward.
TAB(X)
TAB is also used in a PRINT statement; the next item to be PRINTed will
be in column X.
last update: 06.06.2000