0

So lea instruction -- loads effective address -- suppose to be used to load up an address, but I'm kinda confused with this example,

lea (%edx, %ecx, 1), %eax

Is this instruction used to get a byte value of address (base %edx and offset %ecx)? If so, is it possible to use mov instruction instead of using lea in this case?

nrz
  • 10,435
  • 4
  • 39
  • 71
REALFREE
  • 4,378
  • 7
  • 40
  • 73
  • 2
    This particular command adds together EDX and ECX and assigns the result to EAX. MOV can't do that, neither can ADD. LEA can also add an extra constant and multiply the index by a small power of two. – Seva Alekseyev Nov 27 '13 at 05:00
  • Related: [Using LEA on values that aren't addresses / pointers?](https://stackoverflow.com/q/46597055) – Peter Cordes Jun 11 '20 at 18:50

1 Answers1

6

I can't speak for the precise syntax; I'm used to MASM, which writes more or less the same information "backwards".

But in general the LEA instruction is used to compute addresses by combining values from base and index registers with various constants. In the general form what

     LEA  target_register, offset[base_register*k+index_register] ; MASM syntax, read from right to left

does is:

      target_register:=content(base_register)*k+content(index_register)+offset

where k is 0, 1, 2 or 4 (and if k is zero, we don't bother writing "base register*k").

This combines arithmetic and register-to-register moves, so if you had to implement it without using LEA, yes, you'd likely use a MOV instruction (and SHL and ADD...). But LEA does NOT fetch anything from memory, and various forms of the MOV command do, so I don't think of LEA as a kind of MOV instruction.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Right, actually LEA is often used for simple arithmetics (as it doesn't update the flags), it's definitely not a load – Leeor Nov 27 '13 at 01:36
  • (reg, reg, constant) in AT&T is same as offset[base+index] in MASM? i doubt it. But LEA does not fetch anything from memory for sure? – REALFREE Nov 27 '13 at 01:37
  • @Leeor: MOV doesn't update the flags either; what's your point? – Ira Baxter Nov 27 '13 at 01:37
  • @REALFREE: LEA does what I wrote. No memory access. If you replaced the LEA with a MOV opcode, the instruction would compute the address for the target register exactly the same way, but then use that address to fetch a value from memory. – Ira Baxter Nov 27 '13 at 01:38
  • @Ira - I meant as opposed to ADD for e.g. - the point is that it's an arithmetic op, not a memory op – Leeor Nov 27 '13 at 01:39
  • @REALFREE: I don't know what the AT&T syntax says exactly. It has to fit into the structure of what I claim LEA does. – Ira Baxter Nov 27 '13 at 01:39
  • I guess you are right. So lea (%eax,%eax,1), %ecx is equivalent to lea ecx, [eax + eax*1]. So returning to my original question, it just calculates [eax + eax * 1] as address and put into ecx in this case? – REALFREE Nov 27 '13 at 01:44
  • That's probably what it does. You need to consult your AT&T syntax reference to know for sure. – Ira Baxter Nov 27 '13 at 01:47
  • @REALFREE yes, the basic format for the memory operand is `offset(base, index, scale)` in att syntax. That being said you should really consider switching to intel syntax for readability. att is an abomination of assembly syntax IMO. – greatwolf Nov 27 '13 at 04:19
  • @greatwolf ah well I thought so at first time, but once you get used to it, it's alright imo xD – REALFREE Nov 27 '13 at 04:23