C64 Bedtime Coding (Eng) – Machine Language (#02)

(Article by Phaze101)

Machine Code


What is Machine code

So, what is machine code and how it is written?

Here is a simple 6502 program

A2 01
8E 84 03
A9 02
6D 84 03
8D 85 03

As you can see it has no meaning to the human being. Well let me rephrase that. It has no meaning to most human beings, but I am sure that there are some 6502 Gurus out there that will be able to translate that.

The above is the extreme and we will never write such a program thankfully.

It is what the computer understands. Of course, they will be translated into binary (Not Hex) or better +5V and 0 but that is to deep down the line.

So, let’s make things a bit simpler.

Let’s do some BASIC

Here is a simple basic program.  Nothing special, this is a very simple Basic program. If is declaring X and Y with a value. Then it is just adding X + Y and storing the result in A. Then it prints contents of A which is the result.

10 LET X = 1
20 LET Y = 2
30 A = X + Y

What happens when we write such a Basic program? Well, when we tell the computer to run this program the computer will translate the above into Machine Code and execute it for us. Of course, this is a slow process because of the translation that must happen before the machine code is execute.

Please note I am trying to keep it simple here and not going into detail so as not to complicate things for beginners but in fact the process is more complex than what I explained.

What is Assembler?

An assembler is a program that helps the human being write assembly language.

Assembly uses mnemonics (represented by 3 characters in the case of the 6502) to represent CPU instructions. You write assembly using these mnemonics.

Here is the assembly program that does practically the same as the Basic program mentioned earlier.

LDX   #\$1        ;Load the X Register with the value of 1
STX \$C000      ;Store the value of X in memory location $C000
LDA #\$2        ;Load the A Register with the value of 2
ADC \$C000      ;Add the value of X store at $C000 to the A register
STA \$C001      ;Store the result in memory location $C001

For human beings this is more readable. As programmers writing in assembly, we just need to learn these instructions. The 6502 has 56 instructions and each instruction has variations.

If we wanted to run this program all we must do is to tell the assembler to assemble (convert it in this case) the above code into machine code.

Once this is done and there are no errors, we can execute it and it will run at the machine speed. No conversions need to happen since it is already code that the CPU understands.

Assembly Language Format

A line of a typical assembly language source program consists of several elements. Usually it is 5 or 4 columns. Most of the time column 1 is not there. Normally you jump from one column to the next using the tab which is normally set to either 4 space or 8 spaces. I personally prefer 8 spaces.

Columns Explanation

Column 1: The Line Number

Most assemblers do not require this however the assembler will either display a line number automatically next to the line of code or at the bottom / top you will see on which line you are. When an error occurs, the assembler will display the error saying “at line number XX” to indicate where the error is.

Column 2: The Label

The label must always start at column 1.  A label is used to tell the CPU where to go to when there is a branch, jump or jump to subroutine. The Label can also be by its own on the line.

Column 3: The Opcode

This is the assembly instruction such as LDA, STA or JMP. This is a requirement and should be in column 3 if we follow the 5 column format. It can also be on its own like CLC.

Column 4: The Operand

This can contain an operand such as #$01 or $0400. It can also be a label such as Main in which case column 3 would contain a JMP instruction (full instruction is JMP MAIN) or it can be any type of branch instructions. This column is not always required since some instructions do not have any operand.

Column 5: The Operand

An optional remark to describe what you are trying to do. The remark symbol is most of the time a semicolon (;) in most assemblers. Some assemblers can have a remark without the semicolon. I personally always prefer to use the remark symbol since it is clearer when reading it.

Next Week

This is all for the time being. I hope you enjoyed reading it and if you have issues please message me. In the next weeks article, I will be explaining the CPU registers.

For now, things are a bit slow since I must cover the theory, but I can assure you it will change.

Coding is fun 😊


All Articles



Article 3

Article 2

Article 1



Articolo 3

Articolo 2

Articolo 1