Blocking Assignment
Part of the Fundamentals section of Coddy's Verilog journey — lesson 49 of 90.
Verilog has two types of procedural assignments: blocking (=) and non-blocking (<=). In this lesson, we focus on blocking assignment.
Blocking assignment uses the = operator. It is called "blocking" because it blocks the execution of the next statement until the current assignment is complete. The code runs step by step, in order.
Syntax:
variable = expression;When to Use Blocking Assignment
Blocking assignment (=) is used for combinational logic — circuits where outputs change immediately when inputs change, with no clock and no memory.
Examples of combinational logic:
- AND / OR / XOR gates
- Adders and subtractors
- Multiplexers
- Decoders
Verilog example:
always @(*) begin
sum = a + b; // Blocking assignment
carry = a & b; // Blocking assignment
endBlocking in Always Blocks (Combinational Logic)
always @(*) begin
temp = a & b; // Step 1
out = temp | c; // Step 2 (uses temp from step 1)
endThe order matters. This is fine for combinational logic.
Blocking vs Non-Blocking
Blocking (=) | Non-blocking (<=) | |
|---|---|---|
| Execution | One after another | All at once |
| Next line waits? | Yes | No |
| Uses | Combinational logic | Sequential logic (flip-flops) |
Important: Do Not Use Blocking for Flip-Flops
Challenge
Add the missing blocking assignments to swap the values of x and y using a temporary variable.
What to do:
- Assign the value of
xtotemp(save x in temp) - Assign the value of
ytox(move y into x) - Assign the value of
temptoy(move saved x into y)
Cheat sheet
Blocking assignment (=) executes sequentially — each statement completes before the next begins. Used for combinational logic inside always @(*) blocks.
always @(*) begin
temp = a & b; // Step 1
out = temp | c; // Step 2 (uses updated temp)
endBlocking (=) |
Non-blocking (<=) |
|
|---|---|---|
| Execution | One after another | All at once |
| Use for | Combinational logic | Sequential logic (flip-flops) |
Try it yourself
module swap;
reg x, y;
reg temp;
initial begin
x = 1;
y = 0;
$display("Before swap: x=%d, y=%d", x, y);
// TODO: Step 1 - Assign x to temp
// TODO: Step 2 - Assign y to x
// TODO: Step 3 - Assign temp to y
$display("After swap: x=%d, y=%d", x, y);
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic