Skip to content
https://abc.microfintool.com/

Mobile & Accessories

Smartphone Reviews, Specs, Comparisons & Electronics Guides

  • Welcome to ABC Tool: Your Ultimate Portal for Smartphones and Accessories
  • PRIVACY POLICY
  • Blog
  • Contact
Sure, xor’ing a register with itself is the idiom for zeroing it out, but why not sub?

Sure, xor’ing a register with itself is the idiom for zeroing it out, but why not sub?

Posted on April 22, 2026October 10, 2026 By Muhammad Safdar No Comments on Sure, xor’ing a register with itself is the idiom for zeroing it out, but why not sub?
Blog

Everything you need to know about sure xor ing register with itself.

[ad_1]

Matt Godbolt, probably best known for being the proprietor of Compiler Explorer, wrote a brief article on why x86 compilers love the xor eax, eax instruction.

The answer is that it is the most compact way to set a register to zero on x86. In particular, it is several bytes shorter than the more obvious mov eax, 0 since it avoids having to encode the four-byte constant. The x86 architecture does not have a dedicated zero register, so if you need to zero out a register, you’ll have to do it ab initio.

But Matt doesn’t explain why everyone chooses xor as opposed to some other mathematical operation that is guaranteed to result in a zero? In particular, what’s wrong with sub eax, eax? It encodes to the same number of bytes, executes in the same number of cycles. And its behavior with respect to flags is even better:

  xor eax, eax sub eax, eax
OF clear clear
SF clear clear
ZF set set
AF undefined clear
PF set set
CF clear clear

Observe that xor eax, eax leaves the AF flag undefined, whereas sub eax, eax clears it.

I don’t know why xor won the battle, but I suspect it was just a case of swarming.

In my hypothetical history, xor and sub started out with roughly similar popularity, but xor took a slightly lead due to some fluke, perhaps because it felt more “clever”.

When early compilers used xor to zero out a register, this started the snowball, because people would see the compiler generate xor and think, “Well, those compiler writes are smart, they must know something I don’t. Since I was on the fence between xor and sub, this tiny data point is enough to tip it toward xor.”

The predominance of these idioms as a way to zero out a register led Intel to add special xor r, r-detection and sub r, r-detection in the instruction decoding front-end and rename the destination to an internal zero register, bypassing the execution of the instruction entirely. You can imagine that the instruction, in some sense, “takes zero cycles to execute”. The front-end detection also breaks dependency chains: Normally, the output of an xor or sub is dependent on its inputs, but in this special case of xor‘ing or sub‘ing a register with itself, we know that the output is zero, independent of input.

Even though Intel added support for both xor-detection and sub-detection, Stack Overflow worries that other CPU manufacturers may have special-cased xor but not sub, so that makes xor the winner in this ultimately meaningless battle.

Once an instruction has an edge, even if only extremely slight, that’s enough to tip the scales and rally everyone to that side.

Bonus chatter: One of my former colleagues was partial to using sub r, r to zero a register, and when I was reading assembly code, I could tell that he was the author due to the use of sub to zero a register rather than the more popular xor.

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.


[ad_2]
Source link

More on sure xor ing register with itself

Stay tuned for updates on sure xor ing register with itself as the story develops. Related: Lars Andersen (@LarsAnders1620) on X.

Frequently Asked Questions

Why do x86 compilers use xor eax, eax?

It is the most compact way to zero a register on x86, several bytes shorter than mov eax, 0 since it avoids encoding a four-byte constant.

What is the difference between xor and sub for zeroing?

Both encode the same size and run in the same cycles, but xor leaves the AF flag undefined while sub clears it; both clear OF, SF, CF and set ZF and PF.

Why did xor win over sub?

The author suspects it was swarming: xor took an early lead, perhaps because it felt more clever, and early compilers copying each other locked it in.

Views: 49

Post navigation

❮ Previous Post: Motorola Razr 2026, Razr+ 2026, and Razr Ultra 2026 US prices leak
Next Post: Silo S3 teaser hints at the wasteland’s origins ❯

You may also like

The Galaxy S27 Pro might bring a Pro-tier camera setup after all
Blog
The Galaxy S27 Pro might bring a Pro-tier camera setup after all
May 21, 2026
Stop Killing Games fails to secure EU law despite 1.3M signatures
Blog
Stop Killing Games fails to secure EU law despite 1.3M signatures
June 17, 2026
Best Streaming Services of 2026
Blog
Best Streaming Services of 2026
May 5, 2026
iOS 26: Here’s Everything You Need to Know About the iPhone Software
Blog
iOS 26: Here’s Everything You Need to Know About the iPhone Software
April 14, 2026

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • OnePlus 16 Launch Date, Price, Specs: Everything Confirmed for 2026
  • Best 5G Phones 2026 for USA, UK & Europe (Every Budget)
  • Best Foldable Phones 2026: Z Fold 8 vs Pixel 11 Pro Fold vs Razr
  • Google Pixel 10 Review (2026): Still Worth $799?
  • iPhone 16 vs iPhone 17: Should You Upgrade in 2026? (Honest Answer)

Recent Comments

  1. I Hiked Using Robot Legs in the Grand Canyon. I Didn’t Even Need My Cane - Mobile & Accessories on Can Using the Hypershell Exoskeleton on a Bike Replace an E-Bike? I Tested It to Find Out
  2. The media starts receiving the first Trump Mobile T1 Phone units, launch is imminent - Mobile & Accessories on The Competitive Moat That AI Can’t Replicate
  3. Samsung’s giant The Frame Pro TV is $1,500 off until midnight - Mobile & Accessories on All Your Wearables Have One Glaring Weakness. What Can You Do About It?
  4. iPhone 18: Everything We Know About Apple's Most Ambitious Lineup Yet - Mobile & Accessories on This magnetic Qi 2.2 power bank is perfect for the iPhone 17 and it’s on sale right now
  5. Do Camera Sensor Sizes Matter? - Mobile & Accessories on Xiaomi 18 Pro set for a screen upgrade that Samsung fans will be very familiar with

Archives

  • October 2026
  • June 2026
  • May 2026
  • April 2026

Categories

  • Blog
  • Mobiles

Copyright © 2026 Mobile & Accessories.

Theme: Oceanly News by ScriptsTown