Search code examples
assemblyx86nasm

Intel assembly lea instruction


Are the expressions

lea edx, [0+eax*4]

lea edx, [eax*4]

identical?

Is there any important point of using +0(zero)?

Does it effect the performance / result?


Solution

  • Yes, they're identical, [0+eax*4] is the only way to encode an [eax*4] addressing mode.

    In disassembly, you'll see the +0 because an addressing mode with just a scaled index isn't truly encodable in machine code. x86 addressing modes always have a base register or a disp32. But the disp32 can be all zeros.

    (Fun fact: this is why [ebp] assembles as [ebp + disp8] with a zero byte. The encoding that would mean base=EBP with no displacement instead means no base register, just a disp32. The same applies with or without an index).

    All of this applies in 64-bit mode with 64-bit registers as well. See https://agner.org/optimize/ and other links in https://stackoverflow.com/tags/x86/info


    Performance:

    It's often worth it to spend the extra machine-code size to copy-and-scale in one single-uop instruction, instead of mov + shl. Especially if both the mov and shl would need their own REX prefix in 64-bit mode.