8051 Program to exchange data blocks without using XCH

8051-programs
8051-programs

8051 Program to exchange data blocks without using XCH :

8051 Program to exchange data blocks without using XCH is as follows:

MOV R0,#20H
MOV R1,#30H
MOV R2,#0AH
MOV R3,#00H
UP:MOV A,@R0
MOV R3,A
MOV A,@R1
MOV @R0,A
MOV A,R3
MOV @R1,A
INC R0
INC R1
DJNZ R2,UP
RET

Above program exchanges data blocks without using XCH instruction, the logic for the same is as explained below:

Logic for 8051 Program to exchange data blocks without using XCH :

In this program, there are two data blocks. Block 1 starting from address 20H to 29H and Block 2 starting from address 30H to 39H. Aim of program is to exchange data from both data blocks without using XCH instruction. So, we initialize register R0 is Block 1 pointer and register R1 as Block 2 pointer. R2 is initialized as byte counter, as one block contains 10 bytes counter is set to 10. 10 is 0A in hex so R2 is initialized with 0A. Register R3 will be used as intermediate register in this program so it is cleared at initialization. Use of Register R3 is data from Block 1 will be stored in R3, then data from Block 2 will be stored in Block 1. After transferring data to Block 1, data from R3 is transferred to Block 2.

The transfer of data according to above algorithm takes places byte by byte, so 10 program cycles are required to reach the final goal. That is block will be completely exchanged after 10 program cycles. We use indirect addressing mode in above program to get/store data into data blocks pointed by Registers R1 & R2. This code can be used foe exchanging data blocks stored in external memory with few minor changes. DPTR needs to be used as pointer to external memory. Instruction MOVX A,@DPTR and MOVX @DPTR,A must be used at some instances in code. C program can also be easily developed using the logic mentioned above.

In case of doubts or queries, please comment below. Our experts are happy to help you

8051 program to find even numbers

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here