CMP (CoMPare)

A - [operand]

CMP compares the accumulator register (A) to an operand. It subtracts the operand from A and sets the flags accordingly, but discards the result. The flags are set as in the SBC instruction.

The CMP instruction is often used before a branch instruction.

The flags that are set by CMP can be interpreted differently based on whether the operands are signed or unsigned.

Addressing Modes

Mode

Syntax

Bytes

Cycles

Immediate

CMP #const

C9 nn

2

Zero Page

CMP zp

C5 nn

3

Zero Page,X

CMP zp,X

D5 nn

4

Absolute

CMP addr

CD ll hh

4

Absolute,X

CMP addr,X

DD ll hh

4-5

Absolute,Y

CMP addr,Y

D9 ll hh

4-5

(Indirect,X)

CMP (zp,X)

C1 nn

6

(Indirect),Y

CMP (zp),Y

D1 nn

5-6

Flags Affected

  • N (Negative) – Set if the result of the comparison has the most significant bit set (i.e., is negative); otherwise, it is cleared.

  • Z (Zero) – Set if the result of the comparison is zero; otherwise, it is cleared.

  • C (Carry) – Set if A is greater than or equal to the operand; otherwise, it is cleared.

Examples

Compare to Immediate Value

    LDA #10    ; load A with constant 10
    CMP #5     ; compare A with constant 5

Compare to Value in Zero Page

    LDA #10    ; load A with constant 10
    CMP $00    ; compare A with the value at address $00

Compare to Value in Absolute Addressing

    LDA #10    ; load A with constant 10
    CMP $2000  ; compare A with the value at address $2000

Comments