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 ORA num+1 BNE loop This can be expanded for 24-bit, 32-bit, and larger 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 ORA num+1 ORA num+2 ORA num+3 BNE loop The temporary register can be A, X or Y, whichever is most convenient. You can use the same method to do a 16-bit decrement of a value in the XY registers: TXA:BNE skip :\ Skip if low byte not about to wrap from &00 to &FF DEY :\ Decrement high byte .skip DEX :\ Decrement low byte \ The following code tests for zero BNE loop TYA:BNE loop This is the complement to a 16-increment in XY: INX:BNE done INY .done or INX:BNE loop INY:BNE loop With reference to http://www.obelisk.me.uk/6502/algorithms.html.