;------------------ Convert dec ASCII string to HEX value ---------------
;
; Uses A0 as string pointer, D2 as byte counter and returns result in D1
;

ASCIIToHEX:
	clr.l	d1			; Clear D1
	subq	#1,d2			; Adjust byte counter for DBcc
decinloop:
	bsr.S	digitin			; Convert ASCII to HEX
	cmp	#10,d0			; Check if byte is legal
	bcc.S	decinerr		; Exit if not
	mulu	#10,d1			; Shift result
	add.w	d0,d1			; Add new value
	dbra	d2,decinloop		; Continue until error or done
decinok:
	rts
decinerr:
	moveq	#0,d1
	rts
digitin:
	clr.l	d0
	move.b	(a1)+,d0		; Copy byte to D0
	sub.b	#"0",d0			; Convert to HEX
	rts

;--------------- Convert HEX number to dec ASCII string -----------------
; D4 = hexadecimal number to convert
; A0 = Address to string storage
;
; Number must be word sized and only the last byte may be used
;
; Result is stored in two bytes at address in A0

HEXToASCII:	clr.l	d3		; Clear divide variable
		move.w	#10,d3		; First divide number
		moveq	#2-1,d0		; Lenght (-1) to D0	

HexLoop:	move.w	d4,d1		; Number to d1
		divu	d3,d1		; Divide number with D3
		move.b	d1,(a0)		; Save byte	
		add.b	#$30,(a0)+	; Convert to ASCII code
		move.b 	d1,d2		; Move byte value to A2

		mulu	d3,d2		; Multiply D2 with D3
		sub.w	d2,d4		; Decrement D4 with D2
		divu	#10,d3		; Divide D3 with 10
		clr.l	d1		; Clear new number
		clr.l   d2		; Clear byte value
		dbra	d0,HEXLoop	; Continue if byte left
		rts			; Quit program
