cwave.eu5.org
Also see: http://www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com
Free Guestbook
My Guestbook

Last updated on Fri May 21 11:47:49 IST 2010.

Assembly language tutorial for 8051/52

The most important feature of this tutorial is a free webbased simulator.

This tutorial presupposes a basic understanding of some high level language like C or Java.

While learning a high level language one is invariably told that down under the hood a computer uses nothing but numbers to do all its processing. Thus you may not be surprised if I tell you that a C expression like

x - y

when translated to machine language becomes something like

100 4 45

where the 100 represents "subtraction", the 4 denotes the location for the variable x and the 45 denotes the location for y.

I am sure that you'll agree with me that writing the machine language is a lot more difficult than the corresponding C code. There are two reasons:
  1. The machine language has no mnemonic value. You just have to remember that 100 denotes subtraction. We human beings love to use names and symbols rather than numbers to denote things.
  2. There is nothing in the C code to tell us that x is to be stored at location 4 and y at location 45. There is a great deal of arbitrariness about this choice. Yet, it is not completely arbitrary, for example you must make sure that your choice of location for one variable does not clash with another.
When we compile a C program the compiler takes care of these two things for us. It replaces mnemonic symbols like "+" or "*" with appropriate machine language numbers, and also chooses the locations for variables etc for us. Notice that replacing mnemonic symbols is a dumb routine job, while deciding the variable locations etc require intelligence, and the artificial intelligence of a compiler may not always come up with the best possible choice.

An assembly language comes inbetween. In an assembly language you, the programmer, supply the intelligence, but you can use mnemonic symbols. Thus a typical assembly language version of the C code could be

SUB 4 45

Here you are choosing the locations (based on possibly very carefully worked out scheme). But you are not required to memorise that 100 stands for subtraction. A piece of software called an assembler will replace the SUB with 100.

Why assembly language is often difficult to learn

Learning assembly language poses considerably more difficulty than learning a high level language like C or Java. These difficulties stem from two main sources:
  1. Dependence on the hardware: We can talk about learning the C language, but we cannot talk about learning the assembly language, as different microcontrollers and microprocessors have different assembly languages. So one needs to know at least the basic architecture of the microcontroller/processor in question, and also need to have some way to try out assembly programs in that hardware, or at least a simulator.
  2. Dependence on the assembler: This is apparently the greater hardle. Assembly language depends not just on the microcontroller/processor but also on the particular assembler used. Thus, to be completely honest, we cannot really have a tutorial on assembly language for the 8051/52 microcontrollers. There are different assembly languages out there for the same microcontroller. These are all very similar in principle, but yet so very different in implementational details that you cannot use an assembly program written in one dialect and use it with an assembler meant for another. For example, one assembler may need
    
    SUB 4 45
    
    
    while another may need
    
    SUB 4, 45
    
    
    while yet another may expect the operands to be presented in the reverse order:
    
    SUB 45 4
    
    
    The choice of the mnemonic SUB is just an arbitrary one made by the guy who wrote the assembler. So you may even come accross an assembler that expects
    
    SUBW 4 45
    
    
    to mean the same thing! This is a great source of confusion for a beginner.
Assemblers, as you can possibly guess, are rather easy to write. After all, unlike a compiler, an assembler has no built-in intelligence. It merely replaces mnemonics with machine language numbers. As a result there are a great many assemblers out there with different levels of sophistication. Many of them do not even have a proper name, let alone a decent documentation. Thanks to this lack of standard, nobody writes an assembly language book with a specific dialect in mind. Books talk about the abstract principle using abstract code snippets (which follow some unnamed dialect that the author likes).

While learning C or Java it is a simple matter to install a compiler, copy-n-paste an existing "Hello World" program and run it to get a first taste of success. A similar things is extremely difficult to achieve in the world of assembly languages. If you download a simulator from one website and blindly type some assembly code from your book into it, you are bound to get some wierd error message due to mismatch of dialects.

A solution

In order to learn assembly language it is imperative that you have three things:
  1. A document that teaches the basic principles with examples
  2. A simulator that understands the dialect used in the book
  3. A real hardware setup where you can finally try out the code.
It is extremely difficult to get these three things simultaneously outside a regular assembly language class. And this is a bad news for teach-yourself people like myself. Indeed, after a lot of failed attempts to learn assembly language by myself, I had to finally resort to regular course work.

What this tutorial offers

This tutorial is an effort to bring assembly language as close as possible to teach-yourselfers. It is modelled closely after the book by Jonathan Valvano, where the author provides a wonderfully versatile simulator with his book. Since he is the author of both the simulator as well as the book, there is no dialect mismatch.

In this tutorial I have provided a free online simulator (nothing to download/install, just run it inside your browser) and list of lessons. The hardware that I am using is 8051/52. Two microcontrollers in this family are AT89S51, AT89S52 produced by Atmel. These are widely available and extremely cheap.

This tutorial does not tell you how to set up a hardware lab at home, but it is possible and does not cost much. I do have another tutorial here about doing that. However, the two tutorials are independent of each other.

Here is the table of contents for lessons:
Introduction
This page
Lesson 1
Your first taste of assembly
Lesson 2
Some arithmetic
Here is a link to the simulator. This is where you'll spend most of you time. It might look something like the control room of a spacecraft at first sight, but it is really not that daunting!

Next
© Arnab Chakraborty (2010)