Pointer arithmetic? Which is used for well, ... many things.
Pointer arithmetic? Which is used for well, ... many things.
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,%raxAlso Zen of Assembly mentions that LEA can store its result in any register and doesn't alter flags.
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)It happens to do adds and multiplications as well.
I understand things like a[i] would be equivalent in general to *(a+i)
Isn't LEA intended to calculate addresses? It seems it also doesn't alter flags and can put its result in any register.
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).
Which I assumed to be equivalent to something like: res = ptr_base + offset