{"id":22,"date":"2006-08-07T16:23:49","date_gmt":"2006-08-08T00:23:49","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=22"},"modified":"2006-08-07T16:23:49","modified_gmt":"2006-08-08T00:23:49","slug":"shift-oddities","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=22","title":{"rendered":"Shift oddities"},"content":{"rendered":"<p>Most of the x86 instructions will automatically alter the flags depending on the result. Sometimes this is rather frustrating because you actually what to preserve the flags as long as possible, and sometimes you miss a &#8220;mov eax, ecx&#8221; which alters the flags. But at least it&#8217;s guaranteed that an instruction either sets the flags or it doesn&#8217;t touch them, independent of the actual operation&#8230; Or is it?<\/p>\n<p>There is the familiy of shift instructions. A &#8220;shl eax, cl&#8221; will *only* set the sign\/zero flags according to the result if cl != 0 (mod 32). Why would anyone want that? Can someone think of a not so artificial situation, where this is useful? And I&#8217;m quite sure it is harder to design a processor which handles this special case correctly (let alone pipeline considerations).<\/p>\n<p>Because of this odd behavior, a compiler has to translate<\/p>\n<p>if (a >> b) this(); else that();<\/p>\n<p>to something like<\/p>\n<p>shr  eax, cl<br \/>\ntest eax, eax<br \/>\njz   bla<\/p>\n<p>And BTW, the rotate instructions never alter the SF\/ZF flags.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the x86 instructions will automatically alter the flags depending on the result. Sometimes this is rather frustrating because you actually what to preserve the flags as long as possible, and sometimes you miss a &#8220;mov eax, ecx&#8221; which alters the flags. But at least it&#8217;s guaranteed that an instruction either sets the flags &#8230; <a title=\"Shift oddities\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=22\" aria-label=\"Read more about Shift oddities\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,38],"tags":[],"class_list":["post-22","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\/22","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}