1,885
edits
Changes
no edit summary
For full details on these instructions, see the [[6502 Math]] page.
== Test and Comparison Operations ==
The A, X, and Y registers can be directly compared with immediate or memory values:
CMP ; compare (accumulator)
CPX ; compare (X register)
CPY ; compare (Y register)
These operations are performed by subtraction. The appropriate condition flags are set, and the result of the subtraction is discarded. Conditional branch instructions can be used to alter program flow based on the results of the comparisons.
There is another test instruction available:
BIT ; bit test
This instruction places bit 7 of the operand into the N flag and bit 6 of the operand into the V flag. The operand is then ANDed with the accumulator, and the Z flag is set if the result is zero. The result of the AND is discarded. In this way, you can test the value of bit 7, bit 6, or any arbitrary bits (using the operand).
Note that in addition to these instructions, many other instructions (such as register loads) affect condition flags.
{{Admon/tip|Watch the Carry Flag!|Failing to clear the carry flag before addition or to set the carry flag before subtraction is the cause of many bugs in 6502 programs. The carry flag also affects the rotate instructions. Be sure to set or clear this flag with the <code>SEC</code> or <code>CLC</code> instructions when needed!}}