What is the use of the LEA in assembly?

1

This is what I read about LEA:

Load the address of the source operand.

Syntax :

LEA destino, fuente

The source operand must be located in memory, and its displacement is placed in the index or pointer record specified in destination.

Example:

MOV SI, OFFSET VAR1

That is equivalent to:

LEA SI, VAR1


As I did not know it was an offset I searched in and found in Wikipedia the following:
Given an array of A characters that contains "abcdef" , it can be said that the element containing the 'd' character has a offset of 4 from the start from A .

As I understood that in the LEA example in the SI record the size of the VAR1 string is saved so that it is know what is the last index of the chain.

    
asked by FrEqDe 22.07.2017 в 02:55
source

3 answers

3

LEA Load Effective Address :

TL; DR

Compute the absolute or effective address of an offset or memory location and store it in the destination operand without altering any flag.

Consider the following example:

+-----+-----+-----+-----+
| 'h' | 'o' | 'l' | 'a' | <- Valor del array de caracteres "A"
+-----+-----+-----+-----+
|  0  |  1  |  2  |  3  | <-Posición en memoria
+-----+-----+-----+-----+

In C, if we do A[0] , the returned value should be 'h' , no? Then, the LEA instruction in assembler does practically the same, with the difference that the value is not dereferenced , but only the resulting address is loaded in the destination operand.

The following text was taken from a SO response a> .

Imagine you have the following structure:

struct Point {
    int xcoord, ycoord;
};

And you define an array of that structure, then you try to access an element of the array by:

int y = points[i].ycoord; /* Accede al elemento en la posicion [i].  */

Then, assuming that the base address, that is, that the element [0] of the array is in EBX and the variable i in EAX , you have the following instruction:

MOV EDX, [EBX + 8*EAX + 4]  ; El lado derecho es la "Dirección efectiva"

And this will result in the value of the variable y in EDX ; the reason for 8 on the right side is that currently the size of the structure Point is 8 bytes.

Now consider that you execute the following in your program, instead of assigning the value, you assign the memory address of the variable ycoord of the index i in the array.

int *p = &points[i].ycoord; /* Crea un puntero a points[i].ycoord */

The assembly result will be:

LEA ESI, [EBX + 8*EAX + 4] ; Carga solo la dirección de memoria.

In this case you did not want to assign the value of ycoord in the index i of your array, instead, you created a pointer and assigned it to ESI .

Changing all that, and assuming that the index you're trying to access is 4 and that the address of array[0] is zero, the code performs the following calculations:

LEA ESI, [0         + 8      *      4 + 4]
          Dirección | Tamaño x Indice + Offset 
          base                          de variable necesitada.

It is worth mentioning that this example is valid for the code placed above, so it can change.

    
answered by 22.07.2017 / 04:45
source
3
LEA reg, mem

Load the effective address of mem in reg . That is, reg happens to contain a pointer to mem .

In contrast to:

MOV reg, mem

that loads the value contained in mem in reg .

In contrast to:

MOV reg, imm

that loads an immediate operand coded in the instruction in reg . This would be the instruction generated when the assembler reads MOV reg, OFFSET etiqueta .

Although LEA is intended to use a record as a pointer, it is often used for other things, for example, as a multi-byte NOP ( LEA ESI, 0[ESI] ), to multiply a number by 5 ( LEA reg, [reg+4*reg] ), ...

    
answered by 22.07.2017 в 13:57
0

LEA performs the loading of the memory offset value in a register.

link

    
answered by 22.07.2017 в 04:14