Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 564 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the purpose of the LEA instruction?

#1
For me, it just seems like a funky MOV. What's its purpose and when should I use it?
Reply

#2
`lea` is an abbreviation of "load effective address". It loads the address of the location reference by the source operand to the destination operand. For instance, you could use it to:

lea ebx, [ebx+eax*8]

to move `ebx` pointer `eax` items further (in a 64-bit/element array) with a single instruction. Basically, you benefit from complex addressing modes supported by x86 architecture to manipulate pointers efficiently.
Reply

#3
As others have pointed out, LEA (load effective address) is often used as a "trick" to do certain computations, but that's not its primary purpose. The x86 instruction set was designed to support high-level languages like Pascal and C, where arrays—especially arrays of ints or small structs—are common. Consider, for example, a struct representing (x, y) coordinates:

struct Point
{
int xcoord;
int ycoord;
};

Now imagine a statement like:

int y = points[i].ycoord;

where `points[]` is an array of `Point`. Assuming the base of the array is already in `EBX`, and variable `i` is in `EAX`, and `xcoord` and `ycoord` are each 32 bits (so `ycoord` is at offset 4 bytes in the struct), this statement can be compiled to:

MOV EDX, [EBX + 8*EAX + 4] ; right side is "effective address"

which will land `y` in `EDX`. The scale factor of 8 is because each `Point` is 8 bytes in size. Now consider the same expression used with the "address of" operator &:

int *p = &points[i].ycoord;

In this case, you don't want the value of `ycoord`, but its address. That's where `LEA` (load effective address) comes in. Instead of a `MOV`, the compiler can generate

LEA ESI, [EBX + 8*EAX + 4]

which will load the address in `ESI`.

Reply

#4
The LEA instruction can be used to avoid time consuming calculations of effective addresses by the CPU. If an address is used repeatedly it is more effective to store it in a register instead of calculating the effective address every time it is used.
Reply

#5
Here is an example.

// compute parity of permutation from lexicographic index
int parity (int p)
{
assert (p >= 0);
int r = p, k = 1, d = 2;
while (p >= k) {
p /= d;
d += (k << 2) + 6; // only one lea instruction
k += 2;
r ^= p;
}
return r & 1;
}

With -O (optimize) as compiler option, gcc will find the lea instruction for the indicated code line.
Reply

#6
Maybe just another thing about LEA instruction.
You can also use LEA for fast multiplying registers by 3, 5 or 9.

LEA EAX, [EAX * 2 + EAX] ;EAX = EAX * 3
LEA EAX, [EAX * 4 + EAX] ;EAX = EAX * 5
LEA EAX, [EAX * 8 + EAX] ;EAX = EAX * 9


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through