;c2h.asm
;;Willard Goosey
;;goosey@sdc.org
;;7/18/2011

; $Id: c2h.asm,v 1.6 2012/07/30 19:40:21 goosey Exp goosey $
	

;convert 8bit val in C to 2 hex digits in ascii
;derived from
;8080/z80 Assembly Language
;Alan Miller, John Wiley & Sons, 1981

;c2h -- value in C to hex digits
;Entry Conditions:  C=value
;                   HL-> 2 byte buffer
;Exit Conditions: C,A destroyed
;                 HL-> hex digits in ASCII

;c2hp -- value in C to hex digits, HL plussed
;Entry Conditions:  C=value
;                   HL-> 2 byte buffer
;Exit Conditions: C,A destroyed
;                 HL=HL+2

;i2h  -- 16-bit value in BC to hex digits, HL preserved
;Entry Conditions: BC=value
;                  hl-> 4 byte buffer
;Exit Conditions:  BC,A destroyed
;                  HL-> hex digits in ASCII

;i2hp -- 16 bit value in BC to hex digits, HL plussed
;Entry Conditions: BC=value
;                  hl-> 4 byte buffer
;Exit Conditions:  BC,A destroyed
;                  HL=original HL+4

c2h   push h     ;save hl on stack
      call c2hp  ;do the conversion
      pop h      ;restore hl
      ret
;I couldn't bare to mess up this cute fall-thru
;structure but I wanted hl-> buffer on exit
 
c2hp  mov a,c    ;get byte
      rar        ;rotate
      rar        ;4
      rar        ;bits to
      rar        ;the right
      call c2h1  ;upper char
      mov a,c    ;lower char

;fall thru
c2h1  ani 0fh    ;lower nybble
      adi '0'    ;add ascii 0
      cpi '9'+1  ;0-9?
      jc c2h2    ;store
      adi 7      ;a-f

;fall through
c2h2  mov m,a    ;store at hl
      inx h      ;hl++
      ret        ;done

;I2h convert value in BC to hex digits
;HL-> buffer


i2h   push h     ;save hl
      call i2hp  ;do the conversion
      pop h      ;restore hl
      ret        ;all done

;I2hp  convert value in BC to hex digits
;HL moved to point after digits

i2hp  push b     ;save bc
      mov c,b    ;c=b
      call c2hp  ;convert first byte
      pop b      ;restore bc
      call c2hp  ;convert second byte
      ret

;;; end of c2h.asm
	
	 
