6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 10:43 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Parse a hex string
PostPosted: Fri May 05, 2017 9:33 am 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
This must have been done a million times before, but I haven't found a really cunning way to do it, so I was wondering if anyone else has.

Here's one way:

Code:
parsehex:
   LDA #$00
   STA result
   STA result+1
loop:
   LDA (ptr),Y
   CMP #$21        ; Space or any control code terminates
   BCC finished
   CMP #$30        ; '0'
   BCC baddigit
   CMP #$47        ; 'G'
   BCS baddigit
   SBC #$39        ; valid $F6..$FF, $07..$0C
   CMP #$07
   BCC baddigit
   ADC #$09        ; '0'..'9' -> $00..$09
   CMP #$11
   BCC digit0to9
   SBC #$07        ; adjust for 'A'..'F'
digit0to9:
      
   ; multiply by 16 and add new digit
   ASL result
   ROL result+1
   ASL result
   ROL result+1
   ASL result
   ROL result+1
   ASL result
   ROL result+1
   BCS toobig
   
   ORA result
   STA result
   INY
   BCC loop

finished:
   RTS
   
baddigit:
   BRK           ; handle error
   
toobig:
   BRK           ; handle error


Who can do better than that? Extra credit for concise solutions, or (ab)use of the D flag, if indeed it can help at all.


Top
 Profile  
Reply with quote  
 Post subject: Re: Parse a hex string
PostPosted: Fri May 05, 2017 10:48 am 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
Code:
        sec                             ;
        sbc     #'0'                    ;
        bcc     baddigit                ;
        cmp     #10                     ;
        bcc     @digit                  ; 0..9 digit
        sbc     #'a'-'0'-10             ; maybe a..f ?
        cmp     #10                     ;
        bcc     baddigit                ; < 0xa
        cmp     #16                     ;
        bcs     baddigit                ;
@digit:


Top
 Profile  
Reply with quote  
 Post subject: Re: Parse a hex string
PostPosted: Fri May 05, 2017 12:10 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
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

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Last edited by Klaus2m5 on Sat May 06, 2017 11:44 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Parse a hex string
PostPosted: Fri May 05, 2017 12:27 pm 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
The SEC can be removed now.


Top
 Profile  
Reply with quote  
 Post subject: Re: Parse a hex string
PostPosted: Fri May 05, 2017 3:31 pm 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
Cool solution! I love the use of the stack for things like this. Excluding the stack unwinding in the error handling, this is the same size as the original routine too - good job!


Top
 Profile  
Reply with quote  
 Post subject: Re: Parse a hex string
PostPosted: Thu May 11, 2017 3:15 pm 
Offline

Joined: Thu Feb 10, 2011 3:14 am
Posts: 79
Check out the monitor listing from the Apple 1 Operations Guide for Wozniak's code. It uses an EOR plus CMP and ADC to do the conversion instead of two CMPs and an SBC. I may type it up later if I get bored enough.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: