Why would you want to use:
MOV EAX, 22
SHL EAX, 2
...when multiplying by 4 opposed to just using the MUL instruction?
I understand that this can also be done with SHR instead of DIV as well.
What are the advantages of doing this?
Also can you do this with odd numbers or can it only be even numbers?
Using the
SHL/SHRinstruction is, generally speaking, much faster thanMUL/DIV.To answer your second question, you can do this with odd numbers as well, but you do have to add another instruction. So you can't technically just do it using the
SHL/SHR.For example: the following code multiplies by 5 without using the
MULinstruction: