CP
8-bit Arithmetic Group
Expansion:
ComPare
Usage:
cp {r|??}
The compare instruction is used to compare a value in the accumulator (A) with another value (either an immediate value or a value stored within another register). CP then sets the flags in the same way as the SUB instruction but does not store/update the result in the accumulator. This makes it useful for passively comparing a value.
| Instruction | Description | Opcode |
|---|---|---|
| CP A | Compare A with itself | BF |
| CP B | Compare the 8-bit number in B register with value in A register | B8 |
| CP C | Compare the 8-bit number in C register with value in A register | B9 |
| CP D | Compare the 8-bit number in D register with value in A register | BA |
| CP E | Compare the 8-bit number in E register with value in A register | BB |
| CP H | Compare the 8-bit number in H register with value in A register | BC |
| CP L | Compare the 8-bit number in L register with value in A register | BD |
| CP {??} | Compare the 8-bit immediate number with value in A register | FE {??} |
| CP (HL) | Compare the 8-bit number at memory address HL with value in A register | BE |
| CP (IX+displacement) | Compare the 8-bit value in IX with value in A register | DDBE ?? |
| CP (IY+displacement) | Compare the 8-bit value in IY with value in A register | FDBE ?? |
Example
; Example
ld a, 10 ; load the value 10 into register A
cp 15 ; compare A with 15
; flags are set based on the result (A - 15)
; A is not modified
jr z, EQUAL ; jump to label EQUAL if A == 15 (Zero flag set)
jr c, LESS ; jump to label LESS if A < 15 (Carry flag set)
jr nc, GREATER ; jump to label GREATER if A >= 15 (Carry flag not set)
;
EQUAL: ; code for A == 15
jp THEEND
LESS: ; code for A < 15
jp THEEND
GREATER: ; code for A >= 15
;
THEEND: ; end of program