Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

INSTRUCTIONSEXPLANATION OF INSTRUCTIONS
# BEGIN 0000HThe program begins at address 0000H
LXI H, C000HLoad HL pair with the source block address
LXI SP, B000HInitialize stack pointer
MOV B,Msave the length of the source block in register B
MOV C,Msave the length of the source block in register C
INX HIncrement 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,MMove content pointed by HL pair into register E
PUSH DPush content of DE register pair onto the stack
DCR CDecrement register C
INX HIncrement HL pair
JNZ SAVEJump to SAVE if the zero flag is not set
MOV C,BRestore the length of the source block in register C from register B
MOV D,MSave destination address in DE register pair
INX HIncrement HL pair
MOV E, MMove content pointed by HL pair into register E
MOV A, ETo 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 CAdd the content of C to the accumulator
DCR ADecrement content of accumulator
MOV E, AMove content of accumulator to register E
XCHGexchange 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,EMove element from DE register pair to the destination location
DCR CDecrement the counter.
 DCX HUpdate HL pair to the next free location in the destination block to copy another element (if any remaining)
JNZ COPYJump to COPY if the zero flag is not set.
HLTStop the program.
My Personal Notes arrow_drop_up
Last Updated : 04 May, 2022
Like Article
Save Article
Similar Reads