XOR'ing a register with itself is the idiom for zeroing it out. Why not sub?
by ingve on 4/22/2026, 6:38:05 AM
https://devblogs.microsoft.com/oldnewthing/20260421-00/?p=112247
Comments
by: Sweepi
"Bonus bonus chatter: The xor trick doesn’t work for Itanium because mathematical operations don’t reset the NaT bit. Fortunately, Itanium also has a dedicated zero register, so you don’t need this trick. You can just move zero into your desired destination."<p>Will remember for the next time I write asm for Itanium!
4/22/2026, 7:55:59 AM
by: b1temy
Back when I was in university, one of the units touching Assembly[0] required students to use subtraction to zero out the register instead of using the move instruction (which also worked), as it used fewer cycles.<p>I looked it up afterwards and xor was also a valid instruction in that architecture to zero out a register, and used even fewer cycles than the subtraction method; but it was not listed in the subset of the assembly language instructions we were allowed to use for that unit. I suspect that it was deemed a bit off-topic, since you would need to explain what the mathematical XOR operation was (if you didn't already learn about it in other units), when the unit was about something else entirely- but everyone knows what subtraction is, and that subtracting a number by itself leads to zero.<p>[0] Not x86, I do not recall the exact architecture.
4/22/2026, 8:42:43 AM
by: nopurpose
It amazes me how entertaining Raymond's writing on most mundane aspects of computing often is.
4/22/2026, 7:26:19 AM
by: drfuchs
Relatedly, there's a steganographic opportunity to hide info in machine code by using "XOR rax,rax" for a "zero" and "SUB rax,rax" for a "one" in your executable. Shouldn't be too hard to add a compiler feature to allow you to specify the string you want encoded into its output.
4/22/2026, 8:23:59 AM
by: enduku
I ran into this rabbithole while writing an x86-64 asm rewriter.<p>xor was the default zeroing idiom.I onkly did sub reg,reg when I actually want its flags result. Otherwise the main rule is: do not touch either form unless flags liveness makes the rewrite obviously safe. Had about 40 such idioms for the passes.
4/22/2026, 8:38:35 AM
by: NewCzech
The obvious answer is that XOR is faster. To do a subtract, you have to propagate the carry bit from the least-significant bit to the most-significant bit. In XOR you don't have to do that because the output of every bit is independent of the other adjacent bits.<p>Probably, there are ALU pipeline designs where you don't pay an explicit penalty. But not all, and so XOR is faster.<p>Surely, someone as awesome as Raymond Chen knows that. The answer is so obvious and basic I must be missing something myself?
4/22/2026, 7:44:06 AM
by: tliltocatl
It might be because XOR is rarely (in terms of static count, dynamically it surely appears a lot in some hot loops) used for anything else, so it is easier to spot and identify as "special" if you are writing manual assembly.
4/22/2026, 7:56:34 AM
by: empiricus
The hw implementation of xor is simpler than sub, so it should consume slightly less energy. Wondering how much energy was saved in the whole world by using xor instead of sub.
4/22/2026, 8:01:07 AM
by: anematode
My favorite (admittedly not super useful) trick in this domain is that <i>sbb eax, eax</i> breaks the dependency on the previous value of <i>eax</i> (just like <i>xor</i> and <i>sub</i>) and only depends on the carry flag. arm64 is less obtuse and just gives you <i>csetm</i> (special case of <i>csinv</i>) for this purpose.
4/22/2026, 7:49:10 AM
by: defrost
<p><pre><code> Once an instruction has an edge, even if only extremely slight, that’s enough to tip the scales and rally everyone to that side. </code></pre> And this, interestingly, is why life on earth uses left-handed amino acids and right-handed sugars .. and why left handed sugar is perfect for diet sodas.
4/22/2026, 7:53:32 AM
by: jhoechtl
Back in the stone ages XOR ing was just 1 byte of opcode. Habbits stick. In effect XORing is no longer faster since a long time.
4/22/2026, 8:05:38 AM
by: rasz
Looking at some random 1989 Zenith 386SX bios written in assembly so purely programmer preferences:<p>8 'sub al, al', 14 'sub ah, ah', 3 'sub ax, ax'<p>26 'xor al, al', 43 'xor ah, ah', 3 'xor ax, ax'<p>edit: checked a 2010 bios and not a single 'sub x, x'
4/22/2026, 7:56:46 AM
by: jdw64
[dead]
4/22/2026, 8:29:05 AM
by: grebc
If you’re not first, you’re last.
4/22/2026, 7:35:04 AM