<story><title>Things I learned while writing an x86 emulator (2023)</title><url>https://www.timdbg.com/posts/useless-x86-trivia/</url></story><parent_chain></parent_chain><comment><author>aengelke</author><text>Bonus quirk: there&amp;#x27;s BSF&amp;#x2F;BSR, for which the Intel SDM states that on zero input, the destination has an undefined value. (AMD documents that the destination is not modified in that case.) And then there&amp;#x27;s glibc, which happily uses the undocumented fact that the destination is also unmodified on Intel [1]. It took me quite some time to track down the issue in my binary translator. (There&amp;#x27;s also TZCNT&amp;#x2F;LZCNT, which is BSF&amp;#x2F;BSR encoded with F3-prefix -- which is silently ignored on older processors not supporting the extension. So the same code will behave differently on different CPUs. At least, that&amp;#x27;s documented.)&lt;p&gt;Encoding: People often complain about prefixes, but IMHO, that&amp;#x27;s by far not the worst thing. It is well known and somewhat well documented. There are worse quirks: For example, REX&amp;#x2F;VEX&amp;#x2F;EVEX.RXB extension bits are ignored when they do not apply (e.g., MMX registers); except for mask registers (k0-k7), where they trigger #UD -- also fine -- except if the register is encoded in ModRM.rm, in which case the extension bit is ignored again.&lt;p&gt;APX takes the number of quirks to a different level: the REX2 prefix can encode general-purpose registers r16-r31, but not xmm16-xmm31; the EVEX prefix has several opcode-dependent layouts; and the extension bits for a register used depend on the register type (XMM registers use X3:B3:rm and V4:X3:idx; GP registers use B4:B3:rm, X4:X3:idx). I can&amp;#x27;t give a complete list yet, I still haven&amp;#x27;t finished my APX decoder after a year...&lt;p&gt;[1]: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;sourceware.org&amp;#x2F;bugzilla&amp;#x2F;show_bug.cgi?id=31748&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;sourceware.org&amp;#x2F;bugzilla&amp;#x2F;show_bug.cgi?id=31748&lt;/a&gt;</text></comment>
<story><title>Things I learned while writing an x86 emulator (2023)</title><url>https://www.timdbg.com/posts/useless-x86-trivia/</url></story><parent_chain></parent_chain><comment><author>sdsd</author><text>What a cool person. I really enjoy writing assembly, it feels so simple and I really enjoy the vertical aesthetic quality.&lt;p&gt;The closest I&amp;#x27;ve ever come to something like OP (which is to say, not close at all) was when I was trying to help my JS friend understand the stack, and we ended up writing a mini vm with its own little ISA: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;gist.github.com&amp;#x2F;darighost&amp;#x2F;2d880fe27510e0c90f75680bfee86b29&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;gist.github.com&amp;#x2F;darighost&amp;#x2F;2d880fe27510e0c90f75680bfe...&lt;/a&gt;&lt;p&gt;This could have gone much deeper - i&amp;#x27;d have enjoyed that, but doing so would have detracted from the original educational goal lol. I should contact that friend and see if he still wants to study with me. it&amp;#x27;s hard since he&amp;#x27;s making so much money doing fancy web dev, he has no time to go deep into stuff. whereas my unemployed ass is basically an infinite ocean of time and energy.</text></comment>