Searching \ for '[pic]: Comparison of signed 16bit values from addi' 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: 'Comparison of signed 16bit values from addi'.

Exact match. Not showing close matches.
PICList Thread
'[pic]: Comparison of signed 16bit values from addi'
2001\03\08@170344 by jamesnewton

face picon face
...just forwarding this to the list. Please respond at
www.piclist.com/techref/microchip/math/basic.htm
or CC
spam_OUTroger.froudTakeThisOuTspamvirgin.net

Seems like somebody must have done this before....

---
James Newton (PICList Admin #3)
.....jamesnewtonKILLspamspam@spam@piclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org

{Original Message removed}

2001\03\08@210608 by Olin Lathrop

face picon face
> I need signed 16bit compare and branch macros for PIC16F877 working on
> pseudo 16bit registers ACCaLO,ACCaHI like some Microchip application
notes.
> There are heaps of 16bit subtract/add routines but none set any useful
> status bits that can be used to branch on the 8 possibilities (signed &
> unsigned)

Below is code that does a 32 bit compare.  It uses the flags byte and other
definitions in STD.INS.ASPIC, which can be found at
http://www.embedinc.com/pic.  It should be pretty trivial to rip out 16 bits
of the compare.


;   Subroutine COMPA
;
;   Compare REGA and REGB.  The arithmetic flags will be set for REGA
;   compared to REGB.  For example, the GT flag will be set if REGA = 5
;   and REGB = 3.
;
.compa   code
        glbsub  compa, noregs

        movlw   ~flag_ar    ;get mask for all but arithmetic flags
        andwf   flags       ;init all the arithmetic flags to unset

        movf    regb+3, w
        subwf   rega+3, w   ;compare byte 3 (high byte)
        skip_z
        goto    compa_ne

        movf    regb+2, w
        subwf   rega+2, w   ;compare byte 2
        skip_z
        goto    compa_ne

        movf    regb+1, w
        subwf   rega+1, w   ;compare byte 1
        skip_z
        goto    compa_ne

        movf    regb+0, w
        subwf   rega+0, w   ;compare byte 0
        skip_z
        goto    compa_ne
;
;   Equal
;
        bsf     flags, flagb_eq
        return
;
;   The two values are definitely not equal, and the C flag is set according
;   to the comparison.
;
compa_ne
        skip_wgt            ;A is smaller ?
        goto    compa_gt    ;accumulator is larger
        bsf     flags, flagb_lt ;accumulator is smaller
        return
compa_gt                     ;accumulator is larger
        bsf     flags, flagb_gt
        return



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

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


2001\03\08@231332 by Nikolai Golovchenko

flavicon
face
Just an idea. How about modifying the signed format to a biased
format and compare biased values as unsigned numbers?

By a biased format I mean inverted sign bit. That would shift the
least signed value to 0, zero to middle, and highest signed value to
highest unsigned value. For example,

-128 ^ 128 = 0
-127 ^ 128 = 1
-127 ^ 128 = 2
...
-1   ^ 128 = 127
0    ^ 128 = 128
1    ^ 128 = 129
2    ^ 128 = 130
...
127  ^ 128 = 255

Then just use unsigned routines for comparison. To restore the input
values, invert sign bits again.

Hope it helps,

Nikolai

---- Original Message ----
From: James Newton <.....jamesnewtonKILLspamspam.....PICLIST.COM>
Sent: Friday, March 09, 2001 0:04:48
 To: EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU
Subj: [pic]: Comparison of signed 16bit values from addition or              subtraction

{Quote hidden}

> {Original Message removed}

2001\03\09@005051 by Antonio L Benci

flavicon
picon face
part 1 2092 bytes content-type:text/plain; charset=us-ascii (decoded 7bit)

I wrote this routine several years ago...

;*******************************************************************
; A not too optimised 16 bit compare routine for 16bit absolute
; values,
; ie 0 -> 65536.
; Compare WORD to COMP (a word value).
; If WORD = COMP return with 00
; If WORD > COMP return with 01
; If WORD < COMP return with 80
;*******************************************************************

hword   equ     0x10        ; storage for high byte of WORD
lword   equ     hword+1     ; storage for low byte of WORD
hcomp   equ     0x12        ; storage for high byte of COMP
lcomp   equ     hcomp+1     ; storage for low byte of COMP

COMP
   movfw   hcomp           ; get high byte of comp value
   subwf   hword           ; subtract values
   btfsc   status,z        ; first check if result is 0
   goto    COMPL           ; if zero compare low bytes
   btfsc   status,c        ; else test carry bit
   retlw   0x01            ; if WORD > COMP, return with 01h
   retlw   0x80            ; if WORD < COMP, return with 80h

COMPL
   movfw   lcomp           ; get low byte of comp value
   subwf   lbyte           ; subtract values
   btfsc   status,z        ; first check if result is 0
   retlw   0x00            ; if result is 0, return with 00
   btfsc   status,c        ; if c set then WORD > COMP
   retlw   0x01            ; if WORD > COMP, return with 01h
   retlw   0x80            ; if WORD < COMP, return with 80h

end
;******************************************************************

Nino
--
------------------------------------------------------
| Antonio (Nino) L. Benci                            |
| Professional Officer, Electronic Services          |
| School of Physics & Materials Engineering          |
| Monash University                                  |
| email: KILLspamnino.benciKILLspamspamspme.monash.edu                  |
| T: 61 3 9905 3649. F: 61 3 9905 3637               |
| M: 0414 924 833                                    |
------------------------------------------------------


part 2 586 bytes content-type:text/x-vcard; name=Nino.Benci.vcf; charset=us-ascii
(decoded 7bit)

begin:vcard
n:Benci;Antonio L
tel;cell:0414 924 833
tel;fax:+61 3 9905 3637
tel;home:0414 924 833
tel;work:+61 3 9905 3649
x-mozilla-html:FALSE
url:http://www.physics.monash.edu.au/~ninob
org:Monash University;School of Physics & Materials Engineering
version:2.1
email;internet:RemoveMENino.BenciTakeThisOuTspamspme.monash.edu
title:Professional Officer, Electronic Services
adr;quoted-printable:;;PO Box 27=0D=0ASchool of Physics and Materials Engineering=0D=0AMonash University;Monash University;VIC;3800;Australia
x-mozilla-cpt:;10672
fn:Antonio L Benci
end:vcard


part 3 144 bytes
--
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


2001\03\09@070425 by Walter Banks

picon face
This is the technique used by most compilers (including MPC) on
processors that do not have an overflow bit for comparing
signed numbers. Careful implementation can also use this
technique for comparing signed and unsigned numbers of the
same number of bits without casting to a larger number of bits.

The nice thing about using a biased representation to
do the comparison is that exclusive or can be used to add the
bias to the number (or invert the sign bit) This is important
because in most compact implementations the carry bit needs
to be preserved during at least one one of these operations.

Walter Banks



Nikolai Golovchenko wrote:
>
> Just an idea. How about modifying the signed format to a biased
> format and compare biased values as unsigned numbers?
>
> By a biased format I mean inverted sign bit. That would shift the
> least signed value to 0, zero to middle, and highest signed value to
> highest unsigned value.
>

--
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


2001\03\09@082255 by Drew Vassallo

picon face
This was from Tony Nixon... I got it from the PICLIST math library:

;********** 16 BIT
;RAM > X
movf RAMH,w
sublw XH
btfss status,c
goto true
btfss status,z
goto false
movf RAML,w
sublw XL
btfss status,c
goto true

;RAM < X
movlw XH
subwf RAMH,w
btfss status,c
goto true
btfss status,z
goto false
movlw XL
subwf RAML,w
btfss status,c
goto true

;RAM >= X
movf RAMH,w
sublw XH
btfss status,c
goto true
btfss status,z
goto false
movlw XL
subwf RAML,w
btfsc status,c
goto true

;RAM <= X
movlw XH
subwf RAMH,w
btfss status,c
goto true
btfss status,z
goto false
movf RAML,w
sublw XL
btfsc status,c
goto true

--Andrew
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

--
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


2001\03\09@122855 by Nikolai Golovchenko

flavicon
face
Walter Banks wrote:
> This is the technique used by most compilers (including MPC) on
> processors that do not have an overflow bit for comparing
> signed numbers.

Cool, I didn't know that.

>  Careful implementation can also use this
> technique for comparing signed and unsigned numbers of the
> same number of bits without casting to a larger number of bits.

Let me guess. If the most significant bit of the unsigned number is
set, than it's bigger and you quit. If not, the sign bits are inverted
for both numbers and then compared as if they are both unsigned.
Right?

I think you can even do some math using the biased representation. It
is probably not much different from the usual unsigned, if you track
the bias value. Interesting, is someone using that representation for
more than just comparison?

> The nice thing about using a biased representation to
> do the comparison is that exclusive or can be used to add the
> bias to the number (or invert the sign bit) This is important
> because in most compact implementations the carry bit needs
> to be preserved during at least one one of these operations.

> Walter Banks

Nikolai

{Quote hidden}

--
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


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