Variable

From C64-Wiki
Jump to: navigation, search

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[edit]

Commodore BASIC V2 supports three types of variables:

  • 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: A=1.234
  • 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 han 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[edit]

  • Names of variables can be choiced free and descriptive, e.g. SCREEN = 53281 for 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 ?SYNTAX ERROR appears.
Examples of not allowed variable names:

BASIC keywords are bold marked!

SINUS$
FIRSTLY%
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

Using variables[edit]

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[edit]

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:

Rang Operator Remark
1 potentitation
2 +
-
prefix
3 *
/
multiplication
division
4 +
-
addition and adding strings
subtraction
5 <
<=
=
>=
>
<>
comparsion
6 NOT logical negation
7 AND logical AND
8 OR logical OR

The logical operators AND, OR and NOT can also used for bitwise adding of numbers (max. 16 bit).

Examples:

  • Mathematische Operationen:
    • Addition: C=A+B
    • Subtraction: C=A-B
    • Multiplication: C=A*B
    • Division: C=A/B
      A Division through 0 isn't allowed and the BASIC-Fehlermeldung ?DIVISION BY ZERO appears.
    • Negative prefix: C=-B or C=2-(-4)
    • Power of (here square): C=2↑2
    • Exponents: C=A+1E+6
    • PI ("π"; constant value of 3,141592654): C=D*π
  • Comparsion operators controlls IF-THEN- or ON-GOTO/GOSUB in a program: IF A < B THEN 200
  • 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
  • Logical adding of bits with AND, OR and NOT:
REM SET BIT 6 OF A MEMORY ADRESS:
POKE 53269, PEEK(53269) OR 64</code>
  • Adding strins: C$ = A$ + B$

Functions[edit]

  • 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).

Memory using[edit]

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)

Links[edit]

WP-W11.png Wikipedia: Variable_(computer_science)
WP-W11.png Wikipedia: Significand