0

I've already read many answers regarding the LEA instruction and I think, I understand them, but if I'm trying to understand the following assembly code, then this subject doesn't make sense to me anymore.

enter image description here

And the corresponding C program:

enter image description here

So leal (%rdi,%rsi), %eax adds two addresses - addresses of x and y and writes the result of this into %eax? What is the point of this? Why would we need an address of t1? There is nothing in there at this address if we dereference it.

Furthermore, in the next instruction, we add the value of z (or the address of z?) and the address that we calculated in the lea instruction. This all doesn't make sense to me.

Or maybe I don't understand the lea instruction correctly? And the lea instruction adds the values of x and y and writes the result of the addition to a memory location and writes the address of this memory location (t1) into %eax? Why would we then say that the lea instruction doesn't perform a memory access?

I am stuck. Could someone please tell me what are the values (values or addresses) in the registers of the first two instructions? It would really help.

Thanks for your help.

pramort
  • 35
  • 4
  • 1
    `lea` just adds the operands. They don't technically have to be addresses it just takes the form of an address. The first `lea` just adds `x` and `y`, not `&x` and `&y`. That's `t1`. Then adding `z` gives you `t2`. `lea` does not perform a memory access. Notice all the operands are registers (or constants). `lea (%rdi, %rsi), %eax` is basically an optimization for `mov %edi, %eax; add %esi, %eax`. – Jester Jan 30 '21 at 21:54
  • 1
    LEA is just an ALU shift-and-add integer math operation. See the linked duplicate. – Peter Cordes Jan 30 '21 at 22:00
  • @Jester Thank you for your comment. So, if x and y are just values and not addresses, %eax contains an value as the result, e.g., x = 5 and y = 3, %eax contains 8 after the lea instruction? If it is like this, then the name of the instruction is misleading in this case.. – pramort Jan 31 '21 at 08:25
  • That is correct. The source operand still takes the form of an address, but it's not actually used to reference memory so it can be anything it doesn't have to be valid. `x` is treated as if it were a `char*` so you get `eax = &x[y]` which is just `x + y` numerically. – Jester Jan 31 '21 at 12:16

0 Answers0