Codiode

Two's Complement Overflow in Subtraction

MediumVerilog / SystemVerilogBuild

Subtraction overflows under different conditions than addition. Rewriting a - b as a + (-b) and reusing the addition rule breaks when b is the most 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 the problem's testbench. You get the waveform back, not a stored answer.

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

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.

Part of Datapath Design

Problem 14 of 27 in the Datapath Design track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.