SECTION 2.2: INTRODUCTION TO 8051 ASSEMBLY PROGRAMMING
In this section we discuss Assembly language format and define some widely used terminology associated with Assembly language programming.
While the CPU can work only in binary, it can do so at a very high speed. For humans, however, it is quite tedious and slow to deal with Os and Is in order to program the computer. A program that consists of Os and Is is called machine language. In the early days of the computer, programmers coded programs in machine language. Although the hexadecimal system was used as a more efficient way to represent binary numbers, the process of working in machine code was still cumbersome for humans. Eventually, Assembly languages were developed that provided mnemonics for the machine code instructions, plus other features that made programming faster and less prone to error. The term mnemonic is frequently used in computer science and engineering literature to refer to codes and abbreviations that are relatively easy to remember. Assembly language programs must be translated into machine code by a program called an assembler. Assembly language is referred to as a low-level language because it deals directly with the internal structure of the CPU. To program in Assembly language, the programmer must know all the registers of the CPU and the size of each, as well as other details.
Today, one can use many different programming languages, such as BASIC, Pascal, C, C++, Java, and numerous others. These languages are called high-level languages because the programmer does not have to be concerned with the internal details of the CPU. Whereas an assembler is used to translate an Assembly language program into machine code (sometimes also called object code or opcode for operation code), high-level languages are translated into machine code by a program called a compiler. For instance, to write a program in C, one must use a C compiler to translate the program into machine language. Now we look at 8051 Assembly language format and use an 8051 assembler to create a ready-to-run program.
Structure of Assembly language
An Assembly language program consists of, among other things, a series of lines of Assembly language instructions. An Assembly language instruction consists of a mnemonic, optionally followed by one or two operands. The operands are the data items being manipulated, and the mnemonics are the commands to the CPU, telling it what to do with those items.
Program 2-1: Sample of an Assembly Language Program
A given Assembly language program (see Program 2-1) is a series of statements, or lines, which are either Assembly language instructions such as ADD and MOV, or statements called directives. While instructions tell the CPU what to do, directives (also called pseudo-instructions) give directions to the assembler. For example, in the above program while the MOV and ADD instructions are commands to the CPU, ORG and END are directives to the assembler. ORG tells the assembler to place the opcode at memory location 0 while END indicates to the assembler the end of the source code. In other words, one is for the start of the program and the other one for the end of the program.
An Assembly language instruction consists of four fields:
Brackets indicate that a field is optional, and not all lines have them. Brackets should not be typed in. Regarding the above format, the following points should be noted.
The label field allows the program to refer to a line of code by name. The label
field cannot exceed a certain number of characters. Check your assembler for
The Assembly language mnemonic (instruction) and operand(s) fields togeth
er perform the real work of the program and accomplish the tasks for which
the program was written. In Assembly language statements such as
ADD A,B MOV A,#67
ADD and MOV are the mnemonics, which produce opcodes; and “A, B” and “A, #67″ are the operands. Instead of a mnemonic and an operand, these two fields could contain assembler pseudo-instructions, or directives. Remember that directives do not generate any machine code (opcode) and are used only by the assembler, as opposed to instructions that are translated into machine code (opcode) for the CPU to execute. In Program 2-1 the commands ORG (origin) and END are examples of directives (some 8051 assemblers use .ORG and .END). Check your assembler for the rules. More of these pseudo-instructions are discussed in detail in Section 2.5.
The comment field begins with a semicolon comment indicator “;”.
Comments may be at the end of a line or on a line by themselves. The assem
bler ignores comments, but they are indispensable to programmers. Although
comments are optional, it is recommended that they be used to describe the
program and make it easier for someone else to read and understand, or for the
programmer to remember what they wrote.
Notice the label “HERE” in the label field in Program 2-1. Any label referring
to an instruction must be followed by a colon symbol, “:”. In the SJMP (short
jump instruction), the 8051 is told to stay in this loop indefinitely. If your sys
tem has a monitor program you do not need this line and it should be deleted
from your program. In the next section we will see how to create a ready-to-