flat assembler
Message board for the users of flat assembler.

Index > Main > mov and lea -- difference

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
jorido



Joined: 23 Jan 2017
Posts: 53
jorido 08 Oct 2017, 11:14
Is there any difference between these 2?


Code:
    ;................
    var1 db "some text here.....", 10, 0


    ; 1
    lea rdi, [var1]
    
    ; 2
    mov rdi, var1
    
Post 08 Oct 2017, 11:14
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 08 Oct 2017, 11:51
For 64-bit, always use lea since it uses rip-relative addressing (FASM does it implicitly). mov, on the other hand, besides having to add a relocation to mov, it also uses an 64-bit address! (rip-relative is only a 32-bit offset) (unless it's an executable with a fixed position when loaded, but is that a thing anymore?).

For 32-bit code, always use mov since it's smaller.
Post 08 Oct 2017, 11:51
View user's profile Send private message Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 53
jorido 08 Oct 2017, 11:57
my question isn't about that
Post 08 Oct 2017, 11:57
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 08 Oct 2017, 12:00
I don't get it then. Didn't you ask if there is any difference? Those are some pretty big differences in terms of code size (for x64) and efficiency (lack of relocations).

If you mean in terms of what they do, then no, they are the same.
Post 08 Oct 2017, 12:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20448
Location: In your JS exploiting you and your system
revolution 08 Oct 2017, 12:12
Relocations are only relevant for some file formats. The major difference, aside from relocations, is the RIP vs absolute addressing in 64-bit mode, and the number of bytes the instructions use in the code. They might also use different execution ports depending upon the CPU they run on. MOV can load any 64-bit value, LEA cannot address further than +-2G.
Post 08 Oct 2017, 12:12
View user's profile Send private message Visit poster's website Reply with quote
jorido



Joined: 23 Jan 2017
Posts: 53
jorido 08 Oct 2017, 14:00
>> If you mean in terms of what they do, then no, they are the same.

yes, what they do
thx
Post 08 Oct 2017, 14:00
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 02:00
ehemmm!! *_*

@jorido

MOV and LEA makes HUGE differences when doing stack LOCALS. They are NOT the same, like some people claimed xD

locals
var1 db "some text here.....", 10, 0
endl

mov rdi var1
will throw you error for the obvious "academic" reasons. Can't calculate address that is not yet called at runtime.

lea rdi,[var1] ---> the solution. Address is calculated at runtime when it matters.

regardless of 32-bit or 64-bit.

Every LEA will translate to an address regardless. Not all MOV translates to an address. That's another HUGH difference. For example,

mov rdi,[rbp-16] is NOT THE SAME as lea rdi,[rbp-16]

in the case of STOSB where RDI needs to point to a buffer.

Edit by revolution: Trolling removed
Post 09 Oct 2017, 02:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20448
Location: In your JS exploiting you and your system
revolution 09 Oct 2017, 04:15
Yes, for a locally defined variable mov reg,var will fail to assemble. Because you end up trying to assemble mov reg,rbp+offset. But the error message quickly tells you there is a problem.
Post 09 Oct 2017, 04:15
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 09 Oct 2017, 12:11
system error wrote:
ehemmm!! *_*

@jorido

MOV and LEA makes HUGE differences when doing stack LOCALS. They are NOT the same, like some people claimed xD

locals
var1 db "some text here.....", 10, 0
endl

mov rdi var1
will throw you error for the obvious "academic" reasons. Can't calculate address that is not yet called at runtime.

lea rdi,[var1] ---> the solution. Address is calculated at runtime when it matters.

regardless of 32-bit or 64-bit.

Every LEA will translate to an address regardless. Not all MOV translates to an address. That's another HUGH difference. For example,

mov rdi,[rbp-16] is NOT THE SAME as lea rdi,[rbp-16]

in the case of STOSB where RDI needs to point to a buffer.
That's a macro / "high level" feature, so who cares? Local variables are done with [rbp+X] or [rsp+X] in actuality by the CPU and what the assembler outputs, so of course mov won't work (since it dereferences it), but that's not what the OP posted anyway.

Edit by revolution: Trolling removed
Post 09 Oct 2017, 12:11
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 13:12
Edit by revolution: Trolling removed
Post 09 Oct 2017, 13:12
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 13:17
Edit by revolution: Trolling removed
Post 09 Oct 2017, 13:17
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 13:26
Furs wrote:
If you mean in terms of what they do, then no, they are the same.


No!

MOV is a transfer instruction. LEA is an address generation instruction. They may result in the same effect , but in terms of "what they do", they are not.

Edit by revolution: Trolling removed
Post 09 Oct 2017, 13:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20448
Location: In your JS exploiting you and your system
revolution 09 Oct 2017, 13:39
system error wrote:
MOV is a transfer instruction. LEA is an address generation instruction. They may result in the same effect , but in terms of "what they do", they are not.
Technically correct, at an opcode level. Internally we don't know what they do. Some CPUs use the same ALU to compute the result, some don't. And unless we dig very deep into the CPU micro-internals we usually have no idea what happens within. As far as the result is concerned there is no practical distinction to make.
Post 09 Oct 2017, 13:39
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 09 Oct 2017, 13:48
system error wrote:
No!

MOV is a transfer instruction. LEA is an address generation instruction. They may result in the same effect , but in terms of "what they do", they are not.
That makes no sense. Nobody said that "they are the same" in everything, just that in terms of what they do, there is no difference. They clearly are not the same since they are encoded with differently-sized bytes (not just different bytes, but different sizes!). Thus, the larger instruction is clearly worse for the instruction cache and memory usage. But "in terms of what they do", that doesn't matter.

Obviously, all this was already specified in the first post...
Post 09 Oct 2017, 13:48
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 18:50
Furs wrote:
That makes no sense.

<snip>


Which part?

LEA doesn't transfer anything. Its sole purpose is to calculate the address at runtime. Some advanced folks use it for complex math. That's all there is.

That's a HUGE DIFFERENCE.

I can take instructions like XCHG to be of the same "transfer" instructions like MOV. But not LEA. LEA edi,[EBP+16] is NEVER the same in "what they do" against MOV edi, [EBP+16].

Edit by revolution: Trolling removed
Post 09 Oct 2017, 18:50
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 18:56
Edit by revolution: Trolling removed
Post 09 Oct 2017, 18:56
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 19:02
Edit by revolution: Trolling removed
Post 09 Oct 2017, 19:02
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 19:07
Furs wrote:
For 64-bit, always use lea since it uses rip-relative addressing (FASM does it implicitly). mov, on the other hand, besides having to add a relocation to mov, it also uses an 64-bit address! (rip-relative is only a 32-bit offset) (unless it's an executable with a fixed position when loaded, but is that a thing anymore?).

For 32-bit code, always use mov since it's smaller.


Wrong again. LEA doesn't always translates to RIP-relative. Try this code and see it "relatives" to what...

sub rsp,16
lea rdi,[rbp-16] ;??
mov rdi,[rbp-16] ;??

Question: Do you see RIP in there? xD

Edit by revolution: Trolling removed
Post 09 Oct 2017, 19:07
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 09 Oct 2017, 20:43
lol man, again you're derailing, see the opening post. This is his code:
Code:
    ; 1
    lea rdi, [var1]
    
    ; 2
    mov rdi, var1     
Which is RIP-relative and you don't see it in the source since FASM automatically uses it (which is bad in very rare cases, for example, when accessing the TIB/TEB). Not all assemblers use it automatically though, for example GAS doesn't.

But encoded it is RIP-relative, and a disassembly will show that easily.
Post 09 Oct 2017, 20:43
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 09 Oct 2017, 21:40
Edit by revolution: Trolling removed
Post 09 Oct 2017, 21:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.