Search

A technology, a device or an article

IDIV: why the CPU reports a division error when the divisor is equal to one

A nonzero divisor does not rule out #DE. The full RDX:RAX dividend and the quotient range explain two different causes of an IDIV fault.

Paper figure holds a red square beside a small opening between dark blocks
ITNWS AI-generated conceptual illustration. The red square evokes a result that does not fit; this is a range metaphor, not CPU construction.ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

The divisor is equal to one. However, execution stopped at IDIV with exception #DE. If you start the investigation with the belief that such an error occurs only when the divisor is zero, you are left to suspect a damaged register, an incorrect debugger or processor. But x86 integer division has another reason for failure: the quotient does not fit into the allocated register. And the dividend may turn out to be wider than the value that the programmer intended to pass to the instruction. It is this combination that allows you to get an error even with a perfectly good CPU and a non-zero divisor.

Let’s analyze a hypothetical episode without passing it off as a test performed by the editorial team. Let the RAX register immediately before the 64-bit IDIV contain 10, RDX contain 1, and the explicitly specified divisor register contain 1. A person sees the familiar ten and one next to each other and expects ten. The instruction receives another dividend: the signed 128-bit value RDX:RAX. In the chosen example, it is equal to 2^64 + 10. After dividing by one, the result remains the same, and it does not fit into the signed 64-bit RAX.

These semantics are described in the AMD64 manual, volume 3. We use the historical February 2005 revision to explain the long-standing instruction; this is not news about the modern processor. Sources re-checked October 30, 2026. It is important to read not only the name of the operation, but also the table of its implicit operands. The assembly instruction names the divisor, while the dividend and places for the results are determined by the form of the instruction itself.

The 32-bit form uses EDX:EAX, the quotient goes into EAX, the remainder goes into EDX. In 64-bit form, the corresponding registers become RDX:RAX, RAX, and RDX. The byte form uses AX as its dividend and distributes the results between AL and AH. The bit width of the divisor is therefore not equal to the bit width of the full dividend. When a person opens one low-order register and thinks he has seen all the input data, debugging begins with an incomplete picture.

The register left out of the investigation

Let’s get back to the RDX. It is not an incidental implementation detail. Its bits enter into the dividend just as directly as the bits of RAX. A previous calculation might have left the old result there; a called function might have modified the register as part of the calling convention. No matter how plausible the value of RAX may be, the instruction cannot infer that the upper half was intended to be considered zero or a continuation of the sign. In machine code, you need to generate exactly the complete number that needs to be divided.

For a signed 64-bit value already in RAX, CQO creates its sign extension in RDX:RAX. If the initial value is non-negative, the top half becomes zero; if negative, it is filled with one bits. This is not an operation on a divisor, nor is it a general command to “fix the division.” CQO prepares an expanded representation of a particular signed dividend. If a program intentionally divides a genuine 128-bit dividend, automatically replacing its top half would destroy part of the input value.

The distinction is particularly clear for a negative number. Let the representation −10 be written to RAX, and RDX is mistakenly reset to zero. The resulting 128-bit pair describes a large positive value rather than −10. With proper sign extension, division by 3 gives the quotient −3 and the remainder −1. The instruction discards the fractional part of the quotient in the direction of zero; a non-zero remainder has the sign of the dividend. These are IDIV arithmetic rules that register preparation must comply with, not a convenient interpretation of the result after execution.

An unsigned DIV has a different contract. If the original unsigned number occupies only the lower half, the upper half is usually prepared with zero. But the habit of zeroing EDX before DIV cannot be thoughtlessly transferred to the negative signed dividend used by IDIV. The differences between the commands are in the meaning of the bits, the range of the quotient, and the rules of the result. The binary string itself does not carry the tag “I am negative” attached; the desired interpretation is determined by the instructions and the prepared representation.

Red bar extends beyond an upper tray above an empty lower tray and brass ruler
ITNWS AI-generated conceptual illustration. The trays evoke different dividend and result widths; their dimensions do not specify register sizes. — ITNWS · image_gen · Original ITNWS AI-generated conceptual editorial illustration

Now let’s remove the error of the upper half. Assume the sign extension is correct and the original number be the smallest representable 64-bit integer, −2^63. The divisor is −1. The mathematical answer is +2^63. The maximum representable positive value of a signed 64-bit register is 2^63 − 1. Exactly one value is missing, and IDIV returns #DE. No proper sign extension will create additional positive representation within the same 64-bit result.

This edge case is different from the hypothetical opening of the article. There, the program accidentally transmitted a much wider dividend than it intended. Here it supplied exactly the intended number, but the result was out of range. The shared exception indication does not make their causes identical. Correcting operand preparation addresses the first case. In the second, the algorithm must determine an acceptable way to handle the unrepresentable result: a broader representation, an explicit error, or some other pre-selected semantics. Arbitrarily changing the input number for the sake of not throwing an exception changes the problem.

The remainder does not bypass this boundary. The processor instruction computes both results jointly, so the situation with an unrepresentable quotient does not become acceptable just because the program only needs the remainder. The minimum signed value has a mathematical remainder of zero when divided by −1, but the corresponding IDIV still has an unrepresentable quotient. To understand the actual behavior of the code, you need to establish the compiler’s chosen sequence, rather than assigning a the percent operator its own independent hardware operation.

Between expression and exception

