TL:DR my main objection is that there's no scope for future good questions about LEA specifically. It's not an open-ended topic, not really. In assembly language, each instruction makes a specific change to the machine's architectural state. LEA can't even depend on memory contents, only GP integer registers. People just need to understand this, not read or ask a zillion questions about each different context you might use it in.
Questions about how to efficiently get from machine state A to machine state B might or might not have LEA as the answer, but are covered perfectly well by [tag:x86]
You can search for x86 LEA questions without a tag, either with google for site:stackoverflow.com x86 "lea"
, or on SO itself with [x86] code:lea
.
There's a problem here with lots of duplicate and near-duplicate questions, but I'm not convinced tags are best solution to the problem. The OP suggests that tagging them all will make it easier for future readers to wade through the pile, but there's so much duplication that I don't think that's useful. One to four good answers and the x86 manuals are all that's needed to explain LEA itself, and x86 addressing modes.
Links to such answers from the x86 tag wiki would help, but I wish SO had a better mechanism for showing new users that tag wikis exist and often have well-curated collections of links. Most new users, and many experienced users, don't know they exist!
I've already written (attempts at) canonical answers about LEA and also addressing modes. There is some agreement that at least the LEA answer is good. Now (as discussed in comments) the problem we have is of directing searches to a new good answer on a topic that already has lots of old highly-voted old answers. With an OP that's not been seen since Nov '09 on one of the highest-voted LEA questions (What's the purpose of the LEA instruction?), it's not going to get marked accepted if I posted a version of it there, so we have the classic SO problem that's been discussed many times on meta. (But usually not also with lots of duplicates with good answers on the lower-voted questions, too.)
Maybe tags are useful to catalogue the current mess, even if we don't want any future questions in those tags. But this is definitely now how SO should work in theory; real duplicates should be closed, not tagged with a subject. In theory there is no difference between theory and practice. In practice there is.
It's hard to merge because there are so many answers, and differences in x86-16 vs. x86-64 vs. 32-bit, and AT&T vs. NASM vs. MASM syntax make it look ugly (and confusing for beginners) to group multiple answers together with code like
lea dx, symbol
and leaq symbol(%rip), %rdx
, and lea eax, [symbol + edi]
.
I don't think tagging all the LEA questions so people can wade through them more easily is the Stack Overflow way of handling things. Maybe there's more value for beginners in all those different ways of repeating the same thing than I think there is; I'm not a beginner so I can't tell. (I understand that some people do find asm hard or confusing, but I really can't get inside their heads and understand what's so hard about creating steps for the CPU to follow that results in the computation you want.)
The main reason not to introduce an x86-lea tag is: There is basically no scope for asking a new question just about LEA that isn't already a duplicate. Creating a tag normally implies that there's room for an open-ended number of future questions about that topic. lea
could appear as part of the code in any number of questions about blocks of x86 code (debugging, reverse-engineering, or whatever), or how to efficiently implement something, but I think we all agree the tag shouldn't apply unless the question is mostly about lea
itself.
So there are about 2 or 3 total that aren't just duplicates. (How does its syntax work in asm source and what does it do? How to use it for multiplying integers? How it runs internally on AGUs vs. ALUs and with what latency / throughput and whether it's worth using 2 LEA vs. one imul eax, ecx, 37
. And maybe it's "intended" purpose, of pointer math vs. exposing the CPU capabilities for arbitrary use.)
We already have questions for all of those things. I don't see there being scope for any future good question specifically about lea
, except maybe performance tradeoffs on some past or future CPUs where it's different from Sandybridge-family or Bulldozer / Ryzen. But that would be more a question about that microarchitecture than about the instruction, IMO, and wouldn't need an x86-lea
tag.
myself included, struggle with concepts about specific Assembly instructions not just because they're intrinsically complex and ridden with side-effects
Huh? The manual for lea
documents the complete exact behaviour of LEA in all situations. If you found it complex, you were reading explanations like the ones that say "it's for addresses" which lead readers down the wrong path, confusing them when they see LEA used for integer math on non-pointers. It makes LEA sound more "special" than it is.
Or, it's not lea
that's complex, it's x86 addressing modes that are (somewhat) complex, and the syntax differences between different assemblers (MASM vs. NASM vs. GAS) is an added level of complexity. And the complexity of machine code decoding similarly in 16 / 32 / 64-bit mode, as you found on Disassembly shows LEA
with RIP?. That's not an LEA issue; you'd have had exactly the same issue with 32-bit mov eax, [disp32]
disassembled as mov eax, [RIP+rel32]
if interpreted as 64-bit machine code. (Of course you wouldn't have had it at all if people hadn't been going on about using lea
for 32-bit constants instead of the shorter and faster mov r32, imm32
.)
If the real reason for most questions about LEA isn't LEA itself, but some other aspects of x86, then I don't think having an x86-lea
tag helps us that much. Or we're on a slippery slope to having questions about every x86 instruction, and there are far too many of them for that to make sense. Intel already publishes nice x86 instruction-set manuals, and HTML versions of it exist, and see links in the x86 tag wiki.
I think you want LEA questions to be a way for people to learn about all the complexity you can encounter when using it. But some of the things, like addressing modes, can also come up when using them with other instructions.
Your idea of cataloguing all the x86-lea
questions so people can go through them all seems like you're creating a repetitive and not great quality book to teach x86 in general. Because to understand all the answers, you have to learn more than just LEA itself.
In my (not very humble at all) opinion :) most (but not all) of what's been written on SO about LEA is either bogus or covered in my answer on Using LEA on values that aren't addresses / pointers?, which says pretty much everything you need to know to understand LEA, when to use it, how it works, and why it's part of the instruction set in the first place. (In ~1100 words, formatted to be skimmable for the part you want. It's so long because I covered the evolution from 8086 to 386 to x86-64, and the evolution of addressing modes). Along with links to the ISA reference manual entry for lea
, and docs for addressing modes, that just about covers it. But it's still short enough to be a good dup target for most LEA questions; I tried (and probably only partly succeeded) not to get too bogged down in random details.
Beginners that think of asm as a compiled language with variables like C get very confused. I think some people don't understand that lea dx, symbol
has exactly the same effect on the architectural state as mov dx, OFFSET symbol
(a MASM syntax example that's come up at least once), except for advancing IP by 1 extra byte. Everything in asm is bits / bytes / integers, including pointers. Also including the bit-patterns that represent floating-point numbers.
I think the root cause of most of the lea
questions (and upvotes on them) is lack of understanding this, and / or failure to read the manual. Or the real question is about addressing-modes or assembler syntax, rather than the LEA machine instruction itself.
Other answers (on other questions) mostly say the same things in different ways. This is not necessarily a bad thing, as some people may grok ideas more easily the way others express them. But it's still duplication. (And the good is mixed with outdated or mis-information about performance, or claims I disagree with about the "intended" purpose of LEA, or bad advice like using lea dx, symbol
instead of mov dx, offset symbol
, because some people think of asm as a language of its own, rather than a way to express machine code).
I'm not such an egomaniac that I'm going to dup-hammer all the LEA questions as duplicates of my canonical answer, though. It's (IMO) unfortunately not the accepted answer on the question where it's posted, though, and the question uses AT&T syntax, so it's not a perfect dup target for many of the questions. Maybe I should rewrite a version of it with NASM syntax throughout and post it somewhere else, like maybe a new Q&A.
If other people agree with me, though, feel free to close-vote as duplicate (and upvote my canonical answer). I think that's the right move for most of them, but obviously I realize that I'm biased here. I know a lot of people like my answers, and Evan says he found that one very helpful, but self-promotion needs to have limits.
Given the limit of 5 tags per question, there sometimes isn't much room for super-specific tags if your question involves lea
or how/when to use it. [x86] [assembly] [micro-optimization] [performance] [x86-64]
already takes up all 5, and doesn't leave room for [gcc]
or [clang]
if asking about compiler code-gen choices, or whatever.
I think these tags are too specific, especially x86-cmp
and x86-test
. Maybe x86-condition-codes
would be more appropriate? I think the real question is usually what ja
vs. jl
means after cmp eax,0
or equivalently but better test eax,eax
, or about the test eax,eax
idiom itself. But that's one question; we don't need a tag, we need dup close votes. (I often do dup-hammer to that target, because it's a simpler question and less syntax-dependent.)
x64-lea
andx64-test
tags?x64
questions should already be tagged with.x86-64-lea
.Do NOT use this tag for Spring Boot. Use [spring-boot] instead.
. I stopped counting how many times I have had to clean up [boot], but if people even read the excerpt I wouldn't be doing the cleanup which is a daily task for me.