Hi Tony and all, I'm sorry to disappoint you, but your 24x24 routine won't work. O.K., it will work sometimes, but not always. Wasn't it suspicious at the very first glance, that performing 24bit arithmetic one bit at a time takes only 23 cycles? Your example result is right after 23 cycles, but only due to improper placement of left product rotation. What your routine really calculates is: Product = ((Multiplier>>1)*Multipland)<<1 (unsigned arithmetic) e.g. omitting the least significant bit of Multiplier. To correct it - put the product left rotation at the beginning of the loop and increase number of cycles to 24. Also explicitly stating the number base (like D'24') will help others integrate the routine into their programs. Regards, Josef >*********************************************************************** >; MULTTIPLY_24x24 - Routine to mutltiply two 24 bit numbers >; result in 48 bits ! >; Format: Ram = MSB, Ram+2 = LSB ( for 24 bit variable ) ( little >endian ? ) >; Ram used: Multiplier, Multipland 3 bytes each ( 24 bits ) >; Product 6 bytes ( 48 bits ) >; BitCount 1 byte for loop counting >; >; Multiplier is zero at exit ( destroyed ) >; Multipland is preserved >; Product holds result >; >; Probably more efficient to use Ram = LSB, Ram+2 = MSB ( big endian ? ) >; According to helpful hints from Nikolai, however my requirements >; dictated the used format. >; >MULTIPLY_24x24 > ; preload values to test > MOVLW 0xAB > MOVWF Multipland > MOVLW 0xCD > MOVWF Multipland+1 > MOVLW 0xEF > MOVWF Multipland+2 > > MOVLW 0x98 > MOVWF Multiplier > MOVLW 0x76 > MOVWF Multiplier+1 > MOVLW 0x54 > MOVWF Multiplier+2 > > ; these values should generate the reply = 0x6651AF33BC6C > > CLRF Product ; clear destination > CLRF Product+1 > CLRF Product+2 > CLRF Product+3 > CLRF Product+4 > CLRF Product+5 > > > MOVLW 23 > MOVWF BitCount ; number of bits > CLRC ; make sure the carry is initially >cleared > >ADD_LOOP_24x24 > ; note carry assummed to be zero ! > > RLF Multiplier+2,F ; up shift multiplier one bit > RLF Multiplier+1,F > RLF Multiplier,F > > BTFSS STATUS,C ; if carry is set we must add multipland >to the product > GOTO SKIP_LOOP_24x24 ; nope, skip this bit > > MOVF Multipland+2,W ; get LSB of multiplicand > ADDWF Product+5,F ; add it to the lsb of the product > > MOVF Multipland+1,W ; middle byte > BTFSC STATUS,C ; check carry for overflow > INCFSZ Multipland+1,W ; > ADDWF Product+4,F ; handle overflow > > MOVF Multipland,W ; MSB byte > BTFSC STATUS,C ; check carry > INCFSZ Multipland,W > ADDWF Product+3,F ; handle overflow > > ; carry propagation, if set then make sure if 'follows' through > > MOVLW 0x01 ; preload with 1 > BTFSC STATUS,C ; check carry for overflow > ADDWF Product+2,F ; add one to next byte > > BTFSC STATUS,C ; check carry for overflow > ADDWF Product+1,F ; add one to next byte > > BTFSC STATUS,C ; check carry for overflow > ADDWF Product,F ; add one to next byte > > >SKIP_LOOP_24x24 > ; shift and prepare for next bit > > ; note Carry is always assumed to be zero here ! > ; should be handled by carry propagation above. > > RLF Product+5,F > RLF Product+4,F > RLF Product+3,F > RLF Product+2,F > RLF Product+1,F > RLF Product,F > ; note carry should always be zero here ( else overflow ? ) > ; affects rotate at the beginning of ADD_LOOP_24x24 > DECFSZ BitCount,F > GOTO ADD_LOOP_24x24 > NOP > NOP > RETURN > ======================================================================= Electronical devices for chemical laboratory, custom electronics design ----------------------------------------------------------------------- Snail Instruments Josef Hanzal, M. S. Vojanova 615 phone/fax: +420-311-624433 266 01 Beroun e-mail: snail@iol.cz Czech Republic URL: http://www.vitrum.cz/snail/ GPS: 49deg58'28" North, 14deg 4'35" East ======================================================================= -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.