Introduction
A BASIC program is stored in memory, and so in the file, in tokenised form: every
keyword is replaced by one byte of $80 or more, so that PRINT takes
one byte and not five. The program starts at $0801, so a PRG file with a BASIC
program loads at $0801. This page describes BASIC V2, the BASIC of the C64.
A line
| Bytes | Field | Meaning |
|---|---|---|
| 2 | Link | The address of the next line, low byte first. BASIC follows these links to walk the program. |
| 2 | Line number | 0 to 63999, low byte first. |
| n | Text | The line, with keywords as tokens. Text between quotation marks is stored as typed (as PETSCII), and so is everything after REM. |
| 1 | End | A zero byte. |
The explorer checks that each link is the address of the line after it, and reports one that is not.
The end of the program
The program ends with a link of $0000: the two bytes after the last line’s
zero byte. Anything after them in the file is not part of the BASIC program. In a program that
carries machine code, the code usually follows straight after.
The SYS stub
A machine-code program is started from BASIC by SYS address, which
calls the code at that decimal address (and returns to BASIC if the code ends in
RTS). A PRG file of this kind starts with a short BASIC program that does only that:
10 SYS 2061
The stub takes up some bytes ($0801 to $080C here, with its end
marker) and the code starts straight after, at $080D, which is 2061. Some stubs
put a comment after the number, or other statements; some leave a gap before the code. The
explorer takes the first SYS that is followed by a plain decimal number, spaces
allowed, and uses it as the entry point if it lies in the file. It does not evaluate
expressions, so SYS 4096+3 and SYS A are not followed. After
RUN, BASIC runs the stub, and the stub starts the machine code.
The tokens
BASIC V2 has 76 tokens, $80 to $CB, and $FF for the
constant π. A byte of $80 or more outside quotation marks and
REM is a token; the explorer writes a byte in that range that is not a token as
{$cc}.
| Token | Keyword | Token | Keyword | Token | Keyword | Token | Keyword |
|---|---|---|---|---|---|---|---|
80 | END | 81 | FOR | 82 | NEXT | 83 | DATA |
84 | INPUT# | 85 | INPUT | 86 | DIM | 87 | READ |
88 | LET | 89 | GOTO | 8A | RUN | 8B | IF |
8C | RESTORE | 8D | GOSUB | 8E | RETURN | 8F | REM |
90 | STOP | 91 | ON | 92 | WAIT | 93 | LOAD |
94 | SAVE | 95 | VERIFY | 96 | DEF | 97 | POKE |
98 | PRINT# | 99 | 9A | CONT | 9B | LIST | |
9C | CLR | 9D | CMD | 9E | SYS | 9F | OPEN |
A0 | CLOSE | A1 | GET | A2 | NEW | A3 | TAB( |
A4 | TO | A5 | FN | A6 | SPC( | A7 | THEN |
A8 | NOT | A9 | STEP | AA | + | AB | − |
AC | * | AD | / | AE | ^ (up arrow) | AF | AND |
B0 | OR | B1 | > | B2 | = | B3 | < |
B4 | SGN | B5 | INT | B6 | ABS | B7 | USR |
B8 | FRE | B9 | POS | BA | SQR | BB | RND |
BC | LOG | BD | EXP | BE | COS | BF | SIN |
C0 | TAN | C1 | ATN | C2 | PEEK | C3 | LEN |
C4 | STR$ | C5 | VAL | C6 | ASC | C7 | CHR$ |
C8 | LEFT$ | C9 | RIGHT$ | CA | MID$ | CB | GO |
FF | π | ||||||
Links
Reference
- BASIC token – the C64-Wiki’s table of the BASIC V2 tokens