Dumb newbie question about LEA instruction
I'm looking at some Visual C++ generated code (trying to work out why WinDbg won't display member variables properly). In the code I see -
lea ecx,DWORD PTR[edi+12]
Could somebody explain what this does? I know I could plough through the Intel Assembler manual, but I was hoping for a quick explanation just to get my head around the assembler semantics :) The context demands that we just load a 4 byte address from wherever EDI+12 points to and put that address into ECX.
How would this differ from a
mov ecx,DWORD PTR[edi+12]
Apologies for being dumb.
Brendan
[636 byte] By [
bullman] at [2007-11-20 10:54:18]

# 1 Re: Dumb newbie question about LEA instruction
Hello Brendan,
lea ecx,DWORD PTR[edi+12]
sets ECX to the sum EDI+12 to ECX. So if EDI=1, ECX would be set to 13.
With
mov ecx,DWORD PTR[EDI+12]
four bytes starting from address EDI+12 are loaded into ECX.
Your lines:
The context demands that we just load a 4 byte address from wherever EDI+12 points to and put that address into ECX.
Key part is "wherever EDI+12 points to". Unless I got the wrong idea, we really don't mind that EDI+12 points to an address (and maybe that was confusing in your context); we are just interested in copying contents of location EDI+12 into ECX. Then you should code
mov ecx,DWORD PTR[EDI+12]
Let me know if I explained myself.
Best regards,
Iaki Viggers
# 2 Re: Dumb newbie question about LEA instruction
Inaki -
Crystal clear, thank-you.
It was the [] that were fooling me - I expected, from them, that the instruction would fetch the contents of wherever EDI+12 was pointing. But I guess the LEA instruction effectively(!) says "stop, do not load from memory, just use the address that you would have fetched from had this been (e.g.) a MOV". Could one code the LEA without the [], or would that generate an assembly error?
I'll take another look at the generated code - guess I overshot a level of indirection somewhere.
Thanks.
Brendan
# 3 Re: Dumb newbie question about LEA instruction
Actually LEA is suppossed to mean Load Effective Address, and certainly the combination of [] with LEA is confusing in the beginning.
You can code LEA without brackets, but there are slight differences between assemblers like TASM or NASM.
Using TASM (switch to 16-bit code),
LEA cx,WORD PTR[label] ;;;can't remember right now if you need here
;;; OFFSET keyword inside brackets
and
MOV cx, OFFSET label
have the same effect: CX is set to address of variable "label".
On the other hand, NASM doesn't use keyword OFFSET.