Searching \ for '[PIC]: Simple challenge' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Simple challenge'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Simple challenge'
2000\11\01@044126 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi, have a small simple challenge:

I have a 3-byte var ( 24 bits ) that contains
the difference of two other 24bit vars. Now
I want to check that the difference is less than
1 byte ( i.e. diff <= 256 ). The culprit is that
the difference is signed.


For positive check one could use:
( note DataDiff+2 = lsb )

       MOVF    DataDiff,W
       XORWF   DataDiff+1,W
       ; a) positive result= zero flag is set is difference is smaller
than 1 byte
       BTFSS   STATUS,Z
       GOTO    DIFF_TO_LARGE ; nope difference larger than one byte
       ; diff less than one byte

And likewise for negative check one could use:

       COMF    DataDiff,W
       BTFSS   STATUS,Z
       GOTO    DIFF_TO_LARGE   ; nope difference in top byte ?
       COMF    DataDiff+1,W
       BTFSS   STATUS,Z
       GOTO    DIFF_TO_LARGE   ; nope difference in middle byte        
       ; diff less than one byte

Ofcource these can be combined, but it seems un-effective,
and contains quite a few goto's. Are there any other neat
solutions out there ?
I.e. test two bytes for the condition :

IF( !(Byte|Byte+1) || !(~Byte|~Byte+1))GOTO FUN_STUFF



/Tony





Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: spam_OUTtony.kubekTakeThisOuTspamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use .....listservKILLspamspam@spam@mitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@045804 by Alan B. Pearce

face picon face
>I have a 3-byte var ( 24 bits ) that contains
>the difference of two other 24bit vars. Now
>I want to check that the difference is less than
>1 byte ( i.e. diff <= 256 ). The culprit is that
>the difference is signed.

Will an XOR of the two high bytes not detect it for both positive and negative
cases? It obviously does for the positive case. Now for the negative case
0xFFFFFF = -1, and when you Xor the high bytes the result will again be 0. But
0xFFFF00 is -256, so the xor should still work.

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservspamKILLspammitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\01@070557 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi,
Alan Pearce wrote:

>Will an XOR of the two high bytes not detect it for both positive and
negative
>cases? It obviously does for the positive case. Now for the negative
case

Yes ofcource, but I just didn't see it, knew it was something
simple. Sorry.

/Tony


Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: .....tony.kubekKILLspamspam.....flintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use EraseMElistservspam_OUTspamTakeThisOuTmitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@074807 by Scott Dattalo

face
flavicon
face
On Wed, 1 Nov 2000, [Iso-8859-1] Kübek Tony wrote:

{Quote hidden}

How about:

; First check if two most significant bytes are both zero:

    movf  DataDiff,w    ;get Most significant byte
    iorwf DataDiff+1,w  ;compare to middle significant byte
    skpnz
     goto DIFF_IS_POSITIVE_AND_LESS_THAN_OR_EQUAL_TO_256

; At least one is non-zero. Now, check for both being 0xff:
; (anybody see the subtle trick?)
    incf   DataDiff,w
    skpnz
     incfsz DataDiff+1,w
      goto  DIFF_TOO_LARGE


DIFF_IS_NEGATIVE_AND_GREATER_THAN_OR_EQUAL_TO_256:


-----------------------------------------------

Here's another version:
    comf   DataDiff,w
    xorwf  DataDiff+1,w
    skpz
     goto  DIFF_TOO_LARGE

; we get here if both nibbles are the same
; now check to see if one is either a 0 or 0xff

    movf    DataDiff,w
    incfsz  DataDiff,w
     skpnz
      goto  LESS_THAN_OR_EQUAL_TO_256

DIFF_TOO_LARGE



Or I reckon you could do it this way too:


    movf    DataDiff,w
    incfsz  DataDiff,w
     skpnz
      goto  MAYBE_LESS_THAN_OR_EQUAL_TO_256

    goto  out_of_range

MAYBE_LESS_THAN_OR_EQUAL_TO_256:

    movf    DataDiff+1,w
    incfsz  DataDiff+1,w
     skpnz
      goto  LESS_THAN_OR_EQUAL_TO_256

out_of_range:



Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservspamspam_OUTmitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@083038 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi,

I wrote:
>Alan Pearce wrote:

>>Will an XOR of the two high bytes not detect it for both positive and
>>negative
>>cases? It obviously does for the positive case. Now for the negative
>>case

>Yes ofcource, but I just didn't see it, knew it was something
>simple. Sorry.

But this is ofcource NOT true, XOR will 'only' detect if the bytes are
equal NOT that they ALSO are either 0x00 or 0xFF.
So challenge still stands :)

