;multiplication program MULT

;input multiplication factors in HL and DE, one of which must
;necessarily be an 8-bit number;  if not, carry is set
;output product in DE, carry set if overflow.

;**********************  Initial test to find 8-bit factor
MULT:	xra	a	;clear A
	ora	d	;is D zero?
	jz	found	;yes, DE number is 8-bit factor
	xra	a	;no, DE number was not 8-bit factor
	ora	h	;is H zero then?
	stc
	rnz		;no, return with carry set
	xchg		;yes, place 8-bit factor in DE
found:	mov	a,e	;transfer multiplicand to A

;**********************  Multiplication starts in earnest
	lxi	d,0	;clear DE to receive output terms
	ana	a	;8-bit factor now in A; clear carry.
next:	rar		;halve the multiplicand; result odd?
	jnc	even	;no, don't add multiplier term
	xchg		;yes, therefore,
	dad	d	;add multiplier (now in DE) to output.
	rc		;overflow, carry set on return
	xchg		;put multiplier back in HL
even:	ana	a	;already reached 1 by halving?
	rz		;yes, return with result, carry cleared
	dad	h	;no, double the multiplier and
	jnc	next	;continue the process.
	ret		;overflow, carry set on return

