{"id":5485,"date":"2014-03-30T22:47:22","date_gmt":"2014-03-30T22:47:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/inline-assembly-with-jmp-0f-or-b-0f-at-the-beginning-collection-of-common-programming-errors\/"},"modified":"2014-03-30T22:47:22","modified_gmt":"2014-03-30T22:47:22","slug":"inline-assembly-with-jmp-0f-or-b-0f-at-the-beginning-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/inline-assembly-with-jmp-0f-or-b-0f-at-the-beginning-collection-of-common-programming-errors\/","title":{"rendered":"Inline assembly with &ldquo;jmp 0f&rdquo; or &ldquo;b 0f&rdquo; at the beginning-Collection of common programming errors"},"content":{"rendered":"<p><strong>updated<\/strong><\/p>\n<p>Changed the 2nd line of assembly to the mnemonic actually being used (<code>mflr<\/code>) and added more info at the bottom.<\/p>\n<p>I ran across some code (using gcc) resembling the following (paraphrased):<\/p>\n<pre><code>#define SOME_MACRO( someVar ) \\\ndo {                          \\\n  __asm__ (                   \\\n    \"    b 0f\\n\"              \\\n    \"0:  mflr %0\\n\"           \\\n    : \"=r\"( someVar )         \\\n  );                          \\\n} while(0)\n<\/code><\/pre>\n<p>&#8230; where the <code>b<\/code> instruction (ppc) is a short jmp and <code>mflr<\/code> is getting the contents of the &#8216;link register&#8217; &#8212; which is similar to the program counter in some respects. I&#8217;ve seen this sort of thing for intel code as well (cf. the accepted answer in this question).<\/p>\n<p>The branch acts as a no-op &#8230; my question: what purpose does this serve?<\/p>\n<p>I&#8217;m guessing it has something to do with branch prediction stuff, but so far I&#8217;ve only found people&#8217;s code using this idiom while searching.<\/p>\n<p>It looks like I was wrong on the branch prediction guess. <code>mflr<\/code> grabs the contents of the link register.<\/p>\n<p>So, my question boils down to: why is the branch necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>updated Changed the 2nd line of assembly to the mnemonic actually being used (mflr) and added more info at the bottom. I ran across some code (using gcc) resembling the following (paraphrased): #define SOME_MACRO( someVar ) \\ do { \\ __asm__ ( \\ &#8221; b 0f\\n&#8221; \\ &#8220;0: mflr %0\\n&#8221; \\ : &#8220;=r&#8221;( someVar ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5485","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5485"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5485\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}