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 in Assembly Language
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
8051 Programming in Assembly Language

 Assembly programming language is developed by various compilers and the “keiluvison is best suitable for  microcontroller programming development. 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.




Register Addressing Mode:
In this addressing mode, the source and destination must be a register, but not general purpose registers. So the data is not moved within the general purpose bank registers.
Syntax:
MOV A, B;         // A is a SFR register, B is a general purpose register//
MOV R0, R1    //Invalid instruction, GPR to GPR not possible//
EX:  
MOV R0, #02h
MOV A, #30h
ADD R0, A       //R0<—R0+A, the final value is stored in the R0 register//
Direct Addressing Mode
In this addressing mode, the source or destination (or both source and destination) must be an address, but not value.
Syntax:
MOV A,20h        // 20h is an address; A is a register//
MOV 00h, 07h    // both are addressed of the GPS registers//
Ex:



MOV 07h,#01h
MOV A, #08h
ADD A,07h             //A<—A+07h the final value is stored in A//
Indirect Addressing Mode:
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
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.
 Syntax:
MOVC A, @A+DPTR   //C indicates code memory//
MOCX A, @A+DPTR    // X indicate external memory//
EX:    MOV A, #00H        //00H is stored in the A register//
MOV DPTR, #0500H        //DPTR points 0500h address in the memory//
MOVC A, @A+DPTR    //send the value to the A register//
MOV P0, A            //date of A send to the PO registrar//

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 microcontroller can follow CISC instructions with Harvard architecture. In case of the 8051 programming different types of CISC instructions include:
  • Data Transfer Instruction set
  • Sequential Instruction Set
  • Arithmetic Instruction set
  • Branching Instruction set
  • Loop Instrcution Set
  • Conditional Instruction set
  • Unconditional Instruction set
  • Logical Instruction set
  • Boolean Instruction set




Arithmetic Instruction Set:

The arithmetic instructions perform the basic operations such as:
  • Addition
  • Multiplication
  • Subtraction
  • Division
Addition:
ORG 0000h
MOV R0, #03H     // move the value 3 to the register R0//
MOV A, #05H     // move the value 5 to accumulator A//
Add A, 00H         // addA value with R0  value and stores the result inA//
END
Multiplication:

ORG 0000h
MOV R0, #03H     // move the value 3 to the register R0//
MOV A, #05H     // move the value 5 to accumulator A//
MUL A, 03H         // Multiplied result is  stored in the Accumulator A //
END
Register Addressing Mode:
In this addressing mode, the source and destination must be a register, but not general purpose registers. So the data is not moved within the general purpose bank registers.
Syntax:
MOV A, B;         // A is a SFR register, B is a general purpose register//
MOV R0, R1    //Invalid instruction, GPR to GPR not possible//
EX:  
MOV R0, #02h
MOV A, #30h
ADD R0, A       //R0<—R0+A, the final value is stored in the R0 register//
Direct Addressing Mode
In this addressing mode, the source or destination (or both source and destination) must be an address, but not value.
Syntax:
MOV A,20h        // 20h is an address; A is a register//
MOV 00h, 07h    // both are addressed of the GPS registers//
Ex:
MOV 07h,#01h
MOV A, #08h
ADD A,07h             //A<—A+07h the final value is stored in A//
Indirect Addressing Mode:
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
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.
 Syntax:
MOVC A, @A+DPTR   //C indicates code memory//
MOCX A, @A+DPTR    // X indicate external memory//
EX:    MOV A, #00H        //00H is stored in the A register//
MOV DPTR, #0500H        //DPTR points 0500h address in the memory//
MOVC A, @A+DPTR    //send the value to the A register//
MOV P0, A            //date of A send to the PO registrar//

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 microcontroller can follow CISC instructions with Harvard architecture. In case of the 8051 programming different types of CISC instructions include:
  • Data Transfer Instruction set
  • Sequential Instruction Set
  • Arithmetic Instruction set
  • Branching Instruction set
  • Loop Instrcution Set
  • Conditional Instruction set
  • Unconditional Instruction set
  • Logical Instruction set
  • Boolean Instruction set

Arithmetic Instruction Set:

The arithmetic instructions perform the basic operations such as:
  • Addition
  • Multiplication
  • Subtraction
  • Division
Addition:
ORG 0000h
MOV R0, #03H     // move the value 3 to the register R0//
MOV A, #05H     // move the value 5 to accumulator A//
Add A, 00H         // addA value with R0  value and stores the result inA//
END



Multiplication:
ORG 0000h
MOV R0, #03H     // move the value 3 to the register R0//
MOV A, #05H     // move the value 5 to accumulator A//
MUL A, 03H         // Multiplied result is  stored in the Accumulator A //
END
Subtraction:
ORG 0000h
MOV R0, #03H     // move the value 3 to register R0//
MOV A, #05H     // move the value 5 to accumulator A//
SUBB A, 03H     // Result value is stored in the Accumulator A //
END
Division:
ORG 0000h
MOV R0, #03H     // move the value 3 to register R0//
MOV A, #15H     // move the value 5 to accumulator A//
DIV A, 03H     // final value is stored in the Accumulator A //
END

Conditional Instructions

