; MULTIPLY multiplier, multiplicand
; Multiply two 16-bit numbers into a 16-bit product, permitting overflow. 

; Device data and reset vector
        device  pic16c55,xt_osc,wdt_off,protect_off
        reset   start

        org     8
mulcH   ds      1       ; MSB of multiplicand. 
mulcL   ds      1       ; LSB of multiplicand. 
mulpH   ds      1       ; MSB of multiplier.
mulpL   ds      1       ; LSB of multiplier.
prodH   ds      1       ; MSB of product. 
prodL   ds      1       ; LSB of product. 
index   ds      1       ; temporary counter

        org     0

start	mov	!rb,#0		; Outputs for LEDs to 
	mov	!rc,#0		; display 16-bit result.
	mov	mulcH,#0	; Problem: multiply
	mov	mulcL,#0Bh	; 0Bh by 1016h
	mov	mulpH,#10h
	mov	mulpL,#16h
	call	multiply
	mov	rb,prodL	; Show result in binary on 
	mov	rc,prodH	; LEDs connected to ports.
	jmp	$		; Endless loop

Multiply
	clr	prodH		; Clear product to make
	clr	prodL		; way for new calculation.
	mov	index,#16	; Number of bits to calc. 
:loop	clc
	rl	prodL		; Shift product left.
	rl	prodH
	clc
	rl	mulcL		; Shift multiplicand left.
	rl	mulcH
	jnc	:skip		; If carry, add multiplier
	ADD	prodL,mulpL	; to the product. Else skip.
	snc			; 16-bit addition: prod+mulp
	inc	prodH
	ADD	prodH,mulpH
:skip	djnz	index,:loop
	ret