Wednesday 25 October 2017

assembly - What is the purpose of XORing a register with itself?





xor eax, eax will always set
eax to zero, right? So, why does MSVC++ sometimes put it in my
executable's code? Is it more efficient that 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




Also, what
does it mean to do in al, dx?



Answer




Yes, it is more
efficient.



The opcode is shorter than
mov eax, 0, only 2 bytes, and the processor recognizes the
special case and treats it as a mov eax, 0 without a false read
dependency on eax, so the execution time is the
same.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...