On x86 16-bit assembly, if there is a 1
in ax
and 0x10
(16
) in cl
, the following code doesn't put 0x1000
in cl
:
mul cl ; ax = 0x 10
mul cl ; ax = 0x 100
mul cl ; ax = 0x0000 (not 0x1000)
Why doesn't this code work as expected?
On x86 16-bit assembly, if there is a 1
in ax
and 0x10
(16
) in cl
, the following code doesn't put 0x1000
in cl
:
mul cl ; ax = 0x 10
mul cl ; ax = 0x 100
mul cl ; ax = 0x0000 (not 0x1000)
Why doesn't this code work as expected?
Your source is 8 bits (cl
) so the multiplication is performed over al
only.
Use mul cx
instead.
Explanation:
If ax = 100h
, then al = 00h
. Since the result of mul cl
is placed in ax
, what you do is basically to replace the content of ax
with 00h*cl
, which is 00h
.
Opcode MUL
CPU: i8086+ Type of Instruction: User
Affected FLags: CF, OF, AF, PF, SF, ZF
Instruction: MUL src
Note: Unsigned multiply of the accumulator by the source. If "src" is a byte value, then AL is used as the other multiplicand and the result is placed in AX. If "src" is a word value, then AX is multiplied by "src" and DX:AX receives the result. If "src" is a double word value, then EAX is multiplied by "src" and EDX:EAX receives the result. The 386+ uses an early out algorythm which makes multiplying any size value in EAX as fast in the 8 or 16 bit registers.
++++++++++++++++++++++++++++++++++++++
Clocks (i486): MUL reg8 13-18 MUL reg16 13-26 MUL reg32 13-42 MUL mem8 13-18 MUL mem16 13-26 MUL mem32 13-42