Arithmetic Operators
Teil des Abschnitts Grundlagen der Verilog-Journey von Coddy. Lektion 19 von 90.
Verilog stellt grundlegende arithmetische Operatoren bereit: + (Addition), - (Subtraktion), * (Multiplikation) und / (Division).
Codebeispiel:
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
endmoduleAusgabe:
9 + 4 = 13 9 - 4 = 5 9 * 4 = 36 9 / 4 = 2
Wichtige Hinweise
- Division führt eine Ganzzahldivision durch (keine Brüche)
- Stelle sicher, dass das Register result breit genug für Multiplikationsergebnisse ist
Aufgabe
In dieser Herausforderung musst du die korrekten arithmetischen Ausdrücke mit den Operatoren +, -, * und / schreiben.
Was zu tun ist:
- Berechne
a + bund speichere das Ergebnis inadd - Berechne
a - bund speichere das Ergebnis insub - Berechne
a * bund speichere das Ergebnis inmul - Berechne
a / bund speichere das Ergebnis indiv
Probier es selbst
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
endmoduleDiese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.
Alle Lektionen in Grundlagen
1Einführung
Was ist VerilogHardware vs. SoftwareIhr erstes ModulKommentareAbstraktionsebenen des Designs4Operators 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Übe selbstständig: Online-Verilog-Compiler