IMUL

Signed Multiply

stableVMJITAOTinstruction

Encodings

OpcodeInstructionOp/En64-bitCompat/LegacyDescription
F6 /5IMUL r/m8MValidValidAX:= AL r/m byte.
F7 /5IMUL r/m16MValidValidDX:AX := AX r/m word.
F7 /5IMUL r/m32MValidValidEDX:EAX := EAX r/m32.
REX.W + F7 /5IMUL r/m64MValidNot encodableRDX:RAX := RAX r/m64.
0F AF /rIMUL r16, r/m16RMValidValidWord register := word register r/m16.
0F AF /rIMUL r32, r/m32RMValidValidDoubleword register := doubleword register r/m32.
REX.W + 0F AF /rIMUL r64, r/m64RMValidNot encodableQuadword register := Quadword register r/m64.
6B /r ibIMUL r16, r/m16, imm8RMIValidValidWord register := r/m16 sign-extended immediate byte.
6B /r ibIMUL r32, r/m32, imm8RMIValidValidDoubleword register := r/m32 signextended immediate byte.
REX.W + 6B /r ibIMUL r64, r/m64, imm8RMIValidNot encodableQuadword register := r/m64 sign-extended immediate byte.
69 /r iwIMUL r16, r/m16, imm16RMIValidValidWord register := r/m16 immediate word.
69 /r idIMUL r32, r/m32, imm32RMIValidValidDoubleword register := r/m32 immediate doubleword.
REX.W + 69 /r idIMUL r64, r/m64, imm32RMIValidNot encodableQuadword register := r/m64 immediate doubleword.

Operand encoding

Each mode is a value of the Op/En column above. It says which field of the encoded instruction carries each operand, in the order they are written, and whether the instruction reads it, writes it or both.

M

  1. modrm.rm lectura y escrituraModRM byte, r/m field (bits 2-0); with the SIB byte and the displacement when the mod field asks for them

RM

  1. modrm.reg lectura y escrituraModRM byte, reg field (bits 5-3)
  2. modrm.rm lecturaModRM byte, r/m field (bits 2-0); with the SIB byte and the displacement when the mod field asks for them

RMI

  1. modrm.reg lectura y escrituraModRM byte, reg field (bits 5-3)
  2. modrm.rm lecturaModRM byte, r/m field (bits 2-0); with the SIB byte and the displacement when the mod field asks for them
  3. imm8/16/32immediate after the instruction, as wide as the operand

Measured cost

Loading measurements from arch-data...

Flags named

Description

Performs a signed multiplication of two operands. This instruction has three forms, depending on the number of operands.

a general-purpose register or memory location) is multiplied by the value in the AL, AX, EAX, or RAX register (depending on the operand size) and the product (twice the size of the input operand) is stored in the AX, DX:AX, EDX:EAX, or RDX:RAX registers, respectively.

operand (second operand). The destination operand is a general-purpose register and the source operand is an immediate value, a general-purpose register, or a memory location. The intermediate product (twice the size of the input operand) is truncated and stored in the destination operand location.

(the second and the third operands). Here, the first source operand (which can be a general-purpose register or a memory location) is multiplied by the second source operand (an immediate value). The intermediate product (twice the size of the first source operand) is truncated and stored in the destination operand (a general-purpose register).

When an immediate value is used as an operand, it is sign-extended to the length of the destination operand format.

The CF and OF flags are set when the signed integer value of the intermediate product differs from the sign extended operand-size-truncated product, otherwise the CF and OF flags are cleared.

The three forms of the IMUL instruction are similar in that the length of the product is calculated to twice the length of the operands. With the one-operand form, the product is stored exactly in the destination. With the twoand threeoperand forms, however, the result is truncated to the length of the destination before it is stored in the

destination register. Because of this truncation, the CF or OF flag should be tested to ensure that no significant bits are lost.

The twoand three-operand forms may also be used with unsigned operands because the lower half of the product is the same regardless if the operands are signed or unsigned. The CF and OF flags, however, cannot be used to determine if the upper half of the result is non-zero.

