This article mainly introduces the analysis of the use method of the assembly language lea instruction. The article introduces the example code in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.
Lea instruction variants (categorized by size):
- leaf #2 bytes
- leal #4 bytes
- leaq #8 bytes
How to use lea:
leaq a(b, c, d), %rax
First of all, the lea instruction is a variant of the mov instruction. It is said that the lea instruction is the oldest but in some aspects, the most magical instruction.
On the surface, what it does is very simple, calculating the address based on the source operand in brackets, and then loading the address into the target register.
For example:leaq a(b, c, d), %rax first calculates the address a + b + c * d, and then loads the final address into the register rax.
The funny thing is that leaf does not refer to registers in the source operand, it is just a simple calculation. Then such a person can be used as a multiplication instruction.
For example:
rbx * 2
movq $8, %rbx leaq (, %rbx, 2), %rax
rbx * 3
movq $8, %rbx leaq (%rbx, %rbx, 2), %rax
rbx * 3 - 1
movq $8, %rbx leaq -1(%rbx, %rbx, 2), %rax
When to use the lea command:
Before you plan to complete a multiplication operation with five or six instructions, see if you can use two or three leaves instructions to replace it.
Notes:
The value range of d is 1, 2, 4, 8 (64-bit CPU)
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.