If I have:
lea eax, [ebp-20]
push eax
Can I not just write it as:
mov eax, ebp-20
push eax
Why is the first one preferred over the second?
If I have:
lea eax, [ebp-20]
push eax
Can I not just write it as:
mov eax, ebp-20
push eax
Why is the first one preferred over the second?
There are 2 languages - assembly language, and machine code.
There is no reason why you can't invent your own assembly language that generates the same machine code; where the plain text "mov eax,ebp - 20"
is supported (and converted into the same machine code as a lea eax,[ebp-20]
would have generated in other assemblers).
There are 2 reasons why assemblers don't do this:
a) Conventions. It's nice if all assemblers use the same mnemonics for the same things, and if those mnemonics are used by CPU manuals; because this makes it easier to learn assembly language and easier to switch from one assembler to another.
b) Parsing. An assembler ends up having to parse text like "mov eax,[ebp-20]"
and it's easier to recycle the same parsing code to handle "lea eax,[ebp-20]"
and avoid having different code to parse an additional operand format.
Note: There's also a plausible reason for wanting to an assembler to do this. Often lea
is used for integer arithmetic that has nothing to do with effective address calculations; and supporting an alternative mov
syntax would give programmers a way to add context and improve code readability (e.g. use lea
when it is an address and use mov
when it's not an address so it's easier to tell what is/isn't intended to be an address).