flat assembler
Message board for the users of flat assembler.

Index > Main > LEA confusing me

Author
Thread Post new topic Reply to topic
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 31 May 2011, 19:39
Code:
mov eax,1
lea eax,[eax+4]
; eax = 5    


if eax has 1, + 4, so eax = 5, ok. But loading as [xxx] it seams you are returning a value pointed for an address, and not the result itself, like in:

Code:
mov eax,1
mov eax,[eax+4]
; eax = any value at address 5    


for me this is more readable:

Code:
mov eax,1
lea eax,eax+4
; eax = 5    


get my point? Razz what do you think?

_________________
Sorry if bad english.
Post 31 May 2011, 19:39
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4334
Location: Now
edfed 31 May 2011, 19:52
i think the reason you think that is because you still not pointed that lea means load, and not move.

every load instructions in x86 uses a pointer to an adress as an operand.

for example, lgdt loads the content of gdtr pseudo descriptor in a ram location.
Code:
lgdt  [ebx+eax*8+sys.gdt]
    

for example, will use the conventionnal adress formula as an operand,

there are lea, lgdt, lidt, lldt, ltr, lsl, etc... all these instructions uses a memory operand.

and lea is just the Load Effective Adress, means exactlly what it does, it loads the effective adress using the 32 bits adress operand.
Code:
[segment : offset + pointer + index * scale ]
         : effective adress
    

the segment part is not included in the result of the adress calculation because the result will be relative to a segment.
Post 31 May 2011, 19:52
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 31 May 2011, 21:20

most often, I use "lea" to compute a "result",
and not to compute an address.
Code:
        lea     ebx,[(ecx*2) - 6]
   mul     ebx
    
I like this opcode, very convenient
and much more powerful than it appears at the first glance.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 31 May 2011, 21:20
View user's profile Send private message Send e-mail Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 31 May 2011, 22:12
now it makes more sense Smile

thanks
Post 31 May 2011, 22:12
View user's profile Send private message Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen 01 Jun 2011, 14:04
LEA is one of those weird instructions in x86 ISA. It uses source operand that has nothing to do with memory, it is just encoded as a memory operand.
Post 01 Jun 2011, 14:04
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.