Loading lessons...
Computer Arithmetic: Addition and Subtraction of Signed-Magnitude and 2's Complement Data
Computer Arithmetic: Addition and Subtraction of Signed-Magnitude and 2's Complement Data
Unit IV starts with computer arithmetic: the algorithms and hardware a processor uses to add, subtract, multiply and divide. Lesson 2 introduced the three signed representations; here we see how the arithmetic unit actually processes them. This lesson covers addition and subtraction, first for signed-magnitude numbers (the hardest case, because it must compare magnitudes) and then for signed 2's complement numbers (the easy case used in almost every processor).
1. Signed-Magnitude Addition and Subtraction
A signed-magnitude number has a sign bit (0 = +, 1 = −) and a magnitude. Let the magnitudes be A and B. Doing arithmetic by hand, we look at the signs and either add or subtract the magnitudes:
| Operation | Add magnitudes | Subtract magnitudes: A > B | A < B | A = B |
|---|---|---|---|---|
| (+A) + (+B) | +(A + B) | |||
| (+A) + (−B) | +(A − B) | −(B − A) | +(A − B) | |
| (−A) + (+B) | −(A − B) | +(B − A) | +(A − B) | |
| (−A) + (−B) | −(A + B) | |||
| (+A) − (+B) | +(A − B) | −(B − A) | +(A − B) | |
| (+A) − (−B) | +(A + B) | |||
| (−A) − (+B) | −(A + B) | |||
| (−A) − (−B) | −(A − B) | +(B − A) | +(A − B) |
The rule behind the table:
- Addition: if the signs are the same, add the magnitudes and keep the sign of A. If they differ, subtract the smaller magnitude from the larger; the result takes the sign of the larger.
- Subtraction: change the sign of B, then follow the addition rule.
- When A = B with different signs the result is +0 (a −0 must never be produced).
1.1 Hardware
Hardware for signed-magnitude addition and subtraction
- Registers A and B hold the magnitudes, flip-flops As and Bs the signs.
- A complementer passes B (M = 0) or B' (M = 1); with the input carry = M, the parallel adder forms A + B or A + B' + 1 = A − B.
- The adder's output carry goes to E; AVF (add overflow flag) records overflow. The sum goes back into A.
1.2 Algorithm
Flowchart of the signed-magnitude algorithm
- For subtraction, complement the sign of B: Bs ← overline{Bs}.
- If As = Bs: EA ← A + B and AVF ← E (a carry out means the magnitude is too large).
- If As ≠ Bs: EA ← A + overline{B} + 1 and AVF ← 0 (subtracting cannot overflow).
- E = 1 means A ≥ B: the result in A is correct. If A = 0, set As ← 0 (avoid −0).
- E = 0 means A < B: A holds the 2's complement of the true magnitude. Take A ← overline{A} + 1 and change the sign: As ← overline{As}.
With 5-bit magnitudes, compute (+13) + (−7).
- As = 0, Bs = 1: signs differ, subtract. A = 01101, overline{B} + 1 = 11000 + 1 = 11001.
- A + overline{B} + 1 = 01101 + 11001 = 1 00110: E = 1, A = 00110.
- E = 1 so A ≥ B and the result is correct with As = 0.
Compute (+7) + (−13).
- Subtract: 00111 + overline{01101} + 1 = 00111 + 10011 = 0 11010: E = 0.
- E = 0 so A < B: A ← overline{11010} + 1 = 00101 + 1 = 00110, and As ← overline{0} = 1.
Compute (−18) − (−7).
- Subtraction: Bs ← overline{1} = 0. Now As = 1 ≠ Bs = 0: subtract magnitudes.
- 10010 + overline{00111} + 1 = 10010 + 11001 = 1 01011: E = 1, A = 01011 = 11.
- E = 1: result correct, sign As = 1.
Compute (−20) + (−15) with 5-bit magnitudes.
- Signs equal: add magnitudes. 10100 + 01111 = 1 00011: E = 1.
- AVF ← E = 1: the magnitude 35 does not fit in 5 bits (maximum 31).
2. Signed 2's Complement Addition and Subtraction
In 2's complement, the sign bit is treated like any other bit: just add, and to subtract add the 2's complement:
Any carry out of the sign bit is discarded. The hardware is the adder-subtractor of lesson 27 plus an overflow flip-flop V:
Adder-subtractor used for 2's complement arithmetic
Overflow happens when two numbers of the same sign give a result of the other sign. It is detected by
the XOR of the carries out of and into the sign position.
Compute (a) (+70) + (+80), (b) (−35) − (+40), (c) (+100) − (−50), and give V.
- (a) 01000110 + 01010000 = 10010110. C8 = 0, C7 = 1: V = 1, overflow (150 > 127; the result looks like −106).
- (b) −35 = 11011101, and −40 = 11011000. Sum = 1 10110101: result 10110101 = −75, C8 = 1, C7 = 1: V = 0.
- (c) Subtracting −50 is adding +50: 01100100 + 00110010 = 10010110: C8 = 0, C7 = 1: V = 1, overflow.
2.1 Comparing the two methods
| Signed magnitude | Signed 2's complement | |
|---|---|---|
| Sign handling | separate sign logic, compare magnitudes | sign bit added like any bit |
| Subtraction | complement B's sign, then the add rule | add the 2's complement |
| Extra correction | when A < B: complement the result, change sign | none |
| Zero | two forms (+0, −0) to guard against | one form |
| Overflow detection | carry out of the magnitude (same signs only) | V = Cn oplus Cn-1 |
| Used for | floating-point mantissas, multiplication | integer arithmetic in almost all CPUs |
In signed magnitude, overflow is only possible when the magnitudes are added (same signs for addition, different signs for subtraction). In 2's complement, overflow is only possible when the two numbers being added have the same sign. In both, check overflow before trusting the result.
3. Verilog: The Signed-Magnitude Algorithm
The module follows the flowchart step by step with 5-bit magnitudes: complement Bs for subtraction, add or subtract magnitudes, correct the result when E = 0, and force +0. The testbench runs Examples 1 to 4 and an A = B case:
// Signed-magnitude addition and subtraction (Mano's algorithm) with 5-bit
// magnitudes: registers As A, Bs B, the carry E and the overflow flag AVF
module sm_addsub (
input sub, // 0: A + B, 1: A - B
input as_in,
input [4:0] a_in,
input bs_in,
input [4:0] b_in,
output reg as,
output reg [4:0] a,
output reg avf
);
reg bs, e;
reg [5:0] ea;
always @(*) begin
as = as_in;
a = a_in;
bs = bs_in ^ sub; // subtraction: complement the sign of B
avf = 1'b0;
if (as == bs) begin // same signs: add magnitudes
ea = a + b_in;
{e, a} = ea;
avf = e; // a carry out is an overflow
end else begin // different signs: A + B' + 1
ea = a + {1'b0, ~b_in} + 6'd1;
{e, a} = ea;
if (e == 1'b0) begin // A < B: take the 2's complement, change sign
a = ~a + 5'd1;
as = ~as;
end else if (a == 5'd0)
as = 1'b0; // A = B: the result is +0, never -0
end
end
endmodule
module tb;
reg sub, as_in, bs_in;
reg [4:0] a_in, b_in;
wire as, avf;
wire [4:0] a;
sm_addsub u (sub, as_in, a_in, bs_in, b_in, as, a, avf);
task run(input s, input sa, input [4:0] ma, input sb, input [4:0] mb);
begin
sub = s;
as_in = sa;
a_in = ma;
bs_in = sb;
b_in = mb;
#1 $display("(%s%0d) %s (%s%0d) = %s%0d As A = %b %b AVF=%b",
sa ? "-" : "+", ma, s ? "-" : "+", sb ? "-" : "+", mb,
as ? "-" : "+", a, as, a, avf);
end
endtask
initial begin
run(0, 0, 5'd13, 1, 5'd7); // (+13) + (-7)
run(0, 0, 5'd7, 1, 5'd13); // (+7) + (-13)
run(1, 1, 5'd18, 1, 5'd7); // (-18) - (-7)
run(1, 0, 5'd9, 1, 5'd12); // (+9) - (-12)
run(0, 1, 5'd20, 1, 5'd15); // (-20) + (-15): magnitude overflow
run(1, 1, 5'd6, 1, 5'd6); // (-6) - (-6) = +0
end
endmoduleEvery result matches the worked examples. The overflow case sets AVF = 1, and (−6) − (−6) gives +0, not −0:
(+13) + (-7) = +6 As A = 0 00110 AVF=0
(+7) + (-13) = -6 As A = 1 00110 AVF=0
(-18) - (-7) = -11 As A = 1 01011 AVF=0
(+9) - (-12) = +21 As A = 0 10101 AVF=0
(-20) + (-15) = -3 As A = 1 00011 AVF=1
(-6) - (-6) = +0 As A = 0 00000 AVF=0Key points to remember
- Signed magnitude: same signs → add magnitudes (AVF ← E); different signs → A + B' + 1.
- After subtracting magnitudes: E = 1 means A ≥ B (result correct, avoid −0); E = 0 means A < B (complement A, flip As).
- Subtraction = complement Bs, then add.
- 2's complement: add, or add overline{B} + 1; overflow V = Cn oplus Cn-1.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain the hardware and the algorithm (with flowchart) for addition and subtraction of signed-magnitude numbers. (10 Marks)
- Write: the 8-case table, the hardware (A, B, As, Bs, E, AVF, complementer, adder), the flowchart.
- Q2. Perform (+7) + (−13) and (−18) − (−7) using the signed-magnitude algorithm with 5-bit magnitudes. (5 Marks)
- Q3. Explain addition and subtraction of signed 2's complement numbers. How is overflow detected? (5 Marks)
- Q4. Compute (+70) + (+80) and (−35) − (+40) in 8-bit 2's complement and state whether overflow occurs. (5 Marks)
- Q5. Why must the signed-magnitude algorithm check for A = 0 at the end? (2.5 Marks)