To get the product of a register and a constant and store it in another register, the naïve way is to do this:
imul ecx, 3 ; Set ecx to 5 times its previous value
imul edx, eax, 5 ; Store 5 times the contend of eax in edx
lea
Multiplications are expensive operations. It's faster to use a combination of shifts and adds. For the particular case of muliplying the contend of a 32 or 64 bit register that isn't esp
or rsp
by 3 or 5, you can use the lea instruction. This uses the address calculation circuit to calculate the product quickly.
lea ecx, [2*ecx+ecx] ; Load 2*ecx+ecx = 3*ecx into ecx
lea edx, [4*edx+edx] ; Load 4*edx+edx = 5*edx into edx
Many assemblers will also understand
lea ecx, [3*ecx]
lea edx, [5*edx]
For all possible multiplicands other them ebp
or rbp
, the resulting instruction lengh is the same as with using imul
.
ebp
or rbp
it takes one byte more them using imul