-2

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?

Ark1409
  • 25
  • 6
  • 9
    `mov eax, ebp-20` isn't valid in NASM syntax and ins't an encodable instruction. – Michael Petch Dec 21 '20 at 04:29
  • 2
    It's common for people coming from higher-level languages to want to write assembly code like `mov eax, 7*ebx+(edx/9)+(eax*edi)-27`. But you can't write arbitrary algebraic expressions, except for constants; each step of that evaluation requires a separate instruction to be executed, and you have to code each of them individually. – Nate Eldredge Dec 21 '20 at 04:37
  • 4
    *Can I not just write it as:* You could have answered that with a "no" yourself, just by trying it. Then with some more searching, you'd find out why not. There are no forms of `mov` that can do math on the value being copied to the destination. Also, if `mov` worked, the obvious optimization would be `push ebx-20`, not wasting an instruction. – Peter Cordes Dec 21 '20 at 04:40
  • BTW, "why doesn't / can't `mov eax, ebp-20` work" could be a reasonable CPU / language-design question. You could [edit] this to not assume a false premise. Part of the answer to *that* is that assembly is not a *compiled* language; every asm line has to map to one machine instruction, encoded in machine code. (Like Nate said). – Peter Cordes Dec 21 '20 at 19:07

1 Answers1

2

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).

Brendan
  • 35,656
  • 2
  • 39
  • 66