What purpose does xor eax,eax serve?
Maybe I need to brush up on my logical operators...but this one seems pointless.
Could someone here please enlighten me?
Posted on 2002-12-01 03:08:54 by frep45
xor eax,eax = mov eax,0 = sub eax,eax
but xor eax,eax is faster
Posted on 2002-12-01 03:28:16 by greenant
You need only two bytes to zero eax.
Posted on 2002-12-01 03:38:50 by Four-F
xor eax, eax isn't faster. As four-f said it's a size optimization tip only
Posted on 2002-12-01 03:40:41 by Axial

What purpose does xor eax,eax serve?
Maybe I need to brush up on my logical operators...but this one seems pointless.
Could someone here please enlighten me?
xor is based upon this logic:

1 xor 1 = 0
0 xor 0 = 0

1 xor 0 = 1
0 xor 1 = 1


so basically if you have a binary number like this
    11110000001111110011

xor 11110000001111110011
________________________
you'll get 0 if you xor yourself.

:)
Posted on 2002-12-01 03:43:35 by stryker
also remember that xor eax,eax will affect the flags; if you don't want to affect them then use mov eax,0

-stormix
Posted on 2002-12-01 07:33:52 by stormix
you'll get 0 if you xor yourself.

I xor'd myself once.
Posted on 2002-12-01 08:10:22 by drhowarddrfine
Can a register be cleared without effecting flags, and without using MOV?

; 3 bytes
push 0
pop eax
Posted on 2002-12-01 10:55:49 by bitRAKE
"Setting a register to zero with the XOR method doesn't

break its dependency on earlier instructions:
DIV EBX
MOV [MEM], EAX[B]
MOV EAX, 0 ; break dependency
XOR EAX, EAX ; prevent partial register stall[/B]
MOV AL, CL
ADD EBX, EAX
Setting EAX to zero twice here seems redundant, but without
the MOV EAX,0 the last instructions would have to wait for the
slow DIV to finish, and without XOR EAX,EAX you would have a
partial register stall. "

from "How to optimize for the Pentium family of microprocessors"
by Agner Fog
Posted on 2002-12-01 12:22:37 by lingo12
Both the Athlon and P4 break dependancies on XOR reg, reg.
Posted on 2002-12-01 12:38:38 by bitRAKE
The instruction to use to clear eax depends on the CPU you are optimizing for ( 386, 586, 686 ) because different CPU have different architecture
Posted on 2002-12-01 14:25:49 by greenant

Both the Athlon and P4 break dependancies on XOR reg, reg.
About SUB reg,reg that some coders prefer to use instead beware.. the P4 break dependencies on it, the Athlon not.
Posted on 2002-12-01 16:50:08 by Maverick
xor eax, eax is smaller but not necessarily faster, the range of suggestions here seem to cover the alternatives but sometimes the size difference works for you depending on the loop code it is used in.

(xor reg, reg) or (sub reg, reg) are both Intel specific optimisations when using a register for both BYTE and DWORD operations to prevent a stall.

The size gain is trivial and very rarely ever changes the final EXE size, the main proponents of absolute minimum size instructions are people who want to write these instructions to someone elses code and those people are no popular around here.

Regards,

hutch@movsd.com
Posted on 2002-12-01 18:02:36 by hutch--
I use XOR reg,reg all the time to zero a register out.
Posted on 2002-12-01 19:41:39 by x86asm

The size gain is trivial and very rarely ever changes the final EXE size, the main proponents of absolute minimum size instructions are people who want to write these instructions to someone elses code and those people are no popular around here.
I happen to be working on a boot sector right now - not much space - I'd rather not have several layers of boot strapping, and support Floppy/HD/CD booting as well as 8+GB partitions with various BIOS extensions. I really don't feel like using an existing one. :) Currently, the only error message is, "Error!" - I need those bytes for more code. :grin:
Posted on 2002-12-01 19:45:07 by bitRAKE
Rickey,

Just make the boot sector larger, limitations of floppies should not be inflicted on other storage devices as they are just about past it anyway.

Boot error messages in glowing technicolor !!!!

If you made the boot sector 1 meg as a standard, you could convince your users to abandon floppies even faster. :)

Regards,

hutch@movsd.com
Posted on 2002-12-01 22:26:37 by hutch--
Yeah, I should write my own BIOS. :P
Posted on 2002-12-01 22:38:44 by bitRAKE
(Slightly out of topic)

hutch said:

----

The size gain is trivial and very rarely ever changes the final EXE size, the main proponents of absolute minimum size instructions are people who want to write these instructions to someone elses code and those people are no popular around here.

----

Hutch, don't forget all the 'demo-sceners' making small 4k demos, and even 256 bytes demos!
In such application every byte would count.
Just go to http://www.256b.com/ or http://www.pouet.net and check.
It's quiet a lot of people involved in it.
Posted on 2002-12-01 22:52:44 by david