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?
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.
Not the answer you are looking for? Search for more explanations.
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.