5 ms·
> but I have no clue why there are so many lea everywhere. Pointer arithmetic? Which is used for well, ... many things.
by rdtsc 10y ago
> but I have no clue why there are so many lea everywhere.
Pointer arithmetic? Which is used for well, ... many things.
- JoeAltmaier 10y agoPointer creation I think.
- rdtsc 10y agoOriginally it was for address calculations it seems. But can also be used to do other calculations not just for addresses. Which I assumed to be equivalent to something like: res = ptr_base + offset
- rwmj 10y agolea is used to implement addition in OCaml: http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/e86a25aa6c6a6a7d08dd7eb50cfd5d52.fr.html http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/e86a2...
- dibanez 10y agoYea, I've seen compilers use lea for some integer math in C/C++, because it is handled by a different part of the CPU and so can happen in parallel with other integer math ops.
- rwmj 10y agoPeople might say this, but it's not really true. All these instructions are decomposed into micro-ops, and then the micro-ops are run in parallel - if data dependencies allow that - on a common pool of integer ALUs. The reason to use lea is for code compression - it allows you to express two or three operations in a single instruction.
- cperciva 10y agoOn some Intel CPUs there are separate address-computation execution units.
- rainforest 10y agoIsn't LEA also a quick single instruction multiply-add for fixed scales in the SIB? e.g. `LEA EAX, EAX*4 + 0x3`
- efaref 10y agoLEA (load effective address) can perform computations of the form BASE + SCALE * INDEX + OFFSET, where scale can be 1, 2, 4 or 8. This allows optimisation of multiplications and additions into a single instruction, and the compiler takes advantage of that. So if you write: a = 4 * b + c + 10; It will be optimised to a single instruction like: lea 0xa(%rsi,%rdi,4),%rax Rather than the more naive: imul $0x4,%rdi,%rax add %rsi,%rax add $0xa,%rax
- rdtsc 10y agoRight, that's what I mean by pointer arithmetic -- specialized instruction for calculating memory addresses. It seems it can be co-opted to do math and other calculations as well. But at least that was its intended use? Also Zen of Assembly mentions that LEA can store its result in any register and doesn't alter flags.
- efaref 10y agoYes, I think it was designed in particular for determining addresses of fields within structures and variables on the stack, for example if you had: struct foo { int field1; int field2[10]; } Then an access like: fooptr->field2[index] Would compile to: fooptr + sizeof(int) * index + offsetof(struct foo, field2) Which is: lea $4(fooptr,index,4)
- Someone 10y agoThe lea instruction is designed for array indexing. See http://stackoverflow.com/a/1665570 http://stackoverflow.com/a/1665570
- rdtsc 10y agoThat's what I mean by pointer arithmetic. It seems it was intended to calculate memory addresses. It happens to do adds and multiplications as well. I understand things like a[i] would be equivalent in general to *(a+i)
- valleyer 10y agoYour username is an effective proof that the answer to the title question is "no". :)
- rdtsc 10y agoSorry didn't get the joke, how does it interact with reading the timestamp counter? Isn't LEA intended to calculate addresses? It seems it also doesn't alter flags and can put its result in any register.
- valleyer 10y agoThe title question, not anything to do with lea. I at least have never seen a compiler emit a rdtsc. (If there is some case where it does [presumably a benchmarking mode?] I'd be interested to hear of it.)
- rdtsc 10y agoHeh ;-) I see. Hmm, can't see it being emitted unless used in profiling code. But then as an inline assembly. Now rdtscp (notice the "p" at the end) is a fencing instruction as well! So I can see someone using that for barriers or when doing serialization. (Or I guess if a language somehow include getting a fast spinning counter as a feature that could end up as rdtsc* instruction).