The Commodore Basic Programming Language

Published on 13 September 2023 (Updated: 22 January 2024)

Welcome to the Commodore Basic page! Here, you'll find a description of the language as well as a list of sample programs in that language.

This article was written by:

Description

Introduction

According to Wikipedia, Commodore BASIC is a dialect of the BASIC programming language developed for Commodore's 8-bit home computers. It is based on Microsoft BASIC, which was developed by Microsoft founders Bill Gates and Paul Allen. In 1977, Commodore licensed Microsoft BASIC for a one-time fee of $25,000.

BASIC was the first language I ever learned back in High School. Although I never owned a Commodore computer, Commodore BASIC is very similar to what I learned.

Line Numbers

Like other dialects of BASIC at the time, programs were entered using line numbers. The line numbers determine the order of statements in the program. If an existing line number is used, the code at that line number is overwritten. If a line number with nothing after it is used, the code at that line number is deleted. Code can be inserted between existing lines by using a line number that is between two existing line number. For example,

10 PRINT "Hello"
20 PRINT "Goodbyte"
15 PRINT "Something else"
20 PRINT "Goodbye"
30 PRINT "All Done"
30

This would result in the following code (this can be seen using the LIST command, which shows the current program):

10 PRINT "Hello"
15 PRINT "Something else"
20 PRINT "Goodbye"

Line were typically numbered in increments of 10 so that lines could be easily inserted.

Statements

Statements are only allowed to be 80 characters long. Multiple statements can be chained together using :. For example, "Hello" and "World" can be displayed on separate lines like this:

10 PRINT "Hello": PRINT "World"

All keywords are capitalized.

Variables

There are three types of variables:

All three types support arrays.

All variables are global. There are some system variables:

Although variable names can be longer than two characters, only the first two characters matter – e.g., ABC is the same as AB. No part of a variable name can contain a keyword. For example, AIF3 is not allowed since it contains the IF keyword.

Control Flow

Like other BASICs of the time, Commodore BASIC is a completely unstructured language. Therefore, it uses (gasp!) GOTO statements to unconditionally jump to another part of the program.

The IF statement is used to execute a piece of code conditionally. It can also be used in conjunction with GOTO to jump to another part of the program. For example:

50 IF A < 5 THEN B = 7: GOTO 70
60 B = 8
70 PRINT "A ="; A; ", B ="; B

If the value of A is less than 5, the value of B is set to 7, and the code jumps to line 70. Otherwise, the value of B is set to 8. At line 70, the value of A and B are displayed.

The FOR statement can be used to set up loops, where a variable is set up to go over specified range and increment. For example, this displays the number 1 to 10:

100 FOR I = 1 TO 10
110 PRINT I
120 NEXT I

The value of I runs from 1 to 10. The NEXT keyword determines the end of the loop. The STEP keyword can be used to set the increment for the loop. For example, this displays even numbers from 10 down to 0:

100 FOR I = 10 TO 0 STEP -2
110 PRINT I
120 NEXT I

Subroutines are supported via the GOSUB statement. This pushes the line number of the next instruction to be executed on to the stack and jumps to the specified line number. The RETURN statement is used to return control back to the caller. It pops the line number off the stack and jumps to that line number. For example:

150 A = 1: B = 7: GOSUB 1000
160 PRINT "The sum from"; A; "to"; B; "is"; S
170 ...
1000 S = 0
1010 FOR I = A TO B
1020 S = S + I
1030 NEXT I
1040 RETURN

At line 150, A is set to 1, and B is set to 7. Line number 160 is pushed on to the stack, and the program jumps to line 1000. The code a line 1000 through 1030 sets the value of S to the sum of A through B inclusive. At line 1040, the RETURN statement pops 160 from the stack and jumps to that line number. At line 160 the value of A, B, and S are displayed:

The sum from 1 to 7 is 28

Finally, the END statement can be used to stop the execution of the program. It is typically used in the middle of a program. For the above example, if line 170 were an END statement the program would exit instead of falling through to line 1000.

Further Reading

This is just an overview of the Commodore BASIC language. If you want a more in-depth look at this language, see the Commodore BASIC Wiki. If you'd like to try out this language, then clone the cbmbasic GitHub repository, change to the directory where the repository was cloned, and run the make command to build the code. For example:

git clone git@github.com:mist64/cbmbasic.git
cd cbmbasic
make

The output will be called cbmbasic. If you just run cbmbasic with no arguments, you should see something like this:

    **** COMMODORE 64 BASIC V2 ****

 64K RAM SYSTEM  38911 BASIC BYTES FREE

READY.

This will take you into the BASIC interpreter where you can enter a program. You can also run a BASIC program using cbmbasic followed by the path to the program. For example:

cbmbasic hello-world.bas

Articles

There are 37 articles: