News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

lea or mov offset?

Started by rags, December 31, 2004, 06:18:38 PM

Previous topic - Next topic

rags

Which is better to  use:
 
lea ebx, string1
     or
mov ebx, offset string1

my understanding is that they both do the same thing, am I wrong?
Thanks
Regards
God made Man, but the monkey applied the glue -DEVO

donkey

The result is the same but they are very different. LEA performs math on the address, MOV just moves the data. LEA is much slower and should only be used for LOCAL data or simple math functions. Even with local data, unless your final destination is memory, it is better to MOV offsets from EBP/ESP than to use LEA. The old forum had a discussion about this...

http://www.masmforum.com/viewtopic.php?t=1641&start=15
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jimh

Thanks Donkey... I'm going to go read that old post, too. My instruction set reference says that LEA, "Computes the effective address of the second operand" but doesn't say much about timing. Wiith that key word of, "computes" I'd suspect right there that LEA is slower than a flat MOV.

hutch--

rags,

Its a bit to do with the difference between a variable at an offset and a variable constructed on the stack at proc entry. An offset is known at assembly time where a dynamically created variable is only known while the proc is being run. While you will not see much difference between LEA and MOV in simple instances, you save a register load in many circumstances by using the offset rather than loading a register first.

You can use a GLOBAL variable created in the .DATA or .DATA? sections as a parameter for a function call where a stack variable must be loaded into a register first. This is why you see code like,


lea eax, var
push eax


in the middle of a function call if you bother to disassemble it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

rags

Thanks for the explanation Donkey and Hutch. I appreciate the help.
Regards,
God made Man, but the monkey applied the glue -DEVO