the experiment is on
I am doing some static binary instrumentation work, and basically I am trying to insert some instructions below to the beginning of every basic block.
BB23 : push %eax movl index,%eax movl $0x80823d0,buf(,%eax,0x4) add $0x1,%eax cmp $0x400000,%eax jle BB_23_stub movl $0x0,%eax BB_23_stub:movl %eax,index pop %eax
Note that I need to use
cmp instruction, and in order to guarantee that
flags can restore to the original value, I use
popf to store\load
flags on the stack.
Then it becomes this:
BB_23 : push %eax pushf movl index,%eax movl $0x17,buf(,%eax,0x4) add $0x1,%eax cmp $0x400000,%eax jle BB_23_stub movl $0x0,%eax BB_23_stub:movl %eax,index popf pop %eax
I tested the performance with and without
popf (I am using
bzip). And to my surprise, performance penalty could increase even 3 times after using the
popf. The compression results of
bzip are incorrect.
So here is my question:
Why pushf and popf so slow? Am I using it in a correct way?
I cannot afford too much performance penalty introduced by pushf and popf. Is there any way I can avoid the high overhead and also keep the correct semantics? (protecting the value in flags, basically..)
Am I clear enough? Could anyone give me some help?