Searching \ for 'Absolute Difference Algorithm' in subject line. ()
Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=absolute+difference
Search entire site for: 'Absolute Difference Algorithm'.

Truncated match.
'Absolute Difference Algorithm'
1998\01\13@115337 by

Happy New Year,
I was just wondering if anyone has an elegant method for determining
whether two numbers are within an absolute difference, in the 12 bit PIC
universe.

For example...  the two numbers, X & Y, are in registers which must
remain unchanged are to be tested to determine whether they are within
DIFF of each other.

My first try this year is:

MOVF       X,W        ;Try it this way
SUBWF   Y,W

SKPNC                  ; Positive?
GOTO      POS      ; Yes

MOVF       Y,W       ; No, then do it the other way
SUBWF   X,W

POS  SUBWF  DIFF,W  ; Compare against the limit

SKPNC                  ; The carry knows...
GOTO     LESS

Jen :-)
Not elegant but seven instructions and free:

movf    X,W             ; get first number.
subwf   Y,W             ; sub second number.
bc      AA              ; result positive? - Yes, go to AA.
xorlw   h'FF'           ; convert from two's compliment
addlw   1               ;   negative to positive.
AA  subwf   DIFF,w          ; sub difference from DIFF.
bc      LESS            ; result + or 0? - Yes, goto LESS.

On Tue, 13 Jan 1998 11:49:18 EST, you wrote:

->Happy New Year,
->   I was just wondering if anyone has an elegant method for
determining
->whether two numbers are within an absolute difference, in the 12 bit
PIC
->universe.
->
->  For example...  the two numbers, X & Y, are in registers which
must
->remain unchanged are to be tested to determine whether they are
within
->DIFF of each other.
->
->  My first try this year is:
->
->          MOVF       X,W        ;Try it this way
->          SUBWF   Y,W
->
->          SKPNC                  ; Positive?
->          GOTO      POS      ; Yes
->
->          MOVF       Y,W       ; No, then do it the other way
->          SUBWF   X,W
->
->POS  SUBWF  DIFF,W  ; Compare against the limit
->
->          SKPNC                  ; The carry knows...
->          GOTO     LESS
->
->
->   Jen :-)
That's pretty close to perfect.  You could also try this...

movf    X,W
subwf   Y,W

skpc
sublw   0x00    ;If it is negative, make it positive.

subwf   DIFF,W
skpnc
goto    LESS...

Not much of an improvement, but maybe a little cleaner :)

----------
{Quote hidden}

On Wed, 14 Jan 1998 12:02:07 GMT Lynn Richardson
>Not elegant but seven instructions and free:

>    movf    X,W             ; get first number.
>    subwf   Y,W             ; sub second number.
>    bc      AA              ; result positive? - Yes, go to AA.
>    xorlw   h'FF'           ; convert from two's compliment
>    addlw   1               ;   negative to positive.
>AA  subwf   DIFF,w          ; sub difference from DIFF.
>    bc      LESS            ; result + or 0? - Yes, goto LESS.
>

I think that will assemble to 9 instructions when the psuedo-ops are
expanded, equal to the original...

{Quote hidden}

On Wed, 14 Jan 1998 10:22:48 -0700 Justin Crooks <jcrooksWYOMING.COM>
writes:
{Quote hidden}

Correct me if wrong but, I don't believe that "SUBLW" is part of the
12 bit PIC world...

Andy Warren, who reputation exceeds himself in this venue, sent me a
version using the same logic employing a file register either
conveniently or purposefully cleared to 0.

********************************

Perhaps, I should expand a bit on the conditions...

If it helps, DIFF can be a constant instead of a file register.

X & Y are in the range of 0 to 32

DIFF ranges between 4 & 9

It would be nice to optimize this, as there are several similar
operations in the application.

Jen :-)
Sorry, I live in the 14 bit PIC world... I guess I should do a little
research before putting in my 2 bits!!!
:)

----------
{Quote hidden}

Hello Jennifer.

Slightly different variant: (i don't like to use GOTO operator ;)))

;1
MOVFW   X
SUBWF   Y,W
;2
SKPC
MOVFW   Y
SKPC
SUBWF   X,W
;3
SUBWF   DIFF,W
;....

But i think code with with Zero_constant register will be shorter

;2
SKPC
SUBWF   ZERO_VAL,W
;3

WBR Dmitry.

{Quote hidden}

[Goal: See how |X-Y| compares with DIFF]

> Slightly different variant: (i don't like to use GOTO operator ;)))
>
> ;1
>         MOVFW   X
>         SUBWF   Y,W
> ;2
>         SKPC
>         MOVFW   Y
>         SKPC
>         SUBWF   X,W
> ;3
>         SUBWF   DIFF,W
> ;....

If you can spare another register which holds the value (2diff) and you
use the same value of "diff" a lot, you could use:

; First get the difference
movf    x,w
subwf   y,w
; Now see if it's in the range -diff..diff
addwf   diff,w                  ; i.e. in 0..2diff
subwf   diff2,w                 ; If was in range, result >= 0

Perhaps more of a sacrifice than a known-zero register, but I don't think
you can do it in four cycles any other way.
On Wed, 14 Jan 1998 23:57:38 -0600 John Payson <supercatMCS.NET> writes:
>If you can spare another register which holds the value (2diff) and you
>use the same value of "diff" a lot, you could use:
>
>        ; First get the difference
>        movf    x,w
>        subwf   y,w
>        ; Now see if it's in the range -diff..diff
>        addwf   diff,w                  ; i.e. in 0..2diff
>        subwf   diff2,w                 ; If was in range, result >= 0
>
>Perhaps more of a sacrifice than a known-zero register, but I don't
think
>you can do it in four cycles any other way.

Cute!   Only 6 instructions after the conditional test is appended.
Very useful in the target application.   It is worth the couple extra
instructions of overhead to initialize the 2DIFF register, since there
are many tests with the same DIFF value.

Thanks for the suggested algorithms!
Jen :-)

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...