By looking at some simple examples, we can learn how to write Assembly Language code.

Lots of old tech — I don’t think anything shown actually uses a 6502, but the options on unsplash are limited…

Photo by Lorenzo Herrera on Unsplash


Assembly is a very low-level programming language - everything you write is “close to the metal”. What this means is that the language is very related to the hardware it is on. In this article, we will learn how to write a variant of Assembly language. Specifically, we will be looking at 6502 Assembly Language. The 6502 is a classic processor, found in lots of old tech. Don’t let age worry you though, the core tenets of Assembly will be the same whether we are looking at a 6502 or and intel i7.

To learn how to write this code we will take a look at two small examples. The examples have been picked to give us a brief idea of the basic features of the language. We will also look at how we can make our code more readable by using features that the Assembler can provide us.

Technically, this is Part 4 of a “learning assembly” series I am writing. However, wherever possible I am trying to ensure each part is independent. If all you’re interested in is getting the basics of assembly, hopefully, this will be suitable for you as well.

Let’s begin with a very brief recap of the 6502 hardware.

A Small Recap

An Assembly Language could be defined by its instruction set. An instruction is a command that the processor will be able to interpret (like add, or to move something around in memory). The instructions available to us are deeply linked to the hardware itself. Using a 6502 processor gives us access to around 50 instructions. Outside of these 50 instructions, if we want the processor to do something, we will need to create it ourselves.

A deeper dive into the 6502 hardware is available here, but we can go over some things that may be important for us today:

The Accumulator: This is the part of the 6502 hardware that can help us do arithmetic. If we wanted to add two numbers, we could send them into the accumulator, where they would “accumulate” together.

The Carry Register: Imagine if we did the sum 26 + 15.

 (1) <- our carry
  26
+̲ ̲1̲5̲
  41

Well, we’ve added 5 and 6, which gives 1, carry-a-1. Then we could do 20 + 10 + the-10-we’ve-carried-over to give 40. Combining these we get the answer of 41.

Well, when we add binary numbers something similar happens — we still carry things. However, in 6502 we can only keep track of things that fit in an 8-bit number. If we do some maths that results in us carrying something that won’t fit into 8-bits, we need to keep track of this. For example 1000 0001 + 1000 0000 gives 1 0000 0001. However, we cannot fit the left-most 1 we had to carry into 8-bits, but, we also cannot forget about it if we want our maths to work.

This concept of carrying a number if it falls out of 8-bits is handled by the 6502 hardware in the Carry register. It’s just something we’ll try to keep in mind when we do maths.


Addition

Our easiest example will be 1 + 2. Here’s some code:

LDA     #01      
ADC     #02      
STA     $0402

On the left of each line is the instruction while on the right is the data or memory location needed to make the instruction work. This style is how Assembly language is written. Let’s go through the example one line at a time.

Line 1LDA is telling the 6502 to load the piece of data that follows into the Accumulator (A). Here, it is #01. We can prefix our data with a symbol to tell the assembler what sort of data we are passing it. In this case, we are using a hashtag which means our data is a ‘literal’ - we are literally passing it a number. After this line, the contents of A is 1.

Line 2ADC means to add the following value onto whatever is currently in the Accumulator. The ‘C’ in the instruction tells us it will be keeping track of any carrys that have happened in previous calculations. Here, we assume C = 0. Therefore, all we are adding to the Accumulator is#02. Now, the value in A is 3.

Line 3: The STA instruction will send the contents of the Accumulator to a location in memory. In our example, this will send the value 3 to the memory address $0402. Note, we have not used a # - this is because it is not literal, it is an address. Furthermore, we have used $ which tells the assembler this value is in binary.

After these three instructions, we have successfully done 1 + 2 — well done! If we wanted to view the answer we would have to look in $0402.

#learning-to-code #programming #software-engineering #technology #science

Let’s Write Some Assembly Language Code!
2.30 GEEK