CPX

CPX (short for "ComPare X") is the mnemonic for a machine language instruction which compares the contents of the X index register against that of the specified operand by subtracting the latter from the former, and setting the negative and carry flags according to the result. Unlike SBC, the result of the subtraction is discarded rather than stored back into the register, which is thus unaffected by the CPX operation.

To compare integers in machine language, first use a comparison instruction such as CPX, CPY or CMP to compare the numbers, then "test" the result using a conditional branch instruction that acts on the carry and negative flags, i.e. BCC, BCS, BMI, or BPL:

If the compared values are unsigned integers, use BCC to branch if the contents of the X index register is less than that of the memory address, and BCS to branch if the register holds a number equal to or larger than that in memory.

If the compared values are signed integers, use BMI to branch if the contents of the X index register is less than that of the memory address, and BPL to branch if the register holds a number equal to or larger than that in memory.