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

	lea	buffer,a0
	bsr	removespaces	; remove spaces from 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/d2-d3/a0,-(sp)	; save registers
	moveq	#0,d1		; clear d1
	cmp.b	#3,d0		; is string longer than 3 characters ?
	bhi	invalidvalue	; yes! set error flag and exit
	add.w	d0,a0		; get the end of the input string
	subq.b	#1,d0		; adjust length for DBRA
	moveq	#1,d2		; first multiplier to D2

convertdigit:
	moveq	#0,d3		; clear d3
	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

	mulu	d2,d3		; multiply number with base
	add.l	d3,d1		; and add it to the result
	mulu	#10,d2		; multiplier = multiplier * 10
	dbra	d0,convertdigit	; continue until all digits are converted

	bra.s	restoreregs	; Result ok

invalidvalue:
	moveq	#-1,d1		; Set error flag (result = -1)

restoreregs:
	movem.l	(sp)+,d0/d2-d3/a0
	rts

buffer:
	dc.b	"  999",0
