1

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Thanizer
  • 382
  • 1
  • 6
  • 21
  • Your lines are "AT&T assembler syntax", as used by the Gnu assembler (GAS). There are some manuals online. – Kijewski Apr 22 '12 at 01:38

1 Answers1

4

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
  • 2
    I doubt that the scale factor was ever implemented as a multiply, it was probably just a left shift. – Gabriel Southern Apr 22 '12 at 00:56
  • Thanks! I guess I was confused on the "formula" when dereferencing. – Thanizer Apr 22 '12 at 01:04
  • 2
    You've got a typo in there: it's `-4`, not `+4`. – Kijewski Apr 22 '12 at 01:35
  • @Gabriel yes, you're right. It is a shift. My assembler is rusty, but that's the idea. Joining two operations in one if the compiler (or you) can manage to arrange the computations that way. kay: Oops, you're right. It was edited by Bo Persson. – Diego Sevilla Apr 22 '12 at 09:18