GeeksforGeeks App
Open App
Browser
Continue

8085 Program to Copy a Source Block to Destination Block with Overlapping Memory Address

The task is to copy the data from some source block to some destination block given that the destination block has an overlapping memory address with the source block. Data cannot just simply be moved from source block to destination block because the destination block is having an overlapping memory address, it could lead to overriding of data and original data can be lost without copying to its destination position.

Before copying data, the state of the source block and destination block

After copying the state of the destination block and source block is shown

Solution:

The objective behind the solution is to copy the data from the source block to some other temporary location and then copy the data from there to the destination block. Therefore, by doing this overlapping problems can be handled easily. As a temporary location, a stack pointer can be used. First, the data from the source block is pushed onto the stack then all the data is popped out of the stack and saved to the destination memory block.

Algorithm:

1. Load HL pair with the source block location and initialize stack pointer.
2.  Move the length of the Source block in register C and save its copy in register B for further use.
3. Increment HL pair to point to the first element of the source block.
4. [SAVE] Push element of source block pointed by HL pair on top of the stack.
5. Decrement register C
6. Repeat [SAVE] until register C is non-zero. In, other words this will push all elements of the source block on the stack one by one.
7. Restore the length of the source block from register B to register C.
9. [COPY] pop the element from the stack into the DE register pair.
10. Move register E into destination address pointed by HL pair. In other words, copy the element to the destination block.
11. Decrement register C.
12. Increment HL pair to point to next address in destination block
13. Repeat [COPY] until register C is non-zero. In other words, move all elements from the source block to the destination block.
14. Stop the program.

8085 Assembly Program

```// Program begins at address 0000H
# BEGIN 0000H
LXI H,C000H
// Initialize stack pointer
LXI SP,B000H
// save the length of source block in register B and C
MOV B,M
MOV C,M
// Increment HL pair to point to first element of source block
INX H
// Push all elements from source block onto stack
SAVE:
MVI D,00H
MOV E,M
PUSH D
DCR C
INX H
JNZ SAVE
// Restore the length of source block in register C from register B
MOV C,B
// Save destination address in DE register pair
MOV D,M
INX H
MOV E,M
// To perform stable copy (that is to do not change the order of
// elements, perform copy from backward location. So, just add
// the length of block and copy element one by one and keep
// decrementing after copying each element.
MOV A,E
DCR A
MOV E,A
// exchange the content of HL pair with DE register pair
XCHG
// Copy elements from stack to destination location one by one
// Pop one element at a time in DE register pair and save it
// into the destination location which is currently pointed
// by the HL pair
COPY:
POP D
// Move element from DE register pair to destination location
MOV M,E
// decrement the counter
DCR C
// update HL pair to next free location in destination block
// to copy another element (if any remaining)
DCX H
JNZ COPY
// Stop the program
HLT```

Explanation:

My Personal Notes arrow_drop_up