1

When using IMUL instruction with a single 8-bit operand, the result is stored in AX register. I have the following code

    mov eax, 0
    mov ebx, 0

    mov al, 48
    mov bl, 4

    call dumpregs

    imul bl

    call dumpregs

48 * 8 is 192(C0 in hex), which is correctly stored in AX register as 00C0(see the output below). The signed range of AX register is -32,768 to +32,767, and unsigned range is 0 to 65,535. Therefore both the OF and CF flag should be clear(0). But as you can see in the following output, they both are set(1).

OUTPUT

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Tayyab Mazhar
  • 1,560
  • 10
  • 26
  • 2
    According to https://www.felixcloutier.com/x86/imul *“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.”* – Paul R May 18 '19 at 09:17
  • 4
    The logic being that the result will always fit in the double size destination so using flags for that would be pointless. This way the flags tell you if the result can be safely truncated to the original source operand size, in this case 8 bits. Since 192 does not fit in signed 8 bits, the flags are set. In other words it overflowed the 8 bit size so you need to use the full 16 bit output produced. – Jester May 18 '19 at 10:55
  • Ok, so the OF is set because because 192 doesn't fit in signed 8-bits. But, why is CF set? I know that CF is set when the unsigned value is out of range, but for 8-bit 255 is the maximum range, and 192 is under that range, so CF should be clear, but it's not, why?? – Tayyab Mazhar May 18 '19 at 12:43
  • 1
    CF and OF in multiplication operations are always being set/reset in parallel, as specified in the doc mentioned by @PaulR – vitsoft May 18 '19 at 13:17
  • And why is AL taken into consideration when setting AL, when the whole AX flag is the destination. – Tayyab Mazhar May 18 '19 at 13:27
  • @vitsoft why are they set in parallel? The logic for both flags is different. – Tayyab Mazhar May 18 '19 at 13:28
  • 4
    Because **Intel decided so**. The *logic* concerns arithmetic operations (add, sub) only. Carry Flag does not mean *unsigned overflow* in other instructions, such as shifts, rotations, multiplications. – vitsoft May 18 '19 at 13:40

0 Answers0