In 64-bit mode, the instruction's default operation size is 32 bits. Use of the REX.R prefix permits access to additional registers (R8-R15). Use of the REX.W prefix promotes operation to 64 bits. Use of REX.W modifies the three

forms of the instruction as follows.

multiplied by the value in the RAX register and the product is stored in the RDX:RAX registers.

destination operand is promoted to 64 bits.

operand are promoted to 64 bits. If the source operand is an immediate, it is sign extended to 64 bits.

Operation

IF (NumberOfOperands = 1)
   THEN IF (OperandSize = 8)

          THEN
                TMP_XP := AL  SRC (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC *);
                AX := TMP_XP[15:0];

             IF SignExtend(TMP_XP[7:0]) = TMP_XP

                      THEN CF := 0; OF := 0;
                      ELSE CF := 1; OF := 1; FI;

        ELSE IF OperandSize = 16

                THEN
                      TMP_XP := AX  SRC (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC *)
                      DX:AX := TMP_XP[31:0];

                  IF SignExtend(TMP_XP[15:0]) = TMP_XP

                            THEN CF := 0; OF := 0;
                            ELSE CF := 1; OF := 1; FI;

             ELSE IF OperandSize = 32

                      THEN
                            TMP_XP := EAX  SRC (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC*)
                            EDX:EAX := TMP_XP[63:0];

                       IF SignExtend(TMP_XP[31:0]) = TMP_XP

                                  THEN CF := 0; OF := 0;
                                  ELSE CF := 1; OF := 1; FI;
                      ELSE (* OperandSize = 64 *)
                            TMP_XP := RAX  SRC (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC *)
                            EDX:EAX := TMP_XP[127:0];

                       IF SignExtend(TMP_XP[63:0]) = TMP_XP

                                  THEN CF := 0; OF := 0;
                                  ELSE CF := 1; OF := 1; FI;
                      FI;
          FI;


   ELSE IF (NumberOfOperands = 2)

          THEN

                TMP_XP := DEST  SRC (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC *)
                DEST := TruncateToOperandSize(TMP_XP);

             IF SignExtend(DEST)  TMP_XP

                      THEN CF := 1; OF := 1;

                      ELSE CF := 0; OF := 0; FI;

        ELSE (* NumberOfOperands = 3 *)

                TMP_XP := SRC1  SRC2 (* Signed multiplication; TMP_XP is a signed integer at twice the width of the SRC1 *)
                DEST := TruncateToOperandSize(TMP_XP);

             IF SignExtend(DEST)  TMP_XP

                      THEN CF := 1; OF := 1;

                      ELSE CF := 0; OF := 0; FI;

    FI;

FI;

Flags affected

For the one operand form of the instruction, the CF and OF flags are set when significant bits are carried into the upper half of the result and cleared when the result fits exactly in the lower half of the result. For the twoand three-operand forms of the instruction, the CF and OF flags are set when the result must be truncated to fit in the destination operand size and cleared when the result fits exactly in the destination operand size. The SF, ZF, AF, and PF flags are undefined.

Exceptions

Protected mode
#GP(0)If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit. If the DS, ES, FS, or GS register is used to access memory and it contains a NULL NULL segment selector.
#SS(0)If a memory operand effective address is outside the SS segment limit. #PF(fault-code) If a page fault occurs.
#AC(0)If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.
#UDIf the LOCK prefix is used.
Real address mode
#GPIf a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.
#SSIf a memory operand effective address is outside the SS segment limit.
#UDIf the LOCK prefix is used.
Virtual-8086 mode
#GP(0)If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.
#SS(0)If a memory operand effective address is outside the SS segment limit. #PF(fault-code) If a page fault occurs.
#AC(0)If alignment checking is enabled and an unaligned memory reference is made.
#UDIf the LOCK prefix is used.
Compatibility mode
Same exceptions as in protected mode.
64-bit mode
#SS(0)If a memory address referencing the SS segment is in a non-canonical form.
#GP(0)If the memory address is in a non-canonical form. #PF(fault-code) If a page fault occurs.
#AC(0)If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.
#UDIf the LOCK prefix is used.

Sources