imul
Signed multiply
Multiplies signed integers, producing a full implicit product with one operand or a destination-width result with two or three.
8086 and later
- The x86-64 instruction setx86-64 assembly
- Arithmetic, logic and bitsx86-64 assembly
- call, ret and the System V conventionx86-64 assembly
- Moving values aroundx86-64 assembly
- Variables in memory and constantsx86-64 assembly
- Multiply and divide, with the remainderx86-64 assembly
- Recursion: factorial and Fibonaccix86-64 assembly
- A 2D arrayx86-64 assembly
- ● set from the result
- ? undefined afterwards
- – not changed
Forms
Operands
r/m- A general-purpose register or memory location matching the operation’s size, for example
raxorqword [rbp-8]. The byte registersah,bh,chanddhcannot be used with a REX prefix, including when the memory address usesr8–r15. reg- A general-purpose register matching the operation’s size, for example
al,ax,eaxorrax. The byte registersah,bh,chanddhcannot be used with a REX prefix, which is required byspl,bpl,sil,dilandr8–r15. imm- An immediate value encoded in the instruction, written as a number, character or label. Its encoded width and any sign extension depend on the instruction; arithmetic immediates may be sign-extended, while shift counts are used without sign extension.
With one explicit source, its width selects the implicit factor and full product: 8 bits multiplies al into ax; 16 bits multiplies ax into dx:ax; 32 bits multiplies eax into edx:eax; 64 bits multiplies rax into rdx:rax.
With two operands, the destination register is multiplied by the source and keeps only the low destination-width bits. With three, the last two operands are multiplied into the first. imul rax, 5 is shorthand for imul rax, rax, 5.
With one operand, CF and OF are set when the high half of the product is more than a sign extension of the low half, so the product does not fit the operand width. With two or three, they are set when the product had to be truncated to fit the destination. SF, ZF, AF and PF are undefined.
An 8-bit immediate used with a wider operand is sign-extended from −128 through 127. For a 64-bit operation, the longer immediate field is only 32 bits and is also sign-extended; there is no full 64-bit immediate form. The encoded bits determine the value used by the operation. For example, a 32-bit field of 0xffffffff becomes 0xffffffffffffffff (−1), not positive 4294967295. An out-of-range constant can produce a warning and truncated bits; use a register for a value that cannot be represented by these sign extensions.
NASM normally chooses the short immediate encoding when it preserves the value; a byte qualifier is not required. strict with an immediate size can prevent that shortening.
global _start
section .text
_start:
mov rax, 12
mov rbx, 5
mov rcx, 3
mov rsi, 9
imul rax
mov rax, 60 ; syscall 60: exit
xor rdi, rdi
syscall