XOR reg, reg
instead of MOV reg, 0
.Why? Two most important reasons in code optimization: code size and execution speed.
Let's see those two instruction after assembled into machine code:

XOR EAX, EAX
only use 2 bytes, while MOV EAX, 0
consumes 5 bytes (duh)...For speed consideration,
MOV
takes an immediete value that need to be read from memory (RAM) to be fetched into internal processor cache. XOR using no external memory (from processor point of view), thus speed can be gained much faster.
Look at my experiment results
ReplyDeletehttp://www.devtech.cz/knowledge-base/paid-kb/advanced-programming-tips/compare-what-is-faster-mov-vs-xor-vs-add-and-their-long-operand-instructions/