Software - INC and DEC techniques

Typical 16-bit increment

Overflow (incrementing to $0000) sets the Z flag is set (i.e. BEQ branches).

;
      INC NUML
      BNE LABEL
      INC NUMH
LABEL

Typical 16-bit decrement

;
      LDA NUML
      BNE LABEL
      DEC NUMH
LABEL DEC NUML

16-bit decrement, test for zero

;
      LDA NUML
      BNE LABEL
      LDA NUMH
      BEQ ZERO ; branch when NUM = $0000 (NUM is not decremented in that case)
      DEC NUMH
LABEL DEC NUML

Add 255

;
      LDA NUML
      BEQ LABEL
      INC NUMH
LABEL DEC NUML

Subtract 255

;
      INC NUML
      BEQ LABEL
      DEC NUMH
LABEL

Constant time increment

6 cycles

A = high byte, X = low byte

Overflow (incrementing to $0000) sets the carry

CPX #$FF
INX
ADC #$00

Constant time decrement

6 cycles

A = high byte, X = low byte

Underflow (decrementing to $FFFF) clears the carry

CPX #$01
DEX
SBC #$00

Increment and stop at $00

This counts up to $FF then to $00, then stays at $00.

CMP #1
ADC #0

One advantage of a CMP before the ADC (and SBC below) is that the accumulator always contains the correct value. If it were an increment, compare, branch, and decrement (to return to the stop value), then it would briefly, but temporarily, not be at the stop value.

Decrement and stop at $FF

This counts down to $00 then to $FF, then stays at $FF.

CMP #$FF
SBC #0
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License