Chapter3 Number Representation
Chapter3 Number Representation
Number Representation
and
Arithmetic Circuits
Binary numbers
Unsigned numbers
• all bits represent the magnitude of a positive integer
Signed numbers
• left-most bit represents the sign of a number
Table 3.1. Numbers in different systems.
x 0 0 1 1
+y +0 +1 +0 +1
cs 00 01 01 10
Carry Sum
Carry Sum
x y c s
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
x
s
y
x s
HA
y c
c
yi si
ci
ci + 1
(c) Circuit
ci
si
xi
yi
ci + 1
c1
cn FA cn ” 1 c2 FA FA c0
sn – 1 s1 s0
MSB position LSB position
A : a7 a0
x7 x0 y7 y0
c7
s7 s0 0 0
x8 x1 x0 y8 y7 y0
c8
0 s8 s0
x8 x7 x0 y8 y7 y0
c8
s8 s0 P = 3 A : P9 P8 P0
P = 3 A : P9 P8 P0
Magnitude
MSB
bn – 1 bn – 2 b1 b0
Magnitude
Sign
0 denotes +
1 denotes – MSB
•1’s complement
•2’s complement
1’s complement
K = (2n – 1) – P
K = 2n – P
Deriving 2’s complement
K1 = (2n – 1) – P
K2 = 2n – P
ignore ignore
ignore
ignore
( + 5) 0101 0101
– (–2) – 1110 + 0010
( + 7) 0111
Add ⁄ Sub
control
xn – 1 x1 x0
cn n-bit adder c0
sn – 1 s1 s0
g1 p1 g0 p0
c1
c2 c0
Stage 1 Stage 0
s1 s0
x0 y0
g1 p1 g0 p0
c0
c2
c1
s1 s0
c8
c32 Block c24 c16 Block Block c0
3 1 0
s31 – 24 s15 – 8 s7 – 0
G3 P3 G1 P1 G0 P0
s31 – 24 s15 – 8 s7 – 0
c32 c16 c8
Second-level lookahead
endmodule
Figure 3.18. Verilog code for the full-adder using gate level primitives.
module fulladd (Cin, x, y, s, Cout);
input Cin, x, y;
output s, Cout;
endmodule
assign s = x ^ y ^ Cin;
assign Cout = (x & y) | (x & Cin) | (y & Cin);
endmodule
Figure 3.20. Verilog code for the full-adder using continuous assignment.
module fulladd (Cin, x, y, s, Cout);
input Cin, x, y;
output s, Cout;
assign s = x ^ y ^ Cin,
Cout = (x & y) | (x & Cin) | (y & Cin);
endmodule
endmodule
assign s = x ^ y ^ Cin,
assign Cout = (x & y) | (x & Cin) | (y & Cin);
endmodule
endmodule
endmodule
endmodule
endmodule
endmodule
Figure 3.28. An alternative specification of an n-bit adder with carry-out and overflow signals.
module addern (carryin, X, Y, S, carryout, overflow);
parameter n = 32;
input carryin;
input [n-1:0] X, Y;
output reg [n-1:0] S;
output reg carryout, overflow;
endmodule
endmodule
endmodule
endmodule
Figure 3.32. Using the Verilog # operator to set the values of parameters.
Figure 3.33. A ripple-carry adder specified
by using the generate statement.
Figure 3.34. Multiplication of unsigned numbers.
Figure 3.35. A 4x4 multiplier circuit.
Multiplicand M (+14) 01110
Multiplier Q (+11) x 01011
Partial product 0 0 00 1 1 1 0
+ 001110
Partial product 1 0010101
+ 000000
Partial product 2 00 0 1 0 1 0
+ 00 1 1 1 0
Partial product 3 00 1 0 0 1 1
+ 000000
Product P (+154) 0010011010
S E M
Sign
0 denotes+ 8-bit 23 bits of mantissa
1 denotes– excess-127
exponent
64 bits
S E M
Sign
11-bit excess-1023 52 bits of mantissa
exponent
endmodule
Result is (3A6F)16
endmodule
always @(X, Y)
begin
C[0] = 1’b1;
for (k = 0; k < n; k = k + 1)
begin
S[k] = X[k] ^ ~Y[k] ^ C[k];
C[k+1] = (X[k] & ~Y[k]) | (X[k] & C[k]) | (~Y[k] & C[k]);
end
V = C[n] ^ C[n-1];
N = S[n-1];
Z = !S;
end
endmodule
xi pi si
V DD
yi gi
ci + 1
always @(IN)
if (IN == 4'b0101) OUT = 4'b0001;
else if (IN == 4'b0110) OUT = 4'b0010;
else if (IN == 4'b0111) OUT = 4'b0011;
else if (IN == 4'b1001) OUT = 4'b0010;
else if (IN == 4'b1010) OUT = 4'b0100;
else if (IN == 4'b1011) OUT = 4'b0110;
else if (IN == 4'b1101) OUT = 4'b0011;
else if (IN == 4'b1110) OUT = 4'b0110;
else if (IN == 4'b1111) OUT = 4'b1001;
else OUT = 4'b0000;
endmodule