about summary refs log blame commit diff stats
path: root/html/subx/062write-byte.subx.html
blob: 6b322039639c7745e99b0c5d09c181c74a186908 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                                                                      
                                                 

                       

                                                                                           
                                     



                                  

                                                             
                             


                                                                           











































                                                                                                                                                         
                                                                                                                           





















                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                                                                                                                                             


                                                                                                           
                                                                                                                                                                                                                            

                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                             



                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                            




                                                                                               
                                                                                                            
                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                              
                                                                                                                                                                                                                                                                  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu's instructions and their table-driven translation</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffd7; }
body { font-family: monospace; color: #000000; background-color: #ffffd7; }
* { font-size: 1em; }
.muRegEbx { color: #5f00ff; }
.muRegEdi { color: #00af00; }
.muComment { color: #005faf; }
.PreProc { color: #c000c0; }
.muRegEsi { color: #005faf; }
.Delimiter { color: #c000c0; }
.Constant { color: #008787; }
.Special { color: #ff6060; }
.muRegEcx { color: #870000; }
.muRegEdx { color: #af5f00; }
-->
</style>
</head>
<body>
<pre id='vimCodeElement
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - subx/062write-byte.subx</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
* { font-size:12pt; font-size: 1em; }
.subxComment { color: #005faf; }
.subxS2Comment { color: #8a8a8a; }
.LineNr { }
.subxS1Comment { color: #0000af; }
.SpecialChar { color: #d70000; }
.subxFunction { color: #af5f00; text-decoration: underline; }
.subxTest { color: #5f8700; }
.Constant { color: #008787; }
.Normal { color: #000000; background-color: #c6c6c6; padding-bottom: 1px; }
.subxH1Comment { color: #005faf; text-decoration: underline; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/subx/062write-byte.subx'>https://github.com/akkartik/mu/blob/master/subx/062write-byte.subx</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr">  1 </span><span class="subxComment"># write-byte: write a single byte to a buffered-file. The write may be buffered.</span>
<span id="L2" class="LineNr">  2 </span><span class="subxComment"># flush: write out any buffered writes to disk.</span>
<span id="L3" class="LineNr">  3 </span><span class="subxComment">#</span>
<span id="L4" class="LineNr">  4 </span><span class="subxComment"># TODO: Come up with a way to signal failure to write to disk. This is hard</span>
<span id="L5" class="LineNr">  5 </span><span class="subxComment"># since the failure may impact previous calls that were buffered.</span>
<span id="L6" class="LineNr">  6 </span>
<span id="L7" class="LineNr">  7 </span>== data
<span id="L8" class="LineNr">  8 </span>
<span id="L9" class="LineNr">  9 </span><span class="subxComment"># The buffered file for standard output.</span>
<span id="L10" class="LineNr"> 10 </span><span class="SpecialChar"><a href='062write-byte.subx.html#L10'>Stdout</a></span>:
<span id="L11" class="LineNr"> 11 </span>    <span class="subxComment"># file descriptor or (address stream)</span>
<span id="L12" class="LineNr"> 12 </span>    01 00 00 00  <span class="subxComment"># 1 = standard output</span>
<span id="L13" class="LineNr"> 13 </span>    <span class="subxComment"># current write index</span>
<span id="L14" class="LineNr"> 14 </span>    00 00 00 00
<span id="L15" class="LineNr"> 15 </span>    <span class="subxComment"># current read index</span>
<span id="L16" class="LineNr"> 16 </span>    00 00 00 00
<span id="L17" class="LineNr"> 17 </span>    <span class="subxComment"># length (8)</span>
<span id="L18" class="LineNr"> 18 </span>    08 00 00 00
<span id="L19" class="LineNr"> 19 </span>    <span class="subxComment"># data</span>
<span id="L20" class="LineNr"> 20 </span>    00 00 00 00 00 00 00 00  <span class="subxComment"># 8 bytes</span>
<span id="L21" class="LineNr"> 21 </span>
<span id="L22" class="LineNr"> 22 </span><span class="subxComment"># TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But</span>
<span id="L23" class="LineNr"> 23 </span><span class="subxComment"># I don't want to type in 1024 bytes here.</span>
<span id="L24" class="LineNr"> 24 </span>
<span id="L25" class="LineNr"> 25 </span>== code
<span id="L26" class="LineNr"> 26 </span><span class="subxComment">#   instruction                     effective address                                                   register    displacement    immediate</span>
<span id="L27" class="LineNr"> 27 </span><span class="subxS1Comment"># . op          subop               mod             rm32          base        index         scale       r32</span>
<span id="L28" class="LineNr"> 28 </span><span class="subxS1Comment"># . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes</span>
<span id="L29" class="LineNr"> 29 </span>
<span id="L30" class="LineNr"> 30 </span><span class="subxComment"># main:</span>
<span id="L31" class="LineNr"> 31 </span>    e8/call  run-tests/disp32  <span class="subxComment"># 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.</span>
<span id="L32" class="LineNr"> 32 </span>    <span class="subxComment"># syscall(exit, Num-test-failures)</span>
<span id="L33" class="LineNr"> 33 </span>    8b/copy                         0/mod/indirect  5/rm32/.disp32           <span class="Normal"> . </span>           <span class="Normal"> . </span>          3/r32/EBX   <span class="SpecialChar"><a href='051test.subx.html#L87'>Num-test-failures</a></span>/disp32          <span class="subxComment"># copy *Num-test-failures to EBX</span>
<span id="L34" class="LineNr"> 34 </span>    b8/copy-to-EAX  1/imm32/exit
<span id="L35" class="LineNr"> 35 </span>    cd/syscall  0x80/imm8
<span id="L36" class="LineNr"> 36 </span>
<span id="L37" class="LineNr"> 37 </span><span class="subxComment"># Write lower byte of 'n' to 'f'.</span>
<span id="L38" class="LineNr"> 38 </span><span class="subxFunction"><a href='062write-byte.subx.html#L38'>write-byte</a></span>:  <span class="subxComment"># f : (address buffered-file), n : int -&gt; &lt;void&gt;</span>
<span id="L39" class="LineNr"> 39 </span>    <span class="subxS1Comment"># . prolog</span>
<span id="L40" class="LineNr"> 40 </span>    55/push-EBP
<span id="L41" class="LineNr"> 41 </span>    89/copy                         3/mod/direct    5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/ESP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ESP to EBP</span>
<span id="L42" class="LineNr"> 42 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L43" class="LineNr"> 43 </span>    51/push-ECX
<span id="L44" class="LineNr"> 44 </span>    57/push-EDI
<span id="L45" class="LineNr"> 45 </span>    <span class="subxComment"># EDI = f</span>
<span id="L46" class="LineNr"> 46 </span>    8b/copy                         1/mod/*+disp8   5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          7/r32/EDI   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(EBP+8) to EDI</span>
<span id="L47" class="LineNr"> 47 </span>    <span class="subxComment"># ECX = f-&gt;write</span>
<span id="L48" class="LineNr"> 48 </span>    8b/copy                         1/mod/*+disp8   7/rm32/EDI   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ECX   4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(EDI+4) to ECX</span>
<span id="L49" class="LineNr"> 49 </span>    <span class="subxComment"># if (f-&gt;write &gt;= f-&gt;length) flush and clear f's stream</span>
<span id="L50" class="LineNr"> 50 </span>    3b/compare                      1/mod/*+disp8   7/rm32/EDI   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ECX   0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># compare ECX with *(EDI+12)</span>
<span id="L51" class="LineNr"> 51 </span>    7c/jump-if-lesser  $write-byte:to-stream/disp8
<span id="L52" class="LineNr"> 52 </span>    <span class="subxS1Comment"># . flush(f)</span>
<span id="L53" class="LineNr"> 53 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L54" class="LineNr"> 54 </span>    57/push-EDI
<span id="L55" class="LineNr"> 55 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L56" class="LineNr"> 56 </span>    e8/call  <a href='062write-byte.subx.html#L84'>flush</a>/disp32
<span id="L57" class="LineNr"> 57 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L58" class="LineNr"> 58 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to ESP</span>
<span id="L59" class="LineNr"> 59 </span>    <span class="subxS1Comment"># . clear-stream(stream = f+4)</span>
<span id="L60" class="LineNr"> 60 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L61" class="LineNr"> 61 </span>    8d/copy-address                 1/mod/*+disp8   7/rm32/EDI   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/EAX   4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy EDI+4 to EAX</span>
<span id="L62" class="LineNr"> 62 </span>    50/push-EAX
<span id="L63" class="LineNr"> 63 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L64" class="LineNr"> 64 </span>    e8/call  <a href='056write.subx.html#L85'>clear-stream</a>/disp32
<span id="L65" class="LineNr"> 65 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L66" class="LineNr"> 66 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to ESP</span>
<span id="L67" class="LineNr"> 67 </span><span class="Constant">$write-byte:to-stream</span>:
<span id="L68" class="LineNr"> 68 </span>    <span class="subxComment"># write to stream</span>
<span id="L69" class="LineNr"> 69 </span>    <span class="subxComment"># f-&gt;data[f-&gt;read] = LSB(n)</span>
<span id="L70" class="LineNr"> 70 </span>    31/xor                          3/mod/direct    0/rm32/EAX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/EAX  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># clear EAX</span>
<span id="L71" class="LineNr"> 71 </span>    8a/copy-byte                    1/mod/*+disp8   5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/AL    0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># copy byte at *(EBP+12) to AL</span>
<span id="L72" class="LineNr"> 72 </span>    88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX  <span class="Normal"> . </span>          0/r32/AL    0x10/disp8     <span class="Normal"> . </span>                <span class="subxComment"># copy AL to *(EDI+ECX+16)</span>
<span id="L73" class="LineNr"> 73 </span>    <span class="subxComment"># ++f-&gt;read</span>
<span id="L74" class="LineNr"> 74 </span>    ff          0/subop/increment   1/mod/*+disp8   7/rm32/EDI   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># increment *(EDI+4)</span>
<span id="L75" class="LineNr"> 75 </span><span class="Constant">$write-byte:end</span>:
<span id="L76" class="LineNr"> 76 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L77" class="LineNr"> 77 </span>    5f/pop-to-EDI
<span id="L78" class="LineNr"> 78 </span>    59/pop-to-ECX
<span id="L79" class="LineNr"> 79 </span>    <span class="subxS1Comment"># . epilog</span>
<span id="L80" class="LineNr"> 80 </span>    89/copy                         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/EBP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy EBP to ESP</span>
<span id="L81" class="LineNr"> 81 </span>    5d/pop-to-EBP
<span id="L82" class="LineNr"> 82 </span>    c3/return
<span id="L83" class="LineNr"> 83 </span>
<span id="L84" class="LineNr"> 84 </span><span class="subxFunction"><a href='062write-byte.subx.html#L84'>flush</a></span>:  <span class="subxComment"># f : (address buffered-file) -&gt; &lt;void&gt;</span>
<span id="L85" class="LineNr"> 85 </span>    <span class="subxS1Comment"># . prolog</span>
<span id="L86" class="LineNr"> 86 </span>    55/push-EBP
<span id="L87" class="LineNr"> 87 </span>    89/copy                         3/mod/direct    5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/ESP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ESP to EBP</span>
<span id="L88" class="LineNr"> 88 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L89" class="LineNr"> 89 </span>    50/push-EAX
<span id="L90" class="LineNr"> 90 </span>    51/push-ECX
<span id="L91" class="LineNr"> 91 </span>    <span class="subxComment"># EAX = f</span>
<span id="L92" class="LineNr"> 92 </span>    8b/copy                         1/mod/*+disp8   5/rm32/EBP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/EAX   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(EBP+8) to EAX</span>
<span id="L93" class="LineNr"> 93 </span>    <span class="subxComment"># write-stream(f-&gt;fd, data = f+4)</span>
<span id="L94" class="LineNr"> 94 </span>      <span class="subxS2Comment"># . . push args</span>
<span id="L95" class="LineNr"> 95 </span>    8d/copy-address                 1/mod/*+disp8   0/rm32/EAX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ECX   4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy EAX+4 to ECX</span>
<span id="L96" class="LineNr"> 96 </span>    51/push-ECX
<span id="L97" class="LineNr"> 97 </span>    ff          6/subop/push        0/mod/indirect  0/rm32/EAX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># push *EAX</span>
<span id="L98" class="LineNr"> 98 </span>      <span class="subxS2Comment"># . . call</span>
<span id="L99" class="LineNr"> 99 </span>    e8/call  <a href='060write-stream.subx.html#L22'>write-stream</a>/disp32
<span id="L100" class="LineNr">100 </span>      <span class="subxS2Comment"># . . discard args</span>
<span id="L101" class="LineNr">101 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to ESP</span>
<span id="L102" class="LineNr">102 </span><span class="Constant">$flush:end</span>:
<span id="L103" class="LineNr">103 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L104" class="LineNr">104 </span>    59/pop-to-ECX
<span id="L105" class="LineNr">105 </span>    58/pop-to-EAX
<span id="L106" class="LineNr">106 </span>    <span class="subxS1Comment"># . epilog</span>
<span id="L107" class="LineNr">107 </span>    89/copy                         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/EBP  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy EBP to ESP</span>
<span id="L108" class="LineNr">108 </span>    5d/pop-to-EBP
<span id="L109" class="LineNr">109 </span>    c3/return
<span id="L110" class="LineNr">110 </span>
<span id="L111" class="LineNr">111 </span><span class="subxH1Comment"># - tests</span>
<span id="L112" class="LineNr">112 </span>
<span id="L113" class="LineNr">113 </span><span class="subxTest"><a href='062write-byte.subx.html#L113'>test-write-byte-single</a></span>:
<span id="L114" class="LineNr">114 </span>    <span class="subxH1Comment"># - check that read-byte returns first byte of 'file'</span>
<span id="L115" class="LineNr">115 </span>    <span class="subxComment"># setup</span>
<span id="L116" class="LineNr">116 </span>    <span class="subxS1Comment"># . clear-stream(_test-stream)</span>
<span id="L117" class="LineNr">117 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L118" class="LineNr">118 </span>    68/push  <a href='056write.subx.html#L193'>_test-stream</a>/imm32
<span id="L119" class="LineNr">119 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L120" class="LineNr">120 </span>    e8/call  <a href='056write.subx.html#L85'>clear-stream</a>/disp32
<span id="L121" class="LineNr">121 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L122" class="LineNr">122 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to ESP</span>
<span id="L123" class="LineNr">123 </span>    <span class="subxS1Comment"># . clear-stream(_test-buffered-file+4)</span>
<span id="L124" class="LineNr">124 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L125" class="LineNr">125 </span>    b8/copy-to-EAX  <a href='059read-byte.subx.html#L290'>_test-buffered-file</a>/imm32
<span id="L126" class="LineNr">126 </span>    05/add-to-EAX  4/imm32
<span id="L127" class="LineNr">127 </span>    50/push-EAX
<span id="L128" class="LineNr">128 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L129" class="LineNr">129 </span>    e8/call  <a href='056write.subx.html#L85'>clear-stream</a>/disp32
<span id="L130" class="LineNr">130 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L131" class="LineNr">131 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to ESP</span>
<span id="L132" class="LineNr">132 </span>    <span class="subxComment"># write-byte(_test-buffered-file, 'A')</span>
<span id="L133" class="LineNr">133 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L134" class="LineNr">134 </span>    68/push  0x41/imm32
<span id="L135" class="LineNr">135 </span>    68/push  <a href='059read-byte.subx.html#L290'>_test-buffered-file</a>/imm32
<span id="L136" class="LineNr">136 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L137" class="LineNr">137 </span>    e8/call  <a href='062write-byte.subx.html#L38'>write-byte</a>/disp32
<span id="L138" class="LineNr">138 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L139" class="LineNr">139 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              8/imm32           <span class="subxComment"># add to ESP</span>
<span id="L140" class="LineNr">140 </span>    <span class="subxComment"># flush(_test-buffered-file)</span>
<span id="L141" class="LineNr">141 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L142" class="LineNr">142 </span>    68/push  <a href='059read-byte.subx.html#L290'>_test-buffered-file</a>/imm32
<span id="L143" class="LineNr">143 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L144" class="LineNr">144 </span>    e8/call  <a href='062write-byte.subx.html#L84'>flush</a>/disp32
<span id="L145" class="LineNr">145 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L146" class="LineNr">146 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to ESP</span>
<span id="L147" class="LineNr">147 </span>    <span class="subxComment"># check-ints-equal(*_test-stream-&gt;data, 'A', msg)</span>
<span id="L148" class="LineNr">148 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L149" class="LineNr">149 </span>    68/push  <span class="Constant">&quot;F - test-write-byte-single&quot;</span>/imm32
<span id="L150" class="LineNr">150 </span>    68/push  0x41/imm32
<span id="L151" class="LineNr">151 </span>    <span class="subxS2Comment"># . . push *_test-stream-&gt;data</span>
<span id="L152" class="LineNr">152 </span>    b8/copy-to-EAX  <a href='056write.subx.html#L193'>_test-stream</a>/imm32
<span id="L153" class="LineNr">153 </span>    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>          0xc/disp8      <span class="Normal"> . </span>                <span class="subxComment"># push *(EAX+12)</span>
<span id="L154" class="LineNr">154 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L155" class="LineNr">155 </span>    e8/call  <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32
<span id="L156" class="LineNr">156 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L157" class="LineNr">157 </span>    81          0/subop/add         3/mod/direct    4/rm32/ESP   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to ESP</span>
<span id="L158" class="LineNr">158 </span>    <span class="subxS1Comment"># . end</span>
<span id="L159" class="LineNr">159 </span>    c3/return
<span id="L160" class="LineNr">160 </span>
<span id="L161" class="LineNr">161 </span><span class="subxS2Comment"># . . vim&#0058;nowrap:textwidth=0</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
class="p">> xreg2 <span class="Constant">&quot; &quot;</span> xreg1 <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> inverse-square-root var2 =&gt; <span class="Constant">&quot;f3 0f 52/inverse-square-root *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> inverse-square-root *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 52/inverse-square-root *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> min var2/xreg2 =&gt; <span class="Constant">&quot;f3 0f 5d/min %&quot;</span> xreg2 <span class="Constant">&quot; &quot;</span> xreg1 <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> min var2 =&gt; <span class="Constant">&quot;f3 0f 5d/min *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> min *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 5d/min *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> max var2/xreg2 =&gt; <span class="Constant">&quot;f3 0f 5f/max %&quot;</span> xreg2 <span class="Constant">&quot; &quot;</span> xreg1 <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> max var2 =&gt; <span class="Constant">&quot;f3 0f 5f/max *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> max *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 5f/max *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> Remember, when these instructions use indirect mode, they still use an integer register. Floating-point registers can't hold addresses. Most instructions operate exclusively on integer or floating-point operands. The only exceptions are the instructions for converting between integers and floating-point numbers. var/xreg <span class="Special">&lt;-</span> convert var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 2a/convert-to-float %&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> convert var2 =&gt; <span class="Constant">&quot;f3 0f 2a/convert-to-float *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> convert *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 2a/convert-to-float *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> Converting floats to ints performs rounding by default. (We don't mess with the MXCSR control register.) var/reg <span class="Special">&lt;-</span> convert var2/xreg2 =&gt; <span class="Constant">&quot;f3 0f 2d/convert-to-int %&quot;</span> xreg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg <span class="Special">&lt;-</span> convert var2 =&gt; <span class="Constant">&quot;f3 0f 2d/convert-to-int *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg <span class="Special">&lt;-</span> convert *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 2d/convert-to-int *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> There's a separate instruction for truncating the fractional part. var/reg <span class="Special">&lt;-</span> truncate var2/xreg2 =&gt; <span class="Constant">&quot;f3 0f 2c/truncate-to-int %&quot;</span> xreg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg <span class="Special">&lt;-</span> truncate var2 =&gt; <span class="Constant">&quot;f3 0f 2c/truncate-to-int *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg <span class="Special">&lt;-</span> truncate *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 2c/truncate-to-int *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> There are no instructions accepting floating-point literals. To obtain integer literals in floating-point registers, copy them to general-purpose registers and then convert them to floating-point. One pattern you may have noticed above is that the floating-point instructions above always write to registers. The only exceptions are `copy` instructions, which can write to memory locations. var/xreg <span class="Special">&lt;-</span> copy var2/xreg2 =&gt; <span class="Constant">&quot;f3 0f 11/&lt;- %&quot;</span> xreg <span class="Constant">&quot; &quot;</span> xreg2 <span class="Constant">&quot;/x32&quot;</span> copy-to var1, var2/xreg =&gt; <span class="Constant">&quot;f3 0f 11/&lt;- *(ebp+&quot;</span> var1.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> copy var2 =&gt; <span class="Constant">&quot;f3 0f 10/-&gt; *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> var/xreg <span class="Special">&lt;-</span> copy *var2/reg2 =&gt; <span class="Constant">&quot;f3 0f 10/-&gt; *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> Comparisons must always start with a register: compare var1/xreg1, var2/xreg2 =&gt; <span class="Constant">&quot;0f 2f/compare %&quot;</span> xreg2 <span class="Constant">&quot; &quot;</span> xreg1 <span class="Constant">&quot;/x32&quot;</span> compare var1/xreg1, var2 =&gt; <span class="Constant">&quot;0f 2f/compare *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> xreg1 <span class="Constant">&quot;/x32&quot;</span> <span class="muComment">## Blocks</span> In themselves, blocks generate no instructions. However, if a block contains variable declarations, they must be cleaned up when the block ends. Clean up var on the stack =&gt; <span class="Constant">&quot;81 0/subop/add %esp &quot;</span> size-of(var) <span class="Constant">&quot;/imm32&quot;</span> Clean up var/reg =&gt; <span class="Constant">&quot;8f 0/subop/pop %&quot;</span> reg Clean up var/xreg =&gt; <span class="Constant">&quot;f3 0f 10/-&gt; *esp &quot;</span> xreg <span class="Constant">&quot;/x32&quot;</span> <span class="Constant">&quot;81 0/subop/add %esp 4/imm32&quot;</span> <span class="muComment">## Jumps</span> Besides having to clean up any variable declarations (see above) between themselves and their target, jumps translate like this: <span class="PreProc">break</span> =&gt; <span class="Constant">&quot;e9/jump break/disp32&quot;</span> <span class="PreProc">break</span> label =&gt; <span class="Constant">&quot;e9/jump &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop</span> =&gt; <span class="Constant">&quot;e9/jump loop/disp32&quot;</span> <span class="PreProc">loop</span> label =&gt; <span class="Constant">&quot;e9/jump &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-=</span> =&gt; <span class="Constant">&quot;0f 84/jump-if-= break/disp32&quot;</span> <span class="PreProc">break-if-=</span> label =&gt; <span class="Constant">&quot;0f 84/jump-if-= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-=</span> =&gt; <span class="Constant">&quot;0f 84/jump-if-= loop/disp32&quot;</span> <span class="PreProc">loop-if-=</span> label =&gt; <span class="Constant">&quot;0f 84/jump-if-= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-!=</span> =&gt; <span class="Constant">&quot;0f 85/jump-if-!= break/disp32&quot;</span> <span class="PreProc">break-if-!=</span> label =&gt; <span class="Constant">&quot;0f 85/jump-if-!= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-!=</span> =&gt; <span class="Constant">&quot;0f 85/jump-if-!= loop/disp32&quot;</span> <span class="PreProc">loop-if-!=</span> label =&gt; <span class="Constant">&quot;0f 85/jump-if-!= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-&lt;</span> =&gt; <span class="Constant">&quot;0f 8c/jump-if-&lt; break/disp32&quot;</span> <span class="PreProc">break-if-&lt;</span> label =&gt; <span class="Constant">&quot;0f 8c/jump-if-&lt; &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-&lt;</span> =&gt; <span class="Constant">&quot;0f 8c/jump-if-&lt; loop/disp32&quot;</span> <span class="PreProc">loop-if-&lt;</span> label =&gt; <span class="Constant">&quot;0f 8c/jump-if-&lt; &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-&gt;</span> =&gt; <span class="Constant">&quot;0f 8f/jump-if-&gt; break/disp32&quot;</span> <span class="PreProc">break-if-&gt;</span> label =&gt; <span class="Constant">&quot;0f 8f/jump-if-&gt; &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-&gt;</span> =&gt; <span class="Constant">&quot;0f 8f/jump-if-&gt; loop/disp32&quot;</span> <span class="PreProc">loop-if-&gt;</span> label =&gt; <span class="Constant">&quot;0f 8f/jump-if-&gt; &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-&lt;=</span> =&gt; <span class="Constant">&quot;0f 8e/jump-if-&lt;= break/disp32&quot;</span> <span class="PreProc">break-if-&lt;=</span> label =&gt; <span class="Constant">&quot;0f 8e/jump-if-&lt;= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-&lt;=</span> =&gt; <span class="Constant">&quot;0f 8e/jump-if-&lt;= loop/disp32&quot;</span> <span class="PreProc">loop-if-&lt;=</span> label =&gt; <span class="Constant">&quot;0f 8e/jump-if-&lt;= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-&gt;=</span> =&gt; <span class="Constant">&quot;0f 8d/jump-if-&gt;= break/disp32&quot;</span> <span class="PreProc">break-if-&gt;=</span> label =&gt; <span class="Constant">&quot;0f 8d/jump-if-&gt;= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-&gt;=</span> =&gt; <span class="Constant">&quot;0f 8d/jump-if-&gt;= loop/disp32&quot;</span> <span class="PreProc">loop-if-&gt;=</span> label =&gt; <span class="Constant">&quot;0f 8d/jump-if-&gt;= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-addr&lt;</span> =&gt; <span class="Constant">&quot;0f 82/jump-if-addr&lt; break/disp32&quot;</span> <span class="PreProc">break-if-addr&lt;</span> label =&gt; <span class="Constant">&quot;0f 82/jump-if-addr&lt; &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-addr&lt;</span> =&gt; <span class="Constant">&quot;0f 82/jump-if-addr&lt; loop/disp32&quot;</span> <span class="PreProc">loop-if-addr&lt;</span> label =&gt; <span class="Constant">&quot;0f 82/jump-if-addr&lt; &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-addr&gt;</span> =&gt; <span class="Constant">&quot;0f 87/jump-if-addr&gt; break/disp32&quot;</span> <span class="PreProc">break-if-addr&gt;</span> label =&gt; <span class="Constant">&quot;0f 87/jump-if-addr&gt; &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-addr&gt;</span> =&gt; <span class="Constant">&quot;0f 87/jump-if-addr&gt; loop/disp32&quot;</span> <span class="PreProc">loop-if-addr&gt;</span> label =&gt; <span class="Constant">&quot;0f 87/jump-if-addr&gt; &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-addr&lt;=</span> =&gt; <span class="Constant">&quot;0f 86/jump-if-addr&lt;= break/disp32&quot;</span> <span class="PreProc">break-if-addr&lt;=</span> label =&gt; <span class="Constant">&quot;0f 86/jump-if-addr&lt;= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-addr&lt;=</span> =&gt; <span class="Constant">&quot;0f 86/jump-if-addr&lt;= loop/disp32&quot;</span> <span class="PreProc">loop-if-addr&lt;=</span> label =&gt; <span class="Constant">&quot;0f 86/jump-if-addr&lt;= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-addr&gt;=</span> =&gt; <span class="Constant">&quot;0f 83/jump-if-addr&gt;= break/disp32&quot;</span> <span class="PreProc">break-if-addr&gt;=</span> label =&gt; <span class="Constant">&quot;0f 83/jump-if-addr&gt;= &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-addr&gt;=</span> =&gt; <span class="Constant">&quot;0f 83/jump-if-addr&gt;= loop/disp32&quot;</span> <span class="PreProc">loop-if-addr&gt;=</span> label =&gt; <span class="Constant">&quot;0f 83/jump-if-addr&gt;= &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> Similar float variants like `<span class="PreProc">break-if-float&lt;`</span> are aliases for the corresponding `addr` equivalents. The x86 instruction set stupidly has floating-point operations only update a subset of flags. Four sets of conditional jumps are useful for detecting overflow. <span class="PreProc">break-if-carry</span> =&gt; <span class="Constant">&quot;0f 82/jump-if-carry break/disp32&quot;</span> <span class="PreProc">break-if-carry</span> label =&gt; <span class="Constant">&quot;0f 82/jump-if-carry &quot;</span> label <span class="Constant">&quot;/disp32&quot;</span> <span class="PreProc">loop-if-carry</span> =&gt; <span class="Constant">&quot;0f 82/jump-if-carry break/disp32&quot;</span> <span class="PreProc">loop-if-carry</span> label =&gt; <span class="Constant">&quot;0f 82/jump-if-carry &quot;</span> label <span class="Constant">&quot;/disp32&quot;</span> <span class="PreProc">break-if-not-carry</span> =&gt; <span class="Constant">&quot;0f 83/jump-if-not-carry break/disp32&quot;</span> <span class="PreProc">break-if-not-carry</span> label =&gt; <span class="Constant">&quot;0f 83/jump-if-not-carry &quot;</span> label <span class="Constant">&quot;/disp32&quot;</span> <span class="PreProc">loop-if-not-carry</span> =&gt; <span class="Constant">&quot;0f 83/jump-if-not-carry break/disp32&quot;</span> <span class="PreProc">loop-if-not-carry</span> label =&gt; <span class="Constant">&quot;0f 83/jump-if-not-carry &quot;</span> label <span class="Constant">&quot;/disp32&quot;</span> <span class="PreProc">break-if-overflow</span> =&gt; <span class="Constant">&quot;0f 80/jump-if-overflow break/disp32&quot;</span> <span class="PreProc">break-if-overflow</span> label =&gt; <span class="Constant">&quot;0f 80/jump-if-overflow &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-overflow</span> =&gt; <span class="Constant">&quot;0f 80/jump-if-overflow loop/disp32&quot;</span> <span class="PreProc">loop-if-overflow</span> label =&gt; <span class="Constant">&quot;0f 80/jump-if-overflow &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> <span class="PreProc">break-if-not-overflow</span> =&gt; <span class="Constant">&quot;0f 81/jump-if-not-overflow break/disp32&quot;</span> <span class="PreProc">break-if-not-overflow</span> label =&gt; <span class="Constant">&quot;0f 81/jump-if-not-overflow &quot;</span> label <span class="Constant">&quot;:break/disp32&quot;</span> <span class="PreProc">loop-if-not-overflow</span> =&gt; <span class="Constant">&quot;0f 81/jump-if-not-overflow loop/disp32&quot;</span> <span class="PreProc">loop-if-not-overflow</span> label =&gt; <span class="Constant">&quot;0f 81/jump-if-not-overflow &quot;</span> label <span class="Constant">&quot;:loop/disp32&quot;</span> All this relies on a convention that every `<span class="Delimiter">{}</span>` block is delimited by labels ending in `:<span class="PreProc">loop</span>` and `:<span class="PreProc">break</span>`. <span class="muComment">## Returns</span> The `<span class="PreProc">return</span>` instruction cleans up variable declarations just like an unconditional `<span class="PreProc">jump</span>` to end of function, but also emits a series of copies before the final `<span class="PreProc">jump</span>`, copying each argument of `<span class="PreProc">return</span>` to the register appropriate to the respective function output. This doesn't work if a function output register contains a later `<span class="PreProc">return</span>` argument (e.g. if the registers for two outputs are swapped in `<span class="PreProc">return</span>`), so you can't do that. <span class="PreProc">return</span> =&gt; <span class="Constant">&quot;c3/return&quot;</span> --- In the following instructions types are provided for clarity even if they must be provided in an earlier 'var' declaration. <span class="muComment"># Address operations</span> var/reg: (addr T) <span class="Special">&lt;-</span> address var2: T =&gt; <span class="Constant">&quot;8d/copy-address *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> <span class="muComment"># Array operations</span> var/reg: (addr T) <span class="Special">&lt;-</span> index arr/rega: (addr array T), idx/regi: int | if size-of(T) is <span class="Constant">1</span>, <span class="Constant">2</span>, <span class="Constant">4</span> or <span class="Constant">8</span> =&gt; <span class="Constant">&quot;81 7/subop/compare %&quot;</span> rega <span class="Constant">&quot; 0/imm32&quot;</span> <span class="Constant">&quot;0f 84/jump-if-= __mu-abort-null-index-base-address/disp32&quot;</span> <span class="Constant">&quot;(__check-mu-array-bounds *&quot;</span> rega <span class="Constant">&quot; %&quot;</span> regi <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> <span class="Constant">&quot;8d/copy-address *(&quot;</span> rega <span class="Constant">&quot;+&quot;</span> regi <span class="Constant">&quot;&lt;&lt;&quot;</span> log2(size-of(T)) <span class="Constant">&quot;+4) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (addr T) <span class="Special">&lt;-</span> index arr: (array T len), idx/regi: int =&gt; <span class="Constant">&quot;(__check-mu-array-bounds *(ebp+&quot;</span> arr.stack-offset <span class="Constant">&quot;) %&quot;</span> regi <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> <span class="Constant">&quot;8d/copy-address *(ebp+&quot;</span> regi <span class="Constant">&quot;&lt;&lt;&quot;</span> log2(size-of(T)) <span class="Constant">&quot;+&quot;</span> (arr.stack-offset + <span class="Constant">4</span>) <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (addr T) <span class="Special">&lt;-</span> index arr/rega: (addr array T), n =&gt; <span class="Constant">&quot;81 7/subop/compare %&quot;</span> rega <span class="Constant">&quot; 0/imm32&quot;</span> <span class="Constant">&quot;0f 84/jump-if-= __mu-abort-null-index-base-address/disp32&quot;</span> <span class="Constant">&quot;(__check-mu-array-bounds *&quot;</span> rega <span class="Constant">&quot; &quot;</span> n <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> <span class="Constant">&quot;8d/copy-address *(&quot;</span> rega <span class="Constant">&quot;+&quot;</span> (n*size-of(T)+<span class="Constant">4</span>) <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (addr T) <span class="Special">&lt;-</span> index arr: (array T len), n =&gt; <span class="Constant">&quot;(__check-mu-array-bounds *(ebp+&quot;</span> arr.stack-offset <span class="Constant">&quot;) &quot;</span> n <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> <span class="Constant">&quot;8d/copy-address *(ebp+&quot;</span> (arr.stack-offset+<span class="Constant">4</span>+n*size-of(T)) <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (offset T) <span class="Special">&lt;-</span> compute-offset arr: (addr array T), idx/regi: int <span class="muComment"># arr can be in reg or mem</span> =&gt; <span class="Constant">&quot;69/multiply %&quot;</span> regi <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;/imm32 &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (offset T) <span class="Special">&lt;-</span> compute-offset arr: (addr array T), idx: int <span class="muComment"># arr can be in reg or mem</span> =&gt; <span class="Constant">&quot;69/multiply *(ebp+&quot;</span> idx.stack-offset <span class="Constant">&quot;) &quot;</span> size-of(T) <span class="Constant">&quot;/imm32 &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (addr T) <span class="Special">&lt;-</span> index arr/rega: (addr array T), o/rego: (offset T) =&gt; <span class="Constant">&quot;81 7/subop/compare %&quot;</span> rega <span class="Constant">&quot; 0/imm32&quot;</span> <span class="Constant">&quot;0f 84/jump-if-= __mu-abort-null-index-base-address/disp32&quot;</span> <span class="Constant">&quot;(__check-mu-array-bounds %&quot;</span> rega <span class="Constant">&quot; %&quot;</span> rego <span class="Constant">&quot; 1 \&quot;&quot; function-name &quot;</span>\<span class="Constant">&quot;)&quot;</span> <span class="Constant">&quot;8d/copy-address *(&quot;</span> rega <span class="Constant">&quot;+&quot;</span> rego <span class="Constant">&quot;+4) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> Computing the length of an array is complex. var/reg: int <span class="Special">&lt;-</span> length arr/reg2: (addr array T) | if T is byte (TODO) =&gt; <span class="Constant">&quot;8b/-&gt; *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> | if size-of(T) is <span class="Constant">4</span> or <span class="Constant">8</span> or <span class="Constant">16</span> or <span class="Constant">32</span> or <span class="Constant">64</span> or <span class="Constant">128</span> =&gt; <span class="Constant">&quot;8b/-&gt; *&quot;</span> reg2 <span class="Constant">&quot; &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> <span class="Constant">&quot;c1/shift 5/subop/logic-right %&quot;</span> reg <span class="Constant">&quot; &quot;</span> log2(size-of(T)) <span class="Constant">&quot;/imm8&quot;</span> | otherwise x86 has no instruction to divide by a literal, so we need up to <span class="Constant">3</span> extra registers! eax/<span class="muRegEdx">edx</span> for division and say <span class="muRegEcx">ecx</span> =&gt; if reg is not eax <span class="Constant">&quot;50/push-eax&quot;</span> if reg is not <span class="muRegEcx">ecx</span> <span class="Constant">&quot;51/push-ecx&quot;</span> if reg is not <span class="muRegEdx">edx</span> <span class="Constant">&quot;52/push-edx&quot;</span> <span class="Constant">&quot;8b/-&gt; *&quot;</span> reg2 <span class="Constant">&quot; eax/r32&quot;</span> <span class="Constant">&quot;31/xor %edx 2/r32/edx&quot;</span> <span class="muComment"># sign-extend, but array size can't be negative</span> <span class="Constant">&quot;b9/copy-to-ecx &quot;</span> size-of(T) <span class="Constant">&quot;/imm32&quot;</span> <span class="Constant">&quot;f7 7/subop/idiv-eax-edx-by %ecx&quot;</span> if reg is not eax <span class="Constant">&quot;89/&lt;- %&quot;</span> reg <span class="Constant">&quot; 0/r32/eax&quot;</span> if reg is not <span class="muRegEdx">edx</span> <span class="Constant">&quot;5a/pop-to-edx&quot;</span> if reg is not <span class="muRegEcx">ecx</span> <span class="Constant">&quot;59/pop-to-ecx&quot;</span> if reg is not eax <span class="Constant">&quot;58/pop-to-eax&quot;</span> <span class="muComment"># User-defined types</span> If a record (product) type T was defined to have elements a, b, c, ... of types T_a, T_b, T_c, ..., then accessing one of those elements f of type T_f: var/reg: (addr T_f) <span class="Special">&lt;-</span> get var2/reg2: (addr T), f =&gt; <span class="Constant">&quot;81 7/subop/compare %&quot;</span> reg2 <span class="Constant">&quot; 0/imm32&quot;</span> <span class="Constant">&quot;0f 84/jump-if-= __mu-abort-null-get-base-address/disp32&quot;</span> <span class="Constant">&quot;8d/copy-address *(&quot;</span> reg2 <span class="Constant">&quot;+&quot;</span> offset(f) <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> var/reg: (addr T_f) <span class="Special">&lt;-</span> get var2: T, f =&gt; <span class="Constant">&quot;8d/copy-address *(ebp+&quot;</span> var2.stack-offset <span class="Constant">&quot;+&quot;</span> offset(f) <span class="Constant">&quot;) &quot;</span> reg <span class="Constant">&quot;/r32&quot;</span> When the base is an address we perform a null check. <span class="muComment"># Allocating memory</span> allocate in: (addr handle T) =&gt; <span class="Constant">&quot;(allocate Heap &quot;</span> size-of(T) <span class="Constant">&quot; &quot;</span> in <span class="Constant">&quot;)&quot;</span> populate in: (addr handle array T), num <span class="muComment"># can be literal or variable on stack or register</span> =&gt; <span class="Constant">&quot;(allocate-array2 Heap &quot;</span> size-of(T) <span class="Constant">&quot; &quot;</span> num <span class="Constant">&quot; &quot;</span> in <span class="Constant">&quot;)&quot;</span> populate-stream in: (addr handle stream T), num <span class="muComment"># can be literal or variable on stack or register</span> =&gt; <span class="Constant">&quot;(new-stream Heap &quot;</span> size-of(T) <span class="Constant">&quot; &quot;</span> num <span class="Constant">&quot; &quot;</span> in <span class="Constant">&quot;)&quot;</span> <span class="muComment"># Some miscellaneous helpers to avoid error-prone size computations</span> clear x: (addr T) =&gt; <span class="Constant">&quot;(zero-out &quot;</span> s <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> read-from-stream s: (addr stream T), out: (addr T) =&gt; <span class="Constant">&quot;(read-from-stream &quot;</span> s <span class="Constant">&quot; &quot;</span> out <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> write-to-stream s: (addr stream T), in: (addr T) =&gt; <span class="Constant">&quot;(write-to-stream &quot;</span> s <span class="Constant">&quot; &quot;</span> in <span class="Constant">&quot; &quot;</span> size-of(T) <span class="Constant">&quot;)&quot;</span> vim&#0058;ft=mu:nowrap:textwidth=<span class="Constant">0</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->