5

These are the first four lines of a function. I know that the first two lines create a new frame in the stack and are basically 'setup' lines. What do the lea instructions do?

40148e: 48 83 ec 18    sub    $0x18,%rsp
401492: 48 89 f2       mov    %rsi,%rdx
401495: 48 8d 4e 04    lea    0x4(%rsi),%rcx
401499: 48 8d 46 14    lea    0x14(%rsi),%rax
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
DaedalusUsedPerl
  • 772
  • 5
  • 9
  • 25

1 Answers1

5

lea, Load Effective Address, puts the computed "memory address" in the result register. So here, rcx = rsi + 4 and rax = rsi + 0x14.

By the way, the second line doesn't really look like it's part of the code that sets up the stack frame, rsi is the second argument when using System V AMD64 calling conventions.

harold
  • 61,398
  • 6
  • 86
  • 164
  • 3
    Keep in mind that `lea` doesn't necessarily **have** to be used for computing addresses. Since it doesn't occupy the ALU like normal arithmetic instructions, compilers like to use it to perform simple arithmetic ("simple" = the kind supported by `lea` and addressing modes) when the flags aren't needed. – Daniel Kamil Kozar Apr 24 '13 at 07:20
  • 1
    @DanielKamilKozar why do people keep saying that? `lea` uses an ALU on just about any CPU these days. It is not free. "lea in AGU" used to be a thing in a far and gray past.. – harold Apr 24 '13 at 07:26
  • 1
    Source: http://www.agner.org/optimize/instruction_tables.pdf `lea` goes to an AGU on K7, K8, K10, Atom .. and that's about it. – harold Apr 24 '13 at 07:48
  • @DanielKamilKozar that explains to me why i see `lea rdx, [rax - 1]` after a `call strlen`. Thanks! – Hanan Jan 31 '18 at 20:39