CWD (convert word to doubleword) has no operands. It fills every bit of DX with the sign bit of AX: DX becomes 0000h when AX is positive or zero, and FFFFh when AX is negative. AX itself is unchanged, and DX:AX now holds the same signed value as a 32-bit number. No flags change.
Before IDIV
A word IDIV divides the 32-bit DX:AX. Load the dividend into AX, then CWD, then divide. Clearing DX with XOR DX, DX would be right only for non-negative dividends; for a negative one it makes a large positive number and the result is wrong.
MOV AX, -20CWD ; DX:AX = -20MOV BX, 4IDIV BX ; AX = -5, DX = 0
Absolute value without a jump
Because DX ends up as either all zeros or all ones, it can be used as a mask. XOR with all ones inverts AX, and subtracting −1 adds one, which together is a negation; XOR and subtraction with zero change nothing. So these three lines replace AX with its absolute value:
MOV AX, -7CWD ; DX = FFFFh because AX is negativeXOR AX, DX ; FFF9h -> 0006hSUB AX, DX ; 0006h - (-1) = 0007hADD AL, '0' ; digit -> characterMOV DL, ALMOV AH, 02h ; DOS: print DLINT 21h
Pitfalls
CWD always overwrites DX, so save anything kept there. It only sign-extends AX; to widen a value in another register, move it to AX first. A byte has to be widened to a word with CBW before CWD can take it further.
Later CPUs
The 386 adds CDQ, which sign-extends EAX into EDX:EAX for 32-bit IDIV.