Category:Z80 Assembly: Difference between revisions

m
changed tags to display code correctly
(changed tags and cleaned up)
m (changed tags to display code correctly)
Line 97:
===Bit Shifting===
The Z80 does have bit shifting, but thanks to RLCA and RRCA it's often faster to rotate instead. Compare the following two code snippets:
<syntaxhighlight lang="Z80">
<lang z80>SLA A
SLA A
SLA A
SLA A
Line 106 ⟶ 107:
RLCA
RLCA
RLCA ;6 bytes, 23 cycles total</lang>
</syntaxhighlight>
 
Not only is the second method shorter, it's also faster. The accumulator-specific bit rotates take 1 byte and 4 clock cycles each. They are different, however, because unlike the two-byte versions, these <i>do not affect the zero flag.</i> This isn't a big deal, however, as more often than not if you're rotating the accumulator you're not expecting to get zero as the output anyway.
Line 113 ⟶ 115:
 
If you want to know zero or not, you could also use this:
<syntaxhighlight lang="Z80">
 
<lang z80>RLCA
RLCA
RLCA
RLCA
RLCA
AND %11110000 ;6 bytes, 23 cycles total</lang>
</syntaxhighlight>
 
For 16-bit bit shifting, use A instead of the other half of the register pair for faster results (unless you're checking for equality to zero, or you need the accumulator for something else.)
 
<syntaxhighlight lang="Z80">
<lang z80>rept 4 ;inline the following 4 times, back to back:
SRL H
RR L
Line 130 ⟶ 134:
SRL H
RRA
endr</lang>
</syntaxhighlight>
 
===Inlined bytecode===