The CPX & CPY instructions subtracts the data at the address in the operand from the contents of the relevant index register, setting the n, z & c flags based on the result. The register & Memory are unaffected by this operation.
The primary use of the CPX or CPY instructions is to test the value of the index register against loop boundaries.
Data size
On all processors, the data added from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags Affected
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero | ||||||||
c | Set if register value greater than or equal or Cleared if less than memory value |
Instructions
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
CPX #const | E0 | x | x | x | 21 | 22 | Immediate |
CPX addr | EC | x | x | x | 3 | 42 | Absolute |
CPX dp | E4 | x | x | x | 2 | 32, 3 | Direct Page |
CPY #const | C0 | x | x | x | 21 | 22 | Immediate |
CPY addr | CC | x | x | x | 3 | 42 | Absolute |
CPY dp | C4 | x | x | x | 2 | 32, 3 | Direct Page |
Notes:
- 65816: Add 1 byte if m=0 (16-bit memory/accumulator)
- 65816: Add 1 cycle if m=0 (16-bit memory/accumulator)
- 65816: Add 1 cycle if low byte of Direct Page register is not 0