;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:
	xor	a		; Clear A
	or	d		; Is D zero?
	jr	z,found		; Yes, DE number is 8-bit factor
	xor	a		; No, DE number was not 8-bit factor
	or	h		; Is H zero then?
	scf
	ret	nz		; No, return with carry set
	ex	de,hl		; Yes, place 8-bit factor in DE
found:
	ld	a,e		; Transfer multiplicand to A

;**********************  Multiplication starts in earnest
	ld	de,0		; Clear DE to receive output terms
	and	a		; 8-bit factor now in A; clear carry.
next:
	rra			; Halve the multiplicand; result odd?
	jr	nc,even		; No, don't add multiplier term
	ex	de,hl		; Yes, therefore,
	add	hl,de		; Add multiplier (now in DE) to output.
	ret	c		; Overflow, carry set on return
	ex	de,hl		; Put multiplier back in HL
even:
	and	a		; Already reached 1 by halving?
	ret	z		; Yes, return with result, carry cleared
	add	hl,hl		; No, double the multiplier and
	jr	nc,next		; Continue the process.
	ret			; Overflow, carry set on return

	end

