{"id":1216,"date":"2006-03-18T01:10:21","date_gmt":"2006-03-18T09:10:21","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=6"},"modified":"2006-03-18T01:10:21","modified_gmt":"2006-03-18T09:10:21","slug":"how-nop-nearly-became-a-non-nop-on-amd64","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=1216","title":{"rendered":"How NOP nearly became a non-NOP on AMD64"},"content":{"rendered":"<p>AMD64 is a quite clean extension of the i386 instruction set, it obsoletes many rarely used features of the i386 and introduces new registers, making the instruction set a lot more&#8230; logical.<\/p>\n<p>But there is one feature, actually a nice trick of the 8086\/8088, which would make problems with the AMD64 extensions:<\/p>\n<p>The NOP instruction would now clear the upper 32 bits of EAX in 64 bit mode.<\/p>\n<p>How can this be? Just like many RISC CPUs, the 8086 did not have a real NOP instruction. An assembler would translate the mnemonic &#8220;nop&#8221; into &#8220;xchg ax, ax&#8221; (opcode 0x90)- this instruction has no effect, and does not even touch the flags.<\/p>\n<p>The AMD64 was designed for the LP64 model, as opposed to ILP64, i.e. in C, long and pointers are 64 bit, but integers are still 32 bit. So the AMD64 is optimized for 32 bit arithmetic: When working with 32 bit values in the now 64 bit wide registers, the upper 32 bits of the result are always cleared. So an &#8220;add eax, ebx&#8221; would add the lower 32 bits of (the 64 bit registers) RAX and RBX, and clear the upper 32 bits of the result.<\/p>\n<p>So what does &#8220;xchg eax, eax&#8221; do? It does nothing&#8230; and then clears the upper 32 bits of EAX.<\/p>\n<p>AMD decided that the opcode 0x90 should remain a NOP even in 64 bit mode of the AMD64 instruction set, so 0x90 is now an explicit NOP. If you really need to do &#8220;xchg eax, eax&#8221; and thus clear the upper 32 bits, you can use the two-byte encoding 0x87h, 0xC0 &#8211; and this is what an assembler will generate.<\/p>\n<p>Actually 0x90 has been a NOP since the i486. While &#8220;xchg reg, reg&#8221; usually took 3 cycles, &#8220;xchg eax, eax&#8221;, i.e. &#8220;nop&#8221; only took one. All later CPUs also recognized 0x90 as a NOP, because otherwise there would have been a nasty (read and write) dependency on EAX, which could stall the pipeline significantly &#8211; a NOP shouldn&#8217;t really wait for EAX being written by previous instructions, and following instructions reading EAX shouldn&#8217;t have to wait for a NOP&#8230;<\/p>\n<p>  Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AMD64 is a quite clean extension of the i386 instruction set, it obsoletes many rarely used features of the i386 and introduces new registers, making the instruction set a lot more&#8230; logical. But there is one feature, actually a nice trick of the 8086\/8088, which would make problems with the AMD64 extensions: The NOP instruction &#8230; <a title=\"How NOP nearly became a non-NOP on AMD64\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=1216\" aria-label=\"Read more about How NOP nearly became a non-NOP on AMD64\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,38],"tags":[],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-trivia","category-x86"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/1216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1216"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}