-2

I am looking at x86-64 ISA, and the leaq instruction is used to load the address to a designated register and I am looking at the following example:

long m12(long x){
    return x *12;
}

which is equivalent to

leaq (%rdi, %rdi, 2), %rax
salq $2, %rax

I vaguely get what it means here is that leaq does not reference the memory location given by the computed "address", instead it relocates the computed "address" directly. However, %rdi just holds an integer. How is that an address in any sense?

ikegami
  • 367,544
  • 15
  • 269
  • 518
Adam
  • 257
  • 3
  • 12
  • 3
    An address is just an integer. The computer does not know if the integer is meant to be an address or not, it doesn't care either. It just carries out the computation. – fuz Nov 12 '21 at 23:26

1 Answers1

0

LEA doesn't actually have anything to do with addresses. While it's designed to facilitate the computation of addresses, the numbers don't actually have to be addresses and offsets. Keep in mind that LEA doesn't access the computed "address", so it's often used to efficiently compute values that aren't addresses as well.

leaq (%rdi, %rdi, 2), %rax   // rax = rdi + rdi*2 = 3*rdi
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    We actually have another canonical Q&A specifically about LEA on non-pointers, about how it's just a shift-and-add instruction that compilers and humans that way. [Using LEA on values that aren't addresses / pointers?](https://stackoverflow.com/q/46597055) amusingly has the exact same C source and asm in the question, so it's probably from the same book, so it's a perfect duplicate. – Peter Cordes Nov 13 '21 at 02:09
  • @Peter Cordes Cool cool. My answer is a bit light on the details compared to yours since I haven't used x86 assembler since the .com days. :) I updooted yours. – ikegami Nov 13 '21 at 02:27