Why does my assembly have the line "mov $0x0, %eax" when optimization is off, and "xor %eax, %eax" when optimization is on? Also, what does "push %ebp\nmov %esp, %ebp" do?
whats %eax?
I suppose it's because xor %eax %eax puts a zero in %eax a few cycles faster than a mov.
push ebp would put ebp to the stack. Mov esp ebp copies ebp to esp.
The xor can be faster because it won't use a constant as source. Today they'll likely be equally fast on most CPUs, but the xor can produce smaller code (the 0 for the mov has to be stored in the binary, afterall). There are however cases where the xor can be slower - it can introduce a register dependency that may lead to the xor instruction stalling - If another instruction used the register in question as destination before, the xor may stall until that prior result is written to the register and can be used for the xor instruction. The optimizer should be able to figure this one out on a case by case basis - my guess is that you'll see mov and xor used both in different places of compiled code (I'm assuming this is C compilate?), depending on the circumstances.
Join our real-time social learning platform and learn together with your friends!