Introduction to assembly language programming

Introduction

Computers at low level use the binary number system made up of 0's and 1's and while it's possible to program using binary, assembly language is more commonly used. Assembly language uses mnemonics to represent the instructions. For example, instead of having to write:

10101001 01110001

You can write:

LDA #113

You can email me at james.boshikoopa@gmail.com

To return to the main page please go to Programming.

In this example, 'LDA' is the mnemonic and stands for 'LoaD Accumulator' and is easier to remember than group of 0's and 1's above. The mnemonics vary between each processor and some will have more instructions than others or much more powerful ones.

While assembly language is much more difficult to use, both when programming and debugging, it is of use when high speed and low memory usage are important. Especially in the days of the microcomputers, if you wanted to write a game or exploit some feature of the computer, assembly language was the way to go. Nowadays, however, C/C++/C# is more common, with possible a bit of assembly language included for parts of a program than need to be run very fast. Assembly language also finds use in microcontrollers (a simple computer on a chip) which typically have very little memory and thus can greatly benefit from being programmed in assembly language to make good use of the limited resources.

A good point to remember is that regardless of what programming language you use, it gets converted to machine code (0's and 1's). Therefore if you program in assembly language there is no conversion (the mnemonics are easily swapped for the values they represent), and because of this you get greater speed and make better use of memory (at the cost of greater difficulty in programming).

Another thing to mention is that assembly language gives a programmer a more intimate relationship with the hardware. At such low level you have to work with the CPU and other hardware registers, something not usually dealt with in higher level languages. You may have to program your own routines to work with large numbers, for example, which would be taken care of by other programming languages. But in this way you learn greatly how the hardware works.

The 68000 CPU

I used to program an Amiga 500 computer in assembly language and this gave me very good exposure to the 68000 CPU and the other internals of the Amiga. The 68000, by Motorola, was a (for its time) very popular CPU that internally was close to being a 32-bit CPU but yet only has a 16-bit data bus (hence the 68000 commonly being referred to as a 16/32-bit CPU).

To give you a taster for the 68000, I've included below a snippet of a Pac-man clone I wrote in assembly language for the Amiga 500:

Right_pressed Lea Puc_dat,A0

Btst.b #1,Puc_Stat ;What frame is it?

Beq.s right_mouth_open

;right_mouth_closed Adda.w #68,A0

Right_mouth_open Move.l A0,D0

Move.w D0,30(A1)

Swap D0

Move.w D0,26(A1)

Already_facing_right Lea Tile_map,A0

Lea Puc_dat,A1

Moveq #0,D0

Move.b (A1),D0 ;Y coord

Subi.b #44,D0

Divu #16,D0

The particular piece of code above deals with responding to joystick button presses which in turn move and animate the player graphic. The code is split into three parts; labels on the left, instructions and associated data in the middle and comments on the right. While you can probably work out what some of the instructions do, without the labels and comments it would have even less meaning.

The code you see above is not what the CPU sees as such. The program I used to write the code has to convert the mnemonics into the values they represent, and convert the labels into addresses; comments are ignored, they are there only for the programmer. This all happens before the program is run and if there are any errors they will be shown.

All content of this and related pages is copyright (c) James S. 2009-2014