Searching \ for '[PIC:] Code Challenge: Range check and convert Hex' 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/ios.htm?key=rang
Search entire site for: 'Code Challenge: Range check and convert Hex'.

Exact match. Not showing close matches.
PICList Thread
'[PIC:] Code Challenge: Range check and convert Hex'
2001\01\29@165144 by jamesnewton

picon face
Just about done with a job but I need a routine to check that a HEX digit is
valid, and if so convert it to binary, and if not.... here comes the tricky
part... retain the original value. And... (what am I going to say next?) I'm
totally out of registers and the client just dropped the "retain the value"
thing on me. I'm also really pressed for space.

This is for a 16C54 (well, actually, an SX) and the best I can do is very SX
specific:

       mov w, #':'
       mov w, temp-w   ;'9' and lower sets the carry
       inc WReg                ;Make 'A'-':' = 8.
;Yes, on an SX W is a file register (replacing the counter) if OPTIONX
; is specified and you clear the RTW bit in the OPTION register (which
; I always do)
       sc                      ;Skip if Carry
        setb WReg.3    ;If it was '9' or lower, avoid this next test
       sb WReg.3               ;Skip if Bit 3. If the difference + 1 isn't 8 or
        jmp :Bad               ; more then it was between '9' and 'A'
       mov w, #'0'
       snc                     ;skip no carry
        mov w, #'A'-10
       mov w, temp-w   ;w = temp - w
       sb WReg.4               ;Skip if Bit 4. No good if we overflow 16 values
        sc                     ; or if we underflow '0'
         jmp :Bad

which in MChip speak is something like:

       MOVLW ':'
       SUBWF temp,w    ;'9' and lower sets the carry
       movwf WReg
       INCF WReg               ;Make 'A'-':' = 8.
       BTFSS status,c
        BSF WReg,d'3'  ;If it was '9' or lower, avoid this next test
       BTFSS WReg,d'3' ;If the difference + 1 isn't 8 or more then
        GOTO Bad               ; it was between '9' and 'A'
       MOVLW '0'
       BTFSC status,c
        MOVLW 'A'-10
       SUBWF temp,w    ;w = temp - w
       movwf WReg
       BTFSS WReg,d'4' ;No good if we overflow 16 values
        BTFSS status,c ; or if we underflow '0'
         GOTO Bad

where we had to use a second file register called WReg.

Any other ideas? Remember... no second register, but if you like, you can
use W as a file register.

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

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\01\29@231451 by Nikolai Golovchenko

flavicon
face
Three instructions less:

   movlw   -'0'
   btfsc   temp, 6
    movlw  -'A'+10
   addwf   temp, w    ;w = 0:F if hex valid
   andlw 0xF0
   skpz
    goto Bad          ;leave with unmodified temp
   movlw   -'0'       ;repeat the stuff
   btfsc   temp, 6
    movlw  -'A'+10
   addwf   temp, f    ;temp is converted hex

If ascii conversion is in a routine, that would save another
instruction.

Nikolai

---- Original Message ----
From: j newton <.....eplus1KILLspamspam@spam@SAN.RR.COM>
Sent: Monday, January 29, 2001 23:52:16
 To: PICLISTspamKILLspamMITVMA.MIT.EDU
Subj: [PIC:] Code Challenge: Range check and convert Hex

{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\01\30@040527 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi, nothing effective but different in the sense that it accept
also a-f as input ( i've been bitten by this :) ).
I'm not the originator, can't remember who was.
Anyway here goes:


;
***********************************************************************
; IS_HEX - Routine to check if value in W is ASCII hex digit 0-9, A-F or
a-f
; Returns with zero SET if NOT hex ( value preserved )
; Else returns with zero flag clear and value of input as binary in W
;
IS_HEX
       GLOBAL  IS_HEX
                               ;     0-9         A-F        a-f       Z
                               ; 0x30-0x39  0x41-0x46   0x61-0x66
0x4A

       ADDLW   0xC6            ; 0xF6-0xFF  0x07-0x0C   0x27-0x2C
0x10
       ADDLW   0x0A            ; 0x00-0x09  0x11-0x16   0x31-0x36
0x1A
       BTFSC   STATUS,C
       GOTO    IS_09

       ADDLW   0xE9            ;            0xFA-0xFF   0x1A-0x1F
0x03
       ADDLW   0x06            ;            0x00-0x05   0x20-0x25
0x09
       BTFSC   STATUS,C
       GOTO    IS_AF

       ADDLW   0xDA            ;                        0xFA-0xFF
0xE3
       ADDLW   0x06            ;                        0x00-0x05
0xE9
       BTFSC   STATUS,C
       GOTO    IS_AF

       ADDLW   0x61            ;
0x4A
       BSF     STATUS,Z        ; SET zero flag value is not HEX and
return
       RETURN

IS_AF   ADDLW   0x0A            ; add ten ( range is A-F )
IS_09   BCF     STATUS,Z        ; clear zero flag value is HEX
       RETURN
Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: EraseMEtony.kubekspam_OUTspamTakeThisOuTflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2001\01\30@103038 by jamesnewton

face picon face
Thanks Nik... After hours of looking for a trick, the answer was blinding
obvious.

And I guess if it was a PIC with an addlw you could do this:

   movlw   -'0'
   btfsc   temp, 6
    movlw  -'A'+10
   addwf   temp, w    ;w = 0:F if hex valid
   addlw 0xF0
   skpnc
    goto Bad          ;leave with unmodified temp
   andlw 0x0F
   movwf   temp

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

{Original Message removed}

2001\01\30@105332 by jamesnewton

face picon face
Errr... no, hold on a sec.... '@' still doesn't get rejected.

bit 6 is set so it subtracts 'A' - 10 which is 55 from '@' which is 64
leaving 9 which is in range (but not a valid character).

It also fails to reject ':' to '?' because with out bit 6 set, it subtracts
'0' which makes ':' to '?' come out as 0x0A to 0x0F. Also in range (but not
a valid character).

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



{Original Message removed}

2001\01\31@003040 by Nikolai Golovchenko

flavicon
face
Hmm, you are right, James :( I Should have checked it more...

Then I can see only a long way to do the thing, checking for each
range margin one by one:

     movlw '0'
     subwf temp, w
     skpc
      goto BAD
     movlw '9'+1
     subwf temp, w
     movlw -'0'
     skpc
      goto GOOD
     movlw 'A'
     subwf temp, w
     skpc
      goto BAD
     movlw 'F'+1
     subwf temp, w
     skpnc
      goto BAD
     movlw -'A'+10
GOOD
     addwf temp, f
     .
     .
     .
BAD
     .
     .
     .

That is longer than your SX version... But let's see...

       mov w, #':'
       mov w, temp-w   ;'9' and lower sets the carry
       inc WReg                ;Make 'A'-':' = 8.
       sc                      ;Skip if Carry
        setb WReg.3    ;If it was '9' or lower, avoid this next test
       sb WReg.3               ;Skip if Bit 3. If the difference + 1 isn't 8 or
        jmp :Bad               ; more then it was between '9' and 'A'
;;we get here if it was '9' or lower, or if it was 'A'..'H' or
;;every other 8 characters higher
       mov w, #'0'
       snc                     ;skip no carry
        mov w, #'A'-10
       mov w, temp-w   ;w = temp - w
;;here w is corrected so that if we had 0..'0'..'9' we get
;;0-'0'..0..9,
;;if we had 'A'..., we get
;;10...
;;        sb WReg.4               ;Skip if Bit 4. No good if we overflow 16 values
;;         sc                     ; or if we underflow '0'
;;          jmp :Bad
;;Above, the check of WReg.4 doesn't reject all values above 'F' !!!
;;At least it looks so. Every other 8 characters above 'F' seem to
;;get passed?

I don't know how to replace these lines efficiently. The problem
is that you have to repeat the ASCII to bin conversion, because
the value of wreg is lost after the check for above 'F'.

Hope it helps!

Nikolai

---- Original Message ----
From: James Newton <RemoveMEjamesnewtonTakeThisOuTspamPICLIST.COM>
Sent: Tuesday, January 30, 2001 17:51:41
 To: spamBeGonePICLISTspamBeGonespamMITVMA.MIT.EDU
Subj: [PIC:] Code Challenge: Range check and convert Hex

{Quote hidden}

> {Original Message removed}


'[PIC:] Code Challenge: Range check and convert Hex'
2001\02\05@153601 by jamesnewton
face picon face
This code is tested and working on an SX. The Mchip translation follows. You
may have to tweak the +1's and -1's to get it to work on a PIC. If anybody
bothers to test it, I'll add the MChip version to the PICList code library.

;SXKey version
device  SX28L, turbo, STACKX_OPTIONX

       mov NextChar, #$FF
:loop
       mov temp, NextChar

;routine starts here
       clc
       mov w, #'F'
       mov w, temp - w
       jc :Bad

       mov w, #'0'-1
       mov w, temp - w
       jnc :Bad

       mov w, #'@'+1
       sub temp, w
       mov w, #('@' - ':') + 1
       add temp, w
       jc :BadRestore

       mov w, #(':' - '0')
       sb temp.7
        mov w, #('A' - '@')+1
       add temp, w
       jmp :Good

:BadRestore
       mov w, #':'-1
       add temp, w
:Bad
       nop
       jmp :Next
:Good
       nop
:Next
break
       djnz NextChar, :Loop

;MChip verion

NextChar Res d'1' ; Just used to test routine

temp  Res d'1' ; Temporary variable

org 0
start


            MOVLW 0xFF
            MOVWF NextChar
start_loop
            MOVF NextChar,w
            MOVWF temp

;routine starts here
            MOVLW 'F'
            SUBWF temp,w
            BTFSC status,c
            GOTO start_Bad

            MOVLW '0'-1
            SUBWF temp,w
            BTFSS status,c
            GOTO start_Bad

            MOVLW '@'+1
            SUBWF temp
            MOVLW ('@'-':')+1
            ADDWF temp
            BTFSC status,c
            GOTO start_BadRestore

            MOVLW ':'-'0'
            BTFSS temp,d'7'
            MOVLW ('A'-'@')+1
            ADDWF temp
            GOTO start_Good

start_BadRestore
            MOVLW ':'-1
            ADDWF temp
start_Bad
            NOP
            GOTO start_Next
start_Good
            NOP
start_Next
            DECFSZ NextChar
            GOTO start_Loop

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

{Original Message removed}

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