2010/10/16

MOV reg,0 v.s. XOR reg,reg

To set any register to zero, better use 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:
Comparison between MOV and XOR instructions in Delphi BASM CPU View
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.

1 comment:

  1. Look at my experiment results
    http://www.devtech.cz/knowledge-base/paid-kb/advanced-programming-tips/compare-what-is-faster-mov-vs-xor-vs-add-and-their-long-operand-instructions/

    ReplyDelete