flat assembler
Message board for the users of flat assembler.

Index > Main > Interesting notes from Matt Godbolt

Author
Thread Post new topic Reply to topic
Mаt Quasar



Joined: 29 Jun 2025
Posts: 51
Mаt Quasar 05 Dec 2025, 09:19
Matt Godbolt wrote:
Unlike other partial register writes, when writing to an e register like eax, the architecture zeros the top 32 bits for free. So xor eax, eax sets all 64 bits to zero.


How so?

He says nobody uses "xor rax,rax", just "xor eax,eax" is enough even for 64-bit.

https://xania.org/AoCO2025 (Advent of Compiler Optimisations 2025)

Quote:
This December will be the Advent of Compiler Optimisations: I’ll release one blog post and video each day, each detailing a fun and interesting C or C++ optimisation that your compiler can do. I’ll go into the details of when it applies, how to interpret the assembly, and perhaps as importantly, when it doesn’t apply.

I’ll be covering some very low-level, architecture-specific tricks as well as larger, more high-level optimisations. While I mostly cover x86-64, I do touch on 64-bit and 32-bit ARM as well.
Post 05 Dec 2025, 09:19
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8489
Location: Kraków, Poland
Tomasz Grysztar 05 Dec 2025, 09:46
Mаt Quasar wrote:
Matt Godbolt wrote:
Unlike other partial register writes, when writing to an e register like eax, the architecture zeros the top 32 bits for free. So xor eax, eax sets all 64 bits to zero.


How so?
Back when fasm was one of the early assemblers supporting x86-64 architecture I had to answer these kinds of questions repeatedly. Sometimes I had additional insight (and years later I also wrote about it in my article for Paged Out! #1). All kinds of these discussions happened in 2006 here.

In short:
Tomasz Grysztar wrote:
...any instruction that targets the 32-bit register, clears the upper 32 bits of the 64-bit register that contains it. This is a general rule, even the "xchg eax,eax" will clear the upper 32 bits of RAX register.

I also recall that I needed to ask someone with early access to actual AMD64 hardware to test some instruction encodings that I had doubts about when reading the initial versions of the manual. Among them was the test of NOP in long mode, because previously NOP was just understood to be the same as "xchg eax,eax", but in the long mode such XCHG instruction is no longer a no-operation, as it clears the high bits of RAX!
Post 05 Dec 2025, 09:46
View user's profile Send private message Visit poster's website Reply with quote
Mаt Quasar



Joined: 29 Jun 2025
Posts: 51
Mаt Quasar 05 Dec 2025, 09:55
Thanks @Tomasz! I am late comer! So this is no mystery. It is silly I use mov rax, SYSCALL_NO in my hexdump for Linux x64.
Post 05 Dec 2025, 09:55
View user's profile Send private message Visit poster's website Reply with quote
Mаt Quasar



Joined: 29 Jun 2025
Posts: 51
Mаt Quasar 05 Dec 2025, 10:14
Tomasz Grysztar wrote:
(and years later I also wrote about it in my article for Paged Out! #1).


Noted with thanks. PDF extracted for anyone's convenience.


Description: Multi-bitness x86 code
Download
Filename: 6_PDFsam_PagedOut_001_beta1.pdf
Filesize: 73.05 KB
Downloaded: 7 Time(s)

Post 05 Dec 2025, 10:14
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.