Page 1 of 1

What is the difference between "mov" and "lea"?

Posted: Mon May 26, 2014 4:04 am
by dlarudgus20
More directly - what is difference these codes?

Code: Select all

mov ebx, LabelA

Code: Select all

lea ebx, [LabelA]
I know "lea" can be used like this,

Code: Select all

lea eax, [ebx + esi * 2 + 1] ; calculating with one instruction! 
But I've seen "lea" used just instead "mov". For example, the code of higher half bare bones wiki http://wiki.osdev.org/Higher_Half_bare_bones:

Code: Select all

lea ecx, [StartInHigherHalf]
Why do we use "lea"? What is difference?

Re: What is difference from "mov" and "lea"?

Posted: Mon May 26, 2014 4:35 am
by ScropTheOSAdventurer
All it does is put the calculated offset into a register. So,

Code: Select all

lea eax, [1 + 2 + 3 +4]


or

Code: Select all

lea eax, [10]
Ends up giving eax the value of 10.

mov copies the value of one register to another. So, in some cases, lea can replace mov, although in my opinion it just makes for confusing code.

Re: What is difference from "mov" and "lea"?

Posted: Mon May 26, 2014 6:15 am
by Bender
Seriously guys,
LEA = Load effective address
MOV = Load Value at address
The difference is clear.
Some MASM users used LEA instead of MOV. In MASM:

Code: Select all

mov ecx, crap_val
Actually in NASM's terms means:

Code: Select all

mov ecx, dword [crap_val]
Which is quite confusing. You had to get involved with MASMery:

Code: Select all

mov ecx, OFFSET crap_val
Instead you could just use LEA.

Re: What is difference from "mov" and "lea"?

Posted: Mon May 26, 2014 10:24 am
by max
Bender wrote:Actually in NASM's terms means...
Isnt the MASM example you posted this:

Code: Select all

mov dword [crap_val], ecx
in NASM? Just not sure what syntax MASM uses

Re: What is difference from "mov" and "lea"?

Posted: Mon May 26, 2014 1:07 pm
by Combuster
Does this qualify as not having searched?

There's often some redundancy in instruction set architectures. LEA is not interesting because it can do something MOV also could, but it is interesting because it can do a lot of things MOV can't. These small amounts of overlap allow the designers to simplify how the processor works - as an exercise you can try finding all possible notations of NOP.

Re: What is difference from "mov" and "lea"?

Posted: Tue May 27, 2014 3:21 am
by Bender
max wrote:
Bender wrote:Actually in NASM's terms means...
Isnt the MASM example you posted this:

Code: Select all

mov dword [crap_val], ecx
in NASM? Just not sure what syntax MASM uses
Yes the second code block is in NASM.

MASM's "mov r8/16/32, imm" , meant that the register during the operation will contain the value at address "imm" and not "imm", so "mov eax, label" in MASM wouldn't give EAX the address of label (as you'll expect) but it will instead give you a 4-byte value at the address "label"! #-o In MASM "mov eax, [label]" and "mov eax, label" mean the exact same thing!