Data Copy Between Z80 Registers and Memory
To copy data from and into memory, the 16-bit address of a selected memory register must be specified, and this memory address can be specified by using indirect, direct, or other addressing modes in the indirect addressing mode, the address of a memory location is loaded into the HL register, and the register (HL) with the parentheses is used as a memory pointer to copy data. Registers BC and, DE can also be used in this manner, with some restrictions. In the direct addressing mode, the 16-bit address of a memory location is used as the operand of the copy instruction. Methods using index registers are-discussed after the discussion of 2’s complement arithmetic because the index registers include a displacement byte, which is expressed as a signed 2’s complement number. In Z80 mnemonics, the memory address is enclosed in parentheses, as shown in the following list.
Opcode |
Operand |
Bytes |
Addressing Modes |
Description |
||||
LD |
r, (HL) Example: LD B, (HL) |
1 |
Register Indirect |
Copy contents of memory into register r. The memory address is specified indirectly by the number in the HL register; therefore, this is called register indirect addressing. |
||||
LD |
(HL), r Example: LD (HL), C |
1 |
Register Indirect |
Copy contents of register r into memory, the address of which is in HL. |
||||
LD |
(HL), 8-bit Example: LD (HL), 97H |
2 |
Register Indirect & Immediate |
Copy 8-bit data into memory. This mode is a combination of indirect and immediate addressing. |
||||
Note: In these three instructions, the memory address is specified by the contents of register HL, and register r can be any one of the general-purpose registers. |
||||||||
LD |
A, (rp) |
1 |
Register Indirect |
Copy contents of memory into accumulator. |
||||
LD |
(rp), A Example: LD (BC), A |
1 |
Register Indirect |
Copy contents of accumulator into memory. |
||||
Note: In the preceding two instructions, the-memory address is shown by the contents of a register pair (BC or DE). However, these instructions can copy data from and into the accumulator only. |
||||||||
LD |
A, (16-bit) |
3 |
Extended |
Copy contents of memory into accumulator. |
||||
LD |
(16-bit), A Example: LD (2050H), A |
3 |
Extended |
Copy contents of accumulator into memory. |
||||
Note: In these instructions, the memory address is the 16-bit operand, and these instructions can copy data from and into the accumulator only. |
||||||||
General Characteristics
I. No flags are affected by these data copy operations.
2. Memory-related data copy operations can be recognized by the parentheses around the operand.
3. Register HL is a versatile memory pointer; a data byte can be copied from any memory location to any general-purpose register and vice versa. In addition, HL can be used to load a byte directly into memory.
4. A 16-bit direct address and other register pairs (BC and DE) can be used as memory pointers to copy data from a memory location into the accumulator and vice versa. However, these memory pointers cannot be used to copy data between general-purpose registers and memory.
Example
The memory location 2050H contains the data byte 37H. Write instructions to copy the byte from the memory location into register B. Illustrate three different ways of transferring the byte from memory to the microprocessor, and list the associated machine codes.
Solution
1. The first method of copying a byte from memory into the microprocessor is by using the HL register as a memory pointer; this is an illustration of indirect addressing. First, we need to load the memory address into the HL register and then use the contents of HL as a memory pointer .
2. The second method of copying a byte from memory into the microprocessor is by using BC or DE as a memory pointer; this is also the indirect addressing . However, these registers (BC and DE) can be used as pointers to copy into A only. Therefore, one more instruction is necessary to copy from A into B.
3. The third technique is to use the direct extended addressing copies a data byte from memory into A and then into B.
Example
The memory location 2040H contains the data byte F2H. copy the data byte F2H from the memory location 2040H into 2070H using memory pointers Then, clear the memory location 2040H. Enter the machine codes of these instructions in memory locations starting from 2000H Describe how data copy operations are performed.
Solution
Memory Address |
Hex Code |
Opcode |
Operand |
Comments |
2000 |
21 |
LD |
HL, 2040H |
; Set up HL as memory pointer for 2040H |
2001 |
40 |
|||
2002 |
20 |
|||
2003 |
01 |
LD |
BC, 2070H |
; Set up B£; as memory pointer for 2070H |
2004 |
70 |
|||
2005 |
20 |
|||
2006 |
7E |
LD |
A, (HL) |
; Copy data (F2H) into accumulator |
2007 |
02 |
LD |
(B,C),A |
; Copy data into memory (2070H) |
2008 |
36 |
LD |
(HL), 00 |
; Clear location 2040H |
2009 |
00 |
|||
200A |
76 |
HALT |
Description
1. The first two instructions load registers HL and BC with the numbers 2040H and 2070H, respectively, These are not memory-related data copy instructions because the operands Jack parentheses.
2. The next two instructions copy the data byte (F2H) stored in memory location 2040H into the accumulator and from the accumulator into location 2070H
3. The next instruction LD (HL), 00 is a 2-byte instruction; it clears the memory location 2040H by loading 00 into the memory location pointed to by the H register.