Instruction with LEA and MOV lead to same result -

2019-08-14 09:37发布

For testing purposes I have written some x86 assembly code:

lea  ebx, [esi]

I changed the line and wrote:

mov  ebx, esi

and the program does exactly the same. Why?

In esi , there is stored the address of a string. In the first line, I stored the address of the address of the string, right? And in 2 line, it should store only the address of the string.

Here, Amit Singh Tomar wrote that

 mov eax ,var == lea eax [var]

and when I read and applied that to my case, then I was a little bit confused.

edit: I also try to translate that two lines above into pseudo-C code and it looks like this (I assume that ebx and esi are pointers):

 (1st line with lea):

 unknownType *ebx = &(*esi)  // since the brackets mean dereferencing, 
                             // I use the dereferencing operator * 
                             // and since lea is equal to the 
                             // address of operator, I came to this result


 (2nd line with mov):
 unknownType *ebx; 
 ebx = esi ;                 // in the second case, ebx would point also 
                             // to the address of the string

But this would not be the same, right?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-14 10:15

Your translation into pseudo-C is correct (assuming unknownType *esi; above the code shown). What you haven't realized is, again in pseudo-C, that

&(*x) == x

for all valid pointers x. You have not taken the address of the address of the string; you have only taken the address of the string.

查看更多
The star\"
3楼-- · 2019-08-14 10:30

In the original 8088/8086, I don't think there was any difference and I wondered the same question. Maybe one was a byte shorter or executed in a cycle less.

However, since the 80386, the effective addressing modes are expanded.

mov  ebx, 8[esi*4]

With that, lea is significantly more useful since there is no single-instruction way to do

mov  ebx, 8+ esi * 4      // illegal

However, this is perfectly valid and useful:

lea  ebx, 8[esi * 4]      // useful and legal
查看更多
登录 后发表回答