{"id":102769,"date":"2019-08-07T07:00:00","date_gmt":"2019-08-07T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102769"},"modified":"2019-09-13T21:22:24","modified_gmt":"2019-09-14T04:22:24","slug":"20190807-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190807-00\/?p=102769","title":{"rendered":"The SuperH-3, part 3: Status flags and miscellaneous instructions"},"content":{"rendered":"<p>Only four of the bits in the status register are available to user-mode:<\/p>\n<table class=\"cp3\" style=\"border: solid 1px black; border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Bit<\/th>\n<th>Meaning<\/th>\n<th>Notes<\/th>\n<\/tr>\n<tr>\n<td><var>M<\/var><\/td>\n<td>Modulus<\/td>\n<td>Used by division instructions<\/td>\n<\/tr>\n<tr>\n<td><var>Q<\/var><\/td>\n<td>Quotient<\/td>\n<td>Used by division instructions<\/td>\n<\/tr>\n<tr>\n<td><var>S<\/var><\/td>\n<td>Saturate<\/td>\n<td>Used by multiply-add instructions<\/td>\n<\/tr>\n<tr>\n<td><var>T<\/var><\/td>\n<td>Test<\/td>\n<td>Multi-purpose flag<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>(There was no official meaning for the names of the registers, so I made up mnemonics for them.)<\/p>\n<p>Aside from the flags used by special-purpose instructions (multiplication and division), there is basically only one flag: <var>T<\/var>. Each instructions decides how it wishes to consume and produce the <var>T<\/var> flag.<\/p>\n<pre>    CLRT        ; T = 0\r\n    SETT        ; T = 1\r\n\r\n    CLRS        ; S = 0\r\n    SETS        ; S = 1\r\n<\/pre>\n<p>There are four instructions which directly set or clear two of the bits in the status register. We&#8217;ll learn more about the <var>M<\/var> and <var>Q<\/var> registers when we study integer division.<\/p>\n<pre>    MOVT    Rn  ; Rn = T (0 or 1)\r\n<\/pre>\n<p>There is also a special instruction to copy the <var>T<\/var> flag into a register. There is no converse instruction, but we&#8217;ll see later how we could try to synthesize one.<\/p>\n<p>Windows CE requires that the <var>S<\/var> flag be clear at function entry and exit.<\/p>\n<p>Since there wasn&#8217;t much to be said about flags, I&#8217;ll use the rest of my time to cover various miscellaneous instructions.<\/p>\n<pre>    MOVA @(disp, PC), r0    ; r0 = PC + disp\r\n<\/pre>\n<p>The <i>move address<\/i> instruction calculates the effective address of <code>@(disp, PC)<\/code> and stores it into <var>r0<\/var>. The displacement can be a multiple of 4 up to 255 \u00d7 4 = 1020.<\/p>\n<pre>    SWAP.B Rm, Rn           ; Rn = Rm with bottom two bytes swapped\r\n    SWAP.W Rm, Rn           ; Rn = Rm with top and bottom words swapped\r\n    XTRCT  Rm, Rn           ; Rn = (Rn &lt;&lt; 16) | (Rm &gt;&gt; 16)\r\n<\/pre>\n<p>These instructions are for byte swapping or extracting the middle 32 bits of a 64-bit value.<\/p>\n<pre>    PREF  @Rn               ; prefetch memory at Rn\r\n<\/pre>\n<p>The prefetch instruction has no effect if the memory at <var>Rn<\/var> is inaccessible.<\/p>\n<pre>    TRAPA #imm              ; trap to kernel mode\r\n<\/pre>\n<p>The <code>TRAPA<\/code> instruction traps to kernel mode. It carries an 8-bit unsigned immediate payload which kernel mode can use to signify anything it wishes.<\/p>\n<pre>    NOP                     ; do nothing\r\n<\/pre>\n<p>Fortunately, the instruction <code>0000<\/code> is invalid, rather than being a nop.<\/p>\n<pre>    STC     GBR, Rn         ; Rn = GBR\r\n    LDC     Rn, GBR         ; GBR = Rn\r\n    STC     PR, Rn          ; Rn = PR\r\n    LDC     Rn, PR          ; PR = Rn\r\n<\/pre>\n<p>These instructions let you move data into and out of the special registers <var>gbr<\/var> and <var>pr<\/var>. We saw <var>gbr<\/var> when we learned about addressing modes. We&#8217;ll learn about <var>pr<\/var> when we get to control transfer.<\/p>\n<p>Well, that wasn&#8217;t very exciting yet. Let&#8217;s start doing math. <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190808-00\/?p=102774\">Next time<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basically, there&#8217;s only one flag.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-102769","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Basically, there&#8217;s only one flag.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102769"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102769\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}