Reputation: 375
From experience, I have seen that,
lea rax, [2*rax + rax +1]
is equivanelt to:
mul rax, 3
inc rax
But, what is LEA exactly doing? If rax = 3 prior to the instruction, is it returning the address of wherever 10 is stored in memory? How can that address be the number 10 itself?
Any help would be appreciated. Thank you.
Upvotes: 1
Views: 1151
Reputation: 86343
The intel-x86 architecture allows for some complex address calculations when doing memory accesses. This is what's inside the [] brackets.
Since the address calculation is so powerful intel added the LEA instruction which does just the address calculation part of an memory access. It does not access memory at all but stores the result of the calculation in a register instead.
Because all the arithmetic logic was already in the chip this versatile instruction was cheap to add, so intel just did that.
Regarding the use: It is easier to think about LEA as a specialized ADD instruction that can add registers, constants and registers using a limited number of shifts.
Technically it is using the address calculation part of the processor, but it will never access the memory. For you as a programmer it does not make a difference except that LEA will not change the flags.
Upvotes: 4