{"id":33,"date":"2008-07-15T19:26:40","date_gmt":"2008-07-16T03:26:40","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=33"},"modified":"2008-07-15T19:26:40","modified_gmt":"2008-07-16T03:26:40","slug":"optimized-flags-code-for-6502","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=33","title":{"rendered":"Optimized Flags Code for 6502"},"content":{"rendered":"<p>When I disassembled Steve Wozniak&#8217;s <a href=\"http:\/\/www.pagetable.com\/?p=32\">Apple I BASIC<\/a>, I found a 6502 trick that I had never seen before, although I had read a lot of 6502 code, including the very well-written Commodore BASIC (i.e. Microsoft BASIC for 6502).<\/p>\n<p>What is the most optimized way (shortest code) to set, clear and test a flag?<\/p>\n<p>Normally, you would do this (&#8220;flag&#8221; is a byte in the zero page):<\/p>\n<table border=\"1\">\n<tr>\n<td>function<\/td>\n<td>code<\/td>\n<td>bytes<\/td>\n<td>cycles<\/td>\n<\/tr>\n<tr>\n<td>set<\/td>\n<td>\n<pre>lda #1\nsta flag<\/pre>\n<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>clear<\/td>\n<td>\n<pre>lda #0\nsta flag<\/pre>\n<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>test<\/td>\n<td>\n<pre>lda flag\nbeq cleared<\/pre>\n<\/td>\n<td>4<\/td>\n<td>5\/6<\/td>\n<\/tr>\n<\/table>\n<p>Woz did it like this:<\/p>\n<table border=\"1\">\n<tr>\n<td>function<\/td>\n<td>code<\/td>\n<td>bytes<\/td>\n<td>cycles<\/td>\n<\/tr>\n<tr>\n<td>set<\/td>\n<td>\n<pre>lda #$80\nsta flag<\/pre>\n<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>clear<\/td>\n<td>\n<pre>lsr flag<\/pre>\n<\/td>\n<td>2<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>test<\/td>\n<td>\n<pre>bit flag\nbpl cleared<\/pre>\n<\/td>\n<td>4<\/td>\n<td>5\/6<\/td>\n<\/tr>\n<\/table>\n<p>The trick is to store the flag in bit #7. Clearing bit #7 is easy: Just do a logical shift right, which will always write a zero into the MSB; we don&#8217;t care about the contents of the other bits. The &#8220;lsr zp&#8221; is the same speed as the &#8220;lda\/sta zp&#8221; combination, but only occupies 2 bytes instead of four.<\/p>\n<p>The other advantage of this method is that it is possible to test the flag without destroying a register. The &#8220;bit&#8221; instruction will just copy the MSB into the negative flag, while the &#8220;lda&#8221; would have overwritten the accumulator.<\/p>\n<p>I can&#8217;t see why this couldn&#8217;t be adapted to other (CISC, RMW-capable) CPUs as well. If you are fluent in any other assembly language, please post a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I disassembled Steve Wozniak&#8217;s Apple I BASIC, I found a 6502 trick that I had never seen before, although I had read a lot of 6502 code, including the very well-written Commodore BASIC (i.e. Microsoft BASIC for 6502). What is the most optimized way (shortest code) to set, clear and test a flag? Normally, &#8230; <a title=\"Optimized Flags Code for 6502\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=33\" aria-label=\"Read more about Optimized Flags Code for 6502\">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":[2,32],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-2","category-tricks"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/33","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=33"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}