Assembly language


Martin McBride, 2017-04-30
Tags none
Categories none

We have looked at machine code, which represents the instructions a CPU can read an execute directly.

Assembly language is a way to make machine code a bit easier to read. This also makes it a bit easier and quicker to write and debug.

You use a program called an assembler to convert the assembly language.

Mnemonics

The basic idea of assembly language is to use a short keyword (called a mnemonic) to represent each instruction, rather than the binary number that the CPU understands.

Mnemonic means a system such as a pattern of letters, ideas, or associations which assists in remembering something.

So for example, we might use the keyword LDX to mean "load a value into the X register". So instead of the hexadecimal byte values:

A2 20

we would have the assembly language code:

LDX #$20

This might not exactly be quite as easily readable as Python or Basic, but it is a lot better than pure binary.

Other features of assembly language

This code is just to show what assembly language looks like. The majority of programmers never write any code in assembly language!

Here is a short listing of assembly code to calculate Fiboncci numbers:

        LDA #$02  ;a = 2
        STA $01   ;stores a

loop1:  LDX $01   ;x = a
        ADC $00   ;a += x
        STA $01   ;stores a
        STX $00   ;stores x
        DEY       ;y -= 1
        BNE loop1 ;jumps back to loop1 if Z bit != 0

This indicates some extra features most assemblers support:

  • Comments. Anything after the semicolon in a line is a comment, and is ignored by the assembler.
  • Labels. loop1 is a label, and marks a location in memory.

Labels are very useful. The BNE instruction (branch if not equal) jumps to a particular location if the previous calculation gave a non-zero result. It is similar to a while loop. We want to jump back to the location of the LDX instruction. Using a label, we can get the assembler to automatically calculate the address.

Properties of assembly language

As you have seen, each instruction in assembly language maps directly onto an instruction in machine code. So assembly language is still considered a low level programming language. But it is an advancement over raw machine code, so it is termed a 2nd generation language.

Like machine code, assembly language can be used to create highly efficient code which executes quickly, uses as little memory as possible, and can directly access the computer's hardware devices.

The instruction set is specific to the type of processor, and code would not be easily ported to a computer with a different type of processor.

Copyright (c) Axlesoft Ltd 2020