{"id":14828,"date":"2022-11-24T20:08:09","date_gmt":"2022-11-24T20:08:09","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=14828"},"modified":"2024-10-27T04:21:19","modified_gmt":"2024-10-27T04:21:19","slug":"why-is-arm-using-a-link-register-lr","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=14828","title":{"rendered":"Why is ARM using a link register (LR)?"},"content":{"rendered":"<p>It&#8217;s been used for a better performance, but only works for leaf functions.<\/p>\n<hr>\n<p><strong>Working szenario<\/strong> (Leaf function)<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nvoid foo() { return; }\r\nvoid bar() { return; }\r\n\r\nint main()\r\n{\r\n  foo(); \/\/ Write PC+1 (foo()+1) into LR, goto foo and come back via LR\r\n  bar(); \/\/ Write PC+1 (bar()+1) into LR, goto bar and come back via LR\r\n  return 0;\r\n}\r\n<\/pre>\n<p>Nice, it&#8217;s fast, cause we only cache the return address in a register (LR) and not on the stack.<\/p>\n<hr>\n<p><strong>Not working szenario<\/strong> (Non-leaf function)<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nvoid bar();\r\nvoid foo() {\r\n  bar(); \/\/ Write PC+1 (bar()+1) into LR, goto bar and come back via LR\r\n  return; \/\/ You can&#039;t get back to main, cause LR points to bar()+1!\r\n}\r\nvoid bar() { return; }\r\n\r\nint main()\r\n{\r\n  foo(); \/\/ Write PC+1 (foo()+1) into LR, goto foo and come back via LR?\r\n  return 0;\r\n}\r\n<\/pre>\n<p>Shit, for non-leaf functions we have to operate like Intel\/AMD and have to store the LR on the stack.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been used for a better performance, but only works for leaf functions. Working szenario (Leaf function) Nice, it&#8217;s fast,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[83],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/14828"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14828"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/14828\/revisions"}],"predecessor-version":[{"id":16722,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/14828\/revisions\/16722"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14828"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}