piclist 2001\03\08\210608a >
Thread: Comparison of signed 16bit values from addition or subtraction
face picon face BY : Olin Lathrop email (remove spam text)

> I need signed 16bit compare and branch macros for PIC16F877 working on
> pseudo 16bit registers ACCaLO,ACCaHI like some Microchip application
> 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)
        goto    compa_ne

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

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

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

Olin Lathrop, embedded systems consultant in Devens Massachusetts
(978) 772-3129, @spam@olinspamspamspamBeGoneembedinc.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

<004701c0a828$3e6182f0$260bf6cd@pc> 7bit

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Comparison of signed 16bit values from addition or subtraction

month overview.

new search...