⚡ Hardware Algorithm

Interactive Guide to Signed Addition & Subtraction

🎮 Try It Yourself!

🔧 Core Components

📦
Registers A & B
Hold the magnitudes (absolute values) of the numbers being processed
🔄
Sign Flip-Flops
As and Bs store signs: 0 = positive (+), 1 = negative (-)
💾
Accumulator (EA)
Combined register where A holds result and E stores carry-out bit
⚠️
AVF (Overflow)
Indicates arithmetic overflow when adding same-sign numbers

📊 Algorithm Flowchart

Signed Magnitude Flowchart

🎯 Algorithm Logic

Step 1: Sign Comparison

The algorithm uses XOR operation on signs:

As ⊕ Bs

Result = 0: Signs are identical (both + or both -)

Result = 1: Signs are different (one +, one -)

Step 2: Path Selection

ADD Operation

• Same signs → Add magnitudes, check overflow

• Different signs → Subtract magnitudes (redirect to subtraction logic)

SUBTRACT Operation

• Different signs → Add magnitudes (treat as addition)

• Same signs → Subtract magnitudes using 2's complement

➕ Addition Process

When Signs are Identical

EA ← A + B

✓ Add the magnitudes directly

✓ Store sum in register A

✓ Carry-out goes to E flip-flop

✓ Check overflow: AVF ← E

✓ Result sign = original sign (As)

➖ Subtraction Process

When Signs are Identical

EA ← A + B' + 1

✓ Use 2's complement: complement B, add 1

✓ Add to A

✓ AVF ← 0 (no overflow in subtraction)

✓ Check carry bit E:

If E = 1 (A ≥ B)

• Result in A is correct

• Sign stays as As

• If A = 0, force sign to positive (+0)

If E = 0 (A < B)

• Result is 2's complement of true answer

• Take 2's complement: A ← A' + 1

• Flip sign: As ← As'

💡 Key Takeaways

⚡ Efficiency: Single adder handles both addition and subtraction
🔀 Sign Logic: XOR gate determines whether to add or subtract magnitudes
🔢 2's Complement: Enables subtraction through addition of complement
🎭 Dual-Purpose Carry: E bit signals overflow in addition, magnitude comparison in subtraction
Start Learning Signed 2's Complement Arithmetic