Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Follow publication

Difference between LEA and offset in assembly X86

--

This question arises in the mind of every student who studies assembly X86, what is the Difference between LEA and offset?

Well, you can use them to do the same thing but, LEA will always be better if the address is a little complex.

consider that you have the following array of chars:

ASC_TBL DB '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'

if you want to get the 6th element ‘5’ you would do something like this using offset:

mov ax, offset ASC_TBLadd ax, 5h; flags [PF] will be affected

On the other hand, if you are using the LEA the instruction you can simply use one instruction like this:

LEA ax, [ASC_TBL + 5h]; no flags are affected

Notice that:

  • Although using LEA proved that it has an advantage over using offset, it's more efficient to use offset if the address isn't very complex to write or both of them will do the same thing with the same number of instructions. The reason is that offset ASC_TBL is calculated during translation - like being be preprocessed- but, LEA is an actual processor instruction.
  • you can’t use the offset instruction to get the addresses that aren't known before the compilation time.

Stackademic 🎓

Thank you for reading until the end. Before you go:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Mostafa Wael

DevOps & Cloud engineer | Top 2% on StackOverflow @2022, @2023 | Helping Businesses Optimize Infrastructure.

No responses yet

Write a response