Moving Data Operations

Following are the programs that gets you started with basics of assembly language programming 8051. Copy this program code into Keil text file and save it with “.asm” or “.a51” file extension.

Storing Data in SFRs and Registers:

// This program demonstrates how data can be stored in registers.
// This is called Immediate Addressing Mode.

MOV A, #05H;
MOV B,A;
MOV R0,#3CH;
MOV R1,#0F0H;
END

Moving Data in Registers and RAM:

There are four types of Addressing Modes for the data transfer instructions.

  • Immediate Addressing Mode
  • Register Addressing Mode
  • Direct Addressing Mode
  • Indirect Addressing Mode

Refer to Moving Data Operations Instructions for further description.

/* This program file describes different moving data addressing modes.
 Run this program step by step in debug session to understand how these instructions work.*/

// Immediate Addressing Mode
// In this mode, Pound sign "#" is used.
// Whatever value is placed followed by Pound sign "#" is copied to Register.

MOV A,#05H;
MOV B,#33H;
MOV DPH,#12H;
MOV DPL,#34H;
MOV R0,#55H;
MOV R1,#0FFH;

// Register Addressing Mode
// In this mode, data is transferred from source register to destination register.
// Both the Source and Destination Registers are referred with short of their Register Names.

MOV R2,A;
MOV R3,B;
MOV R4,DPL;
MOV R5,DPH;

// Direct Addressing Mode
// In this mode, data is transferred from source register to destination register.
// Unlike Register Addressing Mode, in this mode, Direct Addresses of Registers are used as Source and/or Destination Register.
// Refer to list of Direct Addresses of SFRs from 8051 Architecture.

MOV 80H,#23H; // 80h = Direct Address of Port 0
MOV 90H,#55H; // 90h = Direct Address of Port 1
MOV 0E0H,80H; // 0E0h = Direct Address of Accumulator
MOV 0F0H,90H; // 0F0h = Direct Address of Register B
MOV 1EH,83H; // 1Eh = Direct Address of Internal RAM, 83h = Direct Address of DPH
MOV 1DH,82H; // 1Dh = Direct Address of Internal RAM, 82h = Direct Address of DPL

// Indirect Addressing Mode
// In this mode, at sign "@" is used.
// This mode is used with only two registers R0 and R1.
// In this mode, data is fetched from or data is stored at memory location stored in Pointer Registers R0 and R1.

MOV R0,#33H;
MOV R1,#2CH;
MOV @R1,#55H; // Check the result in Internal RAM memory location 2Ch (D:2Ch)
MOV @R0,#0FFH; // Check the result in Internal RAM memory location 33h (D:33h)
MOV A,#00H;
MOV A,@R0; //Check the result on Port 0
MOV A,@R1; //Check the result on Port 0

END

Changing Register Banks to store Data:

There are four Register Banks in  8051 Microcontroller Architecture, viz. Register Bank 0, Register Bank 1, Register Bank 2 and Register Bank 3.

When Microcontroller starts or restarts, by default Register Bank 0. To change Register Bank, RS0 and RS1 bits of Program Status Word needs to set to 1 or reset to 0.

// By Default Register Bank 0 is selected.
MOV A,#05H;
MOV B,#0FCH;
MOV R0, A; // Accumulator content will be stored to Register 0 of Register Bank 0
MOV R1, B; // Accumulator content will be stored to Register 1 of Register Bank 0
MOV R2, A; // Accumulator content will be stored to Register 2 of Register Bank 0
MOV R3, B; // Accumulator content will be stored to Register 3 of Register Bank 0

// To change Register Bank 1, RS1 = 0 and RS0 = 1.
MOV R0, A; // Accumulator content will be stored to Register 0 of Register Bank 1
MOV R1, B; // Accumulator content will be stored to Register 1 of Register Bank 1
MOV R2, A; // Accumulator content will be stored to Register 2 of Register Bank 1
MOV R3, B; // Accumulator content will be stored to Register 3 of Register Bank 1


// To change Register Bank 2, RS1 = 1 and RS0 = 0.
MOV R0, A; // Accumulator content will be stored to Register 0 of Register Bank 2
MOV R1, B; // Accumulator content will be stored to Register 1 of Register Bank 2
MOV R2, A; // Accumulator content will be stored to Register 2 of Register Bank 2
MOV R3, B; // Accumulator content will be stored to Register 3 of Register Bank 2


// To change Register Bank 3, RS1 = 1 and RS0 = 1.
MOV R0, A; // Accumulator content will be stored to Register 0 of Register Bank 3
MOV R1, B; // Accumulator content will be stored to Register 1 of Register Bank 3
MOV R2, A; // Accumulator content will be stored to Register 2 of Register Bank 3
MOV R3, B; // Accumulator content will be stored to Register 3 of Register Bank 3


END

Data Exchange Instruction

  • Exchange instruction moves data in two directions:
    • From source to destination
    • From destination to source

All addressing modes except immediate may be used in XCH instructions.

/* This program gives explanation how data exchange instructions works.
Using XCH instruction, there is no need of arbitrary or temporary register to exchange data between two registers.*/

// Using XCH instruction
MOV A,#33H;
MOV B,#77H;
MOV R0,#66H;
MOV R1,#55H;
MOV 66H,#0CCH;
MOV 55H,#0FFH;
XCH A,B;
XCH A,R0;
XCH A,R1;
MOV 77H,#11H;
MOV 66H,#22H;
XCH A,@R0;
XCH A,@R1;


/* XCHD instruction is used to exchange lower nibbles of the data stored on Source and Destination Registers
without affecting Upper nibbles of the data in both the registers.*/

XCHD A,@R0;
XCHD A,@R1;

END
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s