;;;a2b.asm
;;;a2b -- convert ASCII binary number to 8-bit char in C
;;;Entry Conditions: HL -> null terminated string
;;;Exit Conditions: Carry clear, C=value
;;;                 Carry set, error

;Willard Goosey
;goosey@sdc.org
;7/10/2011

;$Id: a2b.asm,v 1.8 2012/08/01 02:41:45 goosey Exp goosey $

;derived from 8080/z80 Assembly Language
;Alan Miller, 1981

;7/12/2011 works now
;7/27/2011 carry flag set on error, clear on no-error

a2b:
       push d    ;save de on stack
       lxi d,0   ;clear de
bbin2  mov a,m   ;a= *HL
       cpi 00h   ;set flags
       jz bbin3  ;done?
       sui '0'   ;conv to binary
       jc bbin4  ;<0?
       cpi 2     ;check for non-binary digit
       jnc bbin4 ;bad digit?
       xchg      ;swap de, hl
       dad h     ;hl=hl*2
       xchg      ;swap them back de=de*2
       ora e     ;add old bits to a
       mov e,a   ;store in e
       inx h     ;hl=hl+1
       jmp bbin2 ;next
       
bbin3  mov c,e   ;8-bit value
       pop d     ;restore de
       xra a     ;a=a xor a to clear carry 
       ret       ;done!
       
bbin4  pop d     ;restore de
       mvi c,0ffh   ;result=ff on error
       stc	 ;set carry 
       ret

;;end of a2b.asm
	 