The source language has its own rules. In the historical C Standard Draft N1570, section 6.5.5, division by zero and the unrepresentable quotient refer to the undefined behavior of the corresponding integer expressions. This is not a promise that any build will necessarily stop at IDIV. First there is a contract of language; the optimizer then selects a machine implementation for computations whose behavior the language defines. Jumping straight from a single line of C to a promised exception misses this essential level.

Dividing by a constant often does not require a hardware division instruction at all. The compiler can replace this with multiplications, shifts, and adjustments, preserving the language’s intended output for valid inputs. GCC Optimization Options Documentation describes the transformations and modes of operation of the optimizer, but the set of machine instructions chosen is determined by a particular build. A change in behavior when adding diagnostic output does not prove an accidental defect in the CPU: a debug edit could have changed the placement, registers, and the entire resulting sequence.

That is why checking the divisor for zero does not exhaust the verification of signed division. If the program must process the entire range of input values without undefined behavior, it needs to determine in advance the case of a minimum value with a divisor of −1. With a handwritten assembly, the obligation to correctly form the complete dividend is added. These are different levels of verification: the meaning of the expression in the language and the specific bits of the implicit machine operands. One successful run does not replace proof of their consistency for all promised inputs.

The name of the operating system signal can also be confusing. SIGFPE has historically been associated with arithmetic errors and not exclusively with floating point calculations. Linux sigaction manual lists arithmetic reason codes. But the name of the shell message does not automatically determine which condition IDIV encountered. For diagnosis, you need the address of the instruction, its operand size, a divisor and the complete register pair from the relevant saved state. One line “Floating point exception” is not enough to reach that conclusion.

The architectural divide error is a fault. In Intel SDM system volume, December 2024 revision, this is important for associating the stored instruction pointer with the instruction that caused the event. Analysis of the fault does not allow us to assume that the program has already obtained a suitable quotient and remainder. If the handler simply returns control to the same command with the same inappropriate inputs, the reason will not go away. Continuing execution requires the intended processing strategy rather than relying on another response from a correctly functioning divider.

In this case, an error in reading a divisor from memory is an independent possibility. Before arithmetic, an operand must be obtained; an unreachable address can cause a page fault. Not every stop at a similar assembly line is #DE. Stored system information helps distinguish between a memory-access error and an arithmetic error. It is especially important here not to replace the machine command with a paraphrase of the expression: the address, access width and execution mode belong to the actual event, even if the author’s comment is only about numbers.

Handwritten inline assembly has one more place for divergence. The compiler must be aware of inputs, outputs, and registers it modifies. If the constraint description hides the RDX change or binds the operands incorrectly, the surrounding code may make incorrect assumptions. Having a valid mnemonic name inside a string does not make the entire fragment valid. Responsibilities are divided between the compiler contract and the CPU instruction; the hardware implementation does not correct the inconsistency of the first agreement. This explains why machine operands are checked in the resulting code, and not just in the assembly source.

For the unsigned case, the bound can be checked separately mathematically. If the n-bit divisor d is positive, and the dividend is written as H × 2^n + L, then for H ≥ d the quotient is no less than 2^n. It does not fit into n bits, regardless of the low-order part of L. When H < d and the low-order part is valid, the dividend is less than d × 2^n, so the quotient fits. This is an analytic property of an unsigned representation, not a ready-made check of a signed IDIV. To transfer the inequality without taking into account the sign would mean changing the arithmetic in question again.

With IDIV, overflow should not be sought after execution through OF, as with some other integer operations. A quotient outside the result range causes an exception; arithmetic flags after a successful division do not provide a defined set of arithmetic results for such a subsequent test. The habit of placing a conditional branch “if overflow” after IDIV does not turn failure into a normal control branch. For the promised processing, you need to consider the conditions before the instruction or use the exception handling mechanism defined by the environment. The choice depends on the program interface, but the original architectural boundary remains common.

The rounding difference can be tested without equipment. When −10 is divided by 3, −10 = (−3) × 3 + (−1) holds with a remainder of the smaller modulus. If we take the quotient −4 instead, we get another remainder, +2, corresponding to rounding down. This is a different convention found in mathematical libraries and languages. A match on positive inputs does not prove the algorithms are equivalent. When porting numerical code, negative values are checked precisely because both rule systems can be internally consistent and still return different pairs of results.

Finally, the absence of an exception says nothing about division performance. Latency, throughput and operand dependence concern the implementation and the workload being measured. A detected #DE does not identify a “slow divider”, and eliminating a range error does not measure an application speedup. Investigating performance would require a different experiment. This investigation establishes a narrower result: the mathematical quotient must fit the specified result format, and the full dividend must represent the number the program actually intended to supply.

In the opening episode, the missing information was in the RDX. One was indeed the divisor; ten was only the lower half of a much larger number. In the second episode, all inputs were prepared correctly, but the positive output did not have a 64-bit signed representation. Both stops are explained by the documented contract CPU. Having seen a non-zero divisor, the investigation therefore continues with the question of the full dividend and the range of the quotient. A correctly functioning processor has the right to refuse precisely because it calculated the boundary correctly.

Sources & documentation

  1. AMD64 Architecture Programmer’s Manual, Volume3, historical February2005 ↗
  2. Intel SDM Volume3, historical December2024 ↗
  3. ISO C committee draft N1570, historical April2011 ↗
  4. Linux sigaction manual ↗
  5. GCC optimization options ↗