PIC Microcontoller Math Method

32x16 multiplication for PIC18F

from VegiPete gmail

;******************************************************************************
;Mult32x16
;Multiply a 32 bit number by a 16 bit number (low byte in low memory)
;FSR0 points to the 16 bit number
;FSR1 points to the 32 bit number
;FSRs are unchanged
;WREG,PRODL,PRODH changed
;Puts the 48 bit result in result
;Algorithm merely sums the 8 partial products, however, the
;order is chosen to minimize left over carry bits
;******************************************************************************
Mult32x16:
	clrf	result+4
	clrf	result+5

	movf	POSTINC1,w
	movf	POSTINC1,w
	movf	POSTDEC1,w		;source32+2
	mulwf	INDF0			;source16+0
	movff	PRODL,result+2
	movff	PRODH,result+3

	movf	POSTINC1,w		;source32+1
	mulwf	POSTINC0		;source16+0
	movff	PRODL,result+1
	movf	PRODH,w
	addwf	result+2,f

	movf	POSTDEC1,w		;source32+2
	mulwf	INDF0			;source16+1
	movf	PRODL,w
	addwfc	result+3,f
	movf	PRODH,w
	addwfc	result+4,f		;maybe carry
	btfsc	STATUS,C
	incf	result+5,f

	movf	POSTDEC1,w
	movf	POSTINC1,w		;source32+0
	mulwf	POSTDEC0		;source16+1
	movf	PRODL,w
	addwf	result+1,f
	movf	PRODH,w
	addwfc	result+2,f

	movf	POSTINC1,w
	movf	POSTINC1,w
	movf	POSTDEC1,w		;source32+3
	mulwf	INDF0			;source16+0
	movf	PRODL,w
	addwfc	result+3,f
	movf	PRODH,w
	addwfc	result+4,f		;maybe carry
	btfsc	STATUS,C
	incf	result+5,f

	movf	POSTDEC1,w
	movf	POSTDEC1,w
	movf	POSTINC1,w		;source32+0
	mulwf	POSTINC0		;source16+0
	movff	PRODL,result+0
	movf	PRODH,w
	addwf	result+1,f

	movf	POSTINC1,w		;source32+1
	mulwf	INDF0			;source16+1
	movf	PRODL,w
	addwfc	result+2,f
	movf	PRODH,w
	addwfc	result+3,f

	movf	POSTINC1,w
	movf	POSTDEC1,w		;source32+3
	mulwf	POSTDEC0		;source16+1
	movf	PRODL,w
	addwfc	result+4,f
	movf	PRODH,w
	addwfc	result+5,f		;should be no carry
	
	movf	POSTDEC1,w
	movf	POSTDEC1,w		;move pointer back to start
	return