News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

xor eax, eax

Started by wossname, November 20, 2005, 06:47:54 PM

Previous topic - Next topic

wossname

Why do I keep seeing xor eax, eax everywhere?

I can only imagine that it is a quick way of setting the eax register to 0, but is it faster than mov eax, 0?

Why is this not documented anywhere, and is there a list of other simple optimisation tricks?

Can someone shed any light on this?

Ghirai

It's also smaller.

And XOR is fully documented, but some don't see other uses for it :toothy
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

wossname

Ahh ok.  (I meant that this trick wasn't documented rather than the opcode itself ;)).


liquidsilver

It's simple why it's smaller: mov eax,0 has to store the full 0 value (4 bytes) and an opcode, whereas xor eax,eax just has to store an opcode. Compile it and then you'll see the the mov instruction is much longer.

liquidsilver

Here's another optimization leading from this:
xor eax,eax
inc eax
instead of: mov eax,1
and this works for a while, also think dec>0xFF

Tedd

"xor eax,eax; inc eax" takes fewer bytes, but is generally slower than "mov eax,1"

Many of these 'tricks' will come to you as you learn more asm - xor eax,eax is so common that it will not take long before you see it.

One more for you: "test eax,eax" to check eax for zero, instead of "cmp eax,0"
No snowflake in an avalanche feels responsible.

diablo2oo2


JHER VON ARBANEL

for example xor eax,eax------->mov eax,0
   or eax,eax------------------------>cmp eax,0
  and eax,0fh--------------------->11111111;eax
                                          00001111
                                         ---------------
                                          00001111; u use it like a mask
test eax,0fh ------------------------->same as and but without eax dont modify
u will dicover more shorcuts while u are programing ...  keep working