In the previous 8051 Microcontroller Tutorial, we have seen about the 8051 Microcontroller Instruction Set and Addressing Modes. In this tutorial, we will take a look at the 8051 Microcontroller Assembly Language Programming, the structure of 8051 Assembly Language, example programs, etc.
Before going into the details of the 8051 Microcontroller Assembly Language and Programming, let us get a brief idea about Programming Language in general (specific to Microcontrollers) and also types of Programming Languages.
What is a Programming Language?
Programming in the sense of Microcontrollers (or any computer) means writing a sequence of instructions that are executed by the processor in a particular order to perform a predefined task. Programming also involves debugging and troubleshooting of instructions and instruction sequence to make sure that the desired task is performed.
Like any language, Programming Languages have certain words, grammar and rules. There are three types or levels of Programming Languages for 8051 Microcontroller. These levels are based on how closely the statements in the language resemble the operations or tasks performed by the Microcontroller.
The three levels of Programming Languages are:
- Machine Language
- Assembly Language
- High-level Language
In Machine language or Machine Code, the instructions are written in binary bit patterns i.e. combination of binary digits 1 and 0, which are stored as HIGH and LOW Voltage Levels. This is the lowest level of programming languages and is the language that a Microcontroller or Microprocessor actually understands.
The next level of Programming Language is the Assembly Language. Since Machine Language or Code involves all the instructions in 1’s and 0’s, it is very difficult for humans to program using it.
Assembly Language is a pseudo-English representation of the Machine Language. The 8051 Microcontroller Assembly Language is a combination of English like words called Mnemonics and Hexadecimal codes.
It is also a low level language and requires extensive understanding of the architecture of the Microcontroller.
The name High-level language means that you need not worry about the architecture or other internal details of a microcontroller and they use words and statements that are easily understood by humans.
Few examples of High-level Languages are BASIC, C Pascal, C++ and Java. A program called Compiler will convert the Programs written in High-level languages to Machine Code.
Why Assembly Language?
Although High-level languages are easy to work with, the following reasons point out the advantage of Assembly Language
- The Programs written in Assembly gets executed faster and they occupy less memory.
- With the help of Assembly Language, you can directly exploit all the features of a Microcontroller.
- Using Assembly Language, you can have direct and accurate control of all the Microcontroller’s resources like I/O Ports, RAM, SFRs, etc.
- Compared to High-level Languages, Assembly Language has less rules and restrictions.
Also read this interesting article: 8051 MICROCONTROLLER INSTRUCTION SET
Structure of the 8051 Microcontroller Assembly Language
The Structure or Syntax of the 8051 Microcontroller Assembly Language is discussed here. Each line or statement of the assembly language program of 8051 Microcontroller consists of three fields: Label, Instruction and Comments.
The arrangement of these fields or the order in which they appear is shown below.
NOTE: The brackets for Label and Comments mean that these fields are optional and may not be used in all statements in a program.
Before seeing about these three fields, let us first see an example of how a typical statement or line in an 8051 Microcontroller Assembly Language looks like.
TESTLABEL: MOV A, 24H ; THIS IS A SAMPLE COMMENT
In the above statement, the “TESTLABEL” is the name of the Label, “MOV A, 24H” is the Instruction and the “THIS IS A SAMPLE COMMENT” is a Comment.
The Label is programmer chosen name for a Memory Location or a statement in a program. The Label part of the statement is optional and if present, the Label must be terminated with a Colon (:).
An important point to remember while selecting a name for the Label is that they should reduce the need for documentation.
The Instruction is the main part of the 8051 Microcontroller Assembly Language Programming as it is responsible for the task performed by the Microcontroller. Any Instruction in the Assembly Language consists of two parts: Op-code and Operand(s).
The first part of the Instruction is the Op-code, which is short for Operation Code, specifies the operation to be performed by the Microcontroller. Op-codes in Assembly Language are called as Mnemonics. Op-codes are in binary format (used in Machine Language) while the Mnemonic (which are equivalent to Op-codes) are English like statements.
The second part of the instruction is called the Operand(s) and it represents the Data on which the operation is performed. There are two types of Operands: the Source Operand and the Destination Operand. The Source Operand is the Input of the operation and the Destination Operand is where the result is stored.
The last part of the Structure of 8051 Assembly Language is the Comments. Comments are statements included by the developer for easier understanding of the code and is used for proper documentation of the Program.
Comments are optional and if used, they must begin with a semicolon (;) or double slash (//) depending on the Assembler.
The following statements will show a few possible ways of using Label, Instruction and Comments.
Label without instruction and comment: LABEL:
Line with Label and Instruction: LABEL: MOV A, 22H
Line with Instruction and Comment: MOV A, 22H ; THIS IS A COMMENT
Line with Label and Comment: LABEL: ; THIS IS A COMMENT
Line with only Comment: ; THIS IS A COMMENT
Also read: 8051 MICROCONTROLLER ARCHITECTURE
8051 Microcontroller Assembly Language Directives
Assembly Language Directives are not the instructions to the 8051 Microcontroller Assembler even though they are written in the Mnemonic field of the program. Assembly Language Directives are actually instructions to the Assembler and directs the Assembler Program what to do during the process of Assembling.
The Assembly Language Directives do not have any effect on the contents of the 8051 Microcontroller Memory (except DB and DW directives).
These Directives are dependent on the Assembler Program and in case of ASM51 Assembler, the following are the categories of Directives.
We will now see about few of the important and frequently used Assembly Language Directives.
ORG – Set Origin
The 8051 Microcontroller Assembly Language Program will start assembling from the Program Memory Address 0000H. This is also the address from which the 8051 Microcontroller will start executing the code.
In order place the Program and Data anywhere in the Address Space of the 8051 Microcontroller, you can use the ORG Directive.
DB – Define Byte
The DB Directive is used to define a Byte type variable. Using this directive, you can define data in Decimal, Binary, HEX or ASCII formats. There should be a suffix of ‘B’ for binary and ‘H’ for HEX. The ASCII Characters are placed in single quotation marks (like ‘string’).
DW – Define Word
The Define Word (DW) Directive is used to include a 16-bit data in a program. The functionality of DW is similar to that of DB except that DW generates 16-bit values.
EQU – Equate
Using the EQU Directive, you can associate a Symbol (or Label) with a Value.
The END Directive is used to stop the assembling process. This should be the last statement in the program. END Directive cannot have a Label and the statements beyond END will not be processed by the Assembler.
Examples of 8051 Microcontroller Assembly Language Programming
The following is a simple Assembly Language for 8051 Microcontroller which copies the data from R0 of Bank0 to R0 of Bank3.
MOV R0, #33H
MOV A, R0
MOV R0, A
In the next example, you can Toggle the LEDs ON and OFF (Blinking LEDs) that are connected to PORT1 of the 8051 Microcontroller.
In this tutorial, we have seen about the basics of 8051 Microcontroller Assembly Language Programming, the Structure of Assembly Language for 8051, Assembly Language Directives and few examples.