ASSEMBLY / CHAPTER 11

Division and implicit register pairs

Master

Prepare the full dividend rather than only its low half.

By the end of this chapter

  • Identify DIV and IDIV operands
  • Initialize the high dividend correctly
  • Recognize divide-error conditions

The idea

For a 32-bit DIV operand, unsigned division uses the 64-bit dividend EDX:EAX and returns quotient in EAX and remainder in EDX. Setting only EAX is insufficient if EDX contains unrelated bits. To divide an ordinary unsigned 32-bit value, clear EDX first. The divisor must be nonzero, and the quotient must fit the destination width.

IDIV performs signed division and truncates the quotient toward zero. For a signed 32-bit dividend in EAX, CDQ sign-extends into EDX:EAX before IDIV. The remainder has the dividend’s sign or is zero. Dividing by zero or producing an unrepresentable quotient causes a divide exception; arithmetic flags are not the mechanism for checking those outcomes afterward. These are conceptual traces, not standalone executable browser programs.

Assembly example
; Signed 32-bit division: -17 / 5
mov eax, -17
cdq
mov ecx, 5
idiv ecx
Result
EAX = -3; EDX = -2 as signed 32-bit values.

The quotient truncates toward zero, and -17 = (-3)*5 + (-2).

Try it yourself

Trace unsigned 29/6 and signed -29/6, including the full dividend preparation and both outputs.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Before div ecx divides an unsigned 32-bit value held in EAX, which preparation sets the high half to zero?