20160819 01:25:18 +00:00



# Russian Peasant Multiplication





20200205 16:46:06 +00:00



From Assembly to Basic to Javascript!

20160819 01:33:28 +00:00




20200205 16:46:06 +00:00



Here are my implementations of Russian Peasant Multiplication implemented in various languages:

20160819 01:25:18 +00:00




20200205 16:46:06 +00:00



* 6502 Assembly Language (Both [ca65](rpm_ca65.s) and [merlin32](rpm_m32.s) sources)

20160819 01:25:18 +00:00



* Applesoft BASIC




* JavaScript (Procedural version)




* JavaScript (OOP version)





20160819 01:33:28 +00:00



A .dsk image has been provided as an convenience.

20160819 01:25:18 +00:00







To see how much faster the Assembly version is then the BASIC version:








```




RUN RPM.BAS




BRUN RPM.BIN




```








And enter in `123456789` * `987654321` respectively for A and B ...








 Version  Time 




::




 Applesoft  33 s 




 Assembly  ~1 s 





20160819 14:17:35 +00:00



# So what the heck is it?





20160819 14:52:19 +00:00



An alternative algorithm to implement multiplication using only:

20160819 14:17:35 +00:00




20200205 16:46:25 +00:00



* bitshifts (left and right), and

20160819 14:17:35 +00:00



* addition.





20160819 15:13:09 +00:00



Example of "traditional" multiplication:

20160819 14:17:35 +00:00







In base 10:








```




86




x 57









602




430




====




4902




```








In base 2:





20160819 14:20:26 +00:00



```

20160819 14:17:35 +00:00



01010110 (86)




00111001 (57)









01010110 (86 * 2^0 = 86)

20200205 16:46:48 +00:00



00000000 (86 * 2^1 = 172) < wasted work, partial sum = 0




00000000 (86 * 2^2 = 344) < wasted work, partial sum = 0

20160819 14:17:35 +00:00



01010110 (86 * 2^3 = 688)




01010110 (86 * 2^4 = 1376)




01010110 (86 * 2^5 = 2752)




==============




01001100100110 (4902 = 86*2^0 + 86*2^3 + 86*2^4 + 86*2^5)

20160819 14:20:26 +00:00



```

20160819 14:17:35 +00:00







Example of Russian Peasant multiplication:








In Base 10:








```

20160819 15:13:09 +00:00



A B Sum = 0




86 57 + A = 86 (b is odd)




x 2 = 172 / 2 = 28 = 86




x 2 = 344 / 2 = 14 = 86




x 2 = 688 / 2 = 7 + A = 774 (b is odd)




x 2 = 1376 / 2 = 3 + A = 2150 (b is odd)




x 2 = 2752 / 2 = 1 + A = 4902 (b is odd)

20160819 14:17:35 +00:00



```








In Base 2:








```

20160819 15:13:09 +00:00



A B Sum = 0




01010110 00111001 + A = 00000001010110 (b is odd)




010101100 00011100 = 00000001010110




0101011000 00001110 = 00000001010110




01010110000 00000111 + A = 00001100000110 (b is odd)




010101100000 00000011 + A = 00100001100110 (b is odd)




0101011000000 00000001 + A = 01001100100110 (b is odd)

20160819 14:17:35 +00:00



```
