;*** Text is in Buffer, max 12 characters

	lea	buffer,a0
	bsr	removespaces	; remove spaces from beginning of string
	move.l	a0,a1		; new pointer to a1
	move.w	#$ffff,d0	; length = -1
searchnull:
	addq.w	#1,d0		; length = length + 1
	tst.b	(a1)+		; check for null character
	bne.s	searchnull	; continue until found
	bsr	converttohex	; convert to hex-binary
	rts

incpointer:
	addq.l	#1,a0		; next character
removespaces:
	cmp.b	#' ',(a0)	; is character in (a0) = space
	beq.s	incpointer	; yep! increase pointer to buffer
	rts


converttohex:
	movem.l	d0/d3/d4/a0/a1,-(sp)	; save registers
	moveq	#0,d1			; clear d1
	subq.b	#2,d0			; length = length - 2
	move.b	(a0)+,d3	; first character from string to d3
	cmp.b	#'$',d3		; is value given in hex ?
	beq.s	textishex	; convert it to the value
	addq.b	#1,d0		; adjust string length to decimal
	cmp.b	#8,d0		; is string longer than 8 characters ?
	bhi	invalidvalue	; yes! set error flag and exit
	add.w	d0,a0		; get the end of the input string
	lea	convertdata(pc),a1	; address of convert table
lbc000874:
	moveq	#0,d3		; clear d3
	move.l	(a1)+,d2	; get value from table
	move.b	-(a0),d3	; get character from string
	sub.b	#$30,d3		; convert character to number
	bcs.s	invalidvalue	; if number is < 0 then value is invalid
	cmp.b	#9,d3		; is number > 9 ?
	bhi.s	invalidvalue	; yep! value is invalid

	move.l	d3,d4
	mulu	d2,d3
	swap	d2

	mulu	d2,d4
	swap	d4
	add.l	d4,d3
	add.l	d3,d1
	dbra	d0,lbc000874

	bra.s	reportsize	; Calculate size of result

textishex:
	cmp.b	#7,d0
	bhi.s	invalidvalue
lbc0008a0:
	moveq	#0,d3
	move.b	(a0)+,d3
	sub.b	#$30,d3
	bcs.s	invalidvalue
	cmp.b	#9,d3
	bls.s	lbc0008c4
	bclr	#5,d3
	sub.b	#7,d3
	cmp.b	#9,d3
	bls.s	invalidvalue
	cmp.b	#15,d3
	bhi.s	invalidvalue
lbc0008c4:
	lsl.l	#4,d1
	or.w	d3,d1
	dbra	d0,lbc0008a0

	bra.s	reportsize		; calculate size of result

invalidvalue:
	moveq	#0,d2			; set error flag
	moveq	#0,d1			; clear result
	bra	restoreregs		; restore registers and exit

reportsize:
	moveq	#4,d2			; length = 4 bytes (long)
	cmp.l	#$ff,d1			; is number > $FF (1 byte) ?
	bhi.s	notbyte			; yep! check if it is a word
	subq.b	#1,d2			; length = 3 bytes
notbyte:
	cmp.l	#$ffff,d1		; is number > $FFFF (1 word) ?
	bhi.s	restoreregs		; yep! restore regs and exit
	subq.b	#2,d2			; length = 1 byte

restoreregs:
	movem.l	(sp)+,d0/d3/d4/a0/a1
	rts

convertdata:
	dc.l	1,10,100,1000,10000,100000,1000000
	dc.l	10000000,100000000,$540BE400

buffer:
	dc.b	"999999999",0

