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.
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:
we would have the assembly language code:
This might not exactly be quite as easily readable as Python or Basic, but it is a lot better than pure binary.
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:
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.
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 2021