Menu
Coddy logo textTech

Arithmetic Operators

Part of the Fundamentals section of Coddy's Verilog journey — lesson 19 of 90.

Verilog provides basic arithmetic operators: + (addition), - (subtraction), * (multiplication) and / (division).

Code Example:

module arithmetic_demo;
  reg [3:0] a, b;
  reg [7:0] add, sub, mul, div;
  
  initial begin
    a = 4'd9;
    b = 4'd4;
    
    add = a + b;    // 9 + 4 = 13
    sub = a - b;    // 9 - 4 = 5
    mul = a * b;    // 9 * 4 = 36
    div = a / b;    // 9 / 4 = 2
    
    $display("9 + 4 = %d", add);
    $display("9 - 4 = %d", sub);
    $display("9 * 4 = %d", mul);
    $display("9 / 4 = %d", div);
    $finish;
  end
endmodule

Output:

9 + 4 = 13 9 - 4 = 5 9 * 4 = 36 9 / 4 = 2 

Important Notes

  • Division performs integer division (no fractions)
  • Ensure the result register is wide enough for multiplication results
challenge icon

Challenge

In this challenge, you need to write the correct arithmetic expressions using the operators +, -, * and /.

What to do:

  1. Calculate a + b and store in add
  2. Calculate a - b and store in sub
  3. Calculate a * b and store in mul
  4. Calculate a / b and store in div

Cheat sheet

Verilog arithmetic operators: +, -, *, /

reg [3:0] a, b;
reg [7:0] add, sub, mul, div;

add = a + b;
sub = a - b;
mul = a * b;
div = a / b; // integer division (no fractions)

Note: Ensure the result register is wide enough, especially for multiplication.

Try it yourself

module arithmetic_challenge;
  reg [3:0] a, b;
  reg [7:0] add, sub, mul, div;
  
  initial begin
    a = 4'd12;
    b = 4'd5;
    
    add = ______;   // a + b
    sub = ______;   // a - b
    mul = ______;   // a * b
    div = ______;   // a / b
    
    $display("12 + 5 = %d", add);
    $display("12 - 5 = %d", sub);
    $display("12 * 5 = %d", mul);
    $display("12 / 5 = %d", div);
    $finish;
  end
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals