CMP                CMP Compare memory and accumulator                 CMP
  Operation:  A - M                                     N V - B D I Z C
                                                        / . . . . . / /
  +----------------+-----------------------+---------+---------+----------+
  | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles|
  +----------------+-----------------------+---------+---------+----------+
  |  Immediate     |   CMP #$FF            |   $C9   |    2    |    2     |
  |  ZeroPage      |   CMP $FF             |   $C5   |    2    |    3     |
  |  ZeroPage,X    |   CMP $FF,X           |   $D5   |    2    |    4     |
  |  Absolute      |   CMP $FFFF           |   $CD   |    3    |    4     |
  |  Absolute,X    |   CMP $FFFF,X         |   $DD   |    3    |    4*    |
  |  Absolute,Y    |   CMP $FFFF,Y         |   $D9   |    3    |    4*    |
  |  (Indirect,X)  |   CMP ($FF,X)         |   $C1   |    2    |    6     |
  |  (Indirect),Y  |   CMP ($FF),Y         |   $D1   |    2    |    5*    |
  +----------------+-----------------------+---------+---------+----------+
  * Add 1 if page boundary is crossed.
  For penalty cycles on the 65816, check the desired addressing mode.
 65816 Extensions:
  +----------------+-----------------------+---------+---------+----------+
  | Addressing Mode| Assembly Language Form| OP CODE |No. Bytes|No. Cycles|
  +----------------+-----------------------+---------+---------+----------+
  | AbsoluteLong   |   CMP $FFFFFF         |   $CF   |    4    |     5    |
  | AbsoluteLong,X |   CMP $FFFFFF,X       |   $DF   |    4    |     5    |
  | (Indirect)     |   CMP ($FF)           |   $D2   |    2    |     5    |
  | [Indirect Long]|   CMP [$FF]           |   $C7   |    2    |     6    |
  | [Ind.Long],Y   |   CMP [$FF],Y         |   $D7   |    2    |     6    |
  | Relative,S     |   CMP $FF,S           |   $C3   |    2    |     4    |
  | (Indirect,S),Y |   CMP ($FF,S),Y       |   $D3   |    2    |     7    |
  +----------------+-----------------------+---------+---------+----------+
What it does: Compares the byte in memory to the byte in the Accumulator. Three flags are affected, but the bytes in memory and in the Accumulator are undisturbed. A CMP is actually a subtraction of the byte in memory from the byte in the Accumulator. Therefore, if you LDA #15:CMP #15-the result (of the subtraction) will be zero, and BEQ would be triggered since the CMP would have set the Z flag.
Major uses: This is an important instruction in ML. It is central to IF-THEN and ON-GOTO type structures. In combination with the B branching instructions like BEQ, CMP allows the 6502 chip to make decisions, to take alternative pathways depending on comparisons. CMP throws the N, Z, or C flags up or down. Then a B instruction can branch, depending on the condition of a flag.
Often, an action will affect flags by itself, and a CMP will not be necessary. For example, LDA #15 will put a 0 into the N flag (seventh bit not set) and will put a 0 into the Z flag (the result was not 0). LDA does not affect the C flag. In any event, you could LDA #15: BPL TARGET, and the branch would take effect. However, if you LDA $20 and need to know if the byte loaded is precisely $0D, you must CMP #$OD:BEQ TARGET. So, while CMP is sometimes not absolutely necessary, it will never hurt to include it prior to branching.
Another important branch decision is based on higher than or less than situations. In this case, you use BCC and BCS to test the C (carry) flag. And you've got to keep in mind the order of the numbers being compared. The memory byte is compared to the byte sitting in the Accumulator. The structure is: memory is less than or equal to the Accumulator (BCC is triggered because the carry flag was cleared). Or memory is more than Accumulator (BCS is triggered because the carry flag was set). Here's an example. If you want to find out if the number in the Accumulator is less than $40, just CMP #$41:BCC LESSTHAN (be sure to remember that the carry flag is cleared if a number is less than or equal; that's why we test for less than $40 by comparing with a $41):
LDA #75
CMP #$41; IS IT LESS THAN $40?
BCC LESSTHAN
One final comment about the useful BCC/BCS tests following CMP: It's easy to remember that BCC means less than or equal and BCS means more than if you notice that C is less than S in the alphabet.
The other flag affected by CMPs is the N flag. Its uses are limited since it merely reports the status of the seventh bit; BPL triggers if that bit is clear, BMI triggers if it's set. However, that seventh bit does show whether the number is greater than (or equal to) or less than 128, and you can apply this information to the ASCII code or to look for BASIC keywords or to search data bases (BPL and BMI are used by LADS' data base search routines in the Array subprogram). Nevertheless, since LDA and many other instructions affect the N flag, you can often directly BPL or BMI without any need to CMP first.