Assembly Language

Assembly Language

So if you couldn't tell I'm actually a computer programmer by day. By night I'm a bit of a drag mother, who loves board games, mysticism, mystic artistic expression and to engage in philosophical and religious study. (Yes I am all over the place.) But ... computer programmer.

When program computers, I often find that I work with high-level languages which do a lot of stuff for me. A perfect example of this is memory management and garbage collection. So often I don't have a good idea of what the high-level language is doing for me.

Having spoken to friends who teach computer science at some of the best universities in the world, I have found one thing to be quite consistent. They like to take a bottom up approach. They go from electronics and switches to device style programming to high-level languages. I am trying to fill a hole in me that is left by the insecurity of not having the best degree for Computer Science, Law. Law enables me to engage with the customer and fully coordinate a meeting and make sure what we deliver meets exactly what the customer said they wanted, but I am often coding without a CS degree and that's a bit like building a house on sand at times.

I hope to fill that gap with a number of different small projects like programming arduino projects, metal coding and assembling coding. Doing an Udemy course on programming for the Atari seemed a great place to start. I also have an actual Sega Mega Drive and a course from pluralsite on the memory registers of that so I should be able to take what I learn and apply it to the mega drive... but here goes.

This is the course I am working through:

https://www.udemy.com/course/programming-games-for-the-atari-2600/

Spec

When coding in assembly language, you're writing specifically for the hardware you have so you need to be aware of the processor you have and all the other hardware and how it can be referenced from the motherboard. This becomes a crucial part of programming in assembly.

One of the crucial parts of programming is knowing the CPU which in the case of the Atari was the MOS 6507 (a cheaper version of the MOS 6502), which had 1.19 MHz. In the Sega Mega Drive it was the Motorola 6800 after the price had come down a bit.

Atari also had the Television Interface Adapter chip (TIA). This allowed for interaction with a cathode ray tube television in place of monitor used for modern computers.

The Atari had 128 bytes of RAM in the 6532 RIOT chip (RIOT = RAM input output and timer)

The Atari is used with a Cartridge being plugged directly into the motherboard which is a ROM of 4kb.

There are different versions of the Atari, but the Udemy course is focused on the "heavy sixer" which is version of the machine which was released in 1977 which has 4 switches on the console.

CPU

The CPU has a pin to indicate it is ready for an action it receives an incoming current and it is often indicated with RDY on diagrams.

There is also an interruption request pin indicated IRQ. In using the cheaper 6507 CPU there is not IRQ pin because it is not needed. To make the 6507 cheaper than the 6502 the interruption request was removed, NMI was removed and the address bus was slight more limited in size.

Registers. These are memory inside the CPU for current. We often refer to the Atari 2600 as an 8 bit machine because it can store 8-bit values in each register (1byte).

The data bus is 8 pins (or more in a 16/32/64-bit machine) is designed to put data in and out of registers.

The address bus is 16 pins sticking out of the CPU which indicate a memory location where something might be stored in ROM or RAM etc.

There are 2 chips for tv interaction (TIA), one is for NTSC TVs and the other for PAL TVs.

Additional pins tell us whether we are reading or writing to the CPU, some are for timers, resets, power and grounding. There is also the request bus ready state pin, don't ask me yet what this does!

The CPU we are using has 1.19 million ticks per second.

Inside the CPU we are using here is 7 special areas. The pins can be used for input an output into the various areas. Those areas are the ALU and 6 addressable registers. A register is like a very fast access memory for a CPU.

ALU

The first part is the Arithmetic Logic Unit (or ALU). This is capable of a number of very simple addition, subtraction and binary operations. For multiplication and division it might have to do multiple operations.

PC

The register marked as PC is the "Program Counter". I think this part of the CPU indicates where the program is going to go next. It might indicate what line of code to run next for example. I am not sure about a lot of this information so don't learn from me go to Learn Assembly Language by Making Games for the Atari available on Udemy. It's a cheap fun course with all the information you will need and it will be organised in a more sensible order for learning.

This contains memory addresses so in our 6507 processor this is 16 bits long.

SP

The next part is known as the "Stack Pointer". When using the stack in memory we have a pointer to indicate where the latest value in memory is. Let's imagine you're adding to memory like pushing to an array in JavaScript, the SP will always point to the latest value and if you pop values off the stack it will retreat to previous values.

This contains memory addresses so in our 6507 processor this is 16 bits long, but the last 8 digits are always "00000001...".

P

This next one is the Program flags. Instead of being a binary sorted number, this is a series of binary flags which can be switched on and off to indicate anything that happened in the last calculation run by the CPU. This could have a flag on to indicate that it failed in the previous calculation for example overflow where the calculated value was too big to be represented in the binary values available.

This could include whether the result was negative or zero. This contains 8 flags so is 1 byte in size.

The right most bit is called the "Carry Flag". It determines whether during an addition it was required to carry a flag over to the next binary number, but could not do so because there are only 8 bits the carry flag is switched to 1. For example if you take the maximum number and added one then it would carry that 1 over when it completes the number and it would be too big to save.

The second to right flag is the "zero" flag. (XXXXXXX1X where X is a number that relates to a different flag) this would indicate that the result of the previous calculation was zero.

The third to right flag is used for IRQ disabled. Since this part of the process is not used it's not relevant to this. (XXXXX1XX).

The forth flag is BCD (binary coded decimal). This is an accurate form of calculation, but it is slow so it is not always used. It indicates whether we were using BCD mode or not.

The fifth flag from the right is the break, which indicates a break signal caused the processor to interrupt what it was doing. (XXX1XXXX). Warning this might not be correct. Learn from the course; not from me!

The 6th flat form the right is unused by the processor just ignore that value.

The 7th flag from the right indicates that an overflow occurred and the value was unable to store the result of the arithmetic operation. It can be represented by a V. An example of this is when adding to a number written in two's complement (where the 8th digit is negative) is the positive numbers are carried over to the next column and it's a negative number. For example 0000 0001 + 0111 1111 = 1000 0000 which means 1 + 127 = -128 so this flag is activated to note that answer might not be quite right.

Finally the 8th flag (N) indicates whether the result was a negative number. (1XXXXXXX)

X,Y and A

The X and Y registers are general-purpose registers, which we can use for storing the data that we are working with. The A register is the accumulator, which is used by the ALU for doing work on the data. When the ALU performs a calculation one of the values comes from the accumulator.

These registers are all 8 bits in the 6502 / 6507 processor.

Binary

Obviously an important part of programming for electronics is understanding how a wire essentially can have a current or not and that is its state of 0 or 1, but you also need to understand how this can be abstracted as a number.

I already knew this from my A-level and GCSE Computing. But a quick review of it was useful.

Graeme