View Course Path

Data Transfer instructions in 8051

Any instruction given to the microcontroller contains two parts: an opcode and an operand. The opcode is responsible for telling the microcontroller what to do, whereas the operand holds the data on which the operations are to be performed. The 8051 microcontroller has an 8-bit opcode which gives it the ability to handle 2^8(255) instructions. The operands, on the other hand, can be of 0 bytes, 1 byte or 2 bytes. In this article, we will be talking about the data transfer instructions in 8051 that are a subset of the instructions it offers. These instructions are responsible for moving data from one place to another in the microcontroller.

But before we go on talking about those data transfer instructions. I would like to discuss what happens when a microcontroller starts and how it performs all those complex operations in just a matter of milliseconds.

So when the microcontroller turns on the program counter holds the starting address (0000H). The programmer can change this starting address, but if no changes are made, then the microcontroller puts this address on the address bus. The address bus locates the memory location on the ROM and gets the instruction.

Then the microcontroller increments the program counter according to the size of the instruction. Once the instruction is retrieved, it is placed in the instruction register. Afterward, the instruction decoder decodes the instruction, and the control unit generates the control signals accordingly. Now that you are familiar with the working of the 8051 let us get to understanding its data transfer instructions it offers

List of data transfer instructions

Data transfer instructions are responsible for transferring data between various memory storing elements like registers, RAM, and ROM. The execution time of these instructions varies based on how complex an operation they have to perform. In the table given below, we have listed all the data transfer instruction. In the table [A]= Accumulator; [Rn]=Register in RAM; DPTR=Data Pointer; PC=Program Counter

Operation Mnemonics Description
Register to register
MOV A, Rn [A]<-[Rn]
MOV Rn, A [Rn]<-[A]
XCH A, Rn [A]<-[Rn]
Memory to register
MOV A, @Rn [A]<-[Address in register]
MOV A, address [A]<-[Address]
MOV Rn, address [Rn]<-[Address]
MOVX A, @Rn [A]<-[Address in External ROM]
MOVC A, @A+DPTR [A]<-[Address in Internal ROM]
MOVC A, @A+PC [A]<-[Address in Internal ROM]
MOVX A, @DPTR [A]<-[Address in External ROM]
XCH A, @Rn [A]<-[Address]
XCHD A, @Rn [A]<-[Address]
XCH A, address [A]<-[Address]
Register to memory
MOVX @Ri, A [Address]<-[A]
MOV a8, A [Address]<-[A]
MOV a8, Rn [Address]<-[Rn]
MOVX @DPTR, A [Address]<-[A]
MOV @Rn, A [Address]<-[A]
Data to Register
MOV A, #data [A]<-[Data]
MOV Rn, #data [Rn]<-[Data]
MOV DPTR, #data [DPTR]<-[Data]
Address to Address
MOV a8, @Rn [Address]<-[Address]
MOV address, address [Address]<-[Address]
MOV @Rn, address [Address]<-[Address]
Data to address
MOV address, #data [Address]<-[Data]
MOV @Rn, #d8 [Address]<-[Data]
Stack
PUSH a8 Data added to stack
POP a8 Data removed from the stack

MOV Instruction

The MOV instruction has two operands, the source, and the destination. The second operand is the source, whereas the first one is the destination. This instruction uses various addressing modes to move data in the RAM space of the microcontroller.

Examples

  • MOV A, R0 //Moves data from the register R0 to the accumulator
  • MOV R0,50H //Moves data stored in memory location 50H to Ro
  • MOV A,@R0 //Uses data stored in R0 register as an address and moves the data at that location to the accumulator
Modern 8051 variants like the one we discussed in our post on 8051 architecture can execute an instruction that usually took the older version 12 clock cycles, in one clock cycle. So, in the column on execution time below, you can divide the number of clock cycles by 12 to get the speeds of execution for modern variants of the 8051.
Opcode Operand Description Size  Execution Time Flags affected
MOV
A, Rn Moves data from registers in register banks of RAM to accumulator 1 byte 12 clock cycles None
A, Address Moves data from an address in the RAM space to the accumulator 2 byte 12 clock cycles None
A,@Rn Uses data stored in a register as an address and moves the data at that address to the accumulator 1 byte 12 clock cycles None
A,#Data Moves data given by programmer directly to the accumulator 2 byte 12 clock cycles None
Rn,A Moves data from the accumulator to registers in register bank 1 byte 12 clock cycles None
Rn, Address Moves data from an address in the RAM space to a register in the register banks 2 bytes 24 clock cycles None
Rn,#Data Moves data given by a programmer directly to a register in the register banks 2 bytes 12 clock cycles None
Address,A Moves data to an address in the RAM space from the Accumulator 2 bytes 12 clock cycles None
Address,Rn Moves data to an address in the RAM space to a register in the register banks 2 bytes 24 clock cycles None
Address, Address Moves data from one address to the other 3 bytes 24 clock cycles None
Address,@Ri Uses data stored in a register as an address and moves the data at that address to a register in the register bank 3 bytes 24 clock cycles None
Address,#Data Moves data given by the programmer directly to an address 3 bytes 24 clock cycles None
@Rn,A Moves data from the accumulator to an address which is stored in a register 1 byte 12 clock cycles None
@Rn, Address Moves data from an address to an address which is stored in a register 2 bytes 24 clock cycles None
@Rn,#Data Moves data given by the programmer to an address which is stored in the register 2 bytes 12 clock cycles None
DPTR,#Data Places a memory address of 16-bit size in the DPTR Register 3 bytes 24 clock cycles None