The CPU executes the instructions based on the condition by checking the single bit status or byte status. The 8051 microcontroller consists of various conditional instructions such as:
  • JB  —>Jump below
  • JNB —> Jump if not below
  • JC —> Jump if Carry
  • JNC  —>Jump if  not Carry
  • JZ   —>Jump if Zero
  • JNZ —> Jump if not Zero
Conditional Instructions
Conditional Instructions
 1. Syntax:
JB P1.0, label
– – – – – – – –
– – – – – – – –
Label:     – – – – – – – –
– – – – – – – –
END
2. Syntax:
JNB P1.0, label
– – – – – – – –
– – – – – – – –
Label:     – – – – – – – –
– – – – – – – –
END

3. Syntax:

JC, label
– – – – – – – –
– – – – – – – –
Label:     – – – – – – – –
– – – – – – – –
END
4. Syntax:
JNC, label
– – – – – – – –
– – – – – – – –
Label:     – – – – – – – –
– – – – – – – –
END
5. Syntax:
JZ, label
– – – – – – – –
– – – – – – – –
Label:     – – – – – – – –
– – – – – – – –
END
2. Syntax:
of DJNE
MOV R0, #10H
Label:– – – – – –
– – – – – –
DJNE R0,  label
– – – – – –
– – – – – –
END

Logical Instruction Set:

The 8051 microcontroller instruction set provides the AND, OR, XOR, TEST, NOT and Boolean logic instructions for set and clears the bits based on the need in the program.
Logical Instruction Set
Logical Instruction Set
1. Syntax:
MOV A, #20H    /00100000/
MOV R0, #03H    /00000101/
ORL A, R0        //00100000/00000101=00000000//
2. Syntax:
MOV A, #20H    /00100000/
MOV R0, #03H    /00000101/
ANL A, R0
3. Syntax:
MOV A, #20H    /00100000/
MOV R0, #03H    /00000101/
XRL A, R0

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
3. Syntax:
MOV A, #20H    /00100000/
MOV R0, #03H    /00000101/
XRL A, R0

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

LED programs with 8051 Microcontrller

1. WAP to toggle the PORT1 LEDs
ORG 0000H
TOGLE: MOV P1, #01    //move 00000001 to the p1 register//
CALL DELAY    //execute the delay//
MOV A, P1        //move p1 value to the accumulator//
CPL A        //complement A value //
MOV P1, A        //move 11111110 to the port1 register//
CALL DELAY    //execute the delay//
SJMP TOGLE
DELAY: MOV R5, #10H    //load register R5 with 10//
TWO:        MOV R6, #200    //load register R6 with 200//
ONE:       MOV R7, #200    //load register R7 with 200//
DJNZ R7, $    //decrement R7 till it is zero//
DJNZ R6, ONE    //decrement R7 till it is zero//
DJNZ R5, TWO    //decrement R7 till it is zero//
RET            //go back to the main program //
END

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.
1. WAP to calculate the 500us time delay.
MOV TMOD, #10H        //select the timer mode by the registers//
MOV TH1, #0FEH        // store the delay time in higher bit//
MOV TL1, #32H        // store the delay time in low bit//
JNB TF1, $            //decrement the value of the timer till it is zero//
CLR TF1            //clear the timer flag bit//
CLR TR1            //OFF the timer//
2. WAP to toggle the LEDs withthe 5 sec time delay
ORG 0000H
RETURN: MOV PO, #00H
ACALL DELAY
MOV P0, #0FFH
ACALL DELAY
SJUMP RETURN
DELAY:   MOV R5, #50H      //load register R5 with 50//
DELAY1: MOV R6, #200    //load register R6 with 200//
DELAY2: MOV R7, #229    //load register R7 with 200//
DJNZ R7, $    //decrement R7 till it is zero//
DJNZ R6, DELAY2//decrement R6 till it is zero//
DJNZ R5, DELAY1//decrement R5 till it is zero//
RET            //go back to the main program //
END
3. WAP to count the 250 pulses using mode0 count0
Syntax:
ORG 0000H
MOV TMOD, #50H        //select the counter//
MOV TH0, #15        //move the counting pulses higher bit//
MOV TH1, #9FH        //move the counting pulses, lower bit//
SET TR0            //ON the timer//
JNB  $            //decrement the count value till zero//
CLR TF0            //clear the counter, flag bit//
CLR TR0            //stop the timer//
END



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.
1. WAP to transmit the characters to the Hyper Terminal
MOV SCON, #50H        //set the serial communication//
MOV TMOD, #20H        //select the timer mode//
MOV TH1, #-3        //set the baud rate//
SET TR1            //ON the timer//
MOV SBUF, #’S’        //transmit S to the serial window //
JNB TI, $            //decrement value of the timer till it is zero//
CLR RI            // clear receive interrupt //
CLR TR1            //clear timer//
2. WAP to transmit the Receive the character by the Hyper Terminal
MOV SCON, #50H        //set the serial communication//
MOV TMOD, #20H        //select the timer mode//
MOV TH1, #-6        //set the baud rate//
SET TR1            //on the timer//
MOV SBUF, #’S’        //transmit S to the serial window //
JNB RI, $            //decrement value of timer till it is zero//
CLR RI            // clear receive interrupt //
MOV P0, SBUF         //send the SBUF register value to the port0//
CLR TR1            //clear timer//
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.



No comments: