Variable

From C64-Wiki
(Redirected from variable)
Jump to navigationJump to 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 | edit source]

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

  • Names of variables can be choiced free and descriptive, e.g. SCREEN = 53281 for the memory address 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%
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

Using variables[edit | edit source]

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 address 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 | edit source]

Operators are used to perform operations or logical comparisons on the values of two or more variables of the same type. Operators are grouped into three types: numeric, logical and string. Some operators are only a single character ( +, -, *, /, ↑, <, >, =, . ), while others are keywords (AND, OR and NOT). By using brackets can changed the rank hierarchy. First, the contents of the innermost bracket is calculated. The maximum depth of nested brackets is 10 levels. If more levels are used, 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:

Rank Operator Remark
1 exponentiation
2 +
-
prefix
3 *
/
multiplication
division
4 +
-
addition and string concatenation
subtraction
5 <
<=
=
>=
>
<>
comparison
6 NOT logical NOT
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:

  • Math Operations:
    • 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 error ?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*π
  • Comparison operators controls IF-THEN- or ON-GOTO/GOSUB in a program: IF A < B THEN 200
  • Logical operators AND, OR and NOT are separate 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 ADDRESS:
POKE 53269, PEEK(53269) OR 64
  • Adding strings: C$ = A$ + B$

Functions[edit | edit source]

  • 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 | edit source]

A variable independent of the type used 7 bytes in the memory: 2 bytes for the variable name and the type and 5 bytes reserved for stored data:

  • 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 | edit source]

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