Author Topic: Whats a practical use for lea  (Read 7344 times)

Offline gygzkunw

  • Jr. Member
  • *
  • Posts: 10
Whats a practical use for lea
« on: February 01, 2021, 05:13:56 PM »
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.


Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Whats a practical use for lea
« Reply #1 on: February 01, 2021, 08:40:22 PM »
1. You can use to do simple arithmetic calculations:

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

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

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

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

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

Offline Elawig57

  • Jr. Member
  • *
  • Posts: 5
Re: Whats a practical use for lea
« Reply #2 on: January 06, 2023, 08:36:40 AM »
I just want to know about How is Lea used in the classroom? Optimum Mail
« Last Edit: January 09, 2023, 05:22:35 AM by Elawig57 »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Whats a practical use for lea
« Reply #3 on: January 06, 2023, 10:45:25 AM »
I just want to know about How is Lea used in the classroom?
And I gave you 3 uses...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Whats a practical use for lea
« Reply #4 on: January 06, 2023, 04:00:18 PM »
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