MOVC Instruction

MOVC instruction is responsible for moving data from the Program memory (Flash memory) to the RAM for processing it.

Example

  • MOVC A,@A+DPTR
  • MOVC A,@A+PC

Opcode Operand Description Size Execution time Flags affected
MOVC
A, @A+DPTR Moves data to accumulator from a address stored in the memory location (internal ROM) at A+DPTR 1 byte 24 clock cycles None
A, @A+PC Moves data to accumulator from a address stored in the memory location(internal ROM) at A+PC 1 byte 24 clcok cyles None

MOVX Instruction

The 8051 microcontroller in most cases has an on-chip 4K flash memory, but due to its 16-bit address bus, it can access 64k memory locations. Due to this reason, the 8051 can be interfaced with external memory using ports 0 and 2. To access data in this external memory, the MOVX instruction is used

Opcode Operand Description Size Execution time Flags affected
MOVX
A, @Rn Moves data to accumulator from a memory location (External ROM) 1 byte 24 Clock cycles None
@Rn,A Moves data to Memory location (External ROM) from a register in the register bank 1 byte 24 Clock cycles None
A, @DPTR Moves data to accumulator from a memory location (External ROM) pointed by the Data Pointer 1 byte 24 Clock cycles None
@DPTR,A Moves data to Memory location (External ROM) pointed by data pointer from the accumulator 1 byte 24 Clock cycles None

Stack operations

The RAM of the 8051 microcontroller is home to a set of 32 general-purpose registers (00H-1FH). These registers are 8 bit wide and are bundled in groups of 8 forming four register banks. Stack operations can be used to place data into these registers in an efficient manner. These stack operations use special commands (PUSH, POP) to place and extract data from these general purposes registers.

The PUSH operation

The PUSH operation is used to place data into the stack. When this command is given the value of address stored in the stack pointer is increased by one. After incrementing the address in the stack pointer, data is placed at that memory location. For example, when the 8051 is powered up, it holds the address 07H. When it receives the first PUSH instruction, the address is updated to 08H, and data is stored in that location.

Example (R6 contains 80H and stack pointer points at 07H)

PUSH 6; This instruction moves data stored in register R6 to 08H

The POP operation

The POP operation is used to extract data that is stored in the stack. This operation is the complete opposite of the PUSH operation. It extracts the data from the location which the stack pointer points to and then decreases the value of the SP by 1.

Example (Stack pointer points at memory location 08H which contains 50H)

POP 6; register R6 now contains the data 50H and the stack pointer points to 07H

Opcode Operand Description Execution Time Size Flags affected
PUSH Rn Places value at the top of the stack 24 clock cycles 2 bytes None
POP Rn Extracts the data from the top of the stack 24 clock cycles 2 bytes None

Exchange Instructions 

This operation is used to exchange data between the source and the destination operands.

Example

  • XCH A, R0; exchanges the data stored in the accumulator and R0
  • XCHD A,@R0; Exchanges the lower four bits of a memory location stored in a register with the accumulator

Opcode Operand Description Size Execution time Flags affected
XCH
A,Rn Exchanges the value between a register and the accumulator 1 byte 12 Clock cycles None
A,Address Exchanges the value between the accumulator and a memory location in the RAM 2 bytes 12 Clock cycles None
A,@Rn Exchanges the value between the accumulator and a memory location stored in the register 1 byte 12 Clock cycles None
XCHD A,@Rn Exchanges the lower four bits of a memory location stored in a register with the accumulator 1 byte 12 Clock cycles None

We hope that reading this article has helped you to understand the data transfer instructions in the 8051 microcontroller. If you have any doubts feel free to ask them in the comments section below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.