/Tony



Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: @spam@tony.kubekKILLspamspamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use KILLspamlistservKILLspamspammitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@084312 by Alan B. Pearce

face picon face
>But this is ofcource NOT true, XOR will 'only' detect if the bytes are
>equal NOT that they ALSO are either 0x00 or 0xFF.
>So challenge still stands :)

Well true as far as it goes :) I realised this after looking at the next posting
on this thread. However if one can test one of the upper 2 bytes for 0 or 0xff
before the XOR, does this make it any faster for the complete test?

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use RemoveMElistservTakeThisOuTspammitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\01@085124 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi,

Scott Dattalo wrote:
<snip>
{Quote hidden}

<snip>

Neat.

This one is almost exactly as the one I managed to
figure out. However I still think there are to many
goto's. But maybe I'm just nitpicking.


/Tony


Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: spamBeGonetony.kubekspamBeGonespamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use TakeThisOuTlistservEraseMEspamspam_OUTmitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@090632 by Olin Lathrop

flavicon
face
> Will an XOR of the two high bytes not detect it for both positive and
negative
> cases? It obviously does for the positive case. Now for the negative case
> 0xFFFFFF = -1, and when you Xor the high bytes the result will again be 0.
But
> 0xFFFF00 is -256, so the xor should still work.

It won't work when one number is positive and the other negative but the
difference is still within the 256 threshold.  Consider

 FFFFFF  (-1)
 000001  (+1)

Only a difference of 2 but XOR doesn't work.


*****************************************************************
Olin Lathrop, embedded systems consultant in Devens Massachusetts
(978) 772-3129, RemoveMEolinspamTakeThisOuTcognivis.com, http://www.cognivis.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservEraseMEspam.....mitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\01@093802 by Scott Dattalo

face
flavicon
face
On Wed, 1 Nov 2000, Olin Lathrop wrote:

> > Will an XOR of the two high bytes not detect it for both positive and
> negative
> > cases? It obviously does for the positive case. Now for the negative case
> > 0xFFFFFF = -1, and when you Xor the high bytes the result will again be 0.
> But
> > 0xFFFF00 is -256, so the xor should still work.
>
> It won't work when one number is positive and the other negative but the
> difference is still within the 256 threshold.  Consider
>
>   FFFFFF  (-1)
>   000001  (+1)
>
> Only a difference of 2 but XOR doesn't work.

You and the person to whom you are responding have misunderstood Tony's original
post. If I were you I'd probably say something like, "go back and read the
original posting". But I won't. The 24-bit number being checked already contains
the difference between two other 24-bit numbers. It's true that checking the
upper two bytes with an XOR is insufficient, though.

And then

On Wed, 1 Nov 2000, Alan B. Pearce wrote:

> >But this is ofcource NOT true, XOR will 'only' detect if the bytes are
> >equal NOT that they ALSO are either 0x00 or 0xFF.
> >So challenge still stands :)
>
> Well true as far as it goes :) I realised this after looking at the next
posting
> on this thread. However if one can test one of the upper 2 bytes for 0 or 0xff
> before the XOR, does this make it any faster for the complete test?
>

Good suggestion!

  movf   DataDiff,w
  incfsz DataDiff,w
   skpnz
    movf DataDiff+1,w

;If the first byte is 0 or 0xff, then W contains the 2nd byte
;otherwise, W contains the first (byte + 1)

  andwf  DataDiff+1,w
  xorwf  DataDiff,w
  skpz
   goto  out_of_range

IN_RANGE:

Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use EraseMElistservspammitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\01@095036 by Scott Dattalo

face
flavicon
face
On Wed, 1 Nov 2000, Scott Dattalo wrote:

>    movf   DataDiff,w
>    incfsz DataDiff,w
>     skpnz
>      movf DataDiff+1,w
>
> ;If the first byte is 0 or 0xff, then W contains the 2nd byte
> ;otherwise, W contains the first (byte + 1)
>
>    andwf  DataDiff+1,w
>    xorwf  DataDiff,w
>    skpz
>     goto  out_of_range
>
> IN_RANGE:

which of course can be shortened:


  movf   DataDiff,w
  incfsz DataDiff,w
   skpnz
    movf DataDiff+1,w

;If the first byte is 0 or 0xff, then W contains the 2nd byte
;otherwise, W contains the (first byte + 1)

  xorwf  DataDiff,w
  skpz
   goto  out_of_range

IN_RANGE:



the reason is  (x+1) ^ x = 1


Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use RemoveMElistservEraseMEspamEraseMEmitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\01@100837 by Nikolai Golovchenko

flavicon
face
Yet another one:

       movf DataDiff, w        ;compare higher and middle bytes
       xorwf DataDiff+1, w     ;of difference

       skpz                    ;skip if bytes are equal
        goto DIFF_TOO_LARGE

       incfsz DataDiff+1, w    ;skip if 255 (zero flag not
                               ;modified)
        movf DataDiff+1, w     ;load w if not 255 (to set
                               ;zero flag according to
                               ;value)
       skpz
        goto DIFF_TOO_LARGE


Nikolai

---- Original Message ----
From: Kübek Tony <RemoveMEtony.kubekspam_OUTspamKILLspamFLINTAB.COM>
Sent: Wednesday, November 01, 2000 15:42:00
 To: RemoveMEPICLISTTakeThisOuTspamspamMITVMA.MIT.EDU
Subj: [PIC]: Simple challenge

{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use listservSTOPspamspamspam_OUTmitvma.mit.edu?bodyT%20PICList%20DIGEST




2000\11\01@115921 by Scott Dattalo

face
flavicon
face
On Wed, 1 Nov 2000, Nikolai Golovchenko wrote:

> Yet another one:
>
>         movf DataDiff, w        ;compare higher and middle bytes
>         xorwf DataDiff+1, w     ;of difference
>
>         skpz                    ;skip if bytes are equal
>          goto DIFF_TOO_LARGE
>
>         incfsz DataDiff+1, w    ;skip if 255 (zero flag not
>                                 ;modified)
>          movf DataDiff+1, w     ;load w if not 255 (to set
>                                 ;zero flag according to
>                                 ;value)
>         skpz
>          goto DIFF_TOO_LARGE


If this poor cat ain't dead... Here's another layer of skin:

  incfsz  DataDiff,w
   movf   DataDiff,w

 ; W is zero if `DataDiff' is 0 or 0xff

  addwf   DataDiff+1,w
  xorwf   DataDiff+1,w

  skpz
   goto   DIFF_TOO_LARGE


Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use spamBeGonelistservSTOPspamspamEraseMEmitvma.mit.edu?body=SET%20PICList%20DIGEST




2000\11\02@011147 by Nikolai Golovchenko

flavicon
face
Hi Scott,

Neat idea, but doesn't work :)
It would accept any difference like 00xx or FFxx.


Nikolai

---- Original Message ----
From: Scott Dattalo <KILLspamscottspamBeGonespamDATTALO.COM>
Sent: Wednesday, November 01, 2000 19:01:57
 To: EraseMEPICLISTspamEraseMEMITVMA.MIT.EDU
Subj: [PIC]: Simple challenge

{Quote hidden}

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestspamKILLspammitvma.mit.edu




2000\11\03@135243 by w. v. ooijen / f. hanneman

picon face
what about

incf A, w
sublw 2
; now carry = no borrow = ((A == 0 ) | (A == 1))

movwf A
xorwf B, w
; now zero = ( A == B )

; standard jump if b1 and b2
skpnz
skpc
goto larger_difference

Wouter

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:" =uP ONLY! "[EE]:","[OT]:" =Other "[BUY]:","[AD]:" =Ads




2000\11\04@091950 by Scott Dattalo

face
flavicon
face
In response to:
>
> > If this poor cat ain't dead... Here's another layer of skin:
>
> >    incfsz  DataDiff,w
> >     movf   DataDiff,w
>
> >   ; W is zero if `DataDiff' is 0 or 0xff
>
> >    addwf   DataDiff+1,w
> >    xorwf   DataDiff+1,w
>
> >    skpz
> >

On Thu, 2 Nov 2000, Nikolai Golovchenko wrote:

> Hi Scott,
>
> Neat idea, but doesn't work :)
> It would accept any difference like 00xx or FFxx.

Damn!


But then:

On Fri, 3 Nov 2000, w. v. ooijen / f. hanneman wrote:

{Quote hidden}

I guess I'm just going to have start using Jal. Impressive Wouter! However,
there's a slight bug. Here's the corrected version

 incf   A,w   ;If A=0 or A=ff, then W will become 1 or 0
 addlw  -2    ;subtracting 2 will clear the carry if W = 1 or 0
;(Or you could use `sublw 1' and then use skpc below)

 movf   A,w   ;If the upper two bytes are the same
 xorwf  B,w   ; then the Z bit is set after the xor

 skpnz
  skpnc
   goto larger_difference

in_range:

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\11\06@040326 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi,

Scott Dattalo wrote:

{Quote hidden}

However,
{Quote hidden}

Hmmm... THANKS ! :) ... as I said to many goto's from the start
( my main complaint ) but now...neat.

Thanks again to all who responded to this little trivial task.


/Tony



Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: .....tony.kubekspam_OUTspamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
use TakeThisOuTlistserv.....spamTakeThisOuTmitvma.mit.edu?bodyT%20PICList%20DIGEST




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