Answers and Comments


User Avatar
Written by artem


I would say in real world num*7 will work faster, because it is one instruction vs. two.


User Avatar
Written by aleksin


Multiplication is one of those "difficult" operations for a proccessor, one that eats up a lot of transistors and therefore uses a lot of CPU power. There were a time then processers didn't even implement multiplication via single instruction.

For example, Intel 8085 was provided with an instruction set which consists of various instructions such as MOV, ADD, SUB, JMP but complex operations and other arithmetic operations must be implemented in software. Such as, multiplication was implemented using a multiplication algorithm:

http://en.wikipedia.org/wiki/Multiplication_algorithm

In later processers Intel added MUL, IMUL, and AAM instructions.

But any way, the absolute fastes way to divide or multiply number by a power of 2 constant is to use shift operation which resolves to a single right/left shift instruction:

mov eax, someNumber
SAL EAX, 3

Though the key is not the number of instruction but the complexity of the instruction itself.

The great resource is Agne's Fog "Optimizing subroutines in assembly language"
http://www.agner.org/optimize/optimizing_assembly.pdf


Saved Stories

Sponsored Categories