C64 Bedtime Coding (ENG) – First Steps (#06)

(Article by Phaze101)

First Steps

With this article we have reached the section where we will be switching from theory into practice. We will be using CBM Prog Studio. I will not be wasting time in explaining how to download it, install it and configure it. There are too many videos on YouTube regarding this. If there is a need to explain one of its features or assembly directives, I will explain it when we encounter it.

http://www.ajordison.co.uk/

Please note that from now on it will be hands on. The listings will be images so that no one can do cut and paste from the article to the assembler. I need you to type the code. Only if you practice typing in the code like the old days when copying listing from magazines you will learn what you are doing. Sorry if this is going to put some off you of but it is for your own good. If you really want to learn assembly, you need to have patience. As I said in article one only if you have patience you will succeed in learning assembly. Of course, for those that know it all this might sound absurd, but these articles are aimed at beginners who seriously want to learn assembly. So, if you are serious in learning assembly you will make the effort, to type the listing and have patience.

Soon, who you might see me as your teacher, or your master will be superseded by you. When that happens, I will be more than proud of what you have learnt or achieved in terms of this assembly  course. I will know that my work was not in vein. Then I will be able to learn from you and you’ll become my teacher 😊

Merging All

Let’s recap sometime that we covered in articles 4 and 5. In article 4 we explained the Addressing modes and in article 5 we covered the instruction set.

Now to those that are still confused let’s cover what is the relation or association between the two.

Let’s take one instruction and see how addressing modes and the instruction fit together.

As an example, let’s us LDA which by now you should know it stands for Load Accumulator.

The LDA instruction can use the following addressing modes followed by an example

• Absolute mode – LDA $C000 • Zero Page – LDA$FA
• Immediate – LDA #$01 • Absolute Index X or Absolute Index Y – LDA$C000,X
• Pre-Indexed Indirect X – LDA ($FB,X) • Post Index Indirect Y – LDA ($FB),Y

Why we use $C000. Well the C64 has 4K of memory that is protected from Basic. Our programs will not be bigger than 4K hence why it is convenient to use this memory location. I will be ignoring all my own remarks and only explaining code that needs to be explained. The remarks are there to help you understand better. For those that do not understand English the comments in the code will not be translated into Italian. Please use google to translate them however most of the things are explained in the article anyway. This is extra work that is not need. Please note that I use remarks followed by a series of equal sign (=) at top and bottom of a comment block. I use comment blocks to explain the code block that follows and mark the beginning of a new block of code. My coding style is my own preference. You can select your own. Just want to explain it that is all. The next two tutorials that follow do nothing spectacular however they are here to get you used to typing assembly code in the correct format in the assembler. First Program – Code 01 Here is the main basic code that you need to understand. Let’s go through this and explain it all. Everything in green is a comment first of all. The first thing you will see is the * directive. This is not an CPU instruction but what is called an assembler directive. Basically, you are telling the assembler where in memory your code will be stored which in this case it will be at memory location$C000 or 49152. So, SYS 49152 will execute our code.

The next thing that come up is the Label Start. This is a label that specifies that the start of our program. We do not use it in this case but I like to label the start of program, so it is a case of habit here.

The next 3 instructions are very interesting. The first one LDA #$0 means load the accumulator with a value of 1. We are using immediate addressing here. Notice the hash sign (#) which as explained in article 4 it means the value that follows. The second and third instructions are very easy. They are used in Basic constantly to change the Background colour and the Border colour. You might not recognize the$D020 and $D021 but if you managed to convert the hex numbers to decimal you will realise that these number are the famous 53281 and 53280 respectively. So, what we are doing here? Well we loaded the accumulator with the value of zero which is equivalent to black. We are telling the CPU to store a zero (black) in memory locations 53281 and 53280 respectively. It is the same as doing the Poke commands in basic. The next command is a jump to subroutine JSR. It simply executes the code that waits for us to press the spacebar. If the spacebar is pressed the next instruction is an RTS which means return to where we left with SYS49152 hence return to BASIC. The program has finished executing. When we run this program with as always from now on SYS 49152, we get the below. The program will wait for you to press the spacebar to give you the READY prompt. Try modifying the program to have different colour for border and background. Here is the full basic listing of the first assembly language (no copy and paste sorry allowed 😊 ) Second Step – Code O2 Let pass to tutorial 02. This code creates the same effect that you see in many loaders. To be fair it is simpler than the previous tutorial. We only have one Instruction which is INC \$D020. Basically, we are incrementing the value at \$D020. It doesn’t matter that the increment passes the colour no 16, the high nibble of the byte is ignored and hence only the first 4 bits are used which gives us the color from 0 to 15. When the value of \$D020 reaches \\$FF it starts again from zero.

Remember run your assembled code with SYS 49152 and to press the spacebar to exit from the code.

Here is a full listing of all the code.

Next Week

That is, it for this week. These tutorials should get you started in typing some assembly code. Learn the format of assembly. Get used to some of the errors the assembler display. Learn to use the help of CBM Prg Studio to understand what it is trying to tell you.

Next week will examine different ways of the first routine that we need to have and know for our game. It is used frequently in a game.

As always if you have any issues please message me. That is all for this week.

Coding is Fun 😊