NASM Forum > Programming with NASM

Whats a practical use for lea

(1/1)

gygzkunw:
I know that lea is a shift-addition instruction and lets you load the address of an effective address. I also know it doesn't affect the flag register . But i still cant come up with a practical purpose for why i should use lea when i can use mov. Please i am assembly noob, try and keep it single.

fredericopissarra:
1. You can use to do simple arithmetic calculations:


--- Code: ---; int f( int x ) { return 5 * x; }
f:
  lea eax,[rdi+rdi*4]
  ret
--- End code ---

2. You can use to precalculate a RIP relative address (x86-64):


--- Code: ---  lea rax,[rel var]   ; You can get rid of 'rel' here if rip relative addressing is default.

--- End code ---

3. You can get the address of an element in an array:


--- Code: ---  ; Supose RCX is the index of an array of ints:
  lea rsi,[array+rcx*4]   ; rsi now points to array[rcx]

--- End code ---

Elawig57:
I just want to know about How is Lea used in the classroom? Optimum Mail

fredericopissarra:

--- Quote from: Elawig57 on January 06, 2023, 08:36:40 AM ---I just want to know about How is Lea used in the classroom?

--- End quote ---
And I gave you 3 uses...

Frank Kotler:
LEA uses the classroom to teach itself to students so the students can use it outside of the classroom.

What's really on your mind, Elawig57?

Frank

Navigation

[0] Message Index

Go to full version