
Why learn machine language?Machine language is a way of directly communicating with your C64 computer at the lower level. It's like being fluent in English and learning a secondary language like German and being able to communicate to that person even though it is not your primary language (such as Basic may be). Machine language is also super fast! It's like driving an average vehicle with a top speed of about 100 or getting behind the wheel of a ferrari (Basic to Assembly language) all packaged with nice luxuries. It spoils you when you discover its' raw power! You can perform functions at lightning speeds, write your own compiler, enable smooth raster effects, run music in the background, create a game like Double Dragon, activate fine scrolling, and much, much more. It is very, very powerful. How computers perform mathAccording to the book Machine language for beginners by Richard Mansfield "computers count in groups of twos". They use an ON/OFF state such as turning a light bulb on or off. When it's on a 1 is stored there and an OFF results in 0. How does this relate to computers regarding math? Well computers use powers of 2 to perform calculations. Take for example 2 to the power of 2 and the result is 4. Then 2 to the power of 3 will be 8 and so on. Keep raising the "power number" and you will start to see how the math works. Computer binary math
10 FOR I=0 to 16 20 PRINT 2^I 30 NEXT I 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. What is the 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 0255. Each byte is also comprised of 256 numbers (0255) 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. The hexadecimal systemWith the hexadecimal system, Richard Mansfield also talks about hex numbers "in terms of binary numbers  the oneoff, 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 8bit 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 hexadecimalTo 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 AF. 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 09, 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 hexadecimalObserve 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 bytesNo 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 bytesRichard 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 07. 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 065535. 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 activationTo 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 addressThe 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 bytesNo 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 programA 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 nonexistant 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. Coming soon  machine language instructionsPlease stay tuned as we will eventually be discussing instructions (mnemonics) such as LDA, STA, AND, CMP, ADC, SBC, etc. The best is yet to come! Online ForumFree free to ask questions below or share information. This is your public forum. Thank you. Subscribe to the Programmermind newsletter 