I have an assembly code which is 100 times these two instructions :
movl %eax, -16(%rbp)
movl -12(%rbp), %eax
that’s corresponding to this c code loop:
int i;
int a=5, b;
for (i=0 ; i < sptr->numberOfIterations ; i += 100){
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;
b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a;b=a; // 100 assignments
}
why does the operation of b=a; go to two instructions ? and how comes that I calculate the number of cycles it takes (each b=a;) is one cycle?
I compiled it with g++
You should give the compiler the chance to optimize the code. With the proper optimization flags
-O3 -march=nativemy compiler (gcc) is able to reduce all of that to the following line:no loop, repetition of the code, nothing.
So your compiler may or may not do optimizations and a lot of stuff that you don’t see. And processors are different. Mine here is able to put the constant 5 into an immediate and doesn’t create the variable
aat all.