;;a2d.asm 
;From m100-bounces@lists.bitchin100.com  Wed Jul  6 22:19:23 2011
;Date: Wed, 6 Jul 2011 21:17:53 -0700
;From: Ken Pettit
;To: Model 100 Discussion <m100@lists.bitchin100.com>
;Subject: Re: [M100] Is there a ROM call that does the VAL function?

;I haven't seen a routine in the ROM to convert from ASCII to 16-bit HL, DE
;format.  It's pretty straight forward though:

;$Id: a2d.asm,v 1.6 2012/08/01 02:42:45 goosey Exp goosey $

;a2d -- convert from ASCII to 16-bit int in HL 
;entry conditions: HL->null-terminated string
;exit conditions: DE=number
;                 HL->null
;                 A destroyed
;                 BC destroyed
;		  Carry set = error
;                 Carry clear = success

a2d:
   LXI    D, 0000H  ; Initialize the "result"
TSTNXT:
   MOV  A,M     ; Get next value
   ORA   A         ; Test for end of string
   RZ                  ; Return if end of string
   SUI    30H     ; Convert ASCII to binary
   jm    a2d1      ;jump out if a<0 
   PUSH  H      ; Save pointer on stack
   MOV L,A       ; Convert to 16-bit
   MVI   H,0      ; Zero the upper bits
   XCHG          ; Prepare to multiply current value x 10
   DAD  H        ; x2
   MOV   C,L   ; BC has x2 value
   MOV   B,H  ;    "
   DAD   H      ; x4
   DAD   H      ; x8
   DAD   B      ; HL = x8 + x2 = x10
   DAD   D      ; Add the value of this digit to accumulated value
   XCHG         ; Save accumulated value in DE
   POP   H      ; Get pointer to string
   INX     H      ; Point to next byte in string
   JMP   TSTNXT

a2d1 stc     ;error, set carry flag
     ret     ;done

;;end of a2d.asm 
