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) mailto:jamesnewton@piclist.com 1-619-652-0593 PIC/PICList FAQ: http://www.piclist.com or .org -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Nikolai Golovchenko Sent: Monday, January 29, 2001 20:14 To: PICLIST@MITVMA.MIT.EDU Subject: Re: [PIC:] Code Challenge: Range check and convert Hex Importance: Low 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 Sent: Monday, January 29, 2001 23:52:16 To: PICLIST@MITVMA.MIT.EDU Subj: [PIC:] Code Challenge: Range check and convert Hex > 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) > mailto:jamesnewton@piclist.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. -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body