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
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'
which in MChip speak is something like:
SUBWF temp,w ;'9' and lower sets the carry
INCF WReg ;Make 'A'-':' = 8.
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'
SUBWF temp,w ;w = temp - w
BTFSS WReg,d'4' ;No good if we overflow 16 values
BTFSS status,c ; or if we underflow '0'
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)
piclist.com 1-619-652-0593 jamesnewton
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.
See also: www.piclist.com/techref/microchip/ios.htm?key=rang
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the