# Commodore 64

Machine Language

## How Binary bits work on the Commodore 64

Bits are grouped to form a byte (8 bits together). When you assemble a large group of bits (o's and 1's) you are then looking at a whole number such as 16, 128 or 255.
In order to understand this you will need to know how your computer forms letters. Each letter is formed using bits. Look at your Commodore 64 keyboard or even a PC/Apple keyboard. Each letter corresponds to a different number. The letter "B" is 66 (binary 1000010) and so on. The developers of the 6502/6510 processor assigned each letter and characters using a system called ASCII (American Standard Code for Information Interchange) that is used by many computer manufacturers. ASCII consists of a standard character set from 0-255. Each byte is also comprised of 256 numbers (0-255) to perform many different things.

Bits are groups to form a byte (8 bits together). When you assemble a large group of bits (0's and 1's) you are then looking at a whole number such as 16, 128 or 255.

Machine language programming can use either a decimal or hex based system. Hex is easier to use than decimal since it is arranged in groups of 16 rather instead of 10. According to the book also “all computers work, at the most elementary level, with bits”. These bits are the smallest numbers used by your computer and when combined with other bits you get a binary number. An example of the binary number 16 would look like 00010000.

## What are Bits?

Bits are grouped to form a byte (8 bits together). When you assemble a large group of bits (o’s and 1’s) you are then looking at a whole number such as 16, 128 or 255.

## Purpose of ASCII

In order to understand this you will need to know how your computer forms letters. Each letter is formed using bits. Look at your Commodore 64 keyboard or even a PC/Apple keyboard. Each letter corresponds to a different number. The letter “B” is 66 (binary 1000010) and so on. The developers of the 6502/6510 processor assigned each letter and characters using a system called ASCII (American Standard Code for Information Interchange) that is used by many computer manufacturers. ASCII consists of a standard character set from 0-255. Each byte is also comprised of 256 numbers (0-255) to perform many different things.

Bits are groups to form a byte (8 bits together). When you assemble a large group of bits (0’s and 1’s) you are then looking at a whole number such as 16, 128 or 255.

### Hexadecimal System

With the hexadecimal system, Richard Mansfield also talks about hex numbers “in terms of binary numbers — the one-off, single bit way that the computer handles numbers”.

Each hex number is starts with a dollar ($) sign to notify the computer a hex number will be used rather than decimal. As an example the number 16 in hex is 10. He states if you divide a hex number in two it “looks like binary” which is a 8-bit grouping of bytes into two parts. Don’t worry about figuring this out. The C64 Studio editor allows you to see a number broken down as a Decimal, Hex, or Binary. Just click on the Window menu and select Calculator. A hexadecimal; digit groups by 16 instead of by 10. To calculate a hex value you take the first number and multiply it by 16. Example: Hex: 128 (80) is 16*8 = 128.

## Counting by Hexadecimal

To count by decimal you would start at 0 and go to 9. When you get to 10 you start over with 10 and use the letters A-F. It may take some time to wrap your head around this, but just know it is more efficient to work with hexadecimal over decimal. Since there are only the numbers 0-9, developers had to come up with a better numbering system that was easier to understand. As I stated earlier don’t worry about performing these calculations as there are many tools to do this. You can even use the windows calculator in Programmer mode to do the same thing.

## Analyzing Hexadecimal

Observe a hexadecimal number such as D020 (53280). Now if you divide it into 2 halves such as D0 and 20 and the same with a binary number (1 and 0) you can see the relationship between them.

## A Lesson in Bytes

No we are not talking about a vicious dog bite here. From the computer’s perspective each byte is consisted of 8 bits (remember the binary stuff). Machine language can only work with hexadecimal or decimal numbers between 0 through 255. According to Machine Language for Beginner’s this is because “a single byte (eight bits) can hold no number larger than 255.”

## Memory as a City of Bytes

Richard Mansfield provides an illustration of computer memory by having us imagine a row of homes lined up next to each other. It is in the Christmas season and each house (address) contains a row of bulbs on its roof. Each house is a byte he states and the individual lights are bits. The bits are numbered from 0-7. At first each home owner has their lights turned off (binary 0). However down over the hill is an active community known as ROM (Read only Memory). ROM has houses that light up like a seasonal christmas gathering. This is because ROM already has active bits assigned for specific tasks. The houses are numbered from 0-65535. Since the owners live in the RAM community as of yet there are no lights illuminated. Take for example byte 49152. For my own scenario here let’s pretend that the owners have special switches inside that give them control over the lights. There is a agency (CPU) that can give owners the ability to activate specific lights. Their organization also manages memory by sending out Messengers to the addresses requesting activation. You are a CPU messenger and stop at house 49152. You present the owner with a notice that grants them permission to turn on selected individual bits. The owner examines the document which advises him or her that the Central Processing Agency has authorized a grant to turn on bulbs (bits) 7, 5, 3, and 1 (10101001). Suddenly 4 bulbs are glowing and the CPU Agency (computer) has received notice that a POKE 49152,169 is now active in memory. Ok so maybe this isn’t the greatest example, but I was trying to paint a picture for you.

## Understanding Byte Activation

To grasp this concept it is necessary to divide the bits into numbers. Recall earlier that we used the example of 2 to the power of 2 = 4. Each of these powers is how the computer is able to activate the bits. So overall the bits are translated as Bit 7=128, Bit 6=64, Bit 5=32, Bit 4=16, Bit 3=8, Bit 2=4, Bit 1=2, Bit 0=1. Look at it also as 128 64 32 16 8 4 2 1. So the 169 is calculated by taking bits 7, 5, 3, and 1 and adding them together. So we get 128+32+8+1 = 169. Hopefully that clears things up.

## Construction of an Address

The author of Machine Language for Beginners states that two bytes together form an address. An address consists of two bytes together. The total of the two bytes cannot exceed 65535. Each byte can only hold up to 255 numbers. The math to combine these together consists of a high and low byte. Just know for now that these memory addresses perform different functions. As an example memory location 53265 activates vertical fine scrolling, 53269 turns on game sprites, 53280 changes the screen border, 54272 with 54273 controls the frequency of music, 56333 is used for raster interrupts, and so on.

## A Lesson In Bytes

No we are not talking about a vicious dog bite here. From the computer’s perspective each byte is consisted of 8 bits (remember the binary stuff). Machine language can only work with hexadecimal or decimal numbers between 0 through 255. According to Machine Language for Beginner’s this is because “a single byte (eight bits) can hold no number larger than 255.”

## Using a Monitor Program

A C64 Monitor program such as C64 Studio or Turbo Assembler allows you to communicate directly at the machine language level with your computer. When you are using a monitor, Basic is non-existant and you start dealing directly with CPU using 3 letter instructions. These “instructions” put together form a program that the computer can execute in memory.

## That's the End for Now

I hope you enjoyed this brief introduction of Machine Language. I encourage you to find a good book and beginning learning the basic about building simple code programs and working your way up.