In this section of my tutorial blog I’m going to look at how we can alter the bits instead of just moving them. The most important aspect of coding is about control. I want you to feel empowered and to be able to craft your own games and programs with all of this knowledge.

Math and Logic Commands

Some of the most important commands for the C64’s Assembly language reside in the Math and Logic commands which let us calculate and manipulate bytes. This can be done either directly in the memory or in the registers.

In the above example we have a command called INC $D020. This means that we want the processor to add 1 to the address of D020.

A related command directly below this is DEC $D020 which works in the same way, but in the opposite direction. This decrements the value of the address by 1.

The above example will make your screen flicker between two colours, since it first adds 1 and then subtracts 1.

You can also increment or decrement the X and Y registers but not the A (Accumulator) register. This is done with the INX, DEX, INY and DEY. Another example of a screen colour flicker using these methods would be:

When executed this looks different from the results of the previous example. This is because it is slower first to move the colour value into the X register, then increment it, and then move it back to the memory address.

What if you wanted to increment a byte by 27? Using the logic demonstrated above, you could write an increment statement 27 times. However this would be incredibly counter intuitive. A smarter way would be to use the command ADC. This stands for add with carry which can add any given number to the accumulator.

The following example demonstrates this principle. It adds 27 to the address $0400, which is the character in the upper left corner of the screen. This code also uses a few more new terms to add to your coding arsenal. CLC means clear carry before adding. As the name suggests this clears the current value of the carry so that you can place a new value into it.

It’s important to remember with this example that $1B is hexadecimal for 27.

There’s also a way to subtract using the carry method and that comes from the SBC keyword. You have probably guessed that this stands for subtract with carry. Below is a code example as to how this works.

This concludes our whistle-stop tour of Assembly language and how it is used by the 6502 processor in the Commodore 64. I feel that it’s empowering to users and owners to learn a little about what goes on under the ‘hood’.

Additional Resources

Here are some more resources should you wish to delve a little deeper down the rabbit hole of assembly language.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s