Booth%27s Algorithm Calculator

Booth Algorithm is a multiplication algorithm which takes two register values and provides a product of those registers. LOGIC Behind the Algorithm: Let’s explain the logic using an example of 7 x 3.

booth.py
frombitstringimportBitArray
''
Returns m * r using Booth's algorithm.
x = len(m) and y = len(r). Note that this is the length in base 2.
See http://en.wikipedia.org/wiki/Booth%27s_algorithm
''
defbooth(m, r, x, y):
# Initialize
totalLength=x+y+1
mA=BitArray(int=m, length=totalLength)
rA=BitArray(int=r, length=totalLength)
A=mA<< (y+1)
S=BitArray(int=-m, length=totalLength) << (y+1)
P=BitArray(int=r, length=y)
P.prepend(BitArray(int=0, length=x))
P=P<<1
print'Initial values'
print'A', A.bin
print'S', S.bin
print'P', P.bin
print'Starting calculation'
foriinrange(1,y+1):
ifP[-2:] '0b01':
P=BitArray(int=P.int+A.int, length=totalLength)
print'P + A:', P.bin
elifP[-2:] '0b10':
P=BitArray(int=P.int+S.int, length=totalLength)
print'P + S:', P.bin
P=arith_shift_right(P, 1)
print'P >> 1:', P.bin
P=arith_shift_right(P, 1)
print'P >> 1:', P.bin
returnP.int
defarith_shift_right(x, amt):
l=x.len
x=BitArray(int= (x.int>>amt), length=l)
returnx
# Sample usage: find 86 * 41
b=booth(86, 41, 8, 8)
printb
  1. Modified Booth's Algorithm.
  2. The numerical example of the Booth's Multiplication Algorithm is 7 x 3 = 21 and the binary representation of 21 is 10101. Here, we get the resultant in binary 00010101. Now we convert it into decimal, as (000010101) 10 = 2.4 + 2.3 + 2.2 + 2.1 + 2.0 = 21. Example: Multiply the two numbers 23 and -9 by using the Booth's multiplication algorithm.
Booth%27s
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
This small project was an assignment for CS 441 - Computer Architecture during Fall 2016 at UW Stout. I completed this project with Abby Peterson. We were allowed to use any programming language we wanted. Abby and I chose to use JavaScript and HTML/CSS because we wanted to get experience in a language we hadn't used in class.

Booth 27s Algorithm Calculator Free

Project Details


  1. Write program to calculate 8-bit Booth's Multiplier
  2. Input in decimal
  3. Output in both binary and decimal
  4. Show all steps
  5. Choose any programming language you're comfortable with
Calculator

The Process


Writing the actual Booth's algorithm was quick and easy. The vast majority of the code was formatting. I had some HTML/CSS experience from middle school, but neither Abby nor I had ever used JavaScript. Luckily, it's pretty easy to learn. CSS was pretty frustrating. I had forgotten how finicky it can seem when you aren't fluent in it.
I planned on adding the ability to customize the number of bits (and I hate hard-coding data), so I made the number of bits a global variable and set it in cs441p1_submit(). This ended up being a life saver. I found out that the homework required a different number of bits than I had originally set. Rather than changing countless lines of code, I only changed the 'bit' variable.
We used the toString() and parseInt() functions a lot, because the made it super easy to translate from binary to decimal and vice versa.

Booth 27s Algorithm Calculator Online

AlgorithmOne improvement we could have made was using HTML5 tags, especially for the form input options.
Here is the final result:

Booth 27s Algorithm Calculator Instructions


Booth 27s Algorithm Calculator Download

View on GitHub