Not smaller but faster including Arlet's suggested code:
Code:
parsehex:
LDX #0
STX result
STX result+1
LDA #$80 ; precharge end marker!
PHA
parse:
LDA (ptr),Y
CMP #$21 ; Space or any control code terminates
BCC stuffresult
; as suggested by Arlet
; SEC ; not needed after BCC
SBC #'0'
BCC baddigit ; <'0'
CMP #10
BCC digit0to9 ; '0'-'9'
; ORA #$20 ; allow 'A'-'F'
SBC #'a'-'0'-10
CMP #10 ; <'a'
BCC baddigit
CMP #16 ; >'f'
BCS baddigit
digit0to9:
PHA
INY
BPL parse
stuffresult:
PLA ; stuff low nibble
BMI finished
STA result,X
PLA ; stuff high nibble
BMI finished
ASL A
ASL A
ASL A
ASL A
ORA result,X
STA result,X
INX
CPX #2
BCC stuffresult
toobig:
PLA ; cleanup stack
BPL toobig
BRK ; handle error
finished:
RTS
baddigit:
PLA ; cleanup stack
BPL baddigit
BRK ; handle error
It avoids the cycle costly memory modify shifts by reversing the order of the converted number.
edit: removed the SEC opcode as suggested by Arlet below.
edit2: test >'f' needs BCS, not BCC