The general use of the Assembly language for programming is to achieve a more efficient and faster programs than a compiler would. So some optimization rules should help out with that. But be aware that there are way more rules than dealt with here.
NOTE: This page takes some basic rules described in the Intel documentation and does not consider the µops. Thus, these rules here won't be enough if you want to get a maximum optimization.
The general rule is to keep as less lines as possible. Each instruction takes at least one cycle, while other instructions such as
div might take even up to ca. 130 cycles per instruction. Also avoid using
CMP as much as possible, as this branches the code and the CPU cannot go for a parallelization or such. Use the smallest possible floating-point or SIMD data type, to enable more parallelism. Avoid the use of conditional branches inside loops and consider using SSE instructions to eliminate branches. Avoid the use of unneccessary
MOV as you can access the registers quicker than the RAM.
Here are some ways to optimize code by e.g. using logical operations or simple
reg is an alias for a register and
? for a number.
|Clears the register and sets to 0|
|Same as |
|Clears the |
|If you want to check if only a single bit turned on, use |
|Multiplies a value by a power of two. Division is similar|
|If additional registers are needed or results must be stored somewhere, using the |
|If carry flag is not needed, change the carry flag via |