CodiodeCodiode
Home
Problem Solving
Skill Tracks
My Assignments
Contests
Leaderboard
Community
Settings
Codiode/Problems/Arithmetic

Twos Complement Overflow in Subtraction

MediumVerilog / SystemVerilogBuild

Arithmetic Logic Units (ALUs) must accurately flag boundary violations when mathematical operations exceed the representable range of the data path. In two's complement arithmetic, subtraction requires distinct overflow detection logic compared to addition. Relying on addition-based overflow logic after converting subtraction to a + (-b) introduces edge-case failures, particularly when the subtrahend is the maximum negative number.

The module computes the difference between an 8-bit signed minuend and an 8-bit signed subtrahend. It evaluates the sign bits of the operands and the result to determine if an overflow occurred. An overflow happens when the subtraction of two numbers with opposite signs produces a result that has the opposite sign of the minuend.

This is a purely combinational circuit. There is no clock or reset.

| Signal | Direction | Width | Description | |---|---|---|---| | a | input | 8 | Minuend in two's complement format | | b | input | 8 | Subtrahend in two's complement format | | diff | output | 8 | Difference a - b in two's complement format | | overflow | output | 1 | Asserted to 1 if the subtraction overflows, 0 otherwise |

Constraints

  • The design must be purely combinational with no clock or reset dependencies.
  • Operands and outputs are 8-bit two's complement signed integers.
  • The valid range for operands and results without overflow is -128 to 127.
  • You must derive the overflow flag using the sign bits of the operands and the result directly, rather than converting the operation to an addition.

Topics

combinationalalumath

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against a real testbench — you get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

Sign in to solveSee what Pro unlocks

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Related problems

  • Binary to Gray Code DatapathEasy
  • Fixed Point Multiplication with SaturationHard
  • Absolute Value with Bit GrowthMedium
  • 8 bit Basic ALU with FlagsEasy
  • 8-bit Combinational PopcountMedium
  • 8-bit Logical Barrel ShifterMedium
  • 16-bit Arithmetic Barrel ShifterHard
  • Parameterized ALU with Safe DefaultsMedium

Browse all problems · Learning tracks