DCP

From C64-Wiki
Jump to navigationJump to search

DCP (short for "Decrease then ComPare") is a mnemonic for an illegal opcode machine language instruction.

This illegal opcode is a combination of two operations with the same addressing mode: DEC, followed by CMP

Function: {addr} = {addr} - 1; then compare A and {addr}

Addressing modes[edit | edit source]

Opcode Addressing
mode
Assembler
format
Length
in bytes
Number of
cycles
Dec Hex
207 CF Absolute DCP nnnn 3 6
223 DF Absolute,X DCP nnnn,X 3 7
219 DB Absolute,Y DCP nnnn,Y 3 7
199 C7 Zeropage DCP nn 2 5
215 D7 Zeropage,X DCP nn,X 2 6
195 C3 Indexed-indirect DCP (nn,X) 2 8
211 D3 Indirect-indexed DCP (nn),Y 2 8

DCP supports the 7 different addressing modes shown in the table at right. In the assembler formats listed, nn represents a single-byte (8-bit) figure, and nnnn is a two-byte (16-bit) address.

DCP can be used to effectively implement the indexed-indirect and indirect-indexed addressing modes that DEC lacks

CPU flags[edit | edit source]

DCP affects 3 of the CPU's status flags according to the CMP compare, after the decrement:

  • The negative flag is set if the result is negative, i.e. has its most significant bit set.
  • The zero flag is set if the result is zero, or cleared if it is non-zero.
  • The carry flag is set or cleared depending on the result.

Examples[edit | edit source]

Decrementing a 16 bit pointer

DCP can be used in a simple trick to decrement a 16 bit pointer:

LDA #$FF
DCP pointer ; Decrease pointer low byte and compare result to A=#$FF
BNE +
DEC pointer+1 ; Decrease pointer high byte if underflow occurred
+

The standard implementation

LDA pointer
BNE +
DEC pointer+1
+
DEC pointer

consumes at least 1 more cycle and possibly 1 more byte in code size depending on the addressing mode to access pointer.

Links[edit | edit source]