Commodore 64 Assembly Color
In this article you will learn how to create an example of Commodore 64 assembly color programs. This tutorial guide will be aimed at the absolute beginner wanting to learn simple assembly language (machine language) technqiues.
It gets a lot more complicated to that when you want to understand it at the lower level and see all the bits and bytes, but I won’t go into a lot of detail and save that for a more advanced session.
Learning Assembly Language
The first thing to learn about assembly language is that it consists of simple three letter commands (opcodes) followed by parameters to perform functions of loading, saving, and copying memory.
Memory on the Commodore 64 ranges from $0-65535 bytes. Many of these areas are reserved for page zero, Basic programs, VIC memory, the Kernal, and so on. Our example will use an area that is known as free memory.
Free Memory
It’s importance to know where you are storing your program. For our Commodore 64 assembly color example, I am using a 4k (kilobytes) area starting at $C000 (49152) that is common for Commodore 64 Basic machine language routines.
Since our program is not that large, I figured it would be easier to fit simple segment parts while learning beginning techniques for assembly language.
Border Color Change
The first part of our Commodore 64 assembly color subroutine will change the background color of the screen. First we will load a byte (ranges from 0-255) into memory and place it in a color register at $D020 (53280). The example below will change the screen border color to white.
lda #1 ; color white
sta $D020 ; screen border register
Write message on Screen
The Commodore 64 screen consists of 1000 characters (ranging from 0-999) starting at register $0400 (1024). As we place numbers into these registers, characters will appear based on what is contained in the Commodore 64 PET ASCII character set.
So for our example we are utilizing letters so we started at 0 up to 26 (A-Z). The following below positions the word ‘DISCORD” on the screen using these register areas.
lda #4 ; letter ‘D’
sta $0400 ; screen ram area
lda #9 ; letter ‘I’
sta $0401
lda #19 ; letter ‘S’
sta $0402
lda #3 ; letter ‘C’
sta $0403
lda #15 ; letter ‘O’
sta $0404
lda #18 ; letter ‘R’
sta $0405
lda #4 ; letter ‘D’
sta $0406
Color Ram Memory
Notice that the characters were all in a single color. That is because for our Commodore 64 assembly color routine to get more colors (from 0-15) we need to turn on each register occupied with that color ram area. Color Ram starts at $D800 (55296) and counts up to $D8E7 (56295).
So the first line will start in the upper left corner and each corresponding line after that will be used to position a new character until we have spelled the word ‘DISCORD’.
Easier way to rewrite code
If you had to write out a much longer message in multiple places on the screen this could get quite tedious and make your program longer, consuming more memory faster. Therefore the Commodore 64 assembly color example can also use a loop to quite write a message to the screen.
The code below will read from the line ‘msg12’ and write out x values (counting from 0-7) to write the message to the screen. It will keep repeating back using the bne loop1 until it has completed the entire message.
ldx #0 ; 0-5 (load x register)
loop1
lda msg1,x
sta $0400,x
lda color1,x
sta $d800,x
inx
cpx #7
bne loop1
The following code below will write the word ‘SERVER” underneath the ‘DISCORD’ message to complete our assembly language program.
ldx #0 ; 0-5 (load x register)
loop2
lda msg2,x
sta $0428,x
lda color2,x
sta $d828,x
inx
cpx #6
bne loop2
Try creating your own examples and see what types of messages you can come up with. Let me know in the comments what you have learned in this blog.
[wonderplugin_pdf src=”https://www.c64brain.com/wp-content/uploads/2021/12/Color-Changer.pdf” width=”100%” height=”600px” style=”border:0;”]
Leave A Comment