A variable is a chunk of RAM, set aside to hold some information for a specific purpose, e.g. the score or position of the player in a game, the current horizontal position of the cursor in a text editing application or an street information in a database.
In high-level programming languages such as the Commodore BASIC V2 built into the C64, variables are also assigned a name: Everywhere the program needs to access a particular variable, the high-level program text makes a reference to the variable's name.
In machine language, implementing a variable implies setting aside one or more bytes to hold the required information. Assemblers provide a means to assign a name, in this case called symbol, to the memory addresses in question, serving much the same purpose as the names of variables in "high-level" languages.
Variables in BASIC
- Real variables can hold any decimal figure in the range ±2.93873588×10−38 thru ±1.70141183×1038. Variables of this kind have no suffix to indicate their type. Example:
- Integer variables can hold 16-bit signed binary integers in the range from −32768 thru 32767. The names of integer variables have a % sign as a suffix to indicate that they represent an integer. Examples:
A%, NO%, AB%=1000
- String variables can hold anything from 0 thru 255 PETSCII characters: Names of this kind of variables end with a $ sign. Examples
NA$, P$, BA$="TEXT"
All three types are valid in BASIC's support for arrays as well.
Names of Variables
- Names of variables can be choiced free and descriptive, e.g.
SCREEN = 53281for the memory adress of the background color. It is not allowed to use BASIC keywords (tokens, see article C64-Commands or BASIC V2), also system variables ( ST (STATUS), TI (TIME) and TI$ (TIME$) or FN ), as variable names or a part of it. If you used tokens the BASIC error messsage
Examples of not allowed variable names: BASIC keywords are bold marked! SINUS% STAR BEFORE1$ F3FN2
- The length of variable names are optional, but max. 80 chars (logical input line of BASIC). The BASIC interpreter used only the first 2 chars for controlling the using variables. The variables A$ and AA$ are different, but not AB$ and ABC$.
The variable names are used together without conflicts: AB, AB% and AB$, because the interpreter used also the type information.
It is wise to use only variable names with max. 2 chars.
Example for a conflict of similair variable names: 10 A$="1" 20 AA$="2" 30 AAA$="3" 40 PRINT A$, AA$, AAA$ Display: 1 3 3
- Names of variables can be include the chars A-Z and the numbers 0-9, but the first char of a variable name must be a letter. Optional is set a special char for the variable type (real variables haven't a special char):
- % for integer variables
- $ for string variables
With the following methods can get a variable datas (values or strings):
- With the command LET:
LET A = 123.
- Without the command LET:
A = 123.
- With the command READ from DATA lines, which are in the same program.
- Input the datas over the keyboard with the commands INPUT or GET.
- Reading out the datas of files from disk or cassette with the commands GET# or [INPUT(hash)|INPUT#]].
- Reading out the datas of a memory adress with the command PEEK.
- With assignation of other variables, e.g.:
B = A + 1.
With the following methods can you get the datas (values or strings) from a variable:
- Display the variable datas on the screen with the command PRINT.
- Print out the datas on paper with the printer with the command PRINT#.
- Write the datas in a file with the command PRINT#.
Operators are used to add datas of two or more varibles of the same variable type. It exists 3 different operator types: numeral, logical and string operators. Some operators are only a char (
+, -, *, /, ↑, <, >, =, . ) another a word (
AND, OR and
NOT). By using brackets can changed the rank hierarchy. First the interally bracket is calculated. The max. value of using brackets is 10. If you use more brackets, the BASIC error message ?OUT OF MEMORY appears.
The rank hierarchy of the operators is on the C64, that the operator with the highest priority firstly is calculated:
|addition and adding strings|
- Math Operations:
A Division through 0 isn't allowed and the BASIC error
?DIVISION BY ZEROappears.
- Negative prefix:
- Power of (here square):
- PI ("π"; constant value of 3,141592654):
- Logical operators AND, OR and NOT are seperat or complex terms for controlling a program (ruling of truth of the terms):
10 ON (X AND Y)+2 GOTO 100, 200 99 REM X, Y: 0 | -1 ... FALSe | TRUE 100 PRINT "TRUE":END 200 PRINT "FALSE":END
REM SET BIT 6 OF A MEMORY ADRESS: POKE 53269, PEEK(53269) OR 64
- Adding strings:
C$ = A$ + B$
- Variables can used with the following mathematical BASIC functions: ABS, ATN, COS, EXP, FN, INT, LOG, RND, SGN, SIN, SQR, TAN.
- Also can used the system functions FRE, POS and USR, and also the system variables STATUS (ST), TIME (TI) and TIME$ (TI$).
- For editing string variables can used: LEFT$, MID$, RIGHT$.
- For converting variable types into another type can used the functions: ASC, VAL (char and strings into a number), CHR$, STR$ (number into a char or string) and LEN (length of a string).
A variable independent of the type used 7 bytes in the memory: 2 bytes for the variable name and the type, also 5 bytes for informations:
- Real variables: 1 byte for Exponent and 4 bytes for mantissa (fixed-point part),
- Integer variables: 2 bytes for a 16-bit integer value (rest is 0),
- String variables: 3 bytes for string descriptor (1 byte for length and 2 bytes for pointer on string content, rest is 0)
Array or field variables are used the memory save. With the command DIM is used for each variable (cell) only the datas for the variable type: 5 bytes (real), 2 bytes (integer) and 3 bytes (string)