Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what's the differ between lea and mov offset ?

1,582 views
Skip to first unread message

Amr Mostafa

unread,
Jan 19, 2004, 1:55:56 PM1/19/04
to
Greetings all,

a beginner is here ! :)

I want to know what's the difference between using :

1- lea dx,string
2- mov dx,offset string


I'm confused! as I think both of them gets 'string' address in memory
and puts it in dx

thanks for help!

regards,
amr

Spike

unread,
Jan 19, 2004, 3:16:26 PM1/19/04
to
That's right, both LEA and MOV can load a register with the offset to a
variable for instance.

MOV can also transfer the content of a memory place to a register, LEA
can't.
Both LEA & MOV can calculate "effective offsets" which can be used to do
math much more effective

lookup_table db '0'
db '1'
db '2'
db '3'
db '4'
db '5'
db '6'
db '7'
db '8'
db '9'
db 'A'
db 'B'
db 'C'
db 'D'
db 'E'
db 'F'
code_start:
;Assumes EAX contains a integer between 0-15
mov al,byte[lookup_table+eax]
;EAX will now contain the ASCII character for the earlier
EAX.
ret

This was an example were MOV was used to calculate the effective address and
load the content of the address to itself...

Insted of:
mov al,byte[lookup_table+eax]
you could use:
mov eax, lookup_table+eax

Which would load the offset to the character pointed out by EAX.

Stronger math can be used however.

//Spike

Matt Taylor

unread,
Jan 19, 2004, 4:22:08 PM1/19/04
to
"Amr Mostafa" <am...@hotmail.com> wrote in message
news:6db7e405.0401...@posting.google.com...

> Greetings all,
>
> a beginner is here ! :)
>
> I want to know what's the difference between using :
>
> 1- lea dx,string
> 2- mov dx,offset string
>
>
> I'm confused! as I think both of them gets 'string' address in memory
> and puts it in dx

Yes, but to be clear it fetches nothing from main memory. The lea
instruction computes the address (in this case a constant: offset string)
and stores the result in dx. The mov instruction in this case is identical,
but it encodes into a shorter instruction, and it makes more sense too.

-Matt


xby

unread,
Jan 20, 2004, 9:07:26 AM1/20/04
to
am...@hotmail.com (Amr Mostafa) wrote in message news:<6db7e405.0401...@posting.google.com>...


The offset is evaluated statically by the assembler when you use the
MOV alternative, hence the offset must be available for computation at
assembly time. If the offset is not known at assembly time then you
cannot use the MOV alternative; the LEA alternative must be used since
in this case the offset is evaluated at run time.

Adrian

unread,
Jan 20, 2004, 1:11:15 PM1/20/04
to

LEA is longer, on older processor could be slower, but I'm not sure
LEA is normally used for many optimalization tricks, because it can add
and load : lea eax,[esi+edi+4] for example

Adrian

Alex McDonald

unread,
Jan 20, 2004, 5:51:11 PM1/20/04
to

"xby" <x_...@my-deja.com> wrote in message
news:7d9d52e1.04012...@posting.google.com...

Eh? Both LEA and MOV can be used as in the example above (assembly time)
_and_ for values only known at load time (not run time, which is later). The
only difference is possibly variance in performance from one chipset to
another. You're confusing something like

lea dx,string[ax]

which is using a value in ax as a base to which to add the offset. That's at
runtime. The _offset_ is never calculated at runtime; it's fixed once the
program is loaded.

--
Regards
Alex McDonald

Amr Mostafa

unread,
Jan 20, 2004, 5:54:19 PM1/20/04
to
then I think they are the same and mov offset is faster than lea..
so I have to use mov. but If the offset is not known at assembly time
I have to use lea.. please correct me if I'm wrong

thaaaaanks a lot !
you help is very great and helpful
thank you


( i'm using google to post here so it may take some extra time to be
posted, I tried to use xnews and outlook but I alwayes get 441 you are
not allowed to post in comp.lang.asm.x86 - any one know why ? )

August Derleth

unread,
Jan 20, 2004, 5:55:05 PM1/20/04
to
Adrian wrote:

> LEA is longer, on older processor could be slower, but I'm not sure
> LEA is normally used for many optimalization tricks, because it can add
> and load : lea eax,[esi+edi+4] for example
>
> Adrian
>

lea can also be the fastest way to multiply a register's contents by a
fixed number. Say, multiply eax by 5 the obfuscated way. ;)

lea eax, [eax+eax*4]

