Jump to content

Category:Z80 Assembly: Difference between revisions

changed tags and cleaned up
m (changed tags)
(changed tags and cleaned up)
Line 66:
Suppose you have a byte at some memory location and you want to know if it's odd or even.
 
<syntaxhighlight lang=z80>
If you don't need the actual value, the first choice would be:
<langLD z80>BIT 0A,(HL) ; 21 bytesbyte, 12 7 cycles
JR nz,odd</lang>
 
You could also do the following:
<lang z80>LD A,(HL) ; 1 byte, 7 cycles
BIT 0,A ; 2 bytes, 8 cycles
JR nz,odd</lang>
</syntaxhighlight>
 
If you don't need the actual value, theyou firstcan choice would beuse:
<syntaxhighlight lang=z80>
BIT 0,(HL) ; 2 bytes, 12 cycles
JR nz,odd</lang>
</syntaxhighlight>
 
But there are also a few other ways to do it, which are faster and/or take fewer bytes to execute. <code>BIT 0,A</code> takes 2 bytes to encode. So does <code>AND 1</code> but it's a little faster. It does destroy the accumulator but if you don't care about that, it's better to use <code>AND 1</code>.
 
<syntaxhighlight lang=z80>
But there are also a few other ways to do it, which are faster and/or take fewer bytes to execute. <code>BIT 0,A</code> takes 2 bytes to encode. So does <code>AND 1</code> but it's a little faster. It does destroy the accumulator but if you don't care about that, it's better to use AND 1.
<lang z80>LD A,(HL) ; 1 byte, 7 cycles
AND 1 ; 2 bytes, 7 cycles
JR nz,odd</lang>
</syntaxhighlight>
 
Or is it? There's an even faster way than this that takes only ONE byte to encode:
<syntaxhighlight lang=z80>
<lang z80>LD A,(HL) ; 1 byte, 7 cycles
<lang z80>LD A,(HL) ; 1 byte, 7 cycles
RRCA ; 1 byte, 4 cycles
JR c,odd</lang>
</syntaxhighlight>
 
If you don't care about destroying the accumulator (i.e. you only need the result of the test and not the number being tested) then <code>RRCA</code> outperforms <code>AND 1</code> in every way. <code>AND 1</code> in this case still has its uses if you need bit 0 to reflect the result of the test. If you don't, use <code>RRCA</code> instead.
 
===Bit Shifting===
Cookies help us deliver our services. By using our services, you agree to our use of cookies.