0% found this document useful (0 votes)
78 views

8051 Microcontroller Assembly Language Programming-1

Uploaded by

Manish Dhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

8051 Microcontroller Assembly Language Programming-1

Uploaded by

Manish Dhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8051 Microcontroller Assembly Language

Programming
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.

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

Machine 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.

Assembly Language
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.

High-level Language
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.

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 four fields: Label,
Mnemonic, Operands and Comments.

The arrangement of these fields or the order in which they appear is shown below.
[Label:] Mnemonic [Operands] [; Comments]

NOTE: Brackets indicate that a field is optional and may not be used in all statements in a program.
Brackets should not be typed in.
Before seeing about these four 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, the “MOV” is a Mnemonic, the
“A, 24H” is Operands and the “THIS IS A SAMPLE COMMENT” is a Comment.
Label
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 rule. The Label part of the statement is
optional and if present, the Label must be terminated with a Colon (:).

Mnemonic & Operands (Instruction)


The Assembly language mnemonics and operand(s) field together perform the real work of the program
and accomplish the tasks for which the program was written.

The Operand(s) 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.

Comments
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 are 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

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.

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.

Examples
ORG 0000H ; Tells the Assembler to assemble the next statement at 0000H
or
ORG 000BH ; Tells the Assembler to assemble the next statement at 000BH

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.
Either single or double quotes can be used around ASCII strings. This can be useful for strings, which
contain a single quote such as “O’Leary”.

Examples
ORG 0000H
DB 10 ; Define Byte 10 (Decimal) and store at 0000H
DB 30H ; Define Byte 30 (HEX) and store at 0001H
DB ‘‘STRING’’ ; Define String ‘‘STRING’’ and store at 0002H to 0007H
DB 00001111B ; Define Byte 00001111 (Binary) and store at 0008H
DB 1234H ; Define Byte 34 (HEX) and store at 0009H. Only lower byte is accepted as DB
; can allocate only a Byte of Memory.

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.
Examples
TMP EQU #30 ; Assigns the value #30 to the name “TMP”
RED_LED TMP P1.0 ; P1.0 is defined as “RED_LED”

END
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.
Example
ORG 0000H
MOV A, 20H
MOV R0, #30
END

Examples of 8051 Microcontroller Assembly Language


Programming
Example 1
The following is a simple Assembly Language for 8051 Microcontroller which copies the data from R0
of Bank0 to R0 of Bank3.

ORG 00H
MOV R0, #33H
MOV A, R0
SETB PSW.3
SETB PSW.4
MOV R0, A
END
Example 2
In the next example, you can Toggle the LEDs ON and OFF (Blinking LEDs) that are connected to
PORT1 of the 8051 Microcontroller.

ORG 00H ; Assembly Starts from 0000H.


; Main Program
START: MOV P1, #0XFF ; Move 11111111 to PORT1.
CALLWAIT ; Call WAIT
MOV A, P1 ; Move P1 value to ACC
CPL A ; Complement ACC
MOV P1, A ; Move ACC value to P1
CALLWAIT ; Call WAIT
SJMP START ; Jump to START
WAIT: MOV R2, #10 ; Load Register R2 with 10 (0x0A)
WAIT1: MOV R3, #200 ; Load Register R3 with 10 (0xC8)
WAIT2: MOV R4, #200 ; Load Register R4 with 10 (0xC8)
DJNZ R4, $ ; Decrement R4 till it is 0. Stay there if not 0.
DJNZ R3, WAIT2 ; Decrement R3 till it is 0. Jump to WAIT2 if not 0.
DJNZ R2, WAIT1 ; Decrement R2 till it is 0. Jump to WAIT1 if not 0.
RET ; Return to Main Program
END ; End Assembly

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.

You might also like