Salta al contenuto
Note per Studenti Binary Adders and Subtractors Report

Binary Adders and Subtractors

In questa pagina 7

Binary arithmetic circuits are combinational circuits performing sums, subtractions and other operations on binary numbers. They are the typical iterative circuit: the same sub-function repeats for each bit and identical blocks pass intermediate data (the carries) not appearing in the output. The hierarchical approach is used (Combinational Design - Hierarchy and NAND-NOR MappingA combinational circuit has outputs depending only on the present inputs (no memory, no feedback); a sequential one also depends on history (state, memory, feedback). Design flow: specification, truth table, minimum-cost function, logic diagram, verification. With many inputs the hierarchical approach splits the system into reusable blocks (regularity). Basic blocks: one-variable functions, vectors, enabling. Technology mapping in CMOS: SOP to NAND-NAND, POS to NOR-NOR.Combinational Design - Hierarchy and NAND-NOR Mapping →).

Half adder

The half adder adds two bits X,YX,Y and gives the sum SS (least significant bit of the result) and the carry CC.

XX YY CC SS
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

S=X⊕Y,C=X⋅Y.S=X\oplus Y,\qquad C=X\cdot Y .

X=Y=1X=Y=1: S=0S=0, C=1C=1, i.e. 1+1=1021+1=10_2. It is an XOR plus an AND and has no carry input.

Full adder

The full adder adds three bits: the addends X,YX,Y and the carry in ZZ from the neighbouring less significant bit.

XX YY ZZ CC SS
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

SS is 1 when the number of 1 inputs is odd (odd-parity function): S=X⊕Y⊕Z=∑m(1,2,4,7)S=X\oplus Y\oplus Z=\sum m(1,2,4,7). CC is 1 when at least two inputs are 1 (majority): C=∑m(3,5,6,7)=XY+XZ+YZC=\sum m(3,5,6,7)=XY+XZ+YZ (minimal SOP: 3 prime implicants, all essential).

1+1+1=1121+1+1=11_2 (S=C=1S=C=1); 1+0+1=1021+0+1=10_2 (S=0S=0, C=1C=1).

Propagate and generate. With P=X⊕YP=X\oplus Y (propagate) and G=XYG=XY (generate): S=P⊕ZS=P\oplus Z, C=G+P⋅ZC=G+P\cdot Z.

The carry out exists if the two bits generate a carry alone (X=Y=1X=Y=1) or propagate the carry in (X≠YX\ne Y and Z=1Z=1). So a full adder is: a first half adder (X,Y)→(P,G)(X,Y)\to(P,G), a second half adder (P,Z)→(S,PZ)(P,Z)\to(S,PZ) and an OR joining GG and PZPZ. Equivalence with the majority form: XY+Z(X⊕Y)=XY+X‾YZ+XY‾Z=XY+XZ+YZXY+Z(X\oplus Y)=XY+\overline XYZ+X\overline YZ=XY+XZ+YZ.

Ripple carry adder

To add two nn-bit numbers A=An−1…A0A=A_{n-1}\dots A_0 and B=Bn−1…B0B=B_{n-1}\dots B_0, chain nn full adders: the carry out of each is the carry in of the next, from the LSB (C0C_0, usually 00) to the MSB (CnC_n). The carry ripples along the chain.

 A3 B3     A2 B2     A1 B1     A0 B0
  |  |      |  |      |  |      |  |
 [ FA3 ]<-C3[ FA2 ]<-C2[ FA1 ]<-C1[ FA0 ]<-C0 (=0 or Cin)
  |  |        |         |         |
 C4  S3       S2        S1        S0

0110+01110110+0111 (6+76+7):

bit ii AiA_i BiB_i CiC_i (in) SiS_i Ci+1C_{i+1} (out)
0 0 1 0 1 0
1 1 1 0 0 1
2 1 1 1 1 1
3 0 0 1 1 0

Sum 1101=131101=13, C4=0C_4=0. 1011+01011011+0101 (11+511+5): partial sums all 00 and carries C1=⋯=C4=1C_1=\dots=C_4=1: result 00000000 with C4=1C_4=1, i.e. 100002=1610000_2=16. For unsigned numbers C4=1C_4=1 means overflow (does not fit in 4 bits); for signed numbers the criterion differs (Signed Numbers, Complement Arithmetic and OverflowUnsigned subtraction is correct without borrow out; otherwise the result M-N+2^n is wrong. One's complement is 2^n-1-N (bitwise inversion); two's complement is 2^n-N (one's complement plus 1). Signed numbers: sign-magnitude (two zeros, symmetric range) or two's complement (one zero, range -2^(n-1) to 2^(n-1)-1, MSB weight -2^(n-1)). In two's complement addition and subtraction are the same adder: A-B = A + not B + 1, carry out discarded. Overflow: unsigned if carry out is 1 in an addition; signed if the carry into the sign bit differs from the carry out of it.Signed Numbers, Complement Arithmetic and Overflow →).

A 4-bit adder with carry in has 9 inputs: a truth table of 29=5122^9=512 rows is unmanageable, hence the iterative structure.

Delay

Each full adder can compute its sum only when it receives the carry from the previous one: carries are determined one after the other. With gate delay tGt_G, the carry crosses a stage in about 2tG2t_G (an AND and an OR), so the total delay grows linearly with nn: about 2n tG2n\,t_G for the last sum.

With n=8n=8 and tG=1 nst_G=1\,\mathrm{ns} the last sum arrives after about 16 ns16\,\mathrm{ns}. The ripple adder is the simplest and slowest; for many bits carry look-ahead circuits are used (not covered in the course).

Subtraction: the adder-subtractor

Two's complement subtraction is A−B=A+B‾+1A-B=A+\overline B+1 (Signed Numbers, Complement Arithmetic and OverflowUnsigned subtraction is correct without borrow out; otherwise the result M-N+2^n is wrong. One's complement is 2^n-1-N (bitwise inversion); two's complement is 2^n-N (one's complement plus 1). Signed numbers: sign-magnitude (two zeros, symmetric range) or two's complement (one zero, range -2^(n-1) to 2^(n-1)-1, MSB weight -2^(n-1)). In two's complement addition and subtraction are the same adder: A-B = A + not B + 1, carry out discarded. Overflow: unsigned if carry out is 1 in an addition; signed if the carry into the sign bit differs from the carry out of it.Signed Numbers, Complement Arithmetic and Overflow →). One circuit does both operations: a control signal SS (00 = add, 11 = subtract). Each bit BiB_i goes through an XOR with SS: if S=0S=0 it passes BiB_i, if S=1S=1 it passes Bi‾\overline{B_i} (one's complement). The same SS is the carry in of the first full adder, supplying the "+1". The adder computes A+BA+B (S=0S=0) or A+B‾+1=A−BA+\overline B+1=A-B (S=1S=1). For n=4n=4: four XORs and four full adders in cascade.

Overflow: unsigned addition, carry out =1=1; unsigned subtraction, carry out =1=1 means M≥NM\ge N (correct) and 00 means M<NM<N (result to correct); signed, V=Cin,MSB⊕Cout,MSBV=C_{in,MSB}\oplus C_{out,MSB}.

VHDL

vhdl
entity fa is
  port ( x, y, cin : in  std_logic;
         s, cout   : out std_logic );
end fa;

architecture df of fa is
begin
  s    <= x xor y xor cin;
  cout <= (x and y) or (cin and (x xor y));
end df;

Structural 4-bit adder (four instances of the full adder, internal carries c):

vhdl
architecture ripple of add4 is
  signal c : std_logic_vector(4 downto 0);
begin
  c(0) <= cin;
  FA0 : entity work.fa(df) port map (a(0), b(0), c(0), s(0), c(1));
  FA1 : entity work.fa(df) port map (a(1), b(1), c(1), s(1), c(2));
  FA2 : entity work.fa(df) port map (a(2), b(2), c(2), s(2), c(3));
  FA3 : entity work.fa(df) port map (a(3), b(3), c(3), s(3), c(4));
  cout <= c(4);
end ripple;

Note on the multiplier

As in decimal, the multiplicand is multiplied by each multiplier bit (a product of two bits is an AND), and the partial products are shifted and added. The 2-bit multiplier (A1A0×B1B0A_1A_0\times B_1B_0, product P3P2P1P0P_3P_2P_1P_0) uses 4 ANDs and 2 half adders: P0=A0B0,P1=A1B0⊕A0B1,P2=A1B1⊕(A1B0⋅A0B1),P3=A1B1⋅(A1B0⋅A0B1).P_0=A_0B_0,\quad P_1=A_1B_0\oplus A_0B_1,\quad P_2=A_1B_1\oplus(A_1B_0\cdot A_0B_1),\quad P_3=A_1B_1\cdot(A_1B_0\cdot A_0B_1). Check with A=B=3A=B=3: P0=1P_0=1; P1=1⊕1=0P_1=1\oplus1=0 with carry 11; P2=1⊕1=0P_2=1\oplus1=0; P3=1P_3=1: P=1001=9P=1001=9.

Common mistakes

  • Forgetting the carry in of the full adder (a half adder is not enough for middle bits).
  • Reading C4=1C_4=1 as "wrong result" for signed numbers.
  • Believing the ripple adder is fast: delay grows with nn.
  • Swapping SS (sum bit) and CC (carry) in the half adder outputs.

Versione ripasso

  • Iterative circuit: same sub-function per bit, carries passed along; hierarchical design.
  • Half adder: S=X⊕YS=X\oplus Y, C=XYC=XY; 1+1=1021+1=10_2.
  • Full adder: S=X⊕Y⊕Z=∑m(1,2,4,7)S=X\oplus Y\oplus Z=\sum m(1,2,4,7), C=XY+XZ+YZ=∑m(3,5,6,7)C=XY+XZ+YZ=\sum m(3,5,6,7) (majority). 1+1+1=1121+1+1=11_2, 1+0+1=1021+0+1=10_2.
  • Propagate/generate: P=X⊕YP=X\oplus Y, G=XYG=XY, S=P⊕ZS=P\oplus Z, C=G+PZC=G+PZ (two half adders + OR). X=1,Y=0,Z=1X=1,Y=0,Z=1: P=1,G=0,S=0,C=1P=1,G=0,S=0,C=1.
  • Ripple carry, nn bits: nn full adders in cascade, C0=0C_0=0 or carry in. 0110+0111=1101=130110+0111=1101=13; 1011+0101=00001011+0101=0000 with C4=1C_4=1 (1616): unsigned overflow.
  • Table size: 9 inputs, 29=5122^9=512 rows: use the iterative structure.
  • Delay: about 2tG2t_G per stage, 2n tG2n\,t_G total (8 bits, 1 ns: 16 ns); linear in nn.
  • Adder-subtractor: XOR of each BiB_i with SS, and SS as carry in: S=0⇒A+BS=0\Rightarrow A+B, S=1⇒A+B‾+1=A−BS=1\Rightarrow A+\overline B+1=A-B. Overflow: unsigned CoutC_{out}; signed V=Cin,MSB⊕Cout,MSBV=C_{in,MSB}\oplus C_{out,MSB}.
  • VHDL: s <= x xor y xor cin; cout <= (x and y) or (cin and (x xor y)); structural 4-bit with entity work.fa(df) and c(4 downto 0).
  • 2-bit multiplier: P0=A0B0P_0=A_0B_0, P1=A1B0⊕A0B1P_1=A_1B_0\oplus A_0B_1, P2=A1B1⊕(A1B0⋅A0B1)P_2=A_1B_1\oplus(A_1B_0\cdot A_0B_1), P3=A1B1⋅(A1B0⋅A0B1)P_3=A_1B_1\cdot(A_1B_0\cdot A_0B_1): 4 ANDs, 2 half adders; 3×3=10013\times3=1001.
  • Mistakes: carry in forgotten; CnC_n as signed error; ripple thought fast; SS and CC swapped.

Teoria collegata