4 ms·
LEA (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 m
by efaref 10y ago
LEA (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)