There: fewer clocks (almost certainly), more bytes (probably), but edx
doesn't get smashed. Intel apparently didn't `design' their chips so
much as hack away at mask designs until they got something Microsoft
would develop for, but smart coders know how to use what they've been
handed. ;)

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.

R.Wieser

unread,
Jan 20, 2004, 9:17:09 PM1/20/04
to
Amr Mostafa <am...@hotmail.com> schreef in berichtnieuws
6db7e405.04012...@posting.google.com...

Hello Amr,

> then I think they are the same and mov offset is faster than lea..
> so I have to use mov. but If the offset is not known at assembly time
> I have to use lea.. please correct me if I'm wrong

You are hereby corrected :-)

LEA can do what OFFSET does, but it can do more. In the case of "lea
{reg},{address}" and "mov {reg},offset {address}" you are quite right, "mov
offset..." wil be faster (and shorter).

But imagine loading a register with the start of a table + an index. Assume
you want the result in AX, the index is in BX, and the name of / label to
the table is "Table"

In 'straight" code you would probably write :

mov ax,offset Table
add ax,bx

Using LEA this would be :

lea ax, [Table+bx]

It would even look better if you have a sub-table,pointed to by SI, at an
address in which you want to addres an element pointed to by BX

In "straight" code you would write :

mov ax,offset Table
add ax,si
add ax,bx

Using LEA this would be :

lea eax,[Table+si+bx]

Ofcourse, just a small combination of registers is permitted with LEA.

> thaaaaanks a lot !
> you help is very great and helpful
> thank you

You're welcome :-)

> ( i'm using google to post here so it may take some extra time to be
> posted, I tried to use xnews and outlook but I alwayes get 441 you are
> not allowed to post in comp.lang.asm.x86 - any one know why ? )

Maybe because the Newsgroup-server does not know who you are ?

Probably Google accepts your post because you have authenticated yourself to
it, and a newsgroup-server does not, because you have not authenticated
yourself to it. Ask your ISP for more details on the matter.

Regards,
Rudy Wieser


xby

unread,
Jan 21, 2004, 8:42:09 AM1/21/04
to
"Alex McDonald" <alex...@btopenworld.com> wrote in message news:<bukajo$elm$1...@hercules.btinternet.com>...


First of all, I was explaining the difference between the MOV and LEA
alternatives in general, without referring to the specific examples
brought up by the OP. Hence, there is no confusion on my part.

Second, your example "lea dx,string[ax]" is not valid. In 16 bit
assembly (as the notations AX and DX indicate) one can code
LEA DX,STRING[BX], or
LEA DX,STRING[SI], or
LEA DX,STRING[DI]
since only BX, SI, and DI are valid inside the square brackets.

Alex McDonald

unread,
Jan 22, 2004, 1:19:56 PM1/22/04
to
x_...@my-deja.com (xby) wrote in message news:<7d9d52e1.04012...@posting.google.com>...

OK, so you're saying you can't have MOV if the offset is unknown at
assembly time, but you can use LEA? That's not so. There's no
difference (except possible speed) between MOV reg, const and LEA reg,
const. Regardless of whether the assembler knows the value as in
"MOV/LEA AX,10" or has to leave to the linker /loader the value as in
"MOV/LEA AX,external_address". 16 bit, 32 bit, doesn't make any
difference either.

>
> Second, your example "lea dx,string[ax]" is not valid. In 16 bit
> assembly (as the notations AX and DX indicate) one can code
> LEA DX,STRING[BX], or
> LEA DX,STRING[SI], or
> LEA DX,STRING[DI]
> since only BX, SI, and DI are valid inside the square brackets.

I stand corrected. The principle still applies.

--
Regards
Alex McDonald

Amr Mostafa

unread,
Jan 22, 2004, 3:17:31 PM1/22/04
to
thanks a lot !
that was very useful

while I didn't understand everything said here, but I saved this for
later view after going a little forward with my guide ( by Ralph )

but about my question.. it's extra answered! ;)


regards,
amr

Tim Roberts

unread,
Jan 23, 2004, 2:15:40 PM1/23/04
to
alex...@btopenworld.com (Alex McDonald) wrote:
>
>x_...@my-deja.com (xby) wrote :

>>
>> First of all, I was explaining the difference between the MOV and LEA
>> alternatives in general, without referring to the specific examples
>> brought up by the OP. Hence, there is no confusion on my part.
>
>OK, so you're saying you can't have MOV if the offset is unknown at
>assembly time, but you can use LEA? That's not so. There's no
>difference (except possible speed) between MOV reg, const and LEA reg,
>const.

I suspect/hope he was referring to cases like
lea eax, [ebp+4]
That is an example that cannot be done using MOV.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

xby

unread,
Jan 26, 2004, 10:16:06 AM1/26/04
to
Tim Roberts <ti...@probo.com> wrote in message news:<fjs210t63v2lcrglg...@4ax.com>...

>
> I suspect/hope he was referring to cases like
> lea eax, [ebp+4]
> That is an example that cannot be done using MOV.

Yes, this is an example of what I mean. Whenever one or two registers
is/are coded between square brackets, the offset referred to is known
at run time but not at assembly time. In such cases MOV cannot be
used, but LEA can be used.

0 new messages