ON 20030520@11:43:36 PM at page: http://www.piclist.com/techref/microchip/math/mul/23x8bbaj.htm WS---CA8 Werner Soekoe Code:
I worked with this code of Bryon A Jeff to create a 32x8 bit multiplier that gives a 32 bit result, without any carry. The solution is as follow:

Variable definitions are done as follow:

	cblock 0x73
		Multiplier
		Multiplicant:4
		Product:4
		MultiCount
	endc

Please note that the 0x73 address of the variables should be changed to suit your application. The variables take a total of 10 bytes of RAM.

The code now looks like this:

;-----------------------------
Multiply32x8
; Clear Product
	clrf Product
	clrf Product+1
	clrf Product+2
	clrf Product+3
; Test for an 0 multiplier
	movf Multiplier, W
	btfsc STATUS, Z
		return
; Setup the counter for 8 bits
	movlw 0x08
	movwf MultiCount

MultiplyLoop
	rlf Multiplier, F
	btfss STATUS, C
	goto ShiftLoop

	movf Multiplicant+0, W
	addwf Product+0, F
	btfsc STATUS, C
	call CarryByte1

	movf Multiplicant+1, W
	addwf Product+1, F
        btfsc STATUS, C
        call CarryByte2

	movf Multiplicant+2, W
	addwf Product+2, F
	btfsc STATUS, C
	call CarryByte3

	movf Multiplicant+3, W
	addwf Product+3, F

ShiftLoop
	decfsz MultiCount, F
		goto $+2
	return

	bcf STATUS, C
	rlf Product+0, F	
	rlf Product+1, F	
	rlf Product+2, F	
	rlf Product+3, F	
	goto MultiplyLoop

CarryByte1
	incfsz Product+1, F
		return
CarryByte2
	incfsz Product+2, F
		return
CarryByte3
	incf Product+3, F
		return
;-----------------------------

To test the multiplication, you can use the following code:

	movlw 0x7D
	movwf Multiplicant+0
	movlw 0x8E
	movwf Multiplicant+1
	movlw 0xAB
	movwf Multiplicant+2
	movlw 0x7A
	movwf Multiplicant+3
	movlw 0x8F
	movwf Multiplier

	call Multiply32x8

This does 0x7AAB8E7D times 0x8F, which should result in 0x85D497D3. Please note it does not return 0x4485D497D3, since the 5th byte in the result would have been the carry, which is disgarded. This routine can be used as it is for 24x8 bit multiplication, which will result in a full 32 bit product, with no carry needed. The total instruction cycles to perform the above multiplication is 221. This will vary, according to the complexity of the numbers, but should always be below 1ms which running the PIC at 4Mhz or higher.

Hope this code helps anyone.
ON 20030521@7:43:00 AM at page: http://www.piclist.com/techref/microchip/math/mul/23x8bbaj.htm JMN-EFP-786 James Newton edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\microchip\math\mul\23x8bbaj.htm&version=1 ON 20030521@3:34:54 PM at page: http://www.piclist.com/techref/microchip/math/mul/24x24b-tk.htm JMN-EFP-786 James Newton Published and replied to post 37425.3801041667 |Insert 'Product is defined in the cblock at the beginning and Multiplier is used because it is actually part of Product. Works fine as far as I can tell.' at: '' jasonz@issproinc.com asks:
Nikolai Golovchenko's 24 x 24 multiplication function does not work as listed and appears is in-complete !!! Multipler is define but never used as well as Product is not define but goes potluck. zeroes.
|Delete 'P-' before: '' but after: '