https://github.com/akkartik/mu/blob/master/061read-byte.subx
  1 # read-byte-buffered: one higher-level abstraction atop 'read'.
  2 #
  3 # There are many situations where 'read' is a lot to manage, and we need
  4 # to abstract some details away. One of them is when we want to read a file
  5 # character by character. In this situation we follow C's FILE data structure,
  6 # which manages the underlying file descriptor together with the buffer it
  7 # reads into. We call our version 'buffered-file'. Should be useful with other
  8 # primitives as well, in later layers.
  9 
 10 == data
 11 
 12 # The buffered file for standard input. Also illustrates the layout for
 13 # buffered-file: a pointer to the backing store, followed by a 'buffer' stream
 14 Stdin:  # buffered-file
 15     # file descriptor or (addr stream byte)
 16     0/imm32  # standard input
 17 $Stdin->buffer:
 18     # inlined fields for a stream
 19     #   current write index
 20     0/imm32
 21     #   current read index
 22     0/imm32
 23     #   length
 24     8/imm32
 25     #   data
 26     00 00 00 00 00 00 00 00  # 8 bytes
 27 
 28 # TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But
 29 # I don't want to type in 1024 bytes here.
 30 
 31 == code
 32 #   instruction                     effective address                                                   register    displacement    immediate
 33 # . op          subop               mod             rm32          base        index         scale       r32
 34 # . 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
 35 
 36 # return next byte value in eax, with top 3 bytes cleared.
 37 # On reaching end of file, return 0xffffffff (Eof).
 38 read-byte-buffered:  # f: (addr buffered-file) -> byte-or-Eof/eax
 39     # . prologue
 40     55/push-ebp
 41     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 42     # . save registers
 43     51/push-ecx
 44     56/push-esi
 45     # esi = f
 46     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
 47     # ecx = f->read
 48     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(esi+8) to ecx
 49     # if (f->read >= f->write) populate stream from file
 50     3b/compare                      1/mod/*+disp8   6/rm32/esi   <
<!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 - 035compute_segment_address.cc</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.LineNr { }
.Constant { color: #008787; }
.Comment { color: #005faf; }
.Normal { color: #000000; background-color: #c6c6c6; padding-bottom: 1px; }
.Special { color: #d70000; }
.Identifier { color: #af5f00; }
.Delimiter { color: #c000c0; }
-->
</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;
  }
  var 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/035compute_segment_address.cc'>https://github.com/akkartik/mu/blob/master/035compute_segment_address.cc</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment">//: ELF binaries have finicky rules about the precise alignment each segment</span>
<span id="L2" class="LineNr"> 2 </span><span class="Comment">//: should start at. They depend on the amount of code in a program.</span>
<span id="L3" class="LineNr"> 3 </span><span class="Comment">//: We shouldn't expect people to adjust segment addresses everytime they make</span>
<span id="L4" class="LineNr"> 4 </span><span class="Comment">//: a change to their programs.</span>
<span id="L5" class="LineNr"> 5 </span><span class="Comment">//: Let's start taking the given segment addresses as guidelines, and adjust</span>
<span id="L6" class="LineNr"> 6 </span><span class="Comment">//: them as necessary.</span>
<span id="L7" class="LineNr"> 7 </span><span class="Comment">//: This gives up a measure of control in placing code and data.</span>
<span id="L8" class="LineNr"> 8 </span>
<span id="L9" class="LineNr"> 9 </span><span class="Normal">void</span> <a href='035compute_segment_address.cc.html#L9'>test_segment_name</a><span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span id="L10" class="LineNr">10 </span>  <a href='011run.cc.html#L82'>run</a><span class="Delimiter">(</span>
<span id="L11" class="LineNr">11 </span>      <span class="Constant">&quot;== code 0x09000000\n&quot;</span>
<span id="L12" class="LineNr">12 </span>      <span class="Constant">&quot;05/add-to-EAX  0x0d0c0b0a/imm32\n&quot;</span>
<span id="L13" class="LineNr">13 </span>      <span class="Comment">// code starts at 0x09000000 + p_offset, which is 0x54 for a single-segment binary</span>
<span id="L14" class="LineNr">14 </span>  <span class="Delimiter">);</span>
<span id="L15" class="LineNr">15 </span>  <a href='003trace.cc.html#L292'>CHECK_TRACE_CONTENTS</a><span class="Delimiter">(</span>
<span id="L16" class="LineNr">16 </span>      <span class="Constant">&quot;load: 0x09000054 -&gt; 05\n&quot;</span>
<span id="L17" class="LineNr">17 </span>      <span class="Constant">&quot;load: 0x09000055 -&gt; 0a\n&quot;</span>
<span id="L18" class="LineNr">18 </span>      <span class="Constant">&quot;load: 0x09000056 -&gt; 0b\n&quot;</span>
<span id="L19" class="LineNr">19 </span>      <span class="Constant">&quot;load: 0x09000057 -&gt; 0c\n&quot;</span>
<span id="L20" class="LineNr">20 </span>      <span class="Constant">&quot;load: 0x09000058 -&gt; 0d\n&quot;</span>
<span id="L21" class="LineNr">21 </span>      <span class="Constant">&quot;run: add imm32 0x0d0c0b0a to EAX\n&quot;</span>
<span id="L22" class="LineNr">22 </span>      <span class="Constant">&quot;run: storing 0x0d0c0b0a\n&quot;</span>
<span id="L23" class="LineNr">23 </span>  <span class="Delimiter">);</span>
<span id="L24" class="LineNr">24 </span><span class="Delimiter">}</span>
<span id="L25" class="LineNr">25 </span>
<span id="L26" class="LineNr">26 </span><span class="Comment">//: compute segment address</span>
<span id="L27" class=