Decrementing 16-bit and larger numbers ====================================== Incrementing a 16-bit or larger number in memory is trivial, as the Zero flag is set when a byte wraps from &FF to &00: INC addr+0:BNE done INC addr+1:BNE done INC addr+2:BNE done INC addr+3 .done However, decrementing is not as simple, as you need to check for the wrap from &00 to &FF. The following does not work: DEC num+0:BNE done DEC num+1:BNE done DEC num+2:BNE done DEC num+3 .done Each byte being decremented must be checked /before/ it is decremented. This requires a register to be corrupted. LDA num+0:BNE skip :\ Skip if low byte not about to wrap from &00 to &FF DEC num+1 :\ Decrement high byte .skip DEC num+0 :\ Decrement low byte \ The following code tests for zero BNE loop LDA num+1:BNE loop This can be expanded for 24-bit and 32-bit counters: LDA num+0:BNE dec0 :\ Skip past if b0-b7 not about to wrap LDA num+1:BNE dec1 :\ Skip past if b8-b15 not about to wrap LDA num+2:BNE dec2 :\ Skip past if b16-b24 not about to wrap DEC num+3 :\ Decrement b24-b31 .dec2 DEC num+2 :\ Decrement b16-23 .dec1 DEC num+1 :\ Decrement b8-b15 .dec0 DEC num+0 :\ Decrement b0-b7 \ Test if num=0 BNE loop LDA num+1:BNE loop LDA num+2:BNE loop LDA num+3:BNE loop The temporary register can be A, X or Y, whichever is most convenient. With reference to http://www.obelisk.demon.co.uk/6502/algorithms.html.