3 ms·
After reading https://news.ycombinator.com/item?id=29104841 https://news.ycombinator.com/item?id=29104841 today and wondering why they zero one of registers usi
by TheMatten 5y ago
After reading https://news.ycombinator.com/item?id=29104841 https://news.ycombinator.com/item?id=29104841 today and wondering why they zero one of registers using XOR on itself, it suddenly makes a lot of sense.
- commandlinefan 5y agoI asked why that was when I took assembler in college and the instructor didn't know (he said, "oh, um, it's faster"). I'm glad to finally find the actual correct answer to that question.
- monocasa 5y agoTo be fair, the choice of xor eax, eax is 'faster' than mov eax, 0. The xor option is only two bytes versus the five of mov, which can have I$ and decode width concerns. And back in the day, five bytes was pretty awkward for up to 32 bit data buses, where you'd need at least two cycles to even read the mov instruction. That's why existing code tended to use xor eax, eax in the first place, even before Intel started adding rename hardware to their processors.
- bonzini 5y agoSame for xor ax,ax on 16-bit processors by the way (2 bytes vs 3), and even earlier for the 8-bit 8080 (1 byte vs 2). The xor or sub trick dates to the late 70s.