HALICERY

free-time coding, hardware dev, articles

Top
Home 8042 Blogs About
Home IntelEssential 16/32-bit Instructions CWDE

Last modified: Mon Oct 13 08:07:49 UTC+0200 2025 © A. Tarpai


Sign-extension instructions in 32-bit mode: CWDE

These instructions were originally made on the 8086 to sign-extend the dividend before DIV/IDIV. See also divmul.

But there is an unusual 32-bit promotion of a W=0 instruction: CWDE.

 W=1                                         W=1
 16-bit                                      32-bit

 CWD 99                                      CWD 99 = CDQ
                           32-bit
                           normal
+---------+---------+      promotion        +-------------------+-------------------+
|ssssss<--|    AX   |        ------->       |ssssssssssssssss<--|        EAX        |
+---------+---------+                       +-------------------+-------------------+
     DX        AX                                    EDX                 EAX



           W=0                               W=0
           16-bit                            32-bit

           CBW 98          32-bit            CBW 98 = CWDE
                           abnormal
          +----+----+      promotion        +---------+---------+
          |ss<-| AL |        ------->       |ssssss<--|    AX   |
          +----+----+                       +---------+---------+
              AX                                     EAX

CWDE is a bonus instruction and not to be used with DIV/IDIV (there is no integer divide EAX instruction).

But not to worry, all 4 has different mnemonics and the assembler will insert the correct opcode with optional operand-size prefix 66h based on the current bits setting.

Assembler encoding:

     operation         [BITS 32]               [BITS 16]

     AX <-- AL         66 98   CBW                98   CBW
  DX:AX <-- AX         66 99   CWD                99   CWD
EDX:EAX <-- EAX           99   CDQ             66 99   CDQ
    EAX <-- AX*           98   CWDE            66 98   CWDE

* CWDE: there is honored operand-size for a W=0 instruction

Again, with 32-bit operand-size for CDQ: a 1-byte op can be used to zero out EDX (when EAX stores a smaller value, msb not set).