CPX                  CPX Compare Memory and Index X                   CPX
  Operation:  X - M                                     N V - B D I Z C
                                                        / . . . . . / /
  +----------------+-----------------------+---------+---------+----------+
  | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles|
  +----------------+-----------------------+---------+---------+----------+
  |  Immediate     |   CPX #$FF            |   $E0   |    2    |    2     |
  |  ZeroPage      |   CPX $FF             |   $E4   |    2    |    3     |
  |  Absolute      |   CPX $FFFF           |   $EC   |    3    |    4     |
  +----------------+-----------------------+---------+---------+----------+
  For penalty cycles on the 65816, check the desired addressing mode.
What it does: Compares the byte in memory to the byte in the X Register. Three flags are affected, but the bytes in memory and in the X Register are undisturbed. A CPX is actually a subtraction of the byte in memory from the byte in the X Register. Therefore, if you LDA # 15:CPX #15-the result (of the subtraction) will be zero and BEQ would be triggered since the CPX would have set the Z flag.
Major uses: X is generally used as an index, a counter within loops. Though the Y Register is often preferred as an index since it can serve for the very useful Indirect Y addressing mode (LDA (15),Y)-the X Register is nevertheless pressed into service when more than one index is necessary or when Y is busy with other tasks.
In any case, the flags, conditions, and purposes of CPX are quite similar to CMP (the equivalent comparison instruction for the Accumulator). For further information on the various possible comparisons (greater than, equal, less than, not equal), see CMP above.