http://landcareweb.com/questions/2993/yu-zi-ji-dui-ji-cun-qi-jin-xing-yi-huo-de-mu-de-shi-shi-yao-zhong-fu
问题:
xor eax, eax 永远都会设定 eax 零,对吧?那么,为什么MSVC ++有时会把它放在我的可执行代码中呢?是否更有效率 mov eax, 0?012B1002 in al,dx
012B1003 push ecx
int i = 5;
012B1004 mov dword ptr [i],5
return 0;
012B100B xor eax,eax
答案:
是的,效率更高。
操作码短于
mov eax, 0,只有2个字节,处理器识别特殊情况并将其视为一个 mov eax, 0 没有错误的读取依赖性 eax,所以执行时间是一样的。
参考:https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and
TL;DR summary:
xor same, same is the best choice for all CPUs. No other method has any advantage over it, and it has at least some advantage over any other method. It's officially recommended by Intel and AMD. In 64bit mode, still use xor r32, r32, because writing a 32-bit reg zeros the upper 32. xor r64, r64 is a waste of a byte, because it needs a REX prefix.
没有评论:
发表评论