Basic Line Storage on the VIC-20

BASIC programs are stored in memory using a simple structure that we can investigate and manipulate. This article will show how they are stored and contains a BASIC program to go through each line of its own code and display how it is stored in memory. A good understanding of how BASIC is stored can help us to find novel ways to get the most out of BASIC and to leverage the power of the BASIC interpreter. This can be seen in a previous article: Storing Machine Code in REM Statements on the VIC-20.

A BASIC program consists of a series of lines. Each line has a link to the next line in memory, followed by a line number, then one or more BASIC statements and the end of the line is indicated with a 00 byte. The end of the program is indicated by two 00 bytes in the next line link.

BASIC programs start at different locations depending on how much memory the Vic has and this can be seen in the following table.

Memory BASIC Storage First Location of BASIC Program
Unexpanded $1000-$1DFF (4096-7679) $1001 (4097)
+3K $0400-$1DFF (1024-7679) $0401 (1025)
+8K $1200-$3FFF (4608-16383) $1201 (4609)
+16K $1200-$5FFF (4608-24575) $1201 (4609)
+24K $1200-$7FFF (4608-32767) $1201 (4609)

In order to reduce the storage requirements of a BASIC program and to speed up the interpreter, BASIC statements are condensed using tokens. The following table contains the tokens used by BASIC v2 on the Vic. Where tokens aren't used, PETSCII takes it place in the case of comments, variable names, numbers, etc.

DecimalHexBASIC token DecimalHexBASIC token
12880END166A6SPC(
12981FOR167A7THEN
13082NEXT168A8NOT
13183DATA169A9STEP
13284INPUT#170AA+
13385INPUT171AB-
13486DIM172AC*
13587READ173AD/
13688LET174AE^
13789GOTO175AFAND
1388ARUN176B0OR
1398BIF177B1>
1408CRESTORE178B2=
1418DGOSUB179B3<
1428ERETURN180B4SGN
1438FREM181B5INT
14490STOP182B6ABS
14591ON183B7USR
14692WAIT184B8FRE
14793LOAD185B9POS
14894SAVE186BASQR
14995VERIFY187BBRND
15096DEF188BCLOG
15197POKE189BDEXP
15298PRINT#190BECOS
15399PRINT191BFSIN
1549ACONT192C0TAN
1559BLIST193C1ATN
1569CCLR194C2PEEK
1579DCMD195C3LEN
1589ESYS196C4STR$
1599FOPEN197C5VAL
160A0CLOSE198C6ASC
161A1GET199C7CHR$
162A2NEW200C8LEFT$
163A3TAB(201C9RIGHT$
164A4TO202CAMID$
165A5FN203CBGO

An Example BASIC Program Structure

As an example we'll look at how the following short BASIC program is stored in memory.

10 REM A COMMENT
20 N=4*3:PRINT N

On an unexpanded Vic, BASIC starts at 4097 ($1001) and this table shows how the program is stored in memory. The two-byte values are stored in memory in LSB MSB order and in the table this is how they are listed in the hex column with the 16-bit value in MSB LSB order in parenthesis.

AddressContents
(decimal)
Contents
(hex)
MeaningType
409741131110 ($1011)Next Line Link
409900100A00 ($000A)Line Number
41011438FREMToken
41023220<space>PETSCII
41036541APETSCII
41043220<space>PETSCII
41056743CPETSCII
4106794FOPETSCII
4107774DMPETSCII
4108774DMPETSCII
41096945EPETSCII
4110784ENPETSCII
41118454TPETSCII
4112000End of Line
 
411341271F10 ($101F)Next Line Link
411500201400 ($0014)Line Number
4117784ENPETSCII
4118178B2=Token
411952344PETSCII
4120172AC*Token
412151333PETSCII
4122583A:PETSCII
412315399PRINTToken
41243220<space>PETSCII
4125784ENPETSCII
4126000End of Line
 
41270000End of Program

Video

The following video displays a BASIC program's structure and encoding in memory using the program below and VICMON.

Program to Display Structure of Basic Program

The following program will go through each of its BASIC lines and show the structure and encoding.

100 REM PRINT BASIC STRUCTURE
110 P=PEEK(43)+PEEK(44)*256:REM START OF BASIC
120 GOSUB 5000
130 LL=PEEK(P)+256*PEEK(P+1)
140 LN=PEEK(P+2)+256*PEEK(P+3)
150 GOSUB 1000
160 P=LL
170 IF LL <> 0 THEN GOTO 130
180 END

1000 REM PRINT LINE STRUCTURE
1010 IF LL=0 THEN PRINT P;LL;TAB(12);"*EOP":RETURN
1020 PRINT:GOSUB 4000:PRINT
1030 PRINT P;LL;TAB(12);"*LINK"
1040 PRINT P+2;LN;TAB(12);"*LINE NUM"
1050 P=P+4
1060 B=PEEK(P)
1070 PRINT P;B;TAB(12);
1080 GOSUB 3000:PRINT
1090 P=P+1
1100 GOSUB 2000
1110 IF B <> 0 GOTO 1060
1120 PRINT
1130 RETURN

2000 REM DELAY
2010 FOR I=1 TO 150
2020 NEXT I
2030 RETURN

3000 REM PRINT TOKEN OR CHAR
3010 IF PL = 0 AND B=0 THEN PRINT "*EOL"
3020 IF PL = 0 AND B=32 THEN PRINT "[SPACE]";
3030 IF B >= 128 AND B <= 203 THEN PRINT T$(B-128);:RETURN
3040 PRINT CHR$(B);
3050 RETURN

4000 REM PRINT BASIC LINE
4010 PRINT LN;
4020 BL=P+4
4030 B=PEEK(BL)
4040 IF B = 0 THEN 4080
4050 PL=1:GOSUB 3000:PL=0
4060 BL=BL+1
4070 GOTO 4030
4080 PRINT
4090 RETURN

5000 REM LOAD TOKENS
5010 DIM T$(76)
5020 FOR I=0TO75
5030 READ T$(I)
5040 NEXT I
5050 RETURN

6000 REM TOKENS
6010 DATA "END","FOR","NEXT","DATA","INPUT#","INPUT"
6020 DATA "DIM","READ","LET","GOTO","RUN","IF","RESTORE"
6030 DATA "GOSUB","RETURN","REM","STOP","ON","WAIT","LOAD"
6040 DATA "SAVE","VERIFY","DEF","POKE","PRINT#","PRINT"
6050 DATA "CONT","LIST","CLR","CMD","SYS","OPEN","CLOSE"
6060 DATA "GET","NEW","TAB(","TO","FN","SPC(","THEN","NOT"
6070 DATA "STEP","+","-","*","/","^","AND","OR",">","="
6080 DATA "<","SGN","INT","ABS","USR","FRE","POS","SQR"
6090 DATA "RND","LOG","EXP","COS","SIN","TAN","ATN"
6100 DATA "PEEK","LEN","STR$","VAL","ASC","CHR$","LEFT$"
6110 DATA "RIGHT$","MID$","GO"
Creative Commons License
Basic Line Storage on the VIC-20 by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post

Feedback/Discuss

Related Articles

Getting the Address of BASIC Variables on the VIC-20

Getting the address of a BASIC variable can be useful if you want to pass data to a machine code routine or want to access the bytes of a variable directly to improve speed and reduce garbage collectio...   Read More

Saving and Loading Memory on the VIC-20

Saving and loading memory is quite easy on the VIC-20 once you know how. However, it isn't obvious how to do this and therefore this article will present a few simple ways of doing it from BASIC and A...   Read More

Storing Machine Code in REM Statements on the VIC-20

BASIC programs often contain machine code routines but they take up quite a lot of space in BASIC. An interesting way to reduce the amount of space that they take is to store the machine code in REM s...   Read More

Adding Basic Stubs to Assembly Language on the Commodore VIC-20

To make machine language programs more friendly it's nice to add a Basic stub which contains a line with a SYS statement to start the code. This is easy to do on the VIC-20 and the process gives you a...   Read More

40 Columns in Basic on the Commodore VIC-20

There are a number of programs that allow you to use 40 columns of text from Basic on a Commodore VIC-20. This can be useful as by default the Vic's screen is 22 columns by 23 rows. They are supplied...   Read More