Question

If I say:

lea (%eax,%eax,2), %edx

I've been taught that it essentially means:

edx = eax + eax * 2

How does the format to this work?

And I guess this leads into the second question. If I have something like this:

add -0x4(%esi, %ebx, 4), %eax

I understand that the first operand is added to the second operand and then stored in the second operand, but again, what I don't understand is the first operand's notation. Another example would be if I had:

cmp %eax, (%esi, %ebx, 4)

..., does this mean that the value at %eax is being compared with the value of:

esi + ebx * 4

...? I tried searching a lot of this stuff but I guess I wasn't using the correct words to find a meaningful answer so I decided to post here.

Was it helpful?

Solution

Calculating values with lea is a reminiscent where this operation was either faster (as it was calculated by the circuitry doing address calculation) or smaller in code size. Note that with just one operation you're able to do an add and a multiplication in just one step. Current processors may be faster in doing it instead an add followed by a mul, but it is not clear given speculative execution, arithmetic unit replication, etc.

As for the following instructions, whenever you see the parentheses mean you're dereferencing that address, so:

add -0x4(%esi, %ebx, 4), %eax

means put in %eax what it is in the address given by -4 + esi + ebx*4, so it implies accessing to that position in memory and assigning what's stored there to %eax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top