please dont rip this site

PIC Microcontoller Math Method

Multiply 16x16 bit from Jim Bixby

After looking all over for a fast 16x16-->32 unsigned multiply routine, I adapted stuff from here to create the following. It is easily modified to other multiply resolutions, does nothing tricky like depend on register locations or order, and goes fast (16x16 mpy in about 168 instructions):

;   PIC16 fast unsigned multiply routines 
;	Jim Bixby
	list	r=dec,x=on,t=off,p=16F877
	include	p16F877.INC

;     STATUS bit definitions

	#define	_C		STATUS,0
	#define	_Z		STATUS,2

 cblock 0x20		;Register definitions

	TEMP0, TEMP1, TEMP2, TEMP3	;convention: '0' is most sig byte


	org	0x0000
	goto	MAIN

; a short test routine

	movlw	0x45		;init a couple of arguments
	movwf	AARG0
	movlw	0x30
	movwf	AARG1

	movlw	0xC2
	movwf	BARG0
	movlw	0x51
	movwf	BARG1

	call	MUL16x16U	;call the multiply

	goto	MAIN

; Unsigned multiply routine
; Multiplies of any size can easily be made from the macros - for this code, only MUL16X16
; is made
; The approach here is linear code for speed at the expense of RAM and registers
; Adapted from <unknown>:
; Add16AB macro - add AH:AL to BH:BL, result goes to BH:BL

	movfw	AL
	addwf	BL,f
	movfw	AH
	btfsc	_C
	incfsz	AH,w
	addwf	BH,f

; Macro for adding & right shifting - used once per bit by mulmac

mult    MACRO   bit,A,H,L	;A=multiplier,H:L=result, other arg in W
        btfsc   A,bit
        addwf   H,f
        rrf     H,f
        rrf     L,f
        ENDM                    ; End of macro

; 8x8-->16 multiply macro  A * B --> H:L
;		Invokes mult macro above

mulmac	MACRO	A,B,H,L		; H:L = A*B
	clrf    H
        clrf    L
        movf    B,W		; move the multiplicand to W reg.
        bcf     _C		; Clear the carry bit in the status Reg.
        mult    0,A,H,L
        mult    1,A,H,L
        mult    2,A,H,L
        mult    3,A,H,L
        mult    4,A,H,L
        mult    5,A,H,L
        mult    6,A,H,L
        mult    7,A,H,L
; MUL16x16U
;	AARG0:1 * BARG0:1 --> AARG0:3
;	Invokes the three macros above 
;	164 prog words, 168 inst cycles, 10 registers (6 for call/return arguments + 4)
;	Uses TEMP0:3 also. BARG0:1 unchanged on return

	; 32 bit result is first calculated into T0:T1:A2:A3
	; When done, T0:1 is moved to A0:1 so the final result is in A0:3
	; Register names can be changed at will, and any register can be located
	; anywhere - order is not important.

	mulmac	ARG0, BARG0, TEMP0, TEMP1	;T0:1 <--A0*B0 
	mulmac	AARG1, BARG1, AARG2, AARG3 	;A2:3 <--A1*B1 
	mulmac	AARG1, BARG0, TEMP2, TEMP3 	;T2:3 <--A1*B0 
	Add16AB	TEMP2, TEMP3, TEMP1, AARG2 	; Add T2:3 to T1:A2, carry into T0 
	btfsc 	_C 				; nb: this relies on the _C bit being 
	incf 	TEMP0,f 			; correct after the Add16AB macro 
	mulmac	AARG0, BARG1, TEMP2, TEMP3 	;T2:3 <--A0*B1 
	Add16AB TEMP2, TEMP3, TEMP1, AARG2 	; Add T2:3 to T1:A2, carry into T0 
	btfsc 	_C 
	incf 	TEMP0,f 
	movfw 	TEMP1 				;Move T0:1 to A0:1 
	movwf 	AARG1 				;to finish 
	movfw 	TEMP0 
	movwf 	AARG0 
	retlw 	0 


In "Multiply 16x16 bit from Jim Bixby" there appears to be a typo. The first line of MUL16x16U which now reads mulmac ARG0, BARG0, TEMP0, TEMP1 ;T0:1 <--A0*B0 should be: mulmac AARG0, BARG0, TEMP0, TEMP1 ;T0:1 <--A0*B0 Thanks, Davide +

file: /Techref/microchip/math/mul/16x16jb.htm, 5KB, , updated: 2013/8/19 09:35, local time: 2024/2/28 05:12,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Math Method Multiply 16x16 bit from Jim Bixby</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!