Compile Lea and mov commands

Source: Internet
Author: User

for example, if you use local to define a local variable localvar on the stack, do you know what the actual command is? Generally, it looks like the following:
push EBP
mov ESP, EBP
sub ESP, 4
the stack now has 4 bytes of space, which is your local variable.
next, if you execute mov localvar, 4, what is the actual command? Yes:
mov dword ptr [ebp-4], 4
so the "Address" of this local variable is the ebp-4 -- obviously, it is not a fixed address. Now you need to pass its "Address" as a parameter to a function. You can write it as follows:
invoke/call somefunc, ADDR localvar
the actually generated command is:
Lea eax, [ebp-4]
push eax
call somefunc
Of course, you can also write:
mov eax, EBP
sub eax, 4
push eax
call somefunc
As you can see, here is an additional command. This is the benefit of Lea. As a result, Lea has another wonderful use: for simple arithmetic computing, especially with 32-bit instructions, it is even more powerful ":
For example, if you want to calculate eax * 4 + EBX + 3 and put the result into edX, what should you do?
MoV edX, eax
SHL edX, 2
Add edX, EBX
Add edX, 3
Now we can use the lea command to solve the problem:
Lea edX, [EBX + eax * 4 + 3]

Lea is interpreted as follows:Load Balancing tive address. (add a valid address to start confusing what is the valid address ??? What is the difference between a valid address and mov ax, [address? In fact, they are all equivalent. Later I learned that an offset can be an immediate number or a result of four arithmetic operations, saving space and improving efficiency)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.