Hi Byron, There is a generic multibyte addition routine that is on the Microchip docs. I don't know who made it but works like a charm. ;A = A + B movf B0,w addwf A0,f ; add LSB movf B1,w ; W = next byte skpnc ; skip next instr if there's a carry incfsz B1,w ; inc W and skip next if B1 = 0xFF, C is still set addwf A1,f ; add it except if C and B1 = 0xFF movf B2,w ; carry is propagated here if B1 was 0xFF skpnc ; and C was set incfsz B2,w ; or from the last addition addwf A1,f movf B3,w skpnc incfsz B3,w addwf A3,f . . . subtraction is also similar: ; A = A - B movf B0,w subwf A0,f movf B1,w skpc incfsz B1,w subwf A1,f . . . regards, Reggie Byron A Jeff wrote: > > > > > Hi, > > > > I think the routine may not work on some values because of the 32 bit > > addition. The carry will not be passed to prod2 if prod1 is 0xFF and > > there is a carry from the last addition of prod0 and twofour0. Example: > > 0xFFFFFF * 3 will have and erroneous result. > > Agreed. There needs to be some additional code to propogate the carry all the > way through. Let me take a stab at it. > > The updates are below. I tested them and they seem to work OK. > > BAJ > > > > regards, > > Reggie > > > > > > Saurabh Sinha wrote: > > > > > > Byron, > > > > > > Thank you for your code. It is 100%........ > > > > > > Regards, > > > > > > Saurabh > > > > > > ----- Original Message ----- > > > From: Byron A Jeff > > > To: > > > Sent: Thursday, March 23, 2000 8:50 AM > > > Subject: Re: multiplication routing 24x8 > > > > > > > > > > > > > Byron, > > > > > > > > > > Thank you for your code. I tried to run it, but I am having certain > > > > > problems. Take for example, > > > > > the following problem, in 2s complement. > > > > > > > > > > The following variable declerations are used: > > > > > > > > > > TWOFOUR2 B'00001000' Equivalent: c0=534035 (dec) > > > > > TWOFOUR1 B'00100110' > > > > > TWOFOUR0 B'00010011' > > > > > > > > > > MULTIPLIER B'00000111' Equivalent: u0=7 (dec) > > > > > > > > > > Result: (MPLAB) > > > > > > > > > > PROD2 B'00001000' Equivalent: c0u0 (dec) > > > > > PROD1 B'00100110' > > > > > PROD0 B'00010011' > > > > > > > > > > This does not seem to be the correct answer. > > > > > > > > It's not. It's the partial product generated when you add the original > > > > multiplicand to the first product, namely 0. Note that it's exactly the > > > > same as the original number in TWOFOUR. > > > > > > > > > > Any ideas on where the error could possibly be, or is it just me getting > > > > > confused with 2's comp...? > > > > > > > > Sure. The program didn't loop to multloop so it never finished the > > > > multiplication. > > > > > > > > I'm sure I said that I generated that code off the top of my head. Just > > > checked > > > > the archive and the goto at the bottom of the loop is there. > > > > > > > > I've now compiled it with picasm and tested it with gpsim on my Linux box. > > > > Surprisingly enough I nailed it the first time. It does generate the > > > > expected result if the goto is listed at the bottom of the routine. > > > > > > > > Also note that I set up mult24x8 as a subroutine to be called. > > > > > > > > > > > > > > Thanks in advance for your help. > > > > > > > > No problem. > > > > > > > > > > > > > > Regards, > > > > > > > > > > Saurabh > > > > > ======================================================CODE > > > > > USED================================ > > > > > > > > > > mult24x8: > > > > > clrf prod0 ; Clear the product > > > > > clrf prod1 > > > > > clrf prod2 > > > > > clrf prod3 > > > > > clrf twofour3 ; Clear the top byte of the multiplicand > > > > > return > > > > > > > > > > multloop: > > > > > movf mulitiplier,W ; Check to see if eight bit is empty > > > > > btfsc STATUS,Z ; Done if zero > > > > > return > > > > > bcf STATUS,C ; clear carry > > > > > rrf mulitiplier,F ; Get the next bit > > > > > btfss STATUS,C ; Add if carry is set > > > > > goto multshift ; shift only if 0 > > > > > movf twofour0,W ; Add the 32 bits of multiplicand to > > > product > > > > > addwf prod0,F ; each addwf sets the carry > > > > > btfsc STATUS,C ; Don't increment if no carry > > > > > incf prod1,F ; Add one to next byte if carry occured > ;Replace the line above with a call to the carry propogation routine. > call carryprop1 > > > > > movf twofour1,W ; > > > > > addwf prod1,F ; > > > > > btfsc STATUS,C ; Don't increment if no carry > > > > > incf prod2,F ; Add one to next byte if carry occured > ;Replace the line above with a call to the carry propogation routine. > call carryprop2 > > > > > movf twofour2,W ; > > > > > addwf prod2,F ; > > > > > btfsc STATUS,C ; Don't increment if no carry > > > > > incf prod3,F ; Add one to next byte if carry occured > ;Propogation unnecessary for the last byte. Leave as incf above. > > > > > movf twofour3,W ; > > > > > addwf prod3,F ; > > > > > > > > > > multshift: > > > > > bcf STATUS,C ; clear carry > > > > > rlf twofour0,F ; Shift the 24 bits one bit to the left > > > > > rlf twofour1,F > > > > > rlf twofour2,F > > > > > rlf twofour3,F > > > > > > > > goto multloop ; need to add all partial products. > > carryprop1 incfsz prod1,F > return > carryprop2 incfsz prod2,F > return > carryprop3 incf prod3,F > return > > > > > > > > > > return ; Neither of these returns are required. > > > > > return ; The routine will return when the multiplier is 0. > > > > > > > > > > >