; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.picnpoke.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; COMPARE n1, n2 ; Compares two 16-bit numbers by subtracting one from the other and reports the ; outcome by a code in the w register. Does not alter n1 or n2. Uses the status ; register bit PA2 to preserve the z flag from subtraction of the LSBs. If you ; adapt this code for use in PICs other than 16C5x series, make sure that this ; bit is available. If it isn't, use a bit in one of the file registers instead. ; Device data and reset vector P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start Z_ equ PA2 ; Unused status bit as extra Z flag. org 8 n1H Res d'1' ; MSB of n1. n1L Res d'1' ; LSB of n1. n2H Res d'1' ; MSB of n2. n2L Res d'1' ; LSB of n2. org 0 start MOVLW d'0' ; Outputs for LEDs. TRIS 6h MOVLW 0x00C8 ; Compare 0C836h to MOVWF n1H MOVLW 0x36 ; 2575h. MOVWF n1L MOVLW 0x25 MOVWF n2H MOVLW 0x75 MOVWF n2L CALL Comp16 ; Call the subroutine. MOVWF 6h ; Show result on LEDs GOTO $ ; Endless loop ; Upon return, a code in w will indicate the outcome of the comparison. ; If n1 > n2 then w = 1. If n1 < n2 then w =2. If n1 = n2 then w = 0. Comp16 BCF status,pa2 ; Clear aux Z bit. MOVF n2L,w ; w = n2L SUBWF n1L,0 ; w = n1L-n2L BTFSC status,z ; Copy z bit to Z_. BSF status,pa2 MOVF n2H,w ; If n1L-n2L underflowed, BTFSC status,c ; then increment n2H (same GOTO Comp16_cont INCF n2H,w ; as decrementing n1H). BTFSC status,z ; If n2H overflows, n1 must be < n2 RETLW d'2' ; so return with 2 in w. Comp16_cont SUBWF n1H,0 ; w = n1H-n2H BTFSS status,c ; If n1H underflows, n1=n2. BTFSC status,z ; If both z and Z_ are set, RETLW d'0' ; both subtractions resulted in 0, so Comp16_cont2 RETLW d'1' ; n1=n2. Otherwise n1>n2. end