3

I need to multiply two 32-bit SIGNED numbers using addition and shifting and get 64-bit number stored in memory locations $0408-$040F . This two numbers are stored in 8-bits memories.

 movb #$1F, $0400 ; the first number is $1F230001
 movb #$23, $0401
 movb #$00, $0402
 movb #$01, $0403
 movb #$F8, $0404 ; the second number is $F8012346
 movb #$01, $0405
 movb #$23, $0406
 movb #$46, $0407

I know how to multiply two 8-bits numbers with addition and shifting but I dont know how to go on with the others. I used an 16-bit Accumulator-D (accumulator-A 8bits MSB, accumulator-B 8bits LSB).

I am using CPU12: Reference Manual

Can you help me please, guide me or show me how to do it please? Thank you, I spent much time on this but I don't know how to do it with 32-bits numbers.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
forgatn
  • 271
  • 1
  • 5
  • 18
  • 1
    It's the same algorithm, just with more bits. – Raymond Chen Nov 02 '13 at 02:08
  • Yes but how to do it when I have 4 8-bit memory locations, I can go through 8 bits (shifting, adding) but how to move to continue with next 8-bits? – forgatn Nov 02 '13 at 09:16
  • 1
    Oh, this CPU has a 16-bit accumulator so you never needed to learn how to do 16-bit arithmetic using only 8-bit operations. (Hence you have no experience to generalize from.) Use the ADC instructions. [This question](http://stackoverflow.com/questions/10879819/add-numbers-in-avr-assembly/10894701#10894701) shows how. – Raymond Chen Nov 02 '13 at 13:32

1 Answers1

3

rewrite numbers as 8bit digits (base = 256) and solve multiplication algebraically:

 (a0+(a1<<8)+(a2<<16)+(a3<<24))
*(b0+(b1<<8)+(b2<<16)+(b3<<24))
------------------------------------
=(a0·b0                        )<< 0
+(a0·b1 + a1·b0                )<< 8
+(a0·b2 + a1·b1 + a2·b0        )<<16
+(a0·b3 + a1·b2 + a2·b1 + a3·b0)<<24
+(        a1·b3 + a2·b2 + a3·b1)<<32
+(                a2·b3 + a3·b2)<<40
+(                        a3·b3)<<48

Now there are just 8-bit * 8-bit multiply and 8/16 bit shift/add. Do not forget to carry on to higher digits (like add,adc,adc,...).

Hope I did not make a mistake

PS.

If you have 16 bit multiply can rewrite all process with base 65536 instead also can use Karatsuba algorithm for multiply for some speed up but be careful with the carry it needs more bits in such case.

Also I would do this on non signed numbers and add the sign evaluation later on

Here some related QAs of mine:

Spektre
  • 49,595
  • 11
  • 110
  • 380