A subtractor is a simple circuit that finds the difference between two numbers. It helps computers and devices do math by taking one number away from another.
Subtractor design in Verilog
module subtractor(
input wire [3:0] a, // First 4-bit number
input wire [3:0] b, // Second 4-bit number
output wire [3:0] diff, // Difference output
output wire borrow // Borrow output
);
assign {borrow, diff} = {1'b0, a} - {1'b0, b};
endmoduleThe module keyword defines the subtractor block.
Inputs and outputs are declared with their bit widths.
module subtractor(
input wire [7:0] a,
input wire [7:0] b,
output wire [7:0] diff,
output wire borrow
);
assign {borrow, diff} = {1'b0, a} - {1'b0, b};
endmodulemodule subtractor(
input wire a,
input wire b,
output wire diff,
output wire borrow
);
assign {borrow, diff} = {1'b0, a} - {1'b0, b};
endmoduleThis test program shows how the subtractor works with two examples. It prints the inputs, the difference, and the borrow bit.
module test_subtractor;
reg [3:0] a, b;
wire [3:0] diff;
wire borrow;
subtractor uut (
.a(a),
.b(b),
.diff(diff),
.borrow(borrow)
);
initial begin
a = 4'b1010; // 10 in decimal
b = 4'b0011; // 3 in decimal
#10;
$display("a = %b, b = %b", a, b);
$display("Difference = %b", diff);
$display("Borrow = %b", borrow);
a = 4'b0100; // 4 in decimal
b = 4'b1001; // 9 in decimal
#10;
$display("a = %b, b = %b", a, b);
$display("Difference = %b", diff);
$display("Borrow = %b", borrow);
$finish;
end
endmodule
module subtractor(
input wire [3:0] a,
input wire [3:0] b,
output wire [3:0] diff,
output wire borrow
);
assign {borrow, diff} = {1'b0, a} - {1'b0, b};
endmoduleThe borrow output is 1 when the second number is bigger than the first.
Subtractor works like normal subtraction but in binary form.
Make sure input sizes match to avoid errors.
A subtractor finds the difference between two binary numbers.
It outputs the difference and a borrow bit if needed.
Useful in many digital circuits and computing tasks.
Practice
What is the main function of a subtractor in digital circuits?
Solution
Step 1: Understand the subtractor's purpose
A subtractor is designed to calculate the difference between two binary inputs.Step 2: Compare with other operations
Adders add, multipliers multiply, and storage units store data, so these are not subtractors.Final Answer:
To find the difference between two binary numbers -> Option CQuick Check:
Subtractor = Difference finder [OK]
- Confusing subtractor with adder
- Thinking subtractor stores data
- Mixing subtractor with multiplier
Which of the following is the correct Verilog module header for a 1-bit subtractor?
module subtractor(Solution
Step 1: Check correct input/output declaration
Inputs must be declared with 'input' keyword and outputs with 'output'.Step 2: Verify syntax correctness
module subtractor(input a, input b, output diff, output borrow); correctly declares inputs and outputs with proper keywords and commas.Final Answer:
module subtractor(input a, input b, output diff, output borrow); -> Option AQuick Check:
Correct input/output syntax = module subtractor(input a, input b, output diff, output borrow); [OK]
- Omitting input/output keywords
- Listing inputs and outputs without keywords
- Declaring outputs as inputs
What is the output difference and borrow when inputs a=1 and b=0 are applied to this 1-bit subtractor module?
assign diff = a ^ b ^ borrow_in;
assign borrow_out = (~a & b) | (borrow_in & (~a ^ b));
Assume borrow_in = 0.
Solution
Step 1: Calculate diff with given inputs
diff = a ^ b ^ borrow_in = 1 ^ 0 ^ 0 = 1Step 2: Calculate borrow_out with given inputs
borrow_out = (~a & b) | (borrow_in & (~a ^ b)) = (0 & 0) | (0 & (0 ^ 0)) = 0Final Answer:
diff = 1, borrow_out = 0 -> Option DQuick Check:
1 - 0 = 1, no borrow needed [OK]
- Forgetting borrow_in in diff calculation
- Misapplying borrow logic
- Mixing up a and b in borrow expression
Identify the error in this Verilog code for a 1-bit subtractor:
module subtractor(input a, input b, output diff, output borrow);
assign diff = a - b;
assign borrow = (b > a);
endmodule
Solution
Step 1: Check arithmetic operator usage
Verilog '-' operator performs arithmetic subtraction but does not produce borrow output directly.Step 2: Understand borrow signal generation
Borrow must be calculated using logic expressions, not by comparing b and a with '>' operator.Final Answer:
Using '-' operator for bitwise subtraction is incorrect in Verilog -> Option AQuick Check:
Subtractor needs logic for borrow, not '-' operator [OK]
- Using arithmetic '-' expecting borrow output
- Ignoring borrow logic
- Assuming '>' operator works for borrow
Design a 2-bit subtractor using two 1-bit subtractor modules connected in series. Which of the following correctly describes the borrow connection?
Solution
Step 1: Understand borrow chaining in multi-bit subtractors
Borrow from the lower bit subtractor must feed into the higher bit subtractor's borrow input.Step 2: Verify connection direction
The least significant bit's borrow output connects to the most significant bit's borrow input to propagate borrow correctly.Final Answer:
The borrow output of the least significant bit subtractor connects to the borrow input of the most significant bit subtractor -> Option BQuick Check:
Borrow chains from LSB to MSB [OK]
- Connecting borrow inputs to zero for all bits
- Connecting borrow outputs together incorrectly
- Reversing borrow input/output connections
Ready to prove it?
Pass the test and this pattern moves unknown
