please dont rip this site

Z80 Assembly - Instructions: Moving Data

LD

Syntax: ld op1,op2

The ld instruction copies the value of the second operand into the first operand. It does not alter any of the flags, except for the special cases of reading the I or R registers. The two operands must fit in size; they can be either 8 or 16 bits long. The two tables below show all the possibilities. The first operand is on the left column, the second on the top row. imm denotes an immediate (either 8 or 16-bit constant), n is a signed 8-bit integer (ranging from -128 to 127). ofs is a 16-bit memory address.

8-bit combinations

  imm A B C D E H L IXH IXL IYH IYL (BC) (DE) (HL) (IX+n) (IY+n) (ofs) I R
A X N X X X X X X X X X X X X X X X X * *
B X X N X X X X X X X X X     X X X      
C X X X N X X X X X X X X     X X X      
D X X X X N X X X X X X X     X X X      
E X X X X X N X X X X X X     X X X      
H X X X X X X N X             X X X      
L X X X X X X X N             X X X      
IXH X X X X X X     *** X                    
IXL X X X X X X     X ***                    
IYH X X X X X X         *** X                
IYL X X X X X X         X ***                
(BC)   X                                    
(DE)   X                                    
(HL) X X X X X X X X             **          
(IX+n) X X X X X X X X               **        
(IY+n) X X X X X X X X                 **      
(ofs)   X                                    
I   X                                    
R   X                                    

16-bit combinations

  imm BC DE HL IX IY SP (ofs)
BC X             X
DE X             X
HL X             X
IX X             X
IY X             X
SP X     X X X   X
(ofs)   X X X X X X  

Stack operations

There are two things to note about the stack: a) before using push you have to be sure that SP points to a safe place (pop is normally less critical, as it does not write to the memory); b) you must be aware that the call-ret instructions also use the very same stack to preserve the return address.

PUSH

Syntax: push reg16 (reg16: AF, BC, DE, HL, IX, IY)

When the instruction is executed, SP is decreased by two and the value of reg16 is copied to the memory location pointed by the new value of SP. It does not affect the flags.

POP

Syntax: pop reg16 (reg16: AF, BC, DE, HL, IX, IY)

When the instruction is executed, the value of the word found at the memory location pointed by SP is copied into reg16, then SP is increased by 2. No flags are affected (except for the case of popping into AF).

Block operations

Although the following instructions are basically simple loads, they do affect the flags. S, Z and C are not altered, but H and N are set to zero. The P/V flag is interpreted as overflow. None of them have any operands. It is worth noting that a bunch of ldi's/ldd's is always faster than the corresponding ldir/lddr, so if you are optimising for speed, do not use the repeated load instructions.

LDI

The instruction copies a byte from (HL) to (DE) (i. e. it does an ld (de),(hl)), then increases both HL and DE to advance to the next byte. Besides, it decreases BC, and sets the P/V flag in the case of overflowing.

LDD

Almost the same as ldi. The only difference is that HL and DE are decreased on every execution.

LDIR

This is an ldi repeated until BC reaches zero. Naturally, the P/V flag holds zero after leaving the instruction, since BC does not overflow. After all, this single instruction copies BC bytes from (HL) to (DE), increases both HL and DE by BC, and sets BC to zero.

LDDR

Similarly to the previous one, but with ldd repeated (copying backwards).

Exchange operations

These instructions do not alter the flags. However, they are the only ones that access the shadow registers. You can use them for backing up data in a reasonably fast way, but you have to pay attention, since this action alters the contents of the registers you "save", unlike the stack instructions. If you are smart enough, you can effectively extend the registers you use by taking advantage of the shadow registers. When interrupts are enabled, you must not use these instructions, because shadow registers are used to back up all the 8-bit registers in the system interrupt routine!

EX

Syntax: ex op1,op2

The values of the two operands are exchanged. There is a total number of five combinations possible: ex (sp),hl, ex (sp),ix, ex (sp),iy, ex de,hl, ex af,af'. The last one naturally alters the flags (exchanges them with the shadow flags). You cannot exchange the order given, e. g. there is no ex hl,de!

EXX

There are no operands. This instruction exchanges BC with BC', DE with DE' and HL with HL' at the same time. What's very important to note that it is very fast.

Back to the index


file: /Techref/zilog/z80/app1a.htm, 14KB, , updated: 2018/11/6 16:41, local time: 2024/11/13 03:18,
TOP NEW HELP FIND: 
34.204.176.71:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/zilog/z80/app1a.htm"> Z80 Assembly</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .