Assembly Language is used as a convenient way for humans to work with the opcodes (discussed in Chapter 1) required for a microprocessor to function. This is a step up from having to work with the pure binary in machine code. Assembly language contains three character mnemonics which represent each opcode and a number base of hexadecimal is used to represent the data. I told you the numerical concepts discussed in Chapter 2 would be useful!
The Assembly code is then converted by a program called the Assembler into binary machine code. This can then be run on the C64’s microprocessor.
You may be familiar with several high level languages such as C or C++ and wondering why working with Assembly code is such a big deal. In assembly language, each mnemonic maps directly to a single microprocessor opcode. This gives the programmer a complete level of control. It is, however, longer to code a project using this method.
Another language option on the C64 is an interpreted language called BASIC. It’s termed as an interpreted language as each command is translated to machine code as the program is running. Although easier for a human to code, it executes much slower than anything written in assembly language.
There’s a few important subsets of instructions to cover for the C64. These will help you understand the nature of how the C64 uses Assembly.
The most commonly used mnemonics are:
- lda – Load the Accumulator
- sta – Store the Accumulator
These instructions will transfer a byte of data between the Accumulator (also known as the A register) and one of the processors available memory locations.
When specifying numbers in assembly, the prefixes $ for hexadecimal and % for binary are used. If no prefix accompanies the number, this indicates that it uses decimal.
The # symbol is used to specify a numeric value. If this is not present we are referring to a memory location (address).
Anything following the ; symbol is a comment and is ignored by the assembler. Comments are used by programmers to explain their lines of code. The symbol can also be used at the start of a line of code to disable it. This is known as commenting out the code.
Lets look at an example of a program written in Assembly.
Code Example – Loading and Storing Numbers
The first line loads the number 2 into the Accumulator (the A register).
The second line stores the value in the Accumulator into the memory location specified by the address ($0400).
The output in hexadecimal is A9 02 8D 00 04 and puts a letter B onto the screen at the top left character position.
Code Example – Copying Numbers
This piece of code copies a value from one memory address (hex 400) to another (hex 401). Notice that you can’t copy a byte of memory directly from one memory location to another. You must go through one of the microprocessor registers.