Does using xor reg, reg give advantage over mov reg, 0?

This question already has an answer here:

  • What is the best way to set a register to zero in x86 assembly: xor, mov or and? 1 answer

  • an actual answer for you:

    Intel 64 and IA-32 Architectures Optimization Reference Manual

    Section 3.5.1.8 is where you want to look.

    In short there are situations where an xor or a mov may be preferred. The issues center around dependency chains and preservation of condition codes.


    I stopped being able to fix my own cars after I sold my 1966 HR station wagon. I'm in a similar fix with modern CPUs :-)

    It really will depend on the underlying microcode or circuitry. It's quite possible that the CPU could recognise "XOR Rn,Rn" and simply zero all bits without worrying about the contents. But of course, it may do the same thing with a "MOV Rn, 0" . A good compiler will choose the best variant for the target platform anyway so this is usually only an issue if you're coding in assembler.

    If the CPU is smart enough, your XOR dependency disappears since it knows the value is irrelevant and will set it to zero anyway (again this depends on the actual CPU being used).

    However, I'm long past caring about a few bytes or a few clock cycles in my code - this seems like micro-optimisation gone mad.


    x86 has variable-length instructions. MOV EAX, 0 requires one or two more bytes in code space than XOR EAX, EAX.

    链接地址: http://www.djcxy.com/p/28160.html

    上一篇: x86 uops是如何安排的?

    下一篇: 使用xor reg,reg优于mov reg,0吗?