Before you can design any real complicated programs on the Commodore 64, you will need to explore the Basic language first. Basic (Beginner’s All Symbolic Instruction Code) is the easiest to understand since it uses regular words (known as commands). Below you will find several programs that you can begin writing immediately.
The above screenshot is a picture of the Commodore 64 Owner’s Manual. When the computer was first released back in the early 80’s, this book was packaged along with it to get the user working with Basic almost immediately. Just for simplicity, let’s explore a few commands below.
Basic Commands
Some of the first commands (keywords) you will want to learn on your Commodore 64 is called PRINT
PRINT or ? – used to write numbers or text on the screen.
Let’s write some simple commands to put numbers on your screen.
Your First Program
Although it isn’t demonstrated in this tutorial, you can also add a question mark instead of typing PRINT. An example is
? 15
Type the program below on your Commodore 64 computer or in the VICE C64 Emulator.
PRINT 1
You will then see a 1 printed on the screen. You just told the computer to write the value on the screen with the value after the command. When you write a program like this the computer will not remember it next time unless you write it out again. This is known as ‘immediate mode’. After a few more examples, we will start writing our first computer program that will record our information in memory.
You can write any numbers after the PRINT statement that you want. Try the example below.
PRINT 25
The computer prints the value of 25 on the screen!
Writing your first Program
When we begin to write our first program we will need to use an ordered listing before each instruction. It is common to skip values by 10. Here is an example. I have added some extra PRINT statements to show you that you can repeat instructions also.
10 PRINT 1
20 PRINT 2
30 PRINT 3
Try your hand at printing different values in your program. Just don’t forget to type RUN and enter to tell your Commodore 64 that you want to ‘RUN’ the program in it’s memory. This memory is known as RAM (Random Access Memory). RAM will also stay in memory as long as your computer is turned on and you have not erased the program (which we will learn about soon).
Showing the program in memory
We need to learn about a new command. Type the command LIST and press Return or Enter. You should see your program displayed neatly below the area you just typed. You can also show only certain lines or parts of the program. Type LIST 20 and you will see line twenty. Now try LIST 20- and you will see all lines starting at line 20 to the end of the program. That is a way to see a series of lines written in Basic memory.
Using Math in your Program
Now we will have fun writing programs to emulate an actual calculator. Before we do this I want to talk about something called ‘operators’. An operator is a symbol that is used after a number to achieve a calculation. There are currently 4 operators I would like to talk about to keep this simple for now. They are listed below.+ : Used for adding numbers
– : Used to subtract numbers
* : Used to multiply numbers
/ : Used to divide numbers
We will now write a program to add some numbers.
10 PRINT 25 + 5
When you type this program in, type RUN and press the Return (or Enter key) you will see the result of 30 on the screen. You just told the computer to add 25 plus 5. The next program does the opposite and will reduce a value as a calculation.
10 PRINT 18 – 3
After entering the program, entering RUN and Return you should see the value of 15 on your screen. Next we will ask the computer to multiply some numbers together.
Type the program below
10 PRINT 200 * 35
You should see the result of 7000. Notice that the computer does not add commas in the answer. Next we will ask the computer to divide some values for us.
10 PRINT 33 / 11
The result will show a 3 on your screen. Soon we will cover decimal calculations and eventually something called variables.
You can also append numbers together by using a comma. This will also create spaces between the numbers on your screen. Try the example below.
PRINT 1, 2, 3, 4, 5
If you don’t want the spaces between a long result of numbers, then use a semicolon instead of a comma. Try the example below
PRINT 1;2;3;4;5
This will merge the numbers together with no spaces and you will see 12345 printed on the Commodore 64 screen.
Until then try some practice by writing a bigger program. Try about 5 lines with different calculations and see how you do. As a reminder, be sure to start with 10 and increase by 10 for the next line. So a five line program will have the values 10, 20, 30, 40 & 50.
Erasing your program
Finally you will need to know how to erase your program in memory. Just type the command below and press Return or the Enter key.
NEW
Now your program has been removed from memory so you can start on a new program.
Finally you can also add comments to your program using a new command called REM (stands for remark). This will make it easier for someone to understand what a section of your program is attempting to accomplish.
5 REM NUMBER PROGRAM
Summary of the Lesson
In this lesson you learned how to write your first computer program. First this was done in immediate mode with no line numbers then you added numbers so that the computer could group this in a program in its’ memory. After this you learned how to write out different values after the PRINT statement and finally we showed you how to use math in your program with operators. We also learned how to erase a program from memory using the NEW command.
We hope you enjoyed this learning session. Please return to the top and press the next tab to learn more about Basic commands for your Commodore 64 system. 🙂
Using Variables in your Program
A variable is used to save data for your program. In this example we will just be storing numbers for now, but will cover more advanced stuff like using words (known as strings) as variables. Begin by entering the new program below and observe the value we created here called TT. For this example, TT will represent our total. Much later we will even learn how to use variables to store a total result after a calculation as been processed.
10 TT=5
20 PRINT TT
30 T2=21
40 PRINT T2
The above program uses two variables (TT and T2). Just know you can you use any words with numbers, but the variable cannot start with a number and must be only two characters in length. If you do you more than two characters for your variable however, the remaining letters will be ignored.
Calculating with Variables
You can also use the math operators on your variables. The computer will translate the variable into its corresponding value and still keep track of the results. Let’s write a brand new program.
10 TT=100
20 T2=50
30 PRINT TT + T2
40 AA=8
50 PRINT T2 – AA
Multiple operations on a command
It is also possible to combine operations when writing a program. Type the example below.
10 PRINT (24 / 3) + 2
The result should be 10. Also notice the open and closed parenthesis. These allow the calculation within that area to be solved first and then everything after that is calculation (executed left to right). In this example we take 24 and divide it by 3 to get an answer of 8. Then we add 8 + 2 to arrive at 10. All Math also follows the order of operations. The easiest way I remembered this (in Math class) is to turn each letter into a statement.
P – Parenthesis
E – Excuse (exponent)
M – My (multiply)
D – Dear (divide)
A – Aunt (add)
S – Sally (subtract)
Please Excuse My Dear Aunt Sally
Once again use the RUN command and press the Return key (Enter on the desktop). The values shown will be 150 and 42. The computer reads the first as PRINT 100 +50 = 150. Then the second is read as PRINT 50 – 8. Just know that it is translating the values so that they are understood in memory to the programmer. Much later we will attempt to create a small checking account program. So a value will be entered by the user as a deposit into a bank account. A variable will be used to keep track of the total cash in the checking account. Another variable will be used to run a total against the account. We could also use a variable to take money out of the bank and total that result too. Finally it would even be cool to show the balance remaining in your checking account. Your imagination is the limit here.
Increment Variable Values
Since our goal eventually is to create a simulated bank account, we will next teach you about a process that will allow you to both increase and decrease a variable’s value. It does this by taking the original number and appending a result to it. Likewise it can also detract a result too. Below is our first example.
10 X1 = X1 + 1
20 PRINT X1
Now it is important to know that the computer will result a result to zero if you do not assign a value to it with the equal sign. So X1 will first have a result of zero. Then you see a one added to it. So if you add 0 + 1 = 1. Let’s try setting a value this time and performing a different operation on it.
Decrement Variable Values
10 Y1 = 12
20 Y1 = Y1 – 1
30 PRINT Y1
In line 10 Y1 is first assigned a value of 12. Then we see that a one is subtracted from it. This is calculated as 12 – 1 = 11. You can use any of the operators (+, -, * /, exponents) to get a result. You can also use any numbers in these variables such as the example below
10 RS=200
20 RS = RS * 2
30 PRINT RS
The result will contain 400 since 200 * 2 = 400
Summary of the Lesson
In this tutorial we learned about creating variables that are used by a computer program. Variables are made up of two letters. We also learned how to utilize the order of operations to execute results in a specific sequence. Much later we explored adding and subtract from a sequence of numbers (known as incrementing and decrementing) values.
When we get to loops, you will see the advantage of increasing and decreasing variable values. At that time we will also introduce you to a command called GOTO to keep the program running at all times.
Learning about Strings
Now we are going to have some fun. We are going to make your computer display words on your screen. We could even get it to remember results and show them to you later. This process is known as a string. It is similar to tying a bunch of strings together. This will allow you to store a lot of data in your program’s memory. Before we do that though, let’s see the written results.
Type the following below
PRINT “HI I AM YOUR COMMODORE 64.”
After you press the Return or Enter key you will see the words HI I AM YOUR COMMODORE 64. shown on the screen. You can write any statements you want just be sure to add a quote at the beginning and end of the printed area. This is your first String example.
The Computer knows your name
We are now going to introduce you to a new command that will be used for our String example. It is used to write information on your screen and wait for you to type in an answer. This is known as ‘input’ such we are waiting on an input value.
Try the example below
10 INPUT “WHAT IS YOUR NAME”;NM$
20 PRINT NM$
Okay before we continue, we need to explain what the NM$ is about. This is the way of storing data in a variable that receives words only. We are using it here to capture a word that will be shown to the user afterward. So when you run the program you will see the question WHAT IF YOUR NAME? written on the screen. After you type in your name it will be shown to you. This is the way for the computer to prompt you for an answer before showing you a result. It also adds the question mark so we don’t have to enter it in the program.
Using multiple String Variables
Now we are going to show you a program that will ask for your name and the computer will respond with an answer stored in another variable.
Try the example below
10 CM$=”COMMODORE 64 COMPUTER.”
20 INPUT “WHAT IS YOUR NAME”;NM$
30 PRINT “HELLO ” NM$ “. I AM THE “CM$
Once you have executed this program the computer will again ask you to enter your name. Then it will write a simple message (HELLO) and write your name next to it using the NM$ variable. Finally it will show I AM THE and use the variable CM$ to write out COMMODORE 64 COMPUTER.
So for example if your name is Alex. It will show
HELLO ALEX. I AM THE COMMODORE 64 COMPUTER.
Summary of the Lesson
We learned how to write text on our screen by containing data inside a set of quotes. After this we studied how to get the computer to remember an answer and write it back on the screen. Finally we added to this program and made the computer write back it’s own name in a variable.
Creating a program loop
When you want the program to run without interruption, you will need to make it repeat itself over and over until you exit it. This is known as creating a loop for the program. We will learn a new command called GOTO that will allow us to repeat a section of code over and over. First let’s try a simple example as always.
10 PRINT “I AM THE COMMODORE 64 COMPUTER!”
20 GOTO 10
When you type RUN and execute the program above, you will suddenly see your screen filled with a lot of repeating data that says I AM THE COMMODORE 64 COMPUTER! The program is running in repeated cycles to print the words after the PRINT statement forever, until you press the break key to get out of the program, reset the Commodore 64, or turn it off.
Creating a loop will be very powerful once we begin construction of our banking program. This will allow us to create deposits and withdrawals without leaving the program. It is a way to allow repeated instructions to the computer to give us the results we are looking for.
Although we do not yet have enough information to build our program yet, I wanted to at least build upon that foundation until we are ready to do so.
Running a statement in a looping sequence
Now we are going to learn some new commands. Each of these is necessary to accomplish our next task which will be to print out a set of values. The first part of the command sequence is called FOR. The next command is called TO and then we will use a command called NEXT to complete the statement loop. Let’s finally get down to an example again.
FOR XX=1 to 10: NEXT
Something new as introduced here as well. Do you see that colon between the 10 and the NEXT command? It is known as a separator and allows us to append commands together on one line. When you run this example you won’t see anything printed on the screen yet. This is because the computer has just counted from 1 to 10, but did not contain a command to show results on the screen. Now this time, let’s add some new instructions to our program so we can see the results.
FOR XX=1 to 10: PRINT XX: NEXT
After you finish typing and running this program, you should see a set of number printed down the screen from 1 to 10. This is your second experience with a loop. It obtains repeated values in order until it arrives at the last value. Here is another way of looking at it.
FOR XX (variable) = 1 (start value) TO 10 (end value):PRINT XX (prints 1):NEXT (prints 2,3,4,5,6,7,8,9,10)
This example demonstrates how the computer is able to get a series of numbers displayed from a simple loop instruction. This is also known as a FOR/NEXT loop in the technical world.
Counting the loop backwards
You can also use a FOR/NEXT loop in reverse to count down the values. We also need to introduce a new command called STEP. This command allows use to count in reverse by STEPing through the program until it finishes counting down. The negative after the STEP statement represents the ability to negate the direction of the count. See the example below
FOR YY=100 to 0 STEP – 1:PRINT YY: NEXT
This will show the numbers 100 to 0 counting down. Your screen will scroll each time. However, to prevent the screen from scrolling you can use the semicolon instead.
FOR YY=100 to 0 STEP – 1: PRINT YY;: NEXT
This prints and formats a nice list of numbers on your screen.
Skipping over numbers
We can make a single tweak to our program and then the results will skip over numbers. Try the example below
FOR YY=100 to 0 STEP -4: PRINT YY;: NEXT
Now the program will skip over in counts of 4. An example as seen is 100, 96, 92, 88.
You can also skip over in a positive direction. Try the new example below
FOR YY=100 to 1000 STEP 5:PRINT YY;: NEXT
We hope you are enjoying this session so far. Now try to create your own counting programs and see if you can create some new counting loops.
Summary of the Lesson
In this section we learned how to use a GOTO command to repeat program lines. Then we discovered a new command that created a FOR/NEXT loop to write all a series of data results. We also practiced writing forward and backward loops and even skipping over data values using STEP.
Allowing the computer to make Decisions
It is time to get the computer thinking different. We are going to introduce something called conditions to allow your computer to make choices with data results it is given. This is the first step in teaching a computer to solve problems. We are going to have a lot of fun, so stay on track with this lesson. Eventually we will get to the baking program, but after introducing you to decision making, I would like to create a simple guessing game involving numbers.
The commands we are going to learn next are called IF and THEN. Also we will be exploring a variety of symbols that will be used to check range of our data to ensure we have not arrived at a result. It will make sense soon. First let’s see a program example.
10 RS=10
20 INPUT”WHAT IS 10 x 5 EQUAL TO”;AN
30 IF AN=RS * 5 THEN PRINT “YOU ARE CORRECT! NICE JOB!”: END
40 IF AN <> RS * 5 THEN PRINT “THAT IS WRONG. SORRY.”
50 GOTO 20
This program works in a loop by first asking WHAT IS 10 X 5 EQUAL TO? Then it uses a new variable called AN. Since we are learning something new again I will need to further elaborate on what is going on here. AN is a numerical variable that waits on a number instead of letters to be entered at the answer prompt.
If you enter the correct answer as 50 then the computer will respond with YOU ARE CORRECT! NICE JOB! It works by using the condition to check the variable RS against the calculation result of RS * 5 (RS times 5). Since RS is equal to 10 and we are then multiplying it by 5 it now has an answer of 50 since RS * 5 = 50. Then we place this in a decision IF/THEN result to check for the correct result. After this a new command called END is introduced, which quits running the program.
However, if the result is not correct the computer will print THAT IS WRONG. SORRY. on the screen. This works because it is now checking using a new set of symbols that show <>. Then is also known as greater or less than (or not equal to). It is checking to see if the answer is first < (less than) the result of 50 and then if the answer is > (greater than) the result of 50. Combining these together allows the computer to understand that the result is lower or higher than the intended answer.
Writing a guessing program
Now let’s write a simple game. This game will allow you to respond at the waiting prompt to guess a number the computer is thinking about. We will also be introducing a new command called RND that will allow your computer to mix up data to get a random number. The command INT is referred to as integer and is used to pull only a number from the result. The program is below
10 PRINT “I AM THINKING OF A NUMBER BETWEEN 1 TO 10. SEE IF YOU CAN GUESS IT.”
20 NM=INT(RND(0)*10)+1
30 PRINT
40 INPUT”YOUR ANSWER “;AN
50 IF AN=NM THEN PRINT “YOU GOT IT! GREAT!”: END
60 IF AN<NM
THEN PRINT “TOO LOW. TRY AGAIN.”
70 IF AN>NM THEN PRINT “TOO HIGH. TRY AGAIN.”
80 GOTO 30
When you run this program it will print I AM THINKING OF A NUMBER BETWEEN 1 TO 10. SEE IF YOU CAN GUESS IT. Then a random number is generated in the numeric variable NM. It will pick a value between 1 to 10. Then +1 at the end is used to count above zero, so that zero will never be an answer.
After this we see an empty PRINT. Then is used to create a blank line and makes the program look better. Then we utilize the INPUT command to check AN for an entered result. It is then stored in the AN variable.
Next the condition statements will check for the correct answer (AN=NM), a lower answer (AN<NM) and an answer greater than the result (AN>NM).
Finally the loop will continue forever, until the correct answer is entered, we break out of the program, or shut off the computer (or close the VICE emulator).
Listed below is the total of combinations that can be evaluated against
- = – equal to a result
- < – less than a result
- > – greater than a result
- <= – less than or equal to a result
- >= – greater or equal to a result
Summary of the Lesson
In this section we covered how logic conditions work with programming. You learned how to check if a value is in range (=) or not equal (<>). For fun a guessing game was created to test this. It would evaluate if a guess was higher (>) or lower(<).
Reading Data into memory
Another way of storing data is by placing words and text in slots in memory and then extracting them out with a command. The commands we are now going to learn are READ and DATA. The program is listed here.
10 CT=0
20 FOR J=1 to 5
30 READ V
40 T=T+V
50 NEXT
60 ? “TOTAL RESULT: “;T
70 END
80 DATA 5,10,15,20,25
This one works first by setting a variable called T to zero. Then it uses a FOR/NEXT loop to read from the DATA command on line 80. It reads in 5 values (FOR J=1 To 5). The READ V command reads each value individually. Then it uses a variable increment to tally up all the numbers in line 80 (5,10,15,20,25) or 5+10+15+20+25 = 75. Finally the computer shows TOTAL RESULT: 75 and exits the program with the END command.
Keep in mind that DATA commands cannot contain any calculations and must not start with a comma. They can be mixed up with letters and numbers to produce interesting stored results.
There is a new command to learn called RESTORE. It is used to restore the data pointer to first piece of data in the program. This is necessary since later when you are writing large segments of code you will need to have a way to point to specific data. This is done by using a RESTORE followed by a line number.
Errors in reading DATA statements
It is necessary also to ensure that all of the data values are accounted for that are being read using a FOR/NEXT loop. When data is out of range the message will be seen on the screen.
OUT OF DATA ERROR
This means that the computer was trying to read a specific set of results and failed to locate every value in that DATA statement. At this point you may find yourself counting the numbers in the DATA statements. This is common when move on to programming in machine language and notice that suddenly the computer has failed to retrieve all of the correct values.
Summary of the Lesson
For this part you learned how to read a set of numbers into DATA statements that can printed out later. You also should now understand that calculations cannot be performed on a set of DATA values in the actual statement. That is done outside of the area though.
Learning about Arrays
In this next session we are going to learn about arrays. An array value is similar to a string since it can retain data for later. However arrays can cycle through a list of values just like the DATA statement. Click on the Read-Data tab to learn more if you skipped over that part of the tutorial series. Arrays are great since you can use calculations on them. If you decide to explore game design they could be used to keep track of where treasure chests are, keys to locked doors, and may other goodies. Just know that they are powerful and can produce some pretty amazing results.
Learning about Subscript Variables
In the Commodore 64C Personal Computer System Guide it introduces the reader to subscript variables on page 53. The numbers in the parenthesis below are the subscripted values. A variable or a calculation can be used as a subscript. The following program below as taken from the book as an example. You will also be using a new command called DIM (stands for dimension). This is used to reserve the correct amount of memory for the array.
5 DIM X(5)
10 T=0
20 FOR J=1 TO 5
30 READ X(J)
40 T=T+X(J)
50 NEXT
60 A=T/5
70 ?”AVERAGE =”;A
80 END
90 DATA 5,12,1,34,18
Notice all that the program is using two different types of FOR/NEXT statements. This is known as a nested loop when run together. In this example it is used to read the first part of the array and then the second.
Using two-dimensional arrays
Now we are going to write a program that uses two sides to an array, known as the two-dimension array. This could be used to find a position in a map if you were writing a game or track a high and a low score. It could also be used make a comparison against two types of weight.
The program is below
5 REM WEIGHT LOSS COMPARISONS
10 DIM WT(4,2)
12 PRINT
15 PRINT “————————–“
18 PRINT”WEIGHT LOSSES FOR THE YEAR”
19 PRINT”————————–“
20 FOR A=1 TO 4
30 FOR B=1 TO 2
40 READ X
50 WT(A,B)=X
60 NEXT: NEXT
70 PRINT
80 FOR L=1 TO 4
90 W=1
100 PRINT “BEFORE/AFTER: “;WT(L,W),WT(L,W+1)
110 W=W+2
120 NEXT
200 DATA 210,192
210 DATA 155,180
220 DATA 300,250
230 DATA 165,145
After running the program, results will be printed on the screen. The computer is comparing the before versus the weight they lost afterward. The variable W is being increased by two each time to point to every 2nd element in the subscript array. The array is reading 4 rows of data with FOR A=1 TO 4 (just count each line from 200-230). Then it reads result 1 and 2 using FOR B=1 TO 2.
Summary of the Lesson
In this session you learned about using subscript variables to read individual data values that can be used against calculations. A new command called DIM was introduced. You also learned how to use two-dimension arrays. Finally an example was created for a two-dimension array to read the results of a student weighed in the beginning and after they lost some weight.
You stated:
“Although this is a tech site, I am ashamed of the gospel. ”
I think you meant to say ” I am NOT ashamed of the gospel.”
Yes I totally agree. Not sure how I missed that word, but it can sure make all the difference regarding your belief in the Lord Jesus Christ. So my revision is obviously “I am not ashamed of the gospel that is in Jesus Christ our Lord” – Romans 1:16. Thanks for the call out Christian family!
PS: Now if I can only ever figure out how to edit that bottom area …
Finally got that fixed, as it was hidden away in the email/profile area. Thanks for letting me know.