{"id":106889,"date":"2022-07-25T07:00:00","date_gmt":"2022-07-25T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106889"},"modified":"2022-11-30T06:43:28","modified_gmt":"2022-11-30T14:43:28","slug":"20220725-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220725-00\/?p=106889","title":{"rendered":"Yes, the 8086 wanted to be mechanically translatable from the 8080, but why not add the ability to indirect through AX, CX and DX?"},"content":{"rendered":"<p>Some time ago, I noted that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150417-00\/?p=44213\"> the 8086 was designed so that existing 8080 code could be machine-translated instruction by instruction into 8086<\/a>. The 8086 <code>BX<\/code> register stood in for the <code>HL<\/code> register pair on the 8080, and it is also the only register that you could indirect through, mirroring the corresponding limitation on the 8080.<\/p>\n<p>But that explains only part of the story. Yes, the 8086 had to let you indirect through <code>BX<\/code> so that 8080 instructions which operate on <code>M<\/code> (which was the pseudo-register that represented <code>[HL]<\/code>) could be translated into operations on <code>[BX]<\/code>. But that doesn&#8217;t mean that the 8086 had to forbid indirection through the other registers. After all, the 8086 had plenty of other instructions that didn&#8217;t exist on the 8080.<\/p>\n<p>So you can&#8217;t take away <code>BX<\/code>, but more is better, right? Why didn&#8217;t the 8086 let you indirect through <code>AX<\/code>, <code>CX<\/code> or <code>DX<\/code>, as well as <code>BX<\/code>?<\/p>\n<p>Basically, because there was no room.<\/p>\n<p>The encoding of two-operand instructions on the 8086 went like this:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th style=\"border: solid 1px gray;\">7<\/th>\n<th style=\"border: solid 1px gray;\">6<\/th>\n<th style=\"border: solid 1px gray;\">5<\/th>\n<th style=\"border: solid 1px gray;\">4<\/th>\n<th style=\"border: solid 1px gray;\">3<\/th>\n<th style=\"border: solid 1px gray;\">2<\/th>\n<th style=\"border: solid 1px gray;\">1<\/th>\n<th style=\"border: solid 1px gray;\">0<\/th>\n<td>&nbsp;<\/td>\n<th style=\"border: solid 1px gray;\">7<\/th>\n<th style=\"border: solid 1px gray;\">6<\/th>\n<th style=\"border: solid 1px gray;\">5<\/th>\n<th style=\"border: solid 1px gray;\">4<\/th>\n<th style=\"border: solid 1px gray;\">3<\/th>\n<th style=\"border: solid 1px gray;\">2<\/th>\n<th style=\"border: solid 1px gray;\">1<\/th>\n<th style=\"border: solid 1px gray;\">0<\/th>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px gray;\" colspan=\"6\">op<\/td>\n<td style=\"border: solid 1px gray;\">d<\/td>\n<td style=\"border: solid 1px gray; font-size: 80%;\">w<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px gray; font-size: 80%;\" colspan=\"2\">mod<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">reg<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">r\/m<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>op<\/code> determines the operation to be performed.<\/p>\n<p>The <code>d<\/code> is the direction (reg to r\/m or r\/m to reg).\u00b9<\/p>\n<p>The <code>w<\/code> indicates whether it is a byte operation or a word operation.<\/p>\n<p>The <code>mod<\/code> is the <i>mode<\/i> and describes how the <code>r\/m<\/code> is to be interpreted.<\/p>\n<p>The <code>reg<\/code> is the first operand, always a register (although the <code>d<\/code> bit can reverse the first and second operands).<\/p>\n<p>The interesting thing here is the <code>mod<\/code> + <code>r\/m<\/code> combination, since those capture the possible memory operands.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th rowspan=\"2\">r\/m<\/th>\n<th colspan=\"5\">mode+w<\/th>\n<\/tr>\n<tr>\n<th>00+*<\/th>\n<th>01+*<\/th>\n<th>10+*<\/th>\n<th>11+0<\/th>\n<th>11+1<\/th>\n<\/tr>\n<tr>\n<td>000<\/td>\n<td>* PTR [BX+SI]<\/td>\n<td>* PTR [BX+SI+imm8]<\/td>\n<td>* PTR [BX+SI+imm16]<\/td>\n<td>AL<\/td>\n<td>AX<\/td>\n<\/tr>\n<tr>\n<td>001<\/td>\n<td>* PTR [BX+DI]<\/td>\n<td>* PTR [BX+DI+imm8]<\/td>\n<td>* PTR [BX+DI+imm16]<\/td>\n<td>CL<\/td>\n<td>CX<\/td>\n<\/tr>\n<tr>\n<td>010<\/td>\n<td>* PTR [BP+SI]<\/td>\n<td>* PTR [BP+SI+imm8]<\/td>\n<td>* PTR [BP+SI+imm16]<\/td>\n<td>DL<\/td>\n<td>DX<\/td>\n<\/tr>\n<tr>\n<td>011<\/td>\n<td>* PTR [BP+DI]<\/td>\n<td>* PTR [BP+DI+imm8]<\/td>\n<td>* PTR [BP+DI+imm16]<\/td>\n<td>BL<\/td>\n<td>BX<\/td>\n<\/tr>\n<tr>\n<td>100<\/td>\n<td>* PTR [SI]<\/td>\n<td>* PTR [SI+imm8]<\/td>\n<td>* PTR [SI+imm16]<\/td>\n<td>AH<\/td>\n<td>SP<\/td>\n<\/tr>\n<tr>\n<td>101<\/td>\n<td>* PTR [DI]<\/td>\n<td>* PTR [DI+imm8]<\/td>\n<td>* PTR [DI+imm16]<\/td>\n<td>CH<\/td>\n<td>BP<\/td>\n<\/tr>\n<tr>\n<td>110<\/td>\n<td>imm<\/td>\n<td>* PTR [BP+imm8]<\/td>\n<td>* PTR [BP+imm16]<\/td>\n<td>DH<\/td>\n<td>SI<\/td>\n<\/tr>\n<tr>\n<td>111<\/td>\n<td>* PTR [BX]<\/td>\n<td>* PTR [BX+imm8]<\/td>\n<td>* PTR [BX+imm16]<\/td>\n<td>BL<\/td>\n<td>DI<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The encoding leaves room for 8 memory addressing modes. We are forced to have <code>[BX]<\/code> for compatibility, but we can choose the other seven. You need to be able to indirect through the base pointer so that you can access your local variables and parameters. And it&#8217;s expected that you can indirect through <code>SI<\/code> and <code>DI<\/code> since those are the registers used for block memory operations.<\/p>\n<p>That leaves four more addressing modes, and the architects decided to use the four ways of combining <code>BX<\/code>\/<code>BP<\/code> with <code>SI<\/code>\/<code>DI<\/code>. The <code>BP+x<\/code> addressing modes let you access arrays on the stack, and the <code>BX+x<\/code> addressing modes let you access arrays on the heap, where <code>SI<\/code> and <code>DI<\/code> serve as the index registers.<\/p>\n<p>Now, the architects could have chosen to allow indirection through the other three 16-bit registers, but that would have left room for only one array indexing mode. Giving the instructions to the array indexing modes means that you lose <code>[AX]<\/code>, <code>[CX]<\/code>, and <code>[DX]<\/code>, but that&#8217;s less of a loss because you can still indirect through <code>[SI]<\/code> and <code>[DI]<\/code> (and <code>[BP]<\/code>, but that&#8217;s intended to be the frame pointer, not a general-purpose pointer register).<\/p>\n<p>The other choice would be to increase the number of addressing modes by going to a three-byte instruction encoding, thereby picking up eight more bits. But that seems like quite an excessive step, seeing as the original 8080 consisted only of one-byte instructions. (I&#8217;m not counting immediate bytes toward encoding counts for the purpose of this comparison.)<\/p>\n<p>It was a game of trade-offs, and the trade-off was to pick up indexed addressing, and give up on supporting indirection through all of the 16-bit registers.<\/p>\n<p>\u00b9 Note that this means that register-to-register operations can be encoded two ways:<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th style=\"border: solid 1px gray;\">7<\/th>\n<th style=\"border: solid 1px gray;\">6<\/th>\n<th style=\"border: solid 1px gray;\">5<\/th>\n<th style=\"border: solid 1px gray;\">4<\/th>\n<th style=\"border: solid 1px gray;\">3<\/th>\n<th style=\"border: solid 1px gray;\">2<\/th>\n<th style=\"border: solid 1px gray;\">1<\/th>\n<th style=\"border: solid 1px gray;\">0<\/th>\n<td>&nbsp;<\/td>\n<th style=\"border: solid 1px gray;\">7<\/th>\n<th style=\"border: solid 1px gray;\">6<\/th>\n<th style=\"border: solid 1px gray;\">5<\/th>\n<th style=\"border: solid 1px gray;\">4<\/th>\n<th style=\"border: solid 1px gray;\">3<\/th>\n<th style=\"border: solid 1px gray;\">2<\/th>\n<th style=\"border: solid 1px gray;\">1<\/th>\n<th style=\"border: solid 1px gray;\">0<\/th>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px gray;\" colspan=\"6\">op<\/td>\n<td style=\"border: solid 1px gray;\">0<\/td>\n<td style=\"border: solid 1px gray; font-size: 80%;\">w<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px gray;\">1<\/td>\n<td style=\"border: solid 1px gray;\">1<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">reg1<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">reg2<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px gray;\" colspan=\"6\">op<\/td>\n<td style=\"border: solid 1px gray;\">1<\/td>\n<td style=\"border: solid 1px gray; font-size: 80%;\">w<\/td>\n<td>&nbsp;<\/td>\n<td style=\"border: solid 1px gray;\">1<\/td>\n<td style=\"border: solid 1px gray;\">1<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">reg2<\/td>\n<td style=\"border: solid 1px gray;\" colspan=\"3\">reg1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>These redundant encodings are used by some assemblers to &#8220;fingerprint&#8221; their output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So many addressing modes, so little space.<\/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-106889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>So many addressing modes, so little space.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106889","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=106889"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106889\/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=106889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}