Hi, sorry to You all but Josef found the error :) subtle ( albeit 23 loops with 24 bit numbers.. ;) ). Josef wrote: >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 =3D ((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. You are correct ofcource, below is the 'fixed' version. Took a while before I found it myself which brings up another subject, automated tests, any suggestion for automating tests for these kind of routines ? = Ok this was was quite easily spotted once you started to *read* the code but still sometime one has more advanced routines what then ? And sorry about the omitting the number base, normally I try to = always specify the base, but as this was just the first version I inadvertely left that out. So without further ado here is the corrected version: **CORRECTED 24x24 bit routine ** ; *********************************************************************** ; MULTTIPLY_24x24 - Routine to mutltiply two 24 bit numbers = ; result in 48 bits ! ; Format: Ram =3D MSB, Ram+2 =3D 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 ; ; 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 =3D 0x6651AF33BC6C = CLRF Product ; clear destination CLRF Product+1 CLRF Product+2 CLRF Product+3 CLRF Product+4 CLRF Product+5 = MOVLW D'24' MOVWF BitCount ; number of bits CLRC ; make sure the carry is initially cleared ADD_LOOP_24x24 ; note carry assummed to be zero ! ; shift and prepare for next bit ( first time not really needed ) RLF Product+5,F RLF Product+4,F RLF Product+3,F RLF Product+2,F RLF Product+1,F RLF Product,F = 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 ; if carry set we add one to the source = ADDWF Product+4,F ; and add it (if not zero, in that case mulitpland =3D 0xff->0x00 ) = 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 ; 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 /Tony Tony K=FCbek, Flintab AB = =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2 E-mail: tony.kubek@flintab.com =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2=B2= =B2=B2=B2=B2=B2=B2=B2=B2=B2=B2 -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu