LEA, has become my new favorite assembly language instruction.
But, I'm a novice and prone to incredibly ridiculous errors,...so,...I was wondering,....
When is it a really dumb idea to call LEA ???
Background Information:
The lea instruction places the address specified by its second operand into the register specified by its first operand. Note, the contents of the memory location are not loaded, only the effective address is computed and placed into the register. This is useful for obtaining a pointer into a memory region.
Syntax
lea <reg32>,<mem>
Examples
lea eax, [var] — the address of var is placed in EAX.
lea edi, [ebx+4*esi] — the quantity EBX+4*ESI is placed in EDI.
Here are some of the all-time great LEA threads from here at the MASM Forum:
I Don't See Difference Between mov eax, ebp and lea eax, [ebp], Aug 2011 (http://www.masm32.com/board/index.php?topic=17249.0)
Addresses and OllyDBG, Aug 2011 (http://www.masm32.com/board/index.php?topic=17150.0)
LEA, July 2011 (http://www.masm32.com/board/index.php?topic=16988.0),...This is probably the best discussion of LEA usage of all time and space,...
INVOKE and ADDR Directives, Apr 2011 (http://www.masm32.com/board/index.php?topic=16388.0)
Using ebp, Jun 2007 (http://www.masm32.com/board/index.php?topic=7536.0)
Quote from: baltoro on October 21, 2011, 07:18:16 PMWhen is it a really dumb idea to call LEA ???
... if additional comparison instructions must be used, whereas add/sub/shl,... returns the required information through the flags.
As qWord writes, sometimes you need the flags. Otherwise, lea is very useful, and on recent CPUs a lea eax, [eax+16] is not slower than its add eax, 16 equivalent. Note that lea eax, MyGlobalVar is one byte longer than mov eax, offset MyGlobalVar.
Also.... Using LEA in general conversation with children below the age of 4, bank workers and sheep often causes some confusion.... This is as far as my research has taken me so far however I will keep you updated :wink....
just look at the disassembly
if it is unnecessary to use LEA, it will look dumb :P
lea eax,401000h
lea eax,[esi]
Quote from: dedndave on October 21, 2011, 09:26:25 PM
if it is unnecessary to use LEA, it will look dumb :P
LEA eax, chr$("dont know what you are talking about? :lol")
If using LEA gets the results you want, its hard to do anything drastically wrong with it. You may end up with a sub-optimal instruction sequence and lose a picosecond here and there but that varies from one processor to another. If you are addressing a known OFFSET a MOV is usually more efficient but its not going to bring your computer to a stop if you do this.
Generally you try for preferred instructions that give you reasonably efficient code and stay away from the really old ones that only live in microcode as they are very uneven performers across different generations of processors. The Intel optimisation manual is very good here and Agner's manual is always worth a look.
QuoteYou may end up with a sub-optimal instruction sequence and lose a picosecond here and there
OH NO !!!! :eek
push dwBlasphemer
pop dwHutch
IF 0 ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
include \masm32\include\masm32rt.inc
dave PROTO ptxt:DWORD
.code
start:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
call main
inkey
exit
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
main proc
LOCAL davetxt[128]:BYTE
LOCAL pdave :DWORD
mov pdave, ptr$(davetxt)
cst pdave, chr$("Howdy Dave, but can you see the difference ",63)
invoke dave,pdave
print eax,13,10
ret
main endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
dave proc ptxt:DWORD
mov eax, rev$(rtrim$(ltrim$(lcase$(ucase$(rev$(ptxt))))))
ret
dave endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
end start
i don't get it :8)
i see some guy named "dave" mentioned a lot
i wonder if i know him :bg
Quote
When is it a really dumb idea to call LEA ???
Perhaps really dumb is a too much strong expression but :
When you know that an adress couldn't be modified by the linker use offset,it's faster.
This is valuable for data in the .data section (with perhaps a limitation , if you have multiple .data section).
But if you choose lea , it's not wrong (it's work each time) and it isn't really dumb.
The offset method don't work at each time,the linker reallocate some adress.
Quote from: ToutEnMasm on October 22, 2011, 07:23:45 AM
The offset method don't work at each time,the linker reallocate some adress.
Intereresting - can you post a working example?
It depends on your CPU, but MOV and ADD are generally 'faster' (i.e. better throughput). They utilize less of the CPU, which lets *other* instructions execute in parallel, so your entire program runs quicker on the whole. On my Core2, for instance, MOV and ADD can go to either port 0, 1, or 5, but LEA must go to port 0. It obviously depends on the operands and the surrounding instructions, but basically three MOV's take one clock cycle, whereas three LEA's take three.
-r
Thanks to everyone,...for some one like me, who is a novice assembly programmer,...this is useful information.