DATA

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: DATA
Abbreviation: D, Shift+A
Type: Command
Token code: 131/$83
Handling routine
in BASIC ROM:
43256–43269
$A8F8–A905
List of all BASIC keywords


Remark: This article describes the BASIC command DATA in BASIC V2 at the Commodore 64.

Type: Command
General Programming-Syntax: DATA <constant> [,<constant>]...

The BASIC-Command DATA is used to store constant information in the program code, and is used with the BASIC-command READ. Each DATA-line can contain one or more constants separated by commas. Expressions containing variables etc. will not be evaluated here.

The DATA values will be read from left to right, beginning with the first line containing a DATA statement. Each time a READ instruction is executed the saved DATA position of the last READ is advanced to the next value. Strings can be written normally or in quotes, but characters like comma, space, colon, graphical ones or control characters has to be written inside double quotes like string constants. The BASIC command RESTORE resets the pointer of the current DATA position the program start so that next READ will read from the first DATA found from the beginning of the program (starting with BASIC 3.5 the DATA line can be selected, too).

In case READ uses the wrong variable type the error message ?SYNTAX ERROR appears while referring the DATA line currently read. There is no easy way to get the position of the READ statement which is the real origin of the error. With BASIC 3.5 the error message changes to ?TYPE MISMATCH ERROR and the line number points directly to the READ statement that triggered the condition.

DATA lines may scattered over the whole program code, but it from the sake of execution efficiency it would be better to move them to the end. Otherwise all statements like GOTO or GOSUB jumping to lower line numbers has to walk through all DATA lines every time which leads to be an unnecessary time-penalty.

DATA lines are also commonly used to POKE machine language into memory through BASIC. This usually involves POKEing the code into memory, then running it with an SYS command. Occasionally, a checksum system is used to make sure the code is POKE'd into memory correctly.

Examples[edit | edit source]

Read values of different types and with special characters[edit | edit source]

10 DATA 1, 2, 3, APPLE, "COMMODORE 64"
20 DATA ."IN-DOUBLE-QUOTES"
30 DATA "THIS ; IS : A # TEST , ISN'T IT? "
40 READ A
50 READ B%, C, D$
60 READ E$
70 READ F$, G$
80 PRINT A, B%, C, D$, E$, F$ : PRINT "("G$")"

Characters like space, double quotes, comma, colon are handled correctly, giving following output:

 1         2         3        APPLE
COMMODORE 64        ."IN-DOUBLE-QUOTES"
(THIS ; IS : A # TEST , ISN'T IT? )


How spaces are treated[edit | edit source]

10 DATACOMMODORE 64
11 DATA COMMODORE 64 :
12 DATA COMMODORE 64 , COMMODORE 64
13 DATA " COMMODORE 64 "
20 READ A$,B$,C$,D$,E$
25 PRINT ,,"STRING LENGTH":PRINT
30 PRINT A$, LEN(A$)
40 PRINT B$, LEN(B$)
50 PRINT C$, LEN(C$)
60 PRINT D$, LEN(D$)
70 PRINT E$, LEN(E$)

giving following output:

                    STRING LENGTH

COMMODORE 64         12
COMMODORE 64         13
COMMODORE 64         13
COMMODORE 64         12
COMMODORE 64         14

Each variable B$ and C$ contains a space at the end because the DATA line continues with a colon or comma. For E$ the double quotes preserved the space at the start and the end. For the remaining variables the rule holds that surrounding spaces are simply skipped.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT