Introduction to 8051 Programming in Assembly Language

The assembly language is a low-level programming language used to write program code in terms of mnemonics. Even though there are many high-levellanguages that are currently in demand, assembly programming language is popularly used in many applications.It can be used for direct hardware manipulations. It is also used to write the 8051 programming code efficiently with less number of clock cycles by consuming less memory compared to the other high-level languages.

8051 Programming

8051 Programming in Assembly Language

The assembly language is a fully hardware related programming language. The embedded designers must have sufficient knowledge on hardware of particular processor or controllers before writing the program. The assembly language is developed by mnemonics; therefore, users cannot understand it easily to modify the program.

8051 Programming in Assembly Language

Assembly programming language is developed by various compilers and the “keiluvison” is best suitable for microcontroller programmingdevelopment. Microcontrollers or processors can understand only binary language in the form of ‘0s or 1s’; An assembler converts the assembly language to binary language, and then stores it in the microcontroller memory to perform the specific task.

8051 Microcontroller Architecuture

The 8051 microcontroller is the CISC based Harvard architecture, and it has peripherals like 32 I/O, timers/counters, serial communication and memories. The microcontroller requires a program to perform the operations that require a memory for saving and to read the functions. The 8051 microcontroller consists of RAM and ROM memories to store instructions.

8051 Microcontroller Architecuture

A Register is the main part in the processors and microcontrollers which is contained in the memory that provides a faster way of collecting and storing the data. The 8051 assembly language programming is based on the memory registers. If we want to manipulate data to a processor or controller by performing subtraction, addition, etc., we cannot do that directly in the memory, but it needs registers to process and to store the data. Microcontrollers contain several types of registers that can be classified according to their instructions or content that operate in them.

8051 Microcontroller Programs in Assembly Language

The assembly language is made up of elements which all are used to write the program in sequential manner. Follow the given rules to write programming in assembly language.

Rules of Assembly Language

The assembly code must be written in upper case letters

The labels must be followed by a colon (label:)

All symbols and labels must begin with a letter

All comments are typed in lower case

The last line of the program must be the END directive

The assembly language mnemonics are in the form of op-code, such as MOV, ADD, JMP, and so on, which are used to perform the operations.

Op-code: The op-code is a single instruction that can be executed by the CPU. Here the op-code is a MOV instruction.

Operands: The operands are a single piece of data that can be operated by the op-code. Example, multiplication operation is performed by the operands that are multiplied by the operand.

Syntax: MUL a,b;

The Elements of an Assembly Language Programming:

Assembler Directives

Instruction Set

Addressing Modes

Assembler Directives:

The assembling directives give the directions to the CPU. The 8051 microcontroller consists of various kinds of assembly directives to give the direction to the control unit. The most useful directives are 8051 programming, such as:

ORG

DB

EQU

END

ORG(origin): This directive indicates the start of the program. This is used to set the register address during assembly. For example; ORG 0000h tells the compiler all subsequent code starting at address 0000h.

Syntax: ORG 0000h

DB(define byte): The define byte is used to allow a string of bytes. For example, print the “EDGEFX” wherein each character is taken by the address and finally prints the “string” by the DB directly with double quotes.

Syntax:

ORG 0000h

MOV a, #00h
————-
————-
DB”EDGEFX”

EQU (equivalent): The equivalent directive is used to equate address of the variable.

Syntax:

regequ,09h
—————–
—————–
MOV reg,#2h

END:The END directive is used to indicate the end of the program.

Syntax:

regequ,09h

—————–
—————–
MOV reg,#2h
END

Addressing Modes:

The way of accessing data is called addressing mode. The CPU can access the data in different ways by using addressing modes. The 8051 microcontroller consists of five addressing modes such as:

Immediate Addressing Mode

Register Addressing Mode

Direct Addressing Mode

Indirect Addressing Mode

Base Index Addressing Mode

Immediate Addressing Mode:

In this addressing mode, the source must be a value that can be followed by the ‘#’ and destination must be SFR registers, general purpose registers and address. It is used for immediately storing the value in the memory registers.

Syntax:

MOV A, #20h //A is an accumulator register, 20 is stored in the A//
MOV R0,#15 // R0 is a general purpose register; 15 is stored in the R0 register//
MOV P0, #07h //P0 is a SFR register;07 is stored in the P0//
MOV 20h,#05h //20h is the address of the register; 05 stored in the 20h//

In this addressing mode, the source or destination (or destination or source) must be a indirect address, but not a value. This addressing mode supports the pointer concept. The pointer is a variable that is used to store the address of the other variable. This pointer concept is only used for R0 and R1 registers.

Syntax:

MOVR0, #01h //01 value is stored in the R0 register, R0 address is 08h//
MOV R1, #08h//R1 is the pointer variable that stores address (08h) of R0 //
MOV 20h,@R1 //01 value is stored in the 20h address of the GP register//

Indirect Addressing Mode

Base Index Addressing Mode:

This addressing mode is used to read the data from the external memory or ROM memory. All addressing modes cannot read the data from the code memory. The code must read through the DPTR register. The DPTR is used to point the data in the code or external memory.

Instruction Set:

The instruction set is the structure of the controller or processor that provides commands to the controller to guide the controller for processing data. The instruction set consists of instructions, native data types, addressing modes, interrupt registers, exceptional handling and memory architecture. The 8051 microcontrollercan follow CISC instructions with Harvard architecture. In case of the 8051 programming different types of CISC instructions include:

Call and Jump Instructions:

The call and jump instructions are used to avoid the code replication of the program. When some specific code used more than once in different places in the program, if we mention specific name to code then we could use that name anywhere in the program without entering a code for every time. This reduces the complexity of the program. The 8051 programming consists of call and jump instructions such as LCALL, SJMP.

Shifting Operators

The shift operators are used for sending and receiving the data efficiently. The 8051 microcontroller consist four shift operators:

RR —> Rotate Right

RRC —>Rotate Right through carry

RL —> Rotate Left

RLC —>Rotate Left through carry

Rotate Right (RR):

In this shifting operation, the MSB becomes LSB and all bits shift towards right side bit-by-bit, serially.

Syntax:

MOV A, #25h
RR A

Rotate Left (RL):

In this shifting operation, the MSB becomes LSB and all bits shift towards Left side bit-by-bit, serially.

Syntax:

MOV A, #25h
RL A

RRC Rotate Right through Carry:

In this shifting operation, the LSB moves to carry and the carry becomes MSB, and all the bits are shift towards right side bit by bit position.

Syntax:

MOV A, #27h
RRC A

RLC Rotate Left through Carry:

In this shifting operation, the MSB moves to carry and the carry becomes LSB and all the bits shift towards left side in a bit-by-bit position.

Syntax:

MOV A, #27h
RLC A

Basic Embedded C Programs:

The microcontroller programming differs for each type of operating system. There are many operating systems such as Linux, Windows, RTOS and so on. However, RTOS has several advantages for embedded system development. Some of the Assembly level programming examples are given below.

LED blinking using with 8051 microcontroller:

Number Displaying on 7-segment display using 8051 microcontroller

Timer/Counter calculations and program using 8051 microcontroller

Serial Communication calculations and program using 8051 microcontroller

Timer/Counter Calculations and Program using 8051 Microcontroller:

The delay is the one of the important factors in the application software development. The timers and counters are hardware components of the microcontroller, that are used in many applications to provide the accurate time delay with count pulses. Both the tasks are implemented by the software technique.

Serial Communication Programming Using 8051 Microcontroller:

Serial communication is commonly used for transmitting and receiving the data. The 8051 microcontroller consist of UART/USART serial communication and the signals are transmitted and received by Tx and Rx pins. The UART communication transfers the data bit-by-bit serially. The UART is a half-duplex protocol that transfers and receives the data, but not at the same time.

This is all about the 8051 Programming in Assembly language in brief with example-based programs. We hope this adequate information on assembly language will be certainly helpful for the readers and we look forward for their valuable comments in the comment section below.

Hi Saiduzzaman, Thank you for your appreciation. Also, please check the user friendly website https://www.elprocus.com for project ideas on all the latest technologies.
For customization of projects please email us on team@elprocus.com