DEV Community

GTU Hero
GTU Hero

Posted on

Booth's Multiplication Algorithm, Simulated in Python and Traced Step by Step (7 x -3 in 4 Bits)

Booth's algorithm is one of those Computer Organization and Architecture topics that looks like magic in the textbook flowchart and becomes obvious the moment you trace one example yourself. It multiplies two signed numbers in two's complement without any special handling for negative values. In this post we write a small Python simulator that prints the register table, then read that table line by line so you can reproduce it on paper in an exam.

Disclosure: GTU Hero is my project (link at the end). The simulator below is my own code, and the trace table is copied from a real run.

The registers

For n bit operands, Booth's algorithm uses:

  • M: the multiplicand (n bits)
  • Q: the multiplier (n bits)
  • A: the accumulator, starts at 0 (n bits)
  • Q-1: one extra bit to the right of Q, starts at 0
  • count: starts at n

The final product sits in A and Q together (2n bits).

The rule, in one table

At every step, look at the pair (Q0, Q-1), where Q0 is the least significant bit of Q:

Q0 Q-1 Action
0 0 nothing
1 1 nothing
1 0 A = A - M
0 1 A = A + M

After the action, do an arithmetic shift right on A, Q, Q-1 as one combined register. Arithmetic means the sign bit of A is copied, not replaced with 0. Decrement count. Stop when count hits 0.

Why does this work? A run of 1s in the multiplier, like 0111 1000, is equal to a big power of two minus a small one. Booth subtracts M at the start of a run (pair 10) and adds it back at the end of a run (pair 01). Fewer operations for long runs, and negative multipliers come out right for free because two's complement is built in.

The simulator

def to_bits(v, n):
    return format(v & ((1 << n) - 1), f'0{n}b')

def booth(m, q, n=4):
    mask = (1 << n) - 1
    A, Q, Q_1, M = 0, q & mask, 0, m & mask
    print(f"{'step':<6}{'A':<7}{'Q':<7}{'Q-1':<5}action")
    print(f"{'init':<6}{to_bits(A,n):<7}{to_bits(Q,n):<7}{Q_1:<5}")
    for step in range(1, n + 1):
        pair = (Q & 1, Q_1)
        if pair == (1, 0):
            A = (A - M) & mask; act = "A = A - M"
        elif pair == (0, 1):
            A = (A + M) & mask; act = "A = A + M"
        else:
            act = "no add/sub"
        # arithmetic shift right of A, Q, Q-1 together
        Q_1 = Q & 1
        Q = ((Q >> 1) | ((A & 1) << (n - 1))) & mask
        A = (A >> 1) | (A & (1 << (n - 1)))
        print(f"{step:<6}{to_bits(A,n):<7}{to_bits(Q,n):<7}{Q_1:<5}{act}, then ASR")
    result = (A << n) | Q
    if result & (1 << (2 * n - 1)):
        result -= 1 << (2 * n)
    return result

r = booth(7, -3)
print("result:", r, "binary:", to_bits(r, 8))
Enter fullscreen mode Exit fullscreen mode

Two lines deserve attention. & mask keeps every register at exactly n bits, which is how real hardware behaves (overflow bits just fall off). And the shift order matters: the old LSB of Q goes into Q-1, the old LSB of A goes into the MSB of Q, and A shifts right while keeping its own sign bit.

The output

M = 7 = 0111, Q = -3 = 1101 (two's complement of 0011), and -M = 1001.

step  A      Q      Q-1  action
init  0000   1101   0
1     1100   1110   1    A = A - M, then ASR
2     0001   1111   0    A = A + M, then ASR
3     1101   0111   1    A = A - M, then ASR
4     1110   1011   1    no add/sub, then ASR
result: -21 binary: 11101011
Enter fullscreen mode Exit fullscreen mode

Reading the trace like an examiner

Step 1. Pair (Q0, Q-1) = (1, 0), so A = A - M = 0000 + 1001 = 1001. Now shift 1001 1101 0 right arithmetically: A becomes 1100 (sign bit 1 copied in), the 1 that fell off A goes into Q giving 1110, and the 1 that fell off Q goes into Q-1.

Step 2. Pair = (0, 1), so A = A + M = 1100 + 0111 = 1 0011. Drop the carry (4 bit register), so A = 0011. Shift: A = 0001, Q = 1111, Q-1 = 0.

Step 3. Pair = (1, 0), so A = 0001 + 1001 = 1010. Shift: A = 1101, Q = 0111, Q-1 = 1.

Step 4. Pair = (1, 1), no arithmetic. Shift: A = 1110, Q = 1011, Q-1 = 1.

Count is now 0. The product is A and Q together: 1110 1011.

Verifying the answer

11101011 starts with 1, so it is negative. Take the two's complement to read its magnitude: invert to 00010100, add 1 to get 00010101 = 21. So the product is -21, and 7 x -3 is indeed -21. Always write this verification line in the exam. It takes fifteen seconds and proves the table is right.

Mistakes that cost marks

  1. Logical shift instead of arithmetic shift. If you push a 0 into the MSB of A when A is negative, every later step is wrong. In step 1 above, A = 1001 must shift to 1100, not 0100.
  2. Keeping the carry. In step 2, 1100 + 0111 gives a fifth bit. Registers are 4 bits, so it is discarded.
  3. Checking the wrong pair. The pair is (Q0, Q-1), the two rightmost bits of the combined register, checked before the shift.
  4. Stopping early. You always do exactly n steps, even if the rest of Q looks like zeros.
  5. Not writing -M first. Write M and -M in two's complement at the top of your answer, so every subtraction is just an addition you can check.

Practice

Run booth(-5, 6) and booth(-4, -3), but first build the table by hand and compare. For a harder variant, try 5 bit registers with booth(9, -7, n=5). Then look for "Booth" and "multiplication algorithm" in actual exam papers. The Computer Organization and Architecture previous year papers on gtuhero.com are listed for codes 3140707 and BE04000251, with 14 papers spanning 2020 to 2026, so you can check how often this flowchart plus trace question appears.

Top comments (0)