Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 04 May, 2022

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

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

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.
  8. Load HL pair with destination block address.
  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
// Load HL pair with source block address
    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
    ADD C
    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:

INSTRUCTIONS EXPLANATION OF INSTRUCTIONS
# BEGIN 0000H The program begins at address 0000H
LXI H, C000H Load HL pair with the source block address
LXI SP, B000H Initialize stack pointer
MOV B,M save the length of the source block in register B
MOV C,M save the length of the source block in register C
INX H Increment HL pair to point to the first element of the source block

SAVE:  

MVI D,00H

Push all elements from the source block onto the stack

Move 00H to D, to clear register D

MOV E,M Move content pointed by HL pair into register E
PUSH D Push content of DE register pair onto the stack
DCR C Decrement register C
INX H Increment HL pair
JNZ SAVE Jump to SAVE if the zero flag is not set
MOV C,B Restore the length of the source block in register C from register B
MOV D,M Save destination address in DE register pair
INX H Increment HL pair
MOV E, M Move content pointed by HL pair into register E
MOV A, E 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 the block and copy elements one by one and keep decrementing after copying each element.
ADD C Add the content of C to the accumulator
DCR A Decrement content of accumulator
MOV E, A Move content of accumulator to register E
XCHG exchange the content of HL pair with DE register pair
COPY:
POP D
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
 
MOV M,E Move element from DE register pair to the destination location
DCR C Decrement the counter.
 DCX H Update HL pair to the next free location in the destination block to copy another element (if any remaining)
JNZ COPY Jump to COPY if the zero flag is not set.
HLT Stop the program.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!