Useless instructions: Difference between revisions

→‎{{header|Z80 Assembly}}: added more examples
(→‎{{header|Z80 Assembly}}: added more examples)
Line 275:
 
=={{header|Z80 Assembly}}==
<code>CP 0</code> is nearly useless, but for a different reason than on 6502 Assembly. Unlike the 6502, loading a value into the Z80's main registers <i>never updates the flags*</i>. However, it's much better to use <code>OR A</code>, which bitwise ORs <code>A</code> with itself, setting the zero and carry flags the same way that <code>CP 0</code> would, but takes one fewer byte and 3 fewer CPU states. (Keep in mind that the compare instruction can only compare the accumulator <code>A</code> with something else.) Comparing to zero does set some flags differently, which can matter for a <code>DAA</code>, but these situations are few and far between.
<code>xor a</code> is shorter than <code>ld a,0</code>. The latter doesn't clear the zero or carry flags, which is often useful. But if the flags aren't needed for an upcoming branch, call, or return, it's preferred to use <code>xor a</code>.
 
(*The only exception is the <code>I</code> and <code>R</code> registers, which when loaded into <code>A</code> will set the <code>P/V</code> flag when interrupts are enabled.)
 
<code>RES #,A</code> and <code>SET #,A</code> are one CPU state slower than an AND/OR with the appropriate bit mask. Bit masks allow you to set or clear multiple bits in one go, as well as have a variable operand, which the <code>BIT/SET/RES</code> instructions cannot do without self-modifying code.
 
It should be pointed out that bit masks only really work with <code>A</code>, not other registers. For other registers, <code>SET</code> and <code>RES</code> are perfectly usable.
 
Compare the following code snippets, which are functionally identical but the latter is half the size and more than double the speed:
 
<lang z80>SET 7,A
SET 6,A
RES 5,A
RES 4,A
;TOTAL 8 BYTES, 32 CPU STATES
 
OR %11000000
AND %11001111
;TOTAL 4 BYTES, 14 CPU STATES</lang>
1,489

edits