lea is used in to assign pointers to variables.
int x = 44;
int p = &x;
becomes the following:
mov DWORD PTR [rbp-8], 44 ; We move 44 into a local variable
lea rax, [rbp-8] ; We take the address of the local variable and assign it to RAX
; There is no memory to memory addressing form, hence why our
; destination is a register and not a local variable on the stack
mov DWORD PTR [rbp-4], eax ; We do a mov because mov supports register to memory
In the above lea actually dereferences rbp-8. In other words, it takes what is located at the address stored in rbp minus 8.
In this example, lea does not dereference the register. It instead performs arithmetic on the value stored in the register.
int add_three(int value) {
return value + 3;
}
becomes the following:
add_three:
lea eax, [rdi+3]
ret
In the above. lea does not derference rdi plus 3 as it did with rbp minus 4. Instead, it takes the value stored in rdi and adds 3.
My question is, are there other instances where lea actaully derferences a register in the same with it will registers referring to local variables (e.g., rbp -4 or rsp + 8)?