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

Programming, for all ages and all languages.
Post Reply
dlarudgus20
Member
Member
Posts: 36
Joined: Sat Oct 26, 2013 4:14 am

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

Post 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?
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

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

Post 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.
"Procrastination is the art of keeping up with yesterday."
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

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

Post 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.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

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

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

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

Post 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!
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
Post Reply