Assembly - Multiplication by a constant
Short intro
Assembly languages are set of low-level languages for programming computers and were first developed in the 1950s and are mostly known as second generation programming languages. Important feature of Assembly language, are that, it eliminated much of the error-prone and time-consuming operations required in first-generation programming used in older PC (Tedious operation, such as remembering numeric codes and calculating addresses, became obsolete).
Multiplication under assembly
In assembly you can make a multiplication using the instructions mul (unsigned numbers) and Imul.
It is used as follows:
mul name_registry
The processor increases the content of eax or ax or al (depending on the size of the registry: given in parameter) and then stores the result in eax or ax for the "low" part and the result edx dx or for the "mopart st significant" of the result if the past record of multiple parameter is greater than one byte.
Using
mul is a quite troublesome because you will have to move the multiplied value in eax, the value to multiply in a different registry and the registy holding the result must be eax,(overwrites the value it had).
However, if you just want to multiply a registry by a constant and put the result in the registry of your choice, you can use a shortcut with the instruction "lea" (load effective address). This instruction is used to retrieve the address of a data in memory.
lea destination_registry, [memory_address]; place memory_address in destination_registry
The big advantage is that, instead of using the memory address, you can substitution the multiplication or addition of a registry by a constant.
Imagine that you want to multiply by 7 ecx and save the result in ebx, the command would be:
lea ebx, [ecx * 7]
Note that: you can not multiply two registries together using this shortcut.