8051 Program to exchange data block of 10 Bytes

8051-programs
8051-programs

8051 Program to exchange data block of 10 Bytes:

8051 Program to exchange data block of 10 Bytes, in this article we will be designing a 8051 Program to exchange data block of 10 Bytes, the actual code of the program is as follows:

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

Logic for 8051 Program to exchange data block of 10 Bytes:

The logic of this program is, initialize a pointer to data block 1, initialize second pointer to data block 2. We will be using indirect addressing mode to use both of the registers, ie. Pointers to Block 1 &  Block 2. In 8051 only two registers R0 and R1 can be used in Indirect addressing mode, so we will be using both of them. In above program, data block 1 is stored at location 20H and block 2 is stored at location 30H. We initialize registers R1 and R2 as pointers to memory location  20H and 30H respectively. Register R2 is used as counter and loaded with 0AH, as we exchanging data block of 10 Bytes. We load first byte from data block 1 into accumulator (A). Using XCH A,@R1  we exchange the byte stored in A with the first byte of data block 2. After execution of this instruction, first byte from block 1 will be stored at first location of block 2. And the first byte of block 2 is present in accumulator so now, we move it from accumulator into first location of block 1. This is done using instruction MOV @R0,A. After execution of this instruction, the memory pointers are incremented to point next memory location. Program will take ten such cycles to exchange data blocks having 10 bytes stored. After execution of the program data blocks will completely be exchanged with one another. The code quoted above is tested and verified, in case of any doubts feel free to comment below.

Other articles you might like:

8051 program to exchange data blocks without using XCH.

Making a digital clock using 8051

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here