PIC-PROJECTS.NET

Ideas, Circuits and Code

  • Increase font size
  • Default font size
  • Decrease font size
Home Algorithms Arithmetic Multi-byte Increment & Decrement

Multi-byte Increment & Decrement

E-mail Print PDF

All Microchip devices can increment and decrement an 8-bit file register in a single instruction. This article examines how a value spread over multiple file registers can be efficiently adjusted.

The example code is for 16-, 24- and 32-bit values stored in FILE0, FILE1, FILE2 and FILE3 where FILE0 is the least significant byte and FILE3 is the most significant.

Incrementing

The key to multi-byte increment is spotting when a value rolls over from h'ff' to h'00' as this is the trigger to increment the next most significant byte of the value. This turns out to be very easy as the INCF instruction automatically sets the Z bit in the STATUS register to reflect the result of the increment and when used inconjunction with BTFSC instructions to test the flag.

; 16-bit Increment (All devices)
incf FILE0,F ; Add one to LSB
btfsc STATUS,Z ; Overflowed?
incf FILE1,F ; Yes, add one to MSB

; 24-bit Increment (All devices)
incf FILE0,F
btfsc STATUS,Z
incf FILE1,F
btfsc STATUS,Z
incf FILE2,F

; 32-bit Increment (All devices)
incf FILE0,F
btfsc STATUS,Z
incf FILE1,F
btfsc STATUS,Z
incf FILE2,F
btfsc STATUS,Z
incf FILE3,F

The pattern here isĀ  obvious and could be extended on to values of greater length. High performance processors have an INFSNZ (increment file register and skip if not zero) instruction that can be used to replace the first increment and test to shorten the sequences by one instruction.

; 16-bit Increment (High Performance devices)
infsnz FILE0,F
incf FILE1,F

; 24-bit Increment (High Performance devices)
infsnz FILE0,F
incf FILE1,F
btfsc STATUS,Z
incf FILE2,F

; 32-bit Increment (High Performance devices)
infsnz FILE0,F
incf FILE1,F
btfsc STATUS,Z
incf FILE2,F
btfsc STATUS,Z
incf FILE3,F

On high performance and enhanced mid-range devices the ADDWFC instruction offers an alternative means to adjust the value which is as fast as INCF or INFSNZ sequences for 32-bits and faster for larger values providing WREG is available for use.

; 32-bit Increment (Enhanced Mid-Range and High Performance devices)
movlw .0 ; Load zero into WREG
bsf STATUS,C ; And set the carry
addwfc FILE0,F ; Then add to FILE0..FILE3
addwfc FILE1,F
addwfc FILE2,F
addwfc FILE3,F

Decrementing

When decrementing a value we need to know when a value will change from h'00' to h'ff'. The easiest way to do this is to use MOVF set the Z bit in the status register to decide if the subsequent decrement will cause an underflow.

; 16-bit Decrement (All devices)
movf FILE0,F ; Does FILE0 contain zero?
btfsc STATUS,Z
decf FILE1,F ; Yes, decrement the MSB
decf FILE0,F ; Before decrementing the LSB

; 24-bit Decrement (All devices)
movf FILE0,F ; Does FILE0 contain zero?
btfss STATUS,Z
goto Skip ; No.
movf FILE1,F ; Does FILE1 contain zero?
btfsc STATUS,Z
decf FILE2,F ; Yes.
decf FILE1,F
Skip: decf FILE0,F

; 32-bit Decrement (All devices)
movf FILE0,F ; Does FILE0 contain zero?
btfss STATUS,Z
goto Skip ; No.
movf FILE1,F ; Does FILE1 contain zero?
btfss STATUS,Z
goto Jump ; No.
movf FILE2,F ; Does FILE2 contain zero?
btfsc STATUS,Z
decf FILE3,F ; Yes
decf FILE2,F
Jump: decf FILE1,F
Skip: decf FILE0,F

All the testing makes using DECF for large values inefficient. If WREG is available for use then SUBWF offers a better solution for 24-bit and larger values.

; 24-bit Decrement (All devices)
movlw .1 ; Load one into WREG
subwf FILE0,F ; And subtract from FILE0
btfss STATUS,C ; Underflow?
subwf FILE1,F ; Yes, subtract from next byte
btfss STATUS,C ; Underflow?
subwf FILE2,F ; Yes, subtract from next byte

; 32-bit Decrement (All devices)
movlw .1
subwf FILE0,F
btfss STATUS,C
subwf FILE1,F
btfss STATUS,C
subwf FILE2,F
btfss STATUS,C
subwf FILE3,F

And finally, as in the case of increment, the enhanced mid-range and high performance devices offer an even faster solution using the SUBWFB instruction for 32-bit and larger values.

; 32-bit Decrement (Enhanced Mid-Range and High Performance devices)
movlw .0 ; Load zero into WREG
bcf STATUS,C ; and clear the carry
subwfb FILE0,F ; Then subtract from FILE0..FILE3
subwfb FILE1,F
subwfb FILE2,F
subwfb FILE3,F

Last Updated on Saturday, 04 July 2009 08:04