diff options
Diffstat (limited to 'html/linux/apps')
49 files changed, 8644 insertions, 0 deletions
diff --git a/html/linux/apps/advent2017/1a.mu.html b/html/linux/apps/advent2017/1a.mu.html new file mode 100644 index 00000000..88f60dbc --- /dev/null +++ b/html/linux/apps/advent2017/1a.mu.html @@ -0,0 +1,131 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2017/1a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2017/1a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2017/1a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Advent of code 2017, problem 1a</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment"># <a href="https://adventofcode.com/2017/day/1">https://adventofcode.com/2017/day/1</a></span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># Mu solution by Sumeet Agarwal and Kartik Agaram</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># <a href="https://archive.org/details/2021-06-02-akkartik-sumeet">https://archive.org/details/2021-06-02-akkartik-sumeet</a></span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment">#</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># To build on Linux:</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># $ cd mu/linux</span> +<span id="L10" class="LineNr">10 </span><span class="muComment"># $ ./translate apps/advent2017/1a.mu # emits a.elf</span> +<span id="L11" class="LineNr">11 </span><span class="muComment"># To run on Linux:</span> +<span id="L12" class="LineNr">12 </span><span class="muComment"># Download <a href="https://adventofcode.com/2017/day/1/input">https://adventofcode.com/2017/day/1/input</a></span> +<span id="L13" class="LineNr">13 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L14" class="LineNr">14 </span><span class="muComment"># Type in the number returned at <a href="https://adventofcode.com/2017/day/1">https://adventofcode.com/2017/day/1</a></span> +<span id="L15" class="LineNr">15 </span> +<span id="L16" class="LineNr">16 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1a.mu.html#L16'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L17" class="LineNr">17 </span> <span class="PreProc">var</span> input_stream: (stream byte <span class="Constant">0x8000</span>) +<span id="L18" class="LineNr">18 </span> <span class="PreProc">var</span> input_stream_addr/<span class="muRegEsi">esi</span>: (addr stream byte) <span class="Special"><-</span> address input_stream +<span id="L19" class="LineNr">19 </span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> sum/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L21" class="LineNr">21 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> input_stream_addr +<span id="L22" class="LineNr">22 </span> +<span id="L23" class="LineNr">23 </span> <span class="PreProc">var</span> temp/eax: int <span class="Special"><-</span> <a href='1a.mu.html#L60'>read_digit</a> input_stream_addr +<span id="L24" class="LineNr">24 </span> <span class="PreProc">var</span> first_digit/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy temp +<span id="L25" class="LineNr">25 </span> <span class="PreProc">var</span> this_digit/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy temp +<span id="L26" class="LineNr">26 </span> +<span id="L27" class="LineNr">27 </span> <span class="Delimiter">{</span> +<span id="L28" class="LineNr">28 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> input_stream_addr +<span id="L29" class="LineNr">29 </span> compare done?, <span class="Constant">1</span> +<span id="L30" class="LineNr">30 </span> <span class="PreProc">break-if-=</span> +<span id="L31" class="LineNr">31 </span> +<span id="L32" class="LineNr">32 </span> <span class="PreProc">var</span> next_digit/eax: int <span class="Special"><-</span> <a href='1a.mu.html#L60'>read_digit</a> input_stream_addr +<span id="L33" class="LineNr">33 </span> <span class="muComment"># hacky newline check</span> +<span id="L34" class="LineNr">34 </span> compare next_digit, <span class="Constant">0</span> +<span id="L35" class="LineNr">35 </span> <span class="PreProc">break-if-<</span> +<span id="L36" class="LineNr">36 </span> +<span id="L37" class="LineNr">37 </span> <span class="Delimiter">{</span> +<span id="L38" class="LineNr">38 </span> compare this_digit, next_digit +<span id="L39" class="LineNr">39 </span> <span class="PreProc">break-if-!=</span> +<span id="L40" class="LineNr">40 </span> sum <span class="Special"><-</span> add this_digit +<span id="L41" class="LineNr">41 </span> <span class="Delimiter">}</span> +<span id="L42" class="LineNr">42 </span> +<span id="L43" class="LineNr">43 </span> this_digit <span class="Special"><-</span> copy next_digit +<span id="L44" class="LineNr">44 </span> +<span id="L45" class="LineNr">45 </span> <span class="PreProc">loop</span> +<span id="L46" class="LineNr">46 </span> <span class="Delimiter">}</span> +<span id="L47" class="LineNr">47 </span> +<span id="L48" class="LineNr">48 </span> <span class="muComment"># the last iteration will need to compare the last number to the first</span> +<span id="L49" class="LineNr">49 </span> <span class="Delimiter">{</span> +<span id="L50" class="LineNr">50 </span> compare this_digit, first_digit +<span id="L51" class="LineNr">51 </span> <span class="PreProc">break-if-!=</span> +<span id="L52" class="LineNr">52 </span> sum <span class="Special"><-</span> add this_digit +<span id="L53" class="LineNr">53 </span> <span class="Delimiter">}</span> +<span id="L54" class="LineNr">54 </span> +<span id="L55" class="LineNr">55 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, sum +<span id="L56" class="LineNr">56 </span> +<span id="L57" class="LineNr">57 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/ok +<span id="L58" class="LineNr">58 </span><span class="Delimiter">}</span> +<span id="L59" class="LineNr">59 </span> +<span id="L60" class="LineNr">60 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1a.mu.html#L60'>read_digit</a></span> input_stream_addr: (addr stream byte)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L61" class="LineNr">61 </span> <span class="PreProc">var</span> next_digit/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> input_stream_addr +<span id="L62" class="LineNr">62 </span> next_digit <span class="Special"><-</span> subtract <span class="Constant">0x30</span> +<span id="L63" class="LineNr">63 </span> <span class="PreProc">var</span> next_digit/eax: int <span class="Special"><-</span> copy next_digit +<span id="L64" class="LineNr">64 </span> <span class="PreProc">return</span> next_digit +<span id="L65" class="LineNr">65 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/1a.mu.html b/html/linux/apps/advent2020/1a.mu.html new file mode 100644 index 00000000..e75fe8fc --- /dev/null +++ b/html/linux/apps/advent2020/1a.mu.html @@ -0,0 +1,165 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/1a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/1a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/1a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/1">https://adventofcode.com/2020/day/1</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/1a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># found</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># 1353 667</span> +<span id="L10" class="LineNr">10 </span><span class="muComment"># 902451</span> +<span id="L11" class="LineNr">11 </span><span class="muComment">#</span> +<span id="L12" class="LineNr">12 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L13" class="LineNr">13 </span> +<span id="L14" class="LineNr">14 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1a.mu.html#L14'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L15" class="LineNr">15 </span> <span class="muComment"># data structure</span> +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> numbers-storage: (array int <span class="Constant">0x100</span>) <span class="muComment"># 256 ints</span> +<span id="L17" class="LineNr">17 </span> <span class="PreProc">var</span> numbers/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> address numbers-storage +<span id="L18" class="LineNr">18 </span> <span class="PreProc">var</span> numbers-index/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L19" class="LineNr">19 </span> <span class="muComment"># phase 1: parse each line from stdin and add it to numbers</span> +<span id="L20" class="LineNr">20 </span> <span class="Delimiter">{</span> +<span id="L21" class="LineNr">21 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L23" class="LineNr">23 </span> <span class="Delimiter">{</span> +<span id="L24" class="LineNr">24 </span><span class="CommentedCode">#? print-string 0, "== iter\n"</span> +<span id="L25" class="LineNr">25 </span> <span class="muComment"># read line from stdin</span> +<span id="L26" class="LineNr">26 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L27" class="LineNr">27 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L28" class="LineNr">28 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L30" class="LineNr">30 </span> compare done?, <span class="Constant">0</span>/false +<span id="L31" class="LineNr">31 </span> <span class="PreProc">break-if-!=</span> +<span id="L32" class="LineNr">32 </span><span class="CommentedCode">#? print-stream-to-real-screen line</span> +<span id="L33" class="LineNr">33 </span> <span class="muComment"># convert line to int and append it to numbers</span> +<span id="L34" class="LineNr">34 </span> <span class="PreProc">var</span> n/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L48'>parse-decimal-int-from-stream</a> line +<span id="L35" class="LineNr">35 </span><span class="CommentedCode">#? print-int32-decimal 0, n</span> +<span id="L36" class="LineNr">36 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L37" class="LineNr">37 </span> <span class="PreProc">var</span> dest/<span class="muRegEbx">ebx</span>: (addr int) <span class="Special"><-</span> index numbers, numbers-index +<span id="L38" class="LineNr">38 </span> copy-to *dest, n +<span id="L39" class="LineNr">39 </span> numbers-index <span class="Special"><-</span> increment +<span id="L40" class="LineNr">40 </span><span class="CommentedCode">#? print-string 0, "== "</span> +<span id="L41" class="LineNr">41 </span><span class="CommentedCode">#? print-int32-decimal 0, numbers-index</span> +<span id="L42" class="LineNr">42 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L43" class="LineNr">43 </span> <span class="PreProc">loop</span> +<span id="L44" class="LineNr">44 </span> <span class="Delimiter">}</span> +<span id="L45" class="LineNr">45 </span> <span class="Delimiter">}</span> +<span id="L46" class="LineNr">46 </span> <span class="muComment"># phase 2: for each number in the array, check if 2020-it is in the rest of</span> +<span id="L47" class="LineNr">47 </span> <span class="muComment"># the array</span> +<span id="L48" class="LineNr">48 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L49" class="LineNr">49 </span> <span class="Delimiter">{</span> +<span id="L50" class="LineNr">50 </span> compare i, numbers-index +<span id="L51" class="LineNr">51 </span> <span class="PreProc">break-if->=</span> +<span id="L52" class="LineNr">52 </span> <span class="PreProc">var</span> src/<span class="muRegEbx">ebx</span>: (addr int) <span class="Special"><-</span> index numbers, i +<span id="L53" class="LineNr">53 </span><span class="CommentedCode">#? print-int32-decimal 0, *src</span> +<span id="L54" class="LineNr">54 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L55" class="LineNr">55 </span> <span class="PreProc">var</span> target/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0x7e4</span> <span class="muComment"># 2020</span> +<span id="L56" class="LineNr">56 </span> target <span class="Special"><-</span> subtract *src +<span id="L57" class="LineNr">57 </span> <span class="Delimiter">{</span> +<span id="L58" class="LineNr">58 </span> <span class="PreProc">var</span> found?/eax: boolean <span class="Special"><-</span> <a href='1a.mu.html#L77'>find-after</a> numbers, i, target +<span id="L59" class="LineNr">59 </span> compare found?, <span class="Constant">0</span>/false +<span id="L60" class="LineNr">60 </span> <span class="PreProc">break-if-=</span> +<span id="L61" class="LineNr">61 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"found\n"</span> +<span id="L62" class="LineNr">62 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, *src +<span id="L63" class="LineNr">63 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L64" class="LineNr">64 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, target +<span id="L65" class="LineNr">65 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L66" class="LineNr">66 </span> target <span class="Special"><-</span> multiply *src +<span id="L67" class="LineNr">67 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, target +<span id="L68" class="LineNr">68 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L69" class="LineNr">69 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/success +<span id="L70" class="LineNr">70 </span> <span class="Delimiter">}</span> +<span id="L71" class="LineNr">71 </span> i <span class="Special"><-</span> increment +<span id="L72" class="LineNr">72 </span> <span class="PreProc">loop</span> +<span id="L73" class="LineNr">73 </span> <span class="Delimiter">}</span> +<span id="L74" class="LineNr">74 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/not-found +<span id="L75" class="LineNr">75 </span><span class="Delimiter">}</span> +<span id="L76" class="LineNr">76 </span> +<span id="L77" class="LineNr">77 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1a.mu.html#L77'>find-after</a></span> _numbers: (addr array int), start: int, _target: int<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L78" class="LineNr">78 </span> <span class="PreProc">var</span> numbers/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> copy _numbers +<span id="L79" class="LineNr">79 </span> <span class="PreProc">var</span> target/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy _target +<span id="L80" class="LineNr">80 </span> <span class="PreProc">var</span> len/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> length numbers +<span id="L81" class="LineNr">81 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy start +<span id="L82" class="LineNr">82 </span> i <span class="Special"><-</span> increment +<span id="L83" class="LineNr">83 </span> <span class="Delimiter">{</span> +<span id="L84" class="LineNr">84 </span> compare i, len +<span id="L85" class="LineNr">85 </span> <span class="PreProc">break-if->=</span> +<span id="L86" class="LineNr">86 </span> <span class="PreProc">var</span> src/<span class="muRegEdx">edx</span>: (addr int) <span class="Special"><-</span> index numbers, i +<span id="L87" class="LineNr">87 </span> <span class="muComment"># if *src == target, return true</span> +<span id="L88" class="LineNr">88 </span> compare *src, target +<span id="L89" class="LineNr">89 </span> <span class="Delimiter">{</span> +<span id="L90" class="LineNr">90 </span> <span class="PreProc">break-if-!=</span> +<span id="L91" class="LineNr">91 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L92" class="LineNr">92 </span> <span class="Delimiter">}</span> +<span id="L93" class="LineNr">93 </span> i <span class="Special"><-</span> increment +<span id="L94" class="LineNr">94 </span> <span class="PreProc">loop</span> +<span id="L95" class="LineNr">95 </span> <span class="Delimiter">}</span> +<span id="L96" class="LineNr">96 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L97" class="LineNr">97 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/1b.mu.html b/html/linux/apps/advent2020/1b.mu.html new file mode 100644 index 00000000..4346642b --- /dev/null +++ b/html/linux/apps/advent2020/1b.mu.html @@ -0,0 +1,181 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/1b.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/1b.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/1b.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/1">https://adventofcode.com/2020/day/1</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/1b.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># found</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># 143 407 1470</span> +<span id="L10" class="LineNr"> 10 </span><span class="muComment"># 85555470</span> +<span id="L11" class="LineNr"> 11 </span><span class="muComment">#</span> +<span id="L12" class="LineNr"> 12 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L13" class="LineNr"> 13 </span> +<span id="L14" class="LineNr"> 14 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1b.mu.html#L14'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L15" class="LineNr"> 15 </span> <span class="muComment"># data structure</span> +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> numbers-storage: (array int <span class="Constant">0x100</span>) <span class="muComment"># 256 ints</span> +<span id="L17" class="LineNr"> 17 </span> <span class="PreProc">var</span> numbers/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> address numbers-storage +<span id="L18" class="LineNr"> 18 </span> <span class="PreProc">var</span> numbers-index/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L19" class="LineNr"> 19 </span> <span class="muComment"># phase 1: parse each line from stdin and add it to numbers</span> +<span id="L20" class="LineNr"> 20 </span> <span class="Delimiter">{</span> +<span id="L21" class="LineNr"> 21 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L23" class="LineNr"> 23 </span> <span class="Delimiter">{</span> +<span id="L24" class="LineNr"> 24 </span><span class="CommentedCode">#? print-string 0, "== iter\n"</span> +<span id="L25" class="LineNr"> 25 </span> <span class="muComment"># read line from stdin</span> +<span id="L26" class="LineNr"> 26 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L27" class="LineNr"> 27 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L28" class="LineNr"> 28 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L29" class="LineNr"> 29 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L30" class="LineNr"> 30 </span> compare done?, <span class="Constant">0</span>/false +<span id="L31" class="LineNr"> 31 </span> <span class="PreProc">break-if-!=</span> +<span id="L32" class="LineNr"> 32 </span><span class="CommentedCode">#? print-stream-to-real-screen line</span> +<span id="L33" class="LineNr"> 33 </span> <span class="muComment"># convert line to int and append it to numbers</span> +<span id="L34" class="LineNr"> 34 </span> <span class="PreProc">var</span> n/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L48'>parse-decimal-int-from-stream</a> line +<span id="L35" class="LineNr"> 35 </span><span class="CommentedCode">#? print-int32-decimal 0, n</span> +<span id="L36" class="LineNr"> 36 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L37" class="LineNr"> 37 </span> <span class="PreProc">var</span> dest/<span class="muRegEbx">ebx</span>: (addr int) <span class="Special"><-</span> index numbers, numbers-index +<span id="L38" class="LineNr"> 38 </span> copy-to *dest, n +<span id="L39" class="LineNr"> 39 </span> numbers-index <span class="Special"><-</span> increment +<span id="L40" class="LineNr"> 40 </span><span class="CommentedCode">#? print-string 0, "== "</span> +<span id="L41" class="LineNr"> 41 </span><span class="CommentedCode">#? print-int32-decimal 0, numbers-index</span> +<span id="L42" class="LineNr"> 42 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L43" class="LineNr"> 43 </span> <span class="PreProc">loop</span> +<span id="L44" class="LineNr"> 44 </span> <span class="Delimiter">}</span> +<span id="L45" class="LineNr"> 45 </span> <span class="Delimiter">}</span> +<span id="L46" class="LineNr"> 46 </span> <span class="muComment"># phase 2: for every pair of distinct numbers, check if the rest of the</span> +<span id="L47" class="LineNr"> 47 </span> <span class="muComment"># array has 2020-it</span> +<span id="L48" class="LineNr"> 48 </span> <span class="PreProc">var</span> i/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L49" class="LineNr"> 49 </span> <span class="Delimiter">{</span> +<span id="L50" class="LineNr"> 50 </span> compare i, numbers-index +<span id="L51" class="LineNr"> 51 </span> <span class="PreProc">break-if->=</span> +<span id="L52" class="LineNr"> 52 </span> <span class="muComment"># for j from i+1 to end</span> +<span id="L53" class="LineNr"> 53 </span> <span class="PreProc">var</span> j/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy i +<span id="L54" class="LineNr"> 54 </span> j <span class="Special"><-</span> increment +<span id="L55" class="LineNr"> 55 </span> <span class="Delimiter">{</span> +<span id="L56" class="LineNr"> 56 </span> compare j, numbers-index +<span id="L57" class="LineNr"> 57 </span> <span class="PreProc">break-if->=</span> +<span id="L58" class="LineNr"> 58 </span> <span class="Delimiter">{</span> +<span id="L59" class="LineNr"> 59 </span> compare i, j +<span id="L60" class="LineNr"> 60 </span> <span class="PreProc">break-if-=</span> +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">var</span> target/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy <span class="Constant">0x7e4</span> <span class="muComment"># 2020</span> +<span id="L62" class="LineNr"> 62 </span> <span class="PreProc">var</span> src/<span class="muRegEdi">edi</span>: (addr int) <span class="Special"><-</span> index numbers, i +<span id="L63" class="LineNr"> 63 </span> target <span class="Special"><-</span> subtract *src +<span id="L64" class="LineNr"> 64 </span> <span class="PreProc">var</span> src2/<span class="muRegEcx">ecx</span>: (addr int) <span class="Special"><-</span> index numbers, j +<span id="L65" class="LineNr"> 65 </span> target <span class="Special"><-</span> subtract *src2 +<span id="L66" class="LineNr"> 66 </span> <span class="Delimiter">{</span> +<span id="L67" class="LineNr"> 67 </span> <span class="PreProc">var</span> found?/eax: boolean <span class="Special"><-</span> <a href='1b.mu.html#L93'>find-after</a> numbers, j, target +<span id="L68" class="LineNr"> 68 </span> compare found?, <span class="Constant">0</span>/false +<span id="L69" class="LineNr"> 69 </span> <span class="PreProc">break-if-=</span> +<span id="L70" class="LineNr"> 70 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"found\n"</span> +<span id="L71" class="LineNr"> 71 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, *src +<span id="L72" class="LineNr"> 72 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L73" class="LineNr"> 73 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, *src2 +<span id="L74" class="LineNr"> 74 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L75" class="LineNr"> 75 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, target +<span id="L76" class="LineNr"> 76 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L77" class="LineNr"> 77 </span> target <span class="Special"><-</span> multiply *src +<span id="L78" class="LineNr"> 78 </span> target <span class="Special"><-</span> multiply *src2 +<span id="L79" class="LineNr"> 79 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, target +<span id="L80" class="LineNr"> 80 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L81" class="LineNr"> 81 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/success +<span id="L82" class="LineNr"> 82 </span> <span class="Delimiter">}</span> +<span id="L83" class="LineNr"> 83 </span> <span class="Delimiter">}</span> +<span id="L84" class="LineNr"> 84 </span> j <span class="Special"><-</span> increment +<span id="L85" class="LineNr"> 85 </span> <span class="PreProc">loop</span> +<span id="L86" class="LineNr"> 86 </span> <span class="Delimiter">}</span> +<span id="L87" class="LineNr"> 87 </span> i <span class="Special"><-</span> increment +<span id="L88" class="LineNr"> 88 </span> <span class="PreProc">loop</span> +<span id="L89" class="LineNr"> 89 </span> <span class="Delimiter">}</span> +<span id="L90" class="LineNr"> 90 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/not-found +<span id="L91" class="LineNr"> 91 </span><span class="Delimiter">}</span> +<span id="L92" class="LineNr"> 92 </span> +<span id="L93" class="LineNr"> 93 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='1b.mu.html#L93'>find-after</a></span> _numbers: (addr array int), start: int, _target: int<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L94" class="LineNr"> 94 </span> <span class="PreProc">var</span> numbers/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> copy _numbers +<span id="L95" class="LineNr"> 95 </span> <span class="PreProc">var</span> target/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy _target +<span id="L96" class="LineNr"> 96 </span> <span class="PreProc">var</span> len/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> length numbers +<span id="L97" class="LineNr"> 97 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy start +<span id="L98" class="LineNr"> 98 </span> i <span class="Special"><-</span> increment +<span id="L99" class="LineNr"> 99 </span> <span class="Delimiter">{</span> +<span id="L100" class="LineNr">100 </span> compare i, len +<span id="L101" class="LineNr">101 </span> <span class="PreProc">break-if->=</span> +<span id="L102" class="LineNr">102 </span> <span class="PreProc">var</span> src/<span class="muRegEdx">edx</span>: (addr int) <span class="Special"><-</span> index numbers, i +<span id="L103" class="LineNr">103 </span> <span class="muComment"># if *src == target, return true</span> +<span id="L104" class="LineNr">104 </span> compare *src, target +<span id="L105" class="LineNr">105 </span> <span class="Delimiter">{</span> +<span id="L106" class="LineNr">106 </span> <span class="PreProc">break-if-!=</span> +<span id="L107" class="LineNr">107 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L108" class="LineNr">108 </span> <span class="Delimiter">}</span> +<span id="L109" class="LineNr">109 </span> i <span class="Special"><-</span> increment +<span id="L110" class="LineNr">110 </span> <span class="PreProc">loop</span> +<span id="L111" class="LineNr">111 </span> <span class="Delimiter">}</span> +<span id="L112" class="LineNr">112 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L113" class="LineNr">113 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/2a.mu.html b/html/linux/apps/advent2020/2a.mu.html new file mode 100644 index 00000000..2e6e3488 --- /dev/null +++ b/html/linux/apps/advent2020/2a.mu.html @@ -0,0 +1,159 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/2a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/2a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/2a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/2">https://adventofcode.com/2020/day/2</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/2a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr">10 </span> +<span id="L11" class="LineNr">11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='2a.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> valid-password-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L14" class="LineNr">14 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> slice-storage: slice +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> slice/<span class="muRegEcx">ecx</span>: (addr slice) <span class="Special"><-</span> address slice-storage +<span id="L17" class="LineNr">17 </span> <span class="Delimiter">{</span> +<span id="L18" class="LineNr">18 </span> <span class="muComment"># read line from stdin</span> +<span id="L19" class="LineNr">19 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L20" class="LineNr">20 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L21" class="LineNr">21 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L23" class="LineNr">23 </span> compare done?, <span class="Constant">0</span>/false +<span id="L24" class="LineNr">24 </span> <span class="PreProc">break-if-!=</span> +<span id="L25" class="LineNr">25 </span> <a href='../../304screen.subx.html#L147'>print-stream-to-real-screen</a> line +<span id="L26" class="LineNr">26 </span> <span class="muComment"># slice = next-token(line, '-')</span> +<span id="L27" class="LineNr">27 </span> <a href='../../124next-token.subx.html#L10'>next-token</a> line, <span class="Constant">0x2d</span>, slice +<span id="L28" class="LineNr">28 </span> <span class="muComment"># start = parse-int(slice)</span> +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> _start/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> slice +<span id="L30" class="LineNr">30 </span> <span class="PreProc">var</span> start/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy _start +<span id="L31" class="LineNr">31 </span> <span class="PreProc">var</span> dash/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line <span class="muComment"># skip '-'</span> +<span id="L32" class="LineNr">32 </span> <span class="muComment"># slice = next-token(line, ' ')</span> +<span id="L33" class="LineNr">33 </span> <a href='../../124next-token.subx.html#L10'>next-token</a> line, <span class="Constant">0x20</span>, slice +<span id="L34" class="LineNr">34 </span> <span class="PreProc">var</span> _end/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> slice +<span id="L35" class="LineNr">35 </span> <span class="PreProc">var</span> end/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy _end +<span id="L36" class="LineNr">36 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, start +<span id="L37" class="LineNr">37 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L38" class="LineNr">38 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, end +<span id="L39" class="LineNr">39 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L40" class="LineNr">40 </span> <span class="muComment"># letter = next non-space</span> +<span id="L41" class="LineNr">41 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L42" class="LineNr">42 </span> <span class="PreProc">var</span> letter/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line +<span id="L43" class="LineNr">43 </span> <span class="muComment"># skip some stuff</span> +<span id="L44" class="LineNr">44 </span> <span class="Delimiter">{</span> +<span id="L45" class="LineNr">45 </span> <span class="PreProc">var</span> colon/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line <span class="muComment"># skip ':'</span> +<span id="L46" class="LineNr">46 </span> <span class="Delimiter">}</span> +<span id="L47" class="LineNr">47 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L48" class="LineNr">48 </span> <span class="muComment"># now check the rest of the line</span> +<span id="L49" class="LineNr">49 </span> <span class="PreProc">var</span> <a href='2a.mu.html#L63'>valid?</a>/eax: boolean <span class="Special"><-</span> <a href='2a.mu.html#L63'>valid?</a> start, end, letter, line +<span id="L50" class="LineNr">50 </span> compare <a href='2a.mu.html#L63'>valid?</a>, <span class="Constant">0</span>/false +<span id="L51" class="LineNr">51 </span> <span class="Delimiter">{</span> +<span id="L52" class="LineNr">52 </span> <span class="PreProc">break-if-=</span> +<span id="L53" class="LineNr">53 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"valid!\n"</span> +<span id="L54" class="LineNr">54 </span> valid-password-count <span class="Special"><-</span> increment +<span id="L55" class="LineNr">55 </span> <span class="Delimiter">}</span> +<span id="L56" class="LineNr">56 </span> <span class="PreProc">loop</span> +<span id="L57" class="LineNr">57 </span> <span class="Delimiter">}</span> +<span id="L58" class="LineNr">58 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-password-count +<span id="L59" class="LineNr">59 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L60" class="LineNr">60 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L61" class="LineNr">61 </span><span class="Delimiter">}</span> +<span id="L62" class="LineNr">62 </span> +<span id="L63" class="LineNr">63 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='2a.mu.html#L63'>valid?</a></span> start: int, end: int, letter: byte, password: (addr stream byte)<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L64" class="LineNr">64 </span> <span class="PreProc">var</span> letter-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L65" class="LineNr">65 </span> <span class="muComment"># for every c in password</span> +<span id="L66" class="LineNr">66 </span> <span class="muComment"># if (c == letter)</span> +<span id="L67" class="LineNr">67 </span> <span class="muComment"># ++letter-count</span> +<span id="L68" class="LineNr">68 </span> <span class="Delimiter">{</span> +<span id="L69" class="LineNr">69 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> password +<span id="L70" class="LineNr">70 </span> compare done?, <span class="Constant">0</span>/false +<span id="L71" class="LineNr">71 </span> <span class="PreProc">break-if-!=</span> +<span id="L72" class="LineNr">72 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> password +<span id="L73" class="LineNr">73 </span> compare c, letter +<span id="L74" class="LineNr">74 </span> <span class="Delimiter">{</span> +<span id="L75" class="LineNr">75 </span> <span class="PreProc">break-if-!=</span> +<span id="L76" class="LineNr">76 </span> letter-count <span class="Special"><-</span> increment +<span id="L77" class="LineNr">77 </span> <span class="Delimiter">}</span> +<span id="L78" class="LineNr">78 </span> <span class="PreProc">loop</span> +<span id="L79" class="LineNr">79 </span> <span class="Delimiter">}</span> +<span id="L80" class="LineNr">80 </span> <span class="muComment"># return (start <= letter-count <= end)</span> +<span id="L81" class="LineNr">81 </span> compare letter-count, start +<span id="L82" class="LineNr">82 </span> <span class="Delimiter">{</span> +<span id="L83" class="LineNr">83 </span> <span class="PreProc">break-if->=</span> +<span id="L84" class="LineNr">84 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L85" class="LineNr">85 </span> <span class="Delimiter">}</span> +<span id="L86" class="LineNr">86 </span> compare letter-count, end +<span id="L87" class="LineNr">87 </span> <span class="Delimiter">{</span> +<span id="L88" class="LineNr">88 </span> <span class="PreProc">break-if-<=</span> +<span id="L89" class="LineNr">89 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L90" class="LineNr">90 </span> <span class="Delimiter">}</span> +<span id="L91" class="LineNr">91 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L92" class="LineNr">92 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/2b.mu.html b/html/linux/apps/advent2020/2b.mu.html new file mode 100644 index 00000000..604e06bb --- /dev/null +++ b/html/linux/apps/advent2020/2b.mu.html @@ -0,0 +1,190 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/2b.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/2b.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/2b.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/2">https://adventofcode.com/2020/day/2</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/2b.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr"> 10 </span> +<span id="L11" class="LineNr"> 11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='2b.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr"> 12 </span> <span class="PreProc">var</span> valid-password-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L13" class="LineNr"> 13 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L14" class="LineNr"> 14 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L15" class="LineNr"> 15 </span> <span class="PreProc">var</span> slice-storage: slice +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> slice/<span class="muRegEcx">ecx</span>: (addr slice) <span class="Special"><-</span> address slice-storage +<span id="L17" class="LineNr"> 17 </span> <span class="Delimiter">{</span> +<span id="L18" class="LineNr"> 18 </span> <span class="muComment"># read line from stdin</span> +<span id="L19" class="LineNr"> 19 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L20" class="LineNr"> 20 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L21" class="LineNr"> 21 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L23" class="LineNr"> 23 </span> compare done?, <span class="Constant">0</span>/false +<span id="L24" class="LineNr"> 24 </span> <span class="PreProc">break-if-!=</span> +<span id="L25" class="LineNr"> 25 </span> <a href='../../304screen.subx.html#L147'>print-stream-to-real-screen</a> line +<span id="L26" class="LineNr"> 26 </span> <span class="muComment"># slice = next-token(line, '-')</span> +<span id="L27" class="LineNr"> 27 </span> <a href='../../124next-token.subx.html#L10'>next-token</a> line, <span class="Constant">0x2d</span>, slice +<span id="L28" class="LineNr"> 28 </span> <span class="muComment"># pos1 = parse-int(slice)</span> +<span id="L29" class="LineNr"> 29 </span> <span class="PreProc">var</span> _pos1/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> slice +<span id="L30" class="LineNr"> 30 </span> <span class="PreProc">var</span> pos1/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy _pos1 +<span id="L31" class="LineNr"> 31 </span> <span class="PreProc">var</span> dash/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line <span class="muComment"># skip '-'</span> +<span id="L32" class="LineNr"> 32 </span> <span class="muComment"># slice = next-token(line, ' ')</span> +<span id="L33" class="LineNr"> 33 </span> <a href='../../124next-token.subx.html#L10'>next-token</a> line, <span class="Constant">0x20</span>, slice +<span id="L34" class="LineNr"> 34 </span> <span class="PreProc">var</span> _pos2/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> slice +<span id="L35" class="LineNr"> 35 </span> <span class="PreProc">var</span> pos2/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy _pos2 +<span id="L36" class="LineNr"> 36 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, pos1 +<span id="L37" class="LineNr"> 37 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L38" class="LineNr"> 38 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, pos2 +<span id="L39" class="LineNr"> 39 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L40" class="LineNr"> 40 </span> compare pos1, pos2 +<span id="L41" class="LineNr"> 41 </span> <span class="Delimiter">{</span> +<span id="L42" class="LineNr"> 42 </span> <span class="PreProc">break-if-<=</span> +<span id="L43" class="LineNr"> 43 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"out of order!\n"</span> +<span id="L44" class="LineNr"> 44 </span> <span class="PreProc">return</span> <span class="Constant">1</span> +<span id="L45" class="LineNr"> 45 </span> <span class="Delimiter">}</span> +<span id="L46" class="LineNr"> 46 </span> <span class="muComment"># letter = next non-space</span> +<span id="L47" class="LineNr"> 47 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L48" class="LineNr"> 48 </span> <span class="PreProc">var</span> letter/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line +<span id="L49" class="LineNr"> 49 </span> <span class="muComment"># skip some stuff</span> +<span id="L50" class="LineNr"> 50 </span> <span class="Delimiter">{</span> +<span id="L51" class="LineNr"> 51 </span> <span class="PreProc">var</span> colon/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line <span class="muComment"># skip ':'</span> +<span id="L52" class="LineNr"> 52 </span> <span class="Delimiter">}</span> +<span id="L53" class="LineNr"> 53 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L54" class="LineNr"> 54 </span> <span class="muComment"># now check the rest of the line</span> +<span id="L55" class="LineNr"> 55 </span> <span class="PreProc">var</span> <a href='2b.mu.html#L72'>valid?</a>/eax: boolean <span class="Special"><-</span> <a href='2b.mu.html#L72'>valid?</a> pos1, pos2, letter, line +<span id="L56" class="LineNr"> 56 </span> compare <a href='2b.mu.html#L72'>valid?</a>, <span class="Constant">0</span>/false +<span id="L57" class="LineNr"> 57 </span> <span class="Delimiter">{</span> +<span id="L58" class="LineNr"> 58 </span> <span class="PreProc">break-if-=</span> +<span id="L59" class="LineNr"> 59 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"valid!\n"</span> +<span id="L60" class="LineNr"> 60 </span> valid-password-count <span class="Special"><-</span> increment +<span id="L61" class="LineNr"> 61 </span> <span class="Delimiter">}</span> +<span id="L62" class="LineNr"> 62 </span> <span class="PreProc">loop</span> +<span id="L63" class="LineNr"> 63 </span> <span class="Delimiter">}</span> +<span id="L64" class="LineNr"> 64 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-password-count +<span id="L65" class="LineNr"> 65 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L66" class="LineNr"> 66 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L67" class="LineNr"> 67 </span><span class="Delimiter">}</span> +<span id="L68" class="LineNr"> 68 </span> +<span id="L69" class="LineNr"> 69 </span><span class="muComment"># ideally password would be a random-access array</span> +<span id="L70" class="LineNr"> 70 </span><span class="muComment"># we'll just track an index</span> +<span id="L71" class="LineNr"> 71 </span><span class="muComment"># one benefit: we can easily start at 1</span> +<span id="L72" class="LineNr"> 72 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='2b.mu.html#L72'>valid?</a></span> pos1: int, pos2: int, letter: byte, password: (addr stream byte)<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L73" class="LineNr"> 73 </span> <span class="PreProc">var</span> i/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L74" class="LineNr"> 74 </span> <span class="PreProc">var</span> letter-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L75" class="LineNr"> 75 </span> <span class="muComment"># while password stream isn't empty</span> +<span id="L76" class="LineNr"> 76 </span> <span class="muComment"># c = read byte from password</span> +<span id="L77" class="LineNr"> 77 </span> <span class="muComment"># if (c == letter)</span> +<span id="L78" class="LineNr"> 78 </span> <span class="muComment"># if (i == pos1)</span> +<span id="L79" class="LineNr"> 79 </span> <span class="muComment"># ++letter-count</span> +<span id="L80" class="LineNr"> 80 </span> <span class="muComment"># if (i == pos2)</span> +<span id="L81" class="LineNr"> 81 </span> <span class="muComment"># ++letter-count</span> +<span id="L82" class="LineNr"> 82 </span> <span class="muComment"># ++i</span> +<span id="L83" class="LineNr"> 83 </span> <span class="Delimiter">{</span> +<span id="L84" class="LineNr"> 84 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L85" class="LineNr"> 85 </span><span class="CommentedCode">#? print-int32-decimal 0, i</span> +<span id="L86" class="LineNr"> 86 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L87" class="LineNr"> 87 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> password +<span id="L88" class="LineNr"> 88 </span> compare done?, <span class="Constant">0</span>/false +<span id="L89" class="LineNr"> 89 </span> <span class="PreProc">break-if-!=</span> +<span id="L90" class="LineNr"> 90 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> password +<span id="L91" class="LineNr"> 91 </span><span class="CommentedCode">#? {</span> +<span id="L92" class="LineNr"> 92 </span><span class="CommentedCode">#? var c2/eax: int <- copy c</span> +<span id="L93" class="LineNr"> 93 </span><span class="CommentedCode">#? print-int32-decimal 0, c2</span> +<span id="L94" class="LineNr"> 94 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L95" class="LineNr"> 95 </span><span class="CommentedCode">#? }</span> +<span id="L96" class="LineNr"> 96 </span> compare c, letter +<span id="L97" class="LineNr"> 97 </span> <span class="Delimiter">{</span> +<span id="L98" class="LineNr"> 98 </span> <span class="PreProc">break-if-!=</span> +<span id="L99" class="LineNr"> 99 </span> compare i, pos1 +<span id="L100" class="LineNr">100 </span> <span class="Delimiter">{</span> +<span id="L101" class="LineNr">101 </span> <span class="PreProc">break-if-!=</span> +<span id="L102" class="LineNr">102 </span> letter-count <span class="Special"><-</span> increment +<span id="L103" class="LineNr">103 </span><span class="CommentedCode">#? print-string 0, " hit\n"</span> +<span id="L104" class="LineNr">104 </span> <span class="Delimiter">}</span> +<span id="L105" class="LineNr">105 </span> compare i, pos2 +<span id="L106" class="LineNr">106 </span> <span class="Delimiter">{</span> +<span id="L107" class="LineNr">107 </span> <span class="PreProc">break-if-!=</span> +<span id="L108" class="LineNr">108 </span> letter-count <span class="Special"><-</span> increment +<span id="L109" class="LineNr">109 </span><span class="CommentedCode">#? print-string 0, " hit\n"</span> +<span id="L110" class="LineNr">110 </span> <span class="Delimiter">}</span> +<span id="L111" class="LineNr">111 </span> <span class="Delimiter">}</span> +<span id="L112" class="LineNr">112 </span> i <span class="Special"><-</span> increment +<span id="L113" class="LineNr">113 </span> <span class="PreProc">loop</span> +<span id="L114" class="LineNr">114 </span> <span class="Delimiter">}</span> +<span id="L115" class="LineNr">115 </span> <span class="muComment"># return (letter-count == 1)</span> +<span id="L116" class="LineNr">116 </span> compare letter-count, <span class="Constant">1</span> +<span id="L117" class="LineNr">117 </span> <span class="Delimiter">{</span> +<span id="L118" class="LineNr">118 </span> <span class="PreProc">break-if-!=</span> +<span id="L119" class="LineNr">119 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L120" class="LineNr">120 </span> <span class="Delimiter">}</span> +<span id="L121" class="LineNr">121 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L122" class="LineNr">122 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/3a.mu.html b/html/linux/apps/advent2020/3a.mu.html new file mode 100644 index 00000000..09d7f160 --- /dev/null +++ b/html/linux/apps/advent2020/3a.mu.html @@ -0,0 +1,175 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/3a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/3a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/3a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/3">https://adventofcode.com/2020/day/3</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/3a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr"> 10 </span> +<span id="L11" class="LineNr"> 11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3a.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr"> 12 </span> <span class="muComment"># represent trees in a 2D array of ints</span> +<span id="L13" class="LineNr"> 13 </span> <span class="muComment"># wasteful since each tree is just one bit</span> +<span id="L14" class="LineNr"> 14 </span> <span class="PreProc">var</span> trees-storage: (array int <span class="Constant">0x2800</span>) <span class="muComment"># 10k ints</span> +<span id="L15" class="LineNr"> 15 </span> <span class="PreProc">var</span> trees/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> address trees-storage +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> trees-length/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L17" class="LineNr"> 17 </span> <span class="PreProc">var</span> num-rows: int +<span id="L18" class="LineNr"> 18 </span> <span class="PreProc">var</span> width: int +<span id="L19" class="LineNr"> 19 </span> <span class="muComment"># phase 1: parse each row of trees from stdin</span> +<span id="L20" class="LineNr"> 20 </span> <span class="Delimiter">{</span> +<span id="L21" class="LineNr"> 21 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x40</span>) <span class="muComment"># 64 bytes</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L23" class="LineNr"> 23 </span> <span class="Delimiter">{</span> +<span id="L24" class="LineNr"> 24 </span> <span class="muComment"># read line from stdin</span> +<span id="L25" class="LineNr"> 25 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L26" class="LineNr"> 26 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L27" class="LineNr"> 27 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L28" class="LineNr"> 28 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L29" class="LineNr"> 29 </span> compare done?, <span class="Constant">0</span>/false +<span id="L30" class="LineNr"> 30 </span> <span class="PreProc">break-if-!=</span> +<span id="L31" class="LineNr"> 31 </span> <span class="muComment"># wastefully recompute width on every line</span> +<span id="L32" class="LineNr"> 32 </span> <span class="muComment"># zero error-checking; we assume input lines are all equally long</span> +<span id="L33" class="LineNr"> 33 </span> copy-to width, <span class="Constant">0</span> +<span id="L34" class="LineNr"> 34 </span> <span class="muComment"># turn each byte into a tree and append it</span> +<span id="L35" class="LineNr"> 35 </span> $main:line-loop: <span class="Delimiter">{</span> +<span id="L36" class="LineNr"> 36 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L37" class="LineNr"> 37 </span> compare done?, <span class="Constant">0</span>/false +<span id="L38" class="LineNr"> 38 </span> <span class="PreProc">break-if-!=</span> +<span id="L39" class="LineNr"> 39 </span><span class="CommentedCode">#? print-int32-decimal 0, num-rows</span> +<span id="L40" class="LineNr"> 40 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L41" class="LineNr"> 41 </span><span class="CommentedCode">#? print-int32-decimal 0, width</span> +<span id="L42" class="LineNr"> 42 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L43" class="LineNr"> 43 </span> <span class="PreProc">var</span> dest/<span class="muRegEbx">ebx</span>: (addr int) <span class="Special"><-</span> index trees, trees-length +<span id="L44" class="LineNr"> 44 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line +<span id="L45" class="LineNr"> 45 </span> <span class="muComment"># newline comes only at end of line</span> +<span id="L46" class="LineNr"> 46 </span> compare c, <span class="Constant">0xa</span>/newline +<span id="L47" class="LineNr"> 47 </span> <span class="PreProc">break-if-=</span> +<span id="L48" class="LineNr"> 48 </span> <span class="muComment"># '#' = tree</span> +<span id="L49" class="LineNr"> 49 </span> compare c, <span class="Constant">0x23</span>/hash +<span id="L50" class="LineNr"> 50 </span> <span class="Delimiter">{</span> +<span id="L51" class="LineNr"> 51 </span> <span class="PreProc">break-if-!=</span> +<span id="L52" class="LineNr"> 52 </span> copy-to *dest, <span class="Constant">1</span> +<span id="L53" class="LineNr"> 53 </span> <span class="Delimiter">}</span> +<span id="L54" class="LineNr"> 54 </span> <span class="muComment"># anything else = no tree</span> +<span id="L55" class="LineNr"> 55 </span> <span class="Delimiter">{</span> +<span id="L56" class="LineNr"> 56 </span> <span class="PreProc">break-if-=</span> +<span id="L57" class="LineNr"> 57 </span> copy-to *dest, <span class="Constant">0</span> +<span id="L58" class="LineNr"> 58 </span> <span class="Delimiter">}</span> +<span id="L59" class="LineNr"> 59 </span> increment width +<span id="L60" class="LineNr"> 60 </span> trees-length <span class="Special"><-</span> increment +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">loop</span> +<span id="L62" class="LineNr"> 62 </span> <span class="Delimiter">}</span> +<span id="L63" class="LineNr"> 63 </span> increment num-rows +<span id="L64" class="LineNr"> 64 </span> <span class="PreProc">loop</span> +<span id="L65" class="LineNr"> 65 </span> <span class="Delimiter">}</span> +<span id="L66" class="LineNr"> 66 </span> <span class="Delimiter">}</span> +<span id="L67" class="LineNr"> 67 </span> <span class="muComment"># phase 2: compute</span> +<span id="L68" class="LineNr"> 68 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, num-rows +<span id="L69" class="LineNr"> 69 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"x"</span> +<span id="L70" class="LineNr"> 70 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, width +<span id="L71" class="LineNr"> 71 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L72" class="LineNr"> 72 </span> <span class="PreProc">var</span> row/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L73" class="LineNr"> 73 </span> <span class="PreProc">var</span> col/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L74" class="LineNr"> 74 </span> <span class="PreProc">var</span> num-trees-hit/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L75" class="LineNr"> 75 </span> <span class="Delimiter">{</span> +<span id="L76" class="LineNr"> 76 </span> compare row, num-rows +<span id="L77" class="LineNr"> 77 </span> <span class="PreProc">break-if->=</span> +<span id="L78" class="LineNr"> 78 </span> <span class="PreProc">var</span> curr/eax: int <span class="Special"><-</span> <a href='3a.mu.html#L94'>index2d</a> trees, row, col, width +<span id="L79" class="LineNr"> 79 </span> compare curr, <span class="Constant">0</span> +<span id="L80" class="LineNr"> 80 </span> <span class="Delimiter">{</span> +<span id="L81" class="LineNr"> 81 </span> <span class="PreProc">break-if-=</span> +<span id="L82" class="LineNr"> 82 </span> num-trees-hit <span class="Special"><-</span> increment +<span id="L83" class="LineNr"> 83 </span> <span class="Delimiter">}</span> +<span id="L84" class="LineNr"> 84 </span> <span class="muComment"># right 3, down 1</span> +<span id="L85" class="LineNr"> 85 </span> col <span class="Special"><-</span> add <span class="Constant">3</span> +<span id="L86" class="LineNr"> 86 </span> row <span class="Special"><-</span> add <span class="Constant">1</span> +<span id="L87" class="LineNr"> 87 </span> <span class="PreProc">loop</span> +<span id="L88" class="LineNr"> 88 </span> <span class="Delimiter">}</span> +<span id="L89" class="LineNr"> 89 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, num-trees-hit +<span id="L90" class="LineNr"> 90 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L91" class="LineNr"> 91 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L92" class="LineNr"> 92 </span><span class="Delimiter">}</span> +<span id="L93" class="LineNr"> 93 </span> +<span id="L94" class="LineNr"> 94 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3a.mu.html#L94'>index2d</a></span> _arr: (addr array int), _row: int, _col: int, width: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L95" class="LineNr"> 95 </span> <span class="muComment"># handle repeating columns of trees</span> +<span id="L96" class="LineNr"> 96 </span> <span class="PreProc">var</span> dummy/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L97" class="LineNr"> 97 </span> <span class="PreProc">var</span> col/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L98" class="LineNr"> 98 </span> dummy, col <span class="Special"><-</span> <a href='../../314divide.subx.html#L3'>integer-divide</a> _col, width +<span id="L99" class="LineNr"> 99 </span> <span class="muComment"># compute index</span> +<span id="L100" class="LineNr">100 </span> <span class="PreProc">var</span> index/eax: int <span class="Special"><-</span> copy _row +<span id="L101" class="LineNr">101 </span> index <span class="Special"><-</span> multiply width +<span id="L102" class="LineNr">102 </span> index <span class="Special"><-</span> add col +<span id="L103" class="LineNr">103 </span> <span class="muComment"># look up array</span> +<span id="L104" class="LineNr">104 </span> <span class="PreProc">var</span> arr/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> copy _arr +<span id="L105" class="LineNr">105 </span> <span class="PreProc">var</span> src/eax: (addr int) <span class="Special"><-</span> index arr, index +<span id="L106" class="LineNr">106 </span> <span class="PreProc">return</span> *src +<span id="L107" class="LineNr">107 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/3b.mu.html b/html/linux/apps/advent2020/3b.mu.html new file mode 100644 index 00000000..ff64117f --- /dev/null +++ b/html/linux/apps/advent2020/3b.mu.html @@ -0,0 +1,205 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/3b.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/3b.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/3b.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/3">https://adventofcode.com/2020/day/3</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/3a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr"> 10 </span> +<span id="L11" class="LineNr"> 11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3b.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr"> 12 </span> <span class="muComment"># represent trees in a 2D array of ints</span> +<span id="L13" class="LineNr"> 13 </span> <span class="muComment"># wasteful since each tree is just one bit</span> +<span id="L14" class="LineNr"> 14 </span> <span class="PreProc">var</span> trees-storage: (array int <span class="Constant">0x2800</span>) <span class="muComment"># 10k ints</span> +<span id="L15" class="LineNr"> 15 </span> <span class="PreProc">var</span> trees/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> address trees-storage +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> trees-length/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L17" class="LineNr"> 17 </span> <span class="PreProc">var</span> num-rows: int +<span id="L18" class="LineNr"> 18 </span> <span class="PreProc">var</span> width: int +<span id="L19" class="LineNr"> 19 </span> <span class="muComment"># phase 1: parse each row of trees from stdin</span> +<span id="L20" class="LineNr"> 20 </span> <span class="Delimiter">{</span> +<span id="L21" class="LineNr"> 21 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x40</span>) <span class="muComment"># 64 bytes</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L23" class="LineNr"> 23 </span> <span class="Delimiter">{</span> +<span id="L24" class="LineNr"> 24 </span> <span class="muComment"># read line from stdin</span> +<span id="L25" class="LineNr"> 25 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L26" class="LineNr"> 26 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L27" class="LineNr"> 27 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L28" class="LineNr"> 28 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L29" class="LineNr"> 29 </span> compare done?, <span class="Constant">0</span>/false +<span id="L30" class="LineNr"> 30 </span> <span class="PreProc">break-if-!=</span> +<span id="L31" class="LineNr"> 31 </span> <span class="muComment"># wastefully recompute width on every line</span> +<span id="L32" class="LineNr"> 32 </span> <span class="muComment"># zero error-checking; we assume input lines are all equally long</span> +<span id="L33" class="LineNr"> 33 </span> copy-to width, <span class="Constant">0</span> +<span id="L34" class="LineNr"> 34 </span> <span class="muComment"># turn each byte into a tree and append it</span> +<span id="L35" class="LineNr"> 35 </span> $main:line-loop: <span class="Delimiter">{</span> +<span id="L36" class="LineNr"> 36 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L37" class="LineNr"> 37 </span> compare done?, <span class="Constant">0</span>/false +<span id="L38" class="LineNr"> 38 </span> <span class="PreProc">break-if-!=</span> +<span id="L39" class="LineNr"> 39 </span><span class="CommentedCode">#? print-int32-decimal 0, num-rows</span> +<span id="L40" class="LineNr"> 40 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L41" class="LineNr"> 41 </span><span class="CommentedCode">#? print-int32-decimal 0, width</span> +<span id="L42" class="LineNr"> 42 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L43" class="LineNr"> 43 </span> <span class="PreProc">var</span> dest/<span class="muRegEbx">ebx</span>: (addr int) <span class="Special"><-</span> index trees, trees-length +<span id="L44" class="LineNr"> 44 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line +<span id="L45" class="LineNr"> 45 </span> <span class="muComment"># newline comes only at end of line</span> +<span id="L46" class="LineNr"> 46 </span> compare c, <span class="Constant">0xa</span>/newline +<span id="L47" class="LineNr"> 47 </span> <span class="PreProc">break-if-=</span> +<span id="L48" class="LineNr"> 48 </span> <span class="muComment"># '#' = tree</span> +<span id="L49" class="LineNr"> 49 </span> compare c, <span class="Constant">0x23</span>/hash +<span id="L50" class="LineNr"> 50 </span> <span class="Delimiter">{</span> +<span id="L51" class="LineNr"> 51 </span> <span class="PreProc">break-if-!=</span> +<span id="L52" class="LineNr"> 52 </span> copy-to *dest, <span class="Constant">1</span> +<span id="L53" class="LineNr"> 53 </span> <span class="Delimiter">}</span> +<span id="L54" class="LineNr"> 54 </span> <span class="muComment"># anything else = no tree</span> +<span id="L55" class="LineNr"> 55 </span> <span class="Delimiter">{</span> +<span id="L56" class="LineNr"> 56 </span> <span class="PreProc">break-if-=</span> +<span id="L57" class="LineNr"> 57 </span> copy-to *dest, <span class="Constant">0</span> +<span id="L58" class="LineNr"> 58 </span> <span class="Delimiter">}</span> +<span id="L59" class="LineNr"> 59 </span> increment width +<span id="L60" class="LineNr"> 60 </span> trees-length <span class="Special"><-</span> increment +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">loop</span> +<span id="L62" class="LineNr"> 62 </span> <span class="Delimiter">}</span> +<span id="L63" class="LineNr"> 63 </span> increment num-rows +<span id="L64" class="LineNr"> 64 </span> <span class="PreProc">loop</span> +<span id="L65" class="LineNr"> 65 </span> <span class="Delimiter">}</span> +<span id="L66" class="LineNr"> 66 </span> <span class="Delimiter">}</span> +<span id="L67" class="LineNr"> 67 </span> <span class="muComment"># phase 2: compute</span> +<span id="L68" class="LineNr"> 68 </span> <span class="PreProc">var</span> product/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L69" class="LineNr"> 69 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L94'>num-trees-hit</a> trees, width, num-rows, <span class="Constant">1</span>, <span class="Constant">1</span> +<span id="L70" class="LineNr"> 70 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L71" class="LineNr"> 71 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" x "</span> +<span id="L72" class="LineNr"> 72 </span> product <span class="Special"><-</span> multiply result +<span id="L73" class="LineNr"> 73 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L94'>num-trees-hit</a> trees, width, num-rows, <span class="Constant">3</span>, <span class="Constant">1</span> +<span id="L74" class="LineNr"> 74 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L75" class="LineNr"> 75 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" x "</span> +<span id="L76" class="LineNr"> 76 </span> product <span class="Special"><-</span> multiply result +<span id="L77" class="LineNr"> 77 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L94'>num-trees-hit</a> trees, width, num-rows, <span class="Constant">5</span>, <span class="Constant">1</span> +<span id="L78" class="LineNr"> 78 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L79" class="LineNr"> 79 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" x "</span> +<span id="L80" class="LineNr"> 80 </span> product <span class="Special"><-</span> multiply result +<span id="L81" class="LineNr"> 81 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L94'>num-trees-hit</a> trees, width, num-rows, <span class="Constant">7</span>, <span class="Constant">1</span> +<span id="L82" class="LineNr"> 82 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L83" class="LineNr"> 83 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" x "</span> +<span id="L84" class="LineNr"> 84 </span> product <span class="Special"><-</span> multiply result +<span id="L85" class="LineNr"> 85 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L94'>num-trees-hit</a> trees, width, num-rows, <span class="Constant">1</span>, <span class="Constant">2</span> +<span id="L86" class="LineNr"> 86 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L87" class="LineNr"> 87 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" = "</span> +<span id="L88" class="LineNr"> 88 </span> product <span class="Special"><-</span> multiply result +<span id="L89" class="LineNr"> 89 </span> <a href='../../405screen.mu.html#L440'>print-int32-hex</a> <span class="Constant">0</span>, product +<span id="L90" class="LineNr"> 90 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L91" class="LineNr"> 91 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L92" class="LineNr"> 92 </span><span class="Delimiter">}</span> +<span id="L93" class="LineNr"> 93 </span> +<span id="L94" class="LineNr"> 94 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3b.mu.html#L94'>num-trees-hit</a></span> trees: (addr array int), width: int, num-rows: int, right: int, down: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L95" class="LineNr"> 95 </span><span class="CommentedCode">#? print-string 0, "== "</span> +<span id="L96" class="LineNr"> 96 </span><span class="CommentedCode">#? print-int32-decimal 0, right</span> +<span id="L97" class="LineNr"> 97 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L98" class="LineNr"> 98 </span><span class="CommentedCode">#? print-int32-decimal 0, down</span> +<span id="L99" class="LineNr"> 99 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L100" class="LineNr">100 </span> <span class="PreProc">var</span> row/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L101" class="LineNr">101 </span> <span class="PreProc">var</span> col/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L102" class="LineNr">102 </span> <span class="PreProc">var</span> <a href='3b.mu.html#L94'>num-trees-hit</a>/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L103" class="LineNr">103 </span> <span class="Delimiter">{</span> +<span id="L104" class="LineNr">104 </span> compare row, num-rows +<span id="L105" class="LineNr">105 </span> <span class="PreProc">break-if->=</span> +<span id="L106" class="LineNr">106 </span><span class="CommentedCode">#? print-int32-decimal 0, col</span> +<span id="L107" class="LineNr">107 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L108" class="LineNr">108 </span> <span class="PreProc">var</span> curr/eax: int <span class="Special"><-</span> <a href='3b.mu.html#L121'>index2d</a> trees, row, col, width +<span id="L109" class="LineNr">109 </span> compare curr, <span class="Constant">0</span> +<span id="L110" class="LineNr">110 </span> <span class="Delimiter">{</span> +<span id="L111" class="LineNr">111 </span> <span class="PreProc">break-if-=</span> +<span id="L112" class="LineNr">112 </span> <a href='3b.mu.html#L94'>num-trees-hit</a> <span class="Special"><-</span> increment +<span id="L113" class="LineNr">113 </span> <span class="Delimiter">}</span> +<span id="L114" class="LineNr">114 </span> col <span class="Special"><-</span> add right +<span id="L115" class="LineNr">115 </span> row <span class="Special"><-</span> add down +<span id="L116" class="LineNr">116 </span> <span class="PreProc">loop</span> +<span id="L117" class="LineNr">117 </span> <span class="Delimiter">}</span> +<span id="L118" class="LineNr">118 </span> <span class="PreProc">return</span> <a href='3b.mu.html#L94'>num-trees-hit</a> +<span id="L119" class="LineNr">119 </span><span class="Delimiter">}</span> +<span id="L120" class="LineNr">120 </span> +<span id="L121" class="LineNr">121 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3b.mu.html#L121'>index2d</a></span> _arr: (addr array int), _row: int, _col: int, width: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L122" class="LineNr">122 </span> <span class="muComment"># handle repeating columns of trees</span> +<span id="L123" class="LineNr">123 </span> <span class="PreProc">var</span> dummy/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L124" class="LineNr">124 </span> <span class="PreProc">var</span> col/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L125" class="LineNr">125 </span> dummy, col <span class="Special"><-</span> <a href='../../314divide.subx.html#L3'>integer-divide</a> _col, width +<span id="L126" class="LineNr">126 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L127" class="LineNr">127 </span><span class="CommentedCode">#? print-int32-decimal 0, col</span> +<span id="L128" class="LineNr">128 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L129" class="LineNr">129 </span> <span class="muComment"># compute index</span> +<span id="L130" class="LineNr">130 </span> <span class="PreProc">var</span> index/eax: int <span class="Special"><-</span> copy _row +<span id="L131" class="LineNr">131 </span> index <span class="Special"><-</span> multiply width +<span id="L132" class="LineNr">132 </span> index <span class="Special"><-</span> add col +<span id="L133" class="LineNr">133 </span> <span class="muComment"># look up array</span> +<span id="L134" class="LineNr">134 </span> <span class="PreProc">var</span> arr/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> copy _arr +<span id="L135" class="LineNr">135 </span> <span class="PreProc">var</span> src/eax: (addr int) <span class="Special"><-</span> index arr, index +<span id="L136" class="LineNr">136 </span> <span class="PreProc">return</span> *src +<span id="L137" class="LineNr">137 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/4a.mu.html b/html/linux/apps/advent2020/4a.mu.html new file mode 100644 index 00000000..6f477717 --- /dev/null +++ b/html/linux/apps/advent2020/4a.mu.html @@ -0,0 +1,142 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/4a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/4a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/4a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/4">https://adventofcode.com/2020/day/4</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/4a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr">10 </span> +<span id="L11" class="LineNr">11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='4a.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> curr-passport-field-count/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> valid-passport-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L14" class="LineNr">14 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> line/<span class="muRegEcx">ecx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> slice-storage: slice +<span id="L17" class="LineNr">17 </span> <span class="PreProc">var</span> slice/<span class="muRegEdx">edx</span>: (addr slice) <span class="Special"><-</span> address slice-storage +<span id="L18" class="LineNr">18 </span> $main:line-loop: <span class="Delimiter">{</span> +<span id="L19" class="LineNr">19 </span> <span class="muComment"># read line from stdin</span> +<span id="L20" class="LineNr">20 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L21" class="LineNr">21 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L22" class="LineNr">22 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L23" class="LineNr">23 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L24" class="LineNr">24 </span> compare done?, <span class="Constant">0</span>/false +<span id="L25" class="LineNr">25 </span> <span class="PreProc">break-if-!=</span> +<span id="L26" class="LineNr">26 </span> <a href='../../304screen.subx.html#L147'>print-stream-to-real-screen</a> line +<span id="L27" class="LineNr">27 </span> <span class="muComment"># if line has just a newline, process passport</span> +<span id="L28" class="LineNr">28 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> new-passport?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L30" class="LineNr">30 </span> <span class="Delimiter">{</span> +<span id="L31" class="LineNr">31 </span> compare new-passport?, <span class="Constant">0</span>/false +<span id="L32" class="LineNr">32 </span> <span class="PreProc">break-if-=</span> +<span id="L33" class="LineNr">33 </span> compare curr-passport-field-count, <span class="Constant">7</span> +<span id="L34" class="LineNr">34 </span> <span class="Delimiter">{</span> +<span id="L35" class="LineNr">35 </span> <span class="PreProc">break-if-!=</span> +<span id="L36" class="LineNr">36 </span> valid-passport-count <span class="Special"><-</span> increment +<span id="L37" class="LineNr">37 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"=> "</span> +<span id="L38" class="LineNr">38 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-passport-count +<span id="L39" class="LineNr">39 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L40" class="LineNr">40 </span> <span class="Delimiter">}</span> +<span id="L41" class="LineNr">41 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L42" class="LineNr">42 </span> <span class="PreProc">loop</span> $main:line-loop +<span id="L43" class="LineNr">43 </span> <span class="Delimiter">}</span> +<span id="L44" class="LineNr">44 </span> $main:word-loop: <span class="Delimiter">{</span> +<span id="L45" class="LineNr">45 </span> <a href='../../127next-word.subx.html#L11'>next-word</a> line, slice +<span id="L46" class="LineNr">46 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L9'>slice-empty?</a> slice +<span id="L47" class="LineNr">47 </span> compare done?, <span class="Constant">0</span>/false +<span id="L48" class="LineNr">48 </span> <span class="PreProc">break-if-!=</span> +<span id="L49" class="LineNr">49 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L50" class="LineNr">50 </span> <a href='../../304screen.subx.html#L134'>print-slice-to-real-screen</a> slice +<span id="L51" class="LineNr">51 </span> <span class="muComment"># treat cid as optional</span> +<span id="L52" class="LineNr">52 </span> <span class="PreProc">var</span> optional?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L487'>slice-starts-with?</a> slice, <span class="Constant">"cid:"</span> +<span id="L53" class="LineNr">53 </span> compare optional?, <span class="Constant">0</span>/false +<span id="L54" class="LineNr">54 </span> <span class="Delimiter">{</span> +<span id="L55" class="LineNr">55 </span> <span class="PreProc">break-if-!=</span> +<span id="L56" class="LineNr">56 </span> <span class="muComment"># otherwise assume there are no invalid fields and no duplicate fields</span> +<span id="L57" class="LineNr">57 </span> curr-passport-field-count <span class="Special"><-</span> increment +<span id="L58" class="LineNr">58 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" => "</span> +<span id="L59" class="LineNr">59 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, curr-passport-field-count +<span id="L60" class="LineNr">60 </span> <span class="Delimiter">}</span> +<span id="L61" class="LineNr">61 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L62" class="LineNr">62 </span> <span class="PreProc">loop</span> +<span id="L63" class="LineNr">63 </span> <span class="Delimiter">}</span> +<span id="L64" class="LineNr">64 </span> <span class="PreProc">loop</span> +<span id="L65" class="LineNr">65 </span> <span class="Delimiter">}</span> +<span id="L66" class="LineNr">66 </span> <span class="muComment"># process final passport</span> +<span id="L67" class="LineNr">67 </span> compare curr-passport-field-count, <span class="Constant">7</span> +<span id="L68" class="LineNr">68 </span> <span class="Delimiter">{</span> +<span id="L69" class="LineNr">69 </span> <span class="PreProc">break-if-!=</span> +<span id="L70" class="LineNr">70 </span> valid-passport-count <span class="Special"><-</span> increment +<span id="L71" class="LineNr">71 </span> <span class="Delimiter">}</span> +<span id="L72" class="LineNr">72 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-passport-count +<span id="L73" class="LineNr">73 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L74" class="LineNr">74 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L75" class="LineNr">75 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/4b.mu.html b/html/linux/apps/advent2020/4b.mu.html new file mode 100644 index 00000000..47538296 --- /dev/null +++ b/html/linux/apps/advent2020/4b.mu.html @@ -0,0 +1,381 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/4b.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/4b.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/4b.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/4">https://adventofcode.com/2020/day/4</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/4b.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr"> 10 </span> +<span id="L11" class="LineNr"> 11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='4b.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr"> 12 </span> <span class="PreProc">var</span> curr-passport-field-count/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L13" class="LineNr"> 13 </span> <span class="PreProc">var</span> valid-passport-count/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L14" class="LineNr"> 14 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x100</span>) <span class="muComment"># 256 bytes</span> +<span id="L15" class="LineNr"> 15 </span> <span class="PreProc">var</span> line/<span class="muRegEcx">ecx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> key-slice-storage: slice +<span id="L17" class="LineNr"> 17 </span> <span class="PreProc">var</span> key-slice/<span class="muRegEdx">edx</span>: (addr slice) <span class="Special"><-</span> address key-slice-storage +<span id="L18" class="LineNr"> 18 </span> <span class="PreProc">var</span> val-slice-storage: slice +<span id="L19" class="LineNr"> 19 </span> <span class="PreProc">var</span> val-slice/<span class="muRegEbx">ebx</span>: (addr slice) <span class="Special"><-</span> address val-slice-storage +<span id="L20" class="LineNr"> 20 </span> $main:line-loop: <span class="Delimiter">{</span> +<span id="L21" class="LineNr"> 21 </span> <span class="muComment"># read line from stdin</span> +<span id="L22" class="LineNr"> 22 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L23" class="LineNr"> 23 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L24" class="LineNr"> 24 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L25" class="LineNr"> 25 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L26" class="LineNr"> 26 </span> compare done?, <span class="Constant">0</span>/false +<span id="L27" class="LineNr"> 27 </span> <span class="PreProc">break-if-!=</span> +<span id="L28" class="LineNr"> 28 </span> <a href='../../304screen.subx.html#L147'>print-stream-to-real-screen</a> line +<span id="L29" class="LineNr"> 29 </span> <span class="muComment"># if line has just a newline, process passport</span> +<span id="L30" class="LineNr"> 30 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L31" class="LineNr"> 31 </span> <span class="PreProc">var</span> new-passport?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L32" class="LineNr"> 32 </span> <span class="Delimiter">{</span> +<span id="L33" class="LineNr"> 33 </span> compare new-passport?, <span class="Constant">0</span>/false +<span id="L34" class="LineNr"> 34 </span> <span class="PreProc">break-if-=</span> +<span id="L35" class="LineNr"> 35 </span> compare curr-passport-field-count, <span class="Constant">7</span> +<span id="L36" class="LineNr"> 36 </span> <span class="Delimiter">{</span> +<span id="L37" class="LineNr"> 37 </span> <span class="PreProc">break-if-!=</span> +<span id="L38" class="LineNr"> 38 </span> valid-passport-count <span class="Special"><-</span> increment +<span id="L39" class="LineNr"> 39 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"=> "</span> +<span id="L40" class="LineNr"> 40 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-passport-count +<span id="L41" class="LineNr"> 41 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L42" class="LineNr"> 42 </span> <span class="Delimiter">}</span> +<span id="L43" class="LineNr"> 43 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L44" class="LineNr"> 44 </span> <span class="PreProc">loop</span> $main:line-loop +<span id="L45" class="LineNr"> 45 </span> <span class="Delimiter">}</span> +<span id="L46" class="LineNr"> 46 </span> $main:word-loop: <span class="Delimiter">{</span> +<span id="L47" class="LineNr"> 47 </span> <a href='../../124next-token.subx.html#L464'>skip-chars-matching-whitespace</a> line +<span id="L48" class="LineNr"> 48 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L49" class="LineNr"> 49 </span> compare done?, <span class="Constant">0</span>/false +<span id="L50" class="LineNr"> 50 </span> <span class="PreProc">break-if-!=</span> +<span id="L51" class="LineNr"> 51 </span> <a href='../../124next-token.subx.html#L10'>next-token</a> line, <span class="Constant">0x3a</span>, key-slice <span class="muComment"># ':'</span> +<span id="L52" class="LineNr"> 52 </span> <span class="PreProc">var</span> dummy/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> line <span class="muComment"># skip ':'</span> +<span id="L53" class="LineNr"> 53 </span> <a href='../../127next-word.subx.html#L306'>next-raw-word</a> line, val-slice +<span id="L54" class="LineNr"> 54 </span> <a href='../../304screen.subx.html#L134'>print-slice-to-real-screen</a> key-slice +<span id="L55" class="LineNr"> 55 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" : "</span> +<span id="L56" class="LineNr"> 56 </span> <a href='../../304screen.subx.html#L134'>print-slice-to-real-screen</a> val-slice +<span id="L57" class="LineNr"> 57 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L58" class="LineNr"> 58 </span> <span class="muComment"># treat cid as optional</span> +<span id="L59" class="LineNr"> 59 </span> <span class="PreProc">var</span> cid?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"cid"</span> +<span id="L60" class="LineNr"> 60 </span> compare cid?, <span class="Constant">0</span>/false +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">loop-if-!=</span> +<span id="L62" class="LineNr"> 62 </span> <span class="muComment"># increment field count</span> +<span id="L63" class="LineNr"> 63 </span> curr-passport-field-count <span class="Special"><-</span> increment +<span id="L64" class="LineNr"> 64 </span> <span class="muComment"># - validate fields one by one, setting curr-passport-field-count to impossibly high value to signal invalid</span> +<span id="L65" class="LineNr"> 65 </span> <span class="muComment"># byr</span> +<span id="L66" class="LineNr"> 66 </span> <span class="Delimiter">{</span> +<span id="L67" class="LineNr"> 67 </span> <span class="PreProc">var</span> byr?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"byr"</span> +<span id="L68" class="LineNr"> 68 </span> compare byr?, <span class="Constant">0</span>/false +<span id="L69" class="LineNr"> 69 </span> <span class="PreProc">break-if-=</span> +<span id="L70" class="LineNr"> 70 </span> <span class="muComment"># 1920 <= byr <= 2002</span> +<span id="L71" class="LineNr"> 71 </span> <span class="PreProc">var</span> byr/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> val-slice +<span id="L72" class="LineNr"> 72 </span> compare byr, <span class="Constant">0x780</span> <span class="muComment"># 1920</span> +<span id="L73" class="LineNr"> 73 </span> <span class="Delimiter">{</span> +<span id="L74" class="LineNr"> 74 </span> <span class="PreProc">break-if->=</span> +<span id="L75" class="LineNr"> 75 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L76" class="LineNr"> 76 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L77" class="LineNr"> 77 </span> <span class="Delimiter">}</span> +<span id="L78" class="LineNr"> 78 </span> compare byr, <span class="Constant">0x7d2</span> <span class="muComment"># 2002</span> +<span id="L79" class="LineNr"> 79 </span> <span class="Delimiter">{</span> +<span id="L80" class="LineNr"> 80 </span> <span class="PreProc">break-if-<=</span> +<span id="L81" class="LineNr"> 81 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L82" class="LineNr"> 82 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L83" class="LineNr"> 83 </span> <span class="Delimiter">}</span> +<span id="L84" class="LineNr"> 84 </span> <span class="Delimiter">}</span> +<span id="L85" class="LineNr"> 85 </span> <span class="muComment"># iyr</span> +<span id="L86" class="LineNr"> 86 </span> <span class="Delimiter">{</span> +<span id="L87" class="LineNr"> 87 </span> <span class="PreProc">var</span> iyr?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"iyr"</span> +<span id="L88" class="LineNr"> 88 </span> compare iyr?, <span class="Constant">0</span>/false +<span id="L89" class="LineNr"> 89 </span> <span class="PreProc">break-if-=</span> +<span id="L90" class="LineNr"> 90 </span> <span class="muComment"># 2010 <= iyr <= 2020</span> +<span id="L91" class="LineNr"> 91 </span> <span class="PreProc">var</span> iyr/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> val-slice +<span id="L92" class="LineNr"> 92 </span> compare iyr, <span class="Constant">0x7da</span> <span class="muComment"># 2010</span> +<span id="L93" class="LineNr"> 93 </span> <span class="Delimiter">{</span> +<span id="L94" class="LineNr"> 94 </span> <span class="PreProc">break-if->=</span> +<span id="L95" class="LineNr"> 95 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L96" class="LineNr"> 96 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L97" class="LineNr"> 97 </span> <span class="Delimiter">}</span> +<span id="L98" class="LineNr"> 98 </span> compare iyr, <span class="Constant">0x7e4</span> <span class="muComment"># 2020</span> +<span id="L99" class="LineNr"> 99 </span> <span class="Delimiter">{</span> +<span id="L100" class="LineNr">100 </span> <span class="PreProc">break-if-<=</span> +<span id="L101" class="LineNr">101 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L102" class="LineNr">102 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L103" class="LineNr">103 </span> <span class="Delimiter">}</span> +<span id="L104" class="LineNr">104 </span> <span class="Delimiter">}</span> +<span id="L105" class="LineNr">105 </span> <span class="muComment"># eyr</span> +<span id="L106" class="LineNr">106 </span> <span class="Delimiter">{</span> +<span id="L107" class="LineNr">107 </span> <span class="PreProc">var</span> eyr?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"eyr"</span> +<span id="L108" class="LineNr">108 </span> compare eyr?, <span class="Constant">0</span>/false +<span id="L109" class="LineNr">109 </span> <span class="PreProc">break-if-=</span> +<span id="L110" class="LineNr">110 </span> <span class="muComment"># 2020 <= eyr <= 2030</span> +<span id="L111" class="LineNr">111 </span> <span class="PreProc">var</span> eyr/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> val-slice +<span id="L112" class="LineNr">112 </span> compare eyr, <span class="Constant">0x7e4</span> <span class="muComment"># 2020</span> +<span id="L113" class="LineNr">113 </span> <span class="Delimiter">{</span> +<span id="L114" class="LineNr">114 </span> <span class="PreProc">break-if->=</span> +<span id="L115" class="LineNr">115 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L116" class="LineNr">116 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L117" class="LineNr">117 </span> <span class="Delimiter">}</span> +<span id="L118" class="LineNr">118 </span> compare eyr, <span class="Constant">0x7ee</span> <span class="muComment"># 2030</span> +<span id="L119" class="LineNr">119 </span> <span class="Delimiter">{</span> +<span id="L120" class="LineNr">120 </span> <span class="PreProc">break-if-<=</span> +<span id="L121" class="LineNr">121 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L122" class="LineNr">122 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L123" class="LineNr">123 </span> <span class="Delimiter">}</span> +<span id="L124" class="LineNr">124 </span> <span class="Delimiter">}</span> +<span id="L125" class="LineNr">125 </span> <span class="muComment"># hgt</span> +<span id="L126" class="LineNr">126 </span> <span class="Delimiter">{</span> +<span id="L127" class="LineNr">127 </span> <span class="PreProc">var</span> hgt?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"hgt"</span> +<span id="L128" class="LineNr">128 </span> compare hgt?, <span class="Constant">0</span>/false +<span id="L129" class="LineNr">129 </span> <span class="PreProc">break-if-=</span> +<span id="L130" class="LineNr">130 </span> <span class="muComment"># convert val</span> +<span id="L131" class="LineNr">131 </span> <span class="PreProc">var</span> s: (handle array byte) +<span id="L132" class="LineNr">132 </span> <span class="PreProc">var</span> s2/eax: (addr handle array byte) <span class="Special"><-</span> address s +<span id="L133" class="LineNr">133 </span> <a href='../../315slice.subx.html#L5'>_slice-to-string</a> val-slice, s2 +<span id="L134" class="LineNr">134 </span> <span class="PreProc">var</span> s3/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *s2 +<span id="L135" class="LineNr">135 </span> <span class="PreProc">var</span> s4/<span class="muRegEbx">ebx</span>: (addr array byte) <span class="Special"><-</span> copy s3 +<span id="L136" class="LineNr">136 </span> <span class="muComment"># check suffix</span> +<span id="L137" class="LineNr">137 </span> <span class="PreProc">var</span> start/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> length s4 +<span id="L138" class="LineNr">138 </span> start <span class="Special"><-</span> subtract <span class="Constant">2</span> <span class="muComment"># luckily both 'in' and 'cm' have the same length</span> +<span id="L139" class="LineNr">139 </span> <span class="Delimiter">{</span> +<span id="L140" class="LineNr">140 </span> <span class="PreProc">var</span> suffix-h: (handle array byte) +<span id="L141" class="LineNr">141 </span> <span class="PreProc">var</span> suffix-ah/<span class="muRegEcx">ecx</span>: (addr handle array byte) <span class="Special"><-</span> address suffix-h +<span id="L142" class="LineNr">142 </span> <a href='../../411string.mu.html#L2'>substring</a> s4, start, <span class="Constant">2</span>, suffix-ah +<span id="L143" class="LineNr">143 </span> <span class="PreProc">var</span> suffix/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *suffix-ah +<span id="L144" class="LineNr">144 </span> <span class="Delimiter">{</span> +<span id="L145" class="LineNr">145 </span> <span class="PreProc">var</span> match?/eax: boolean <span class="Special"><-</span> <a href='../../105string-equal.subx.html#L15'>string-equal?</a> suffix, <span class="Constant">"in"</span> +<span id="L146" class="LineNr">146 </span> compare match?, <span class="Constant">0</span>/false +<span id="L147" class="LineNr">147 </span> <span class="PreProc">break-if-=</span> +<span id="L148" class="LineNr">148 </span> <span class="muComment"># if suffix is "in", 59 <= val <= 96</span> +<span id="L149" class="LineNr">149 </span> <span class="PreProc">var</span> num-h: (handle array byte) +<span id="L150" class="LineNr">150 </span> <span class="PreProc">var</span> num-ah/<span class="muRegEcx">ecx</span>: (addr handle array byte) <span class="Special"><-</span> address num-h +<span id="L151" class="LineNr">151 </span> <a href='../../411string.mu.html#L2'>substring</a> s4, <span class="Constant">0</span>, start, num-ah +<span id="L152" class="LineNr">152 </span> <span class="PreProc">var</span> num/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *num-ah +<span id="L153" class="LineNr">153 </span> <span class="PreProc">var</span> val/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L23'>parse-decimal-int</a> num +<span id="L154" class="LineNr">154 </span> compare val, <span class="Constant">0x3b</span> <span class="muComment"># 59</span> +<span id="L155" class="LineNr">155 </span> <span class="Delimiter">{</span> +<span id="L156" class="LineNr">156 </span> <span class="PreProc">break-if->=</span> +<span id="L157" class="LineNr">157 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L158" class="LineNr">158 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L159" class="LineNr">159 </span> <span class="Delimiter">}</span> +<span id="L160" class="LineNr">160 </span> compare val, <span class="Constant">0x60</span> <span class="muComment"># 96</span> +<span id="L161" class="LineNr">161 </span> <span class="Delimiter">{</span> +<span id="L162" class="LineNr">162 </span> <span class="PreProc">break-if-<=</span> +<span id="L163" class="LineNr">163 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L164" class="LineNr">164 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L165" class="LineNr">165 </span> <span class="Delimiter">}</span> +<span id="L166" class="LineNr">166 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L167" class="LineNr">167 </span> <span class="Delimiter">}</span> +<span id="L168" class="LineNr">168 </span> <span class="Delimiter">{</span> +<span id="L169" class="LineNr">169 </span> <span class="PreProc">var</span> match?/eax: boolean <span class="Special"><-</span> <a href='../../105string-equal.subx.html#L15'>string-equal?</a> suffix, <span class="Constant">"cm"</span> +<span id="L170" class="LineNr">170 </span> compare match?, <span class="Constant">0</span>/false +<span id="L171" class="LineNr">171 </span> <span class="PreProc">break-if-=</span> +<span id="L172" class="LineNr">172 </span> <span class="muComment"># if suffix is "cm", 150 <= val <= 193</span> +<span id="L173" class="LineNr">173 </span> <span class="PreProc">var</span> num-h: (handle array byte) +<span id="L174" class="LineNr">174 </span> <span class="PreProc">var</span> num-ah/<span class="muRegEcx">ecx</span>: (addr handle array byte) <span class="Special"><-</span> address num-h +<span id="L175" class="LineNr">175 </span> <a href='../../411string.mu.html#L2'>substring</a> s4, <span class="Constant">0</span>, start, num-ah +<span id="L176" class="LineNr">176 </span> <span class="PreProc">var</span> num/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *num-ah +<span id="L177" class="LineNr">177 </span> <span class="PreProc">var</span> val/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L23'>parse-decimal-int</a> num +<span id="L178" class="LineNr">178 </span> compare val, <span class="Constant">0x96</span> <span class="muComment"># 150</span> +<span id="L179" class="LineNr">179 </span> <span class="Delimiter">{</span> +<span id="L180" class="LineNr">180 </span> <span class="PreProc">break-if->=</span> +<span id="L181" class="LineNr">181 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L182" class="LineNr">182 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L183" class="LineNr">183 </span> <span class="Delimiter">}</span> +<span id="L184" class="LineNr">184 </span> compare val, <span class="Constant">0xc1</span> <span class="muComment"># 193</span> +<span id="L185" class="LineNr">185 </span> <span class="Delimiter">{</span> +<span id="L186" class="LineNr">186 </span> <span class="PreProc">break-if-<=</span> +<span id="L187" class="LineNr">187 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L188" class="LineNr">188 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L189" class="LineNr">189 </span> <span class="Delimiter">}</span> +<span id="L190" class="LineNr">190 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L191" class="LineNr">191 </span> <span class="Delimiter">}</span> +<span id="L192" class="LineNr">192 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L193" class="LineNr">193 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L194" class="LineNr">194 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L195" class="LineNr">195 </span> <span class="Delimiter">}</span> +<span id="L196" class="LineNr">196 </span> <span class="Delimiter">}</span> +<span id="L197" class="LineNr">197 </span> <span class="muComment"># hcl</span> +<span id="L198" class="LineNr">198 </span> <span class="Delimiter">{</span> +<span id="L199" class="LineNr">199 </span> <span class="PreProc">var</span> hcl?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"hcl"</span> +<span id="L200" class="LineNr">200 </span> compare hcl?, <span class="Constant">0</span>/false +<span id="L201" class="LineNr">201 </span> <span class="PreProc">break-if-=</span> +<span id="L202" class="LineNr">202 </span> <span class="muComment"># convert val</span> +<span id="L203" class="LineNr">203 </span> <span class="PreProc">var</span> s: (handle array byte) +<span id="L204" class="LineNr">204 </span> <span class="PreProc">var</span> s2/eax: (addr handle array byte) <span class="Special"><-</span> address s +<span id="L205" class="LineNr">205 </span> <a href='../../315slice.subx.html#L5'>_slice-to-string</a> val-slice, s2 +<span id="L206" class="LineNr">206 </span> <span class="PreProc">var</span> s3/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *s2 +<span id="L207" class="LineNr">207 </span> <span class="muComment"># check length</span> +<span id="L208" class="LineNr">208 </span> <span class="PreProc">var</span> len/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> length s3 +<span id="L209" class="LineNr">209 </span> compare len, <span class="Constant">7</span> +<span id="L210" class="LineNr">210 </span> <span class="Delimiter">{</span> +<span id="L211" class="LineNr">211 </span> <span class="PreProc">break-if-=</span> +<span id="L212" class="LineNr">212 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L213" class="LineNr">213 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L214" class="LineNr">214 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L215" class="LineNr">215 </span> <span class="Delimiter">}</span> +<span id="L216" class="LineNr">216 </span> <span class="muComment"># check first byte</span> +<span id="L217" class="LineNr">217 </span> <span class="Delimiter">{</span> +<span id="L218" class="LineNr">218 </span> <span class="PreProc">var</span> c/eax: (addr byte) <span class="Special"><-</span> index s3, <span class="Constant">0</span> +<span id="L219" class="LineNr">219 </span> <span class="PreProc">var</span> c2/eax: byte <span class="Special"><-</span> copy-byte *c +<span id="L220" class="LineNr">220 </span> compare c2, <span class="Constant">0x23</span>/hash +<span id="L221" class="LineNr">221 </span> <span class="PreProc">break-if-=</span> +<span id="L222" class="LineNr">222 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid2\n"</span> +<span id="L223" class="LineNr">223 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L224" class="LineNr">224 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L225" class="LineNr">225 </span> <span class="Delimiter">}</span> +<span id="L226" class="LineNr">226 </span> <span class="muComment"># check remaining bytes</span> +<span id="L227" class="LineNr">227 </span> <span class="PreProc">var</span> i/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy <span class="Constant">1</span> <span class="muComment"># skip 0</span> +<span id="L228" class="LineNr">228 </span> <span class="Delimiter">{</span> +<span id="L229" class="LineNr">229 </span> compare i, <span class="Constant">7</span> +<span id="L230" class="LineNr">230 </span> <span class="PreProc">break-if->=</span> +<span id="L231" class="LineNr">231 </span> <span class="PreProc">var</span> c/eax: (addr byte) <span class="Special"><-</span> index s3, i +<span id="L232" class="LineNr">232 </span> <span class="Delimiter">{</span> +<span id="L233" class="LineNr">233 </span> <span class="PreProc">var</span> c2/eax: byte <span class="Special"><-</span> copy-byte *c +<span id="L234" class="LineNr">234 </span> <span class="PreProc">var</span> valid?/eax: boolean <span class="Special"><-</span> <a href='../../118parse-hex-int.subx.html#L701'>hex-digit?</a> c2 +<span id="L235" class="LineNr">235 </span> compare valid?, <span class="Constant">0</span> +<span id="L236" class="LineNr">236 </span> <span class="PreProc">loop-if-=</span> $main:word-loop +<span id="L237" class="LineNr">237 </span> <span class="Delimiter">}</span> +<span id="L238" class="LineNr">238 </span> i <span class="Special"><-</span> increment +<span id="L239" class="LineNr">239 </span> <span class="PreProc">loop</span> +<span id="L240" class="LineNr">240 </span> <span class="Delimiter">}</span> +<span id="L241" class="LineNr">241 </span> <span class="Delimiter">}</span> +<span id="L242" class="LineNr">242 </span> <span class="muComment"># ecl</span> +<span id="L243" class="LineNr">243 </span> <span class="Delimiter">{</span> +<span id="L244" class="LineNr">244 </span> <span class="PreProc">var</span> ecl?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"ecl"</span> +<span id="L245" class="LineNr">245 </span> compare ecl?, <span class="Constant">0</span>/false +<span id="L246" class="LineNr">246 </span> <span class="PreProc">break-if-=</span> +<span id="L247" class="LineNr">247 </span> <span class="PreProc">var</span> amb?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"amb"</span> +<span id="L248" class="LineNr">248 </span> compare amb?, <span class="Constant">0</span>/false +<span id="L249" class="LineNr">249 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L250" class="LineNr">250 </span> <span class="PreProc">var</span> blu?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"blu"</span> +<span id="L251" class="LineNr">251 </span> compare blu?, <span class="Constant">0</span>/false +<span id="L252" class="LineNr">252 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L253" class="LineNr">253 </span> <span class="PreProc">var</span> brn?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"brn"</span> +<span id="L254" class="LineNr">254 </span> compare brn?, <span class="Constant">0</span>/false +<span id="L255" class="LineNr">255 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L256" class="LineNr">256 </span> <span class="PreProc">var</span> gry?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"gry"</span> +<span id="L257" class="LineNr">257 </span> compare gry?, <span class="Constant">0</span>/false +<span id="L258" class="LineNr">258 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L259" class="LineNr">259 </span> <span class="PreProc">var</span> grn?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"grn"</span> +<span id="L260" class="LineNr">260 </span> compare grn?, <span class="Constant">0</span>/false +<span id="L261" class="LineNr">261 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L262" class="LineNr">262 </span> <span class="PreProc">var</span> hzl?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"hzl"</span> +<span id="L263" class="LineNr">263 </span> compare hzl?, <span class="Constant">0</span>/false +<span id="L264" class="LineNr">264 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L265" class="LineNr">265 </span> <span class="PreProc">var</span> oth?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> val-slice, <span class="Constant">"oth"</span> +<span id="L266" class="LineNr">266 </span> compare oth?, <span class="Constant">0</span>/false +<span id="L267" class="LineNr">267 </span> <span class="PreProc">loop-if-!=</span> $main:word-loop +<span id="L268" class="LineNr">268 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L269" class="LineNr">269 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L270" class="LineNr">270 </span> <span class="Delimiter">}</span> +<span id="L271" class="LineNr">271 </span> <span class="muComment"># pid</span> +<span id="L272" class="LineNr">272 </span> <span class="Delimiter">{</span> +<span id="L273" class="LineNr">273 </span> <span class="PreProc">var</span> pid?/eax: boolean <span class="Special"><-</span> <a href='../../123slice.subx.html#L120'>slice-equal?</a> key-slice, <span class="Constant">"pid"</span> +<span id="L274" class="LineNr">274 </span> compare pid?, <span class="Constant">0</span>/false +<span id="L275" class="LineNr">275 </span> <span class="PreProc">break-if-=</span> +<span id="L276" class="LineNr">276 </span> <span class="muComment"># convert val</span> +<span id="L277" class="LineNr">277 </span> <span class="PreProc">var</span> s: (handle array byte) +<span id="L278" class="LineNr">278 </span> <span class="PreProc">var</span> s2/eax: (addr handle array byte) <span class="Special"><-</span> address s +<span id="L279" class="LineNr">279 </span> <a href='../../315slice.subx.html#L5'>_slice-to-string</a> val-slice, s2 +<span id="L280" class="LineNr">280 </span> <span class="PreProc">var</span> s3/eax: (addr array byte) <span class="Special"><-</span> <a href='../../120allocate.subx.html#L258'>lookup</a> *s2 +<span id="L281" class="LineNr">281 </span> <span class="muComment"># check length</span> +<span id="L282" class="LineNr">282 </span> <span class="PreProc">var</span> len/eax: int <span class="Special"><-</span> length s3 +<span id="L283" class="LineNr">283 </span> compare len, <span class="Constant">9</span> +<span id="L284" class="LineNr">284 </span> <span class="Delimiter">{</span> +<span id="L285" class="LineNr">285 </span> <span class="PreProc">break-if-=</span> +<span id="L286" class="LineNr">286 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L287" class="LineNr">287 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L288" class="LineNr">288 </span> <span class="PreProc">loop</span> $main:word-loop +<span id="L289" class="LineNr">289 </span> <span class="Delimiter">}</span> +<span id="L290" class="LineNr">290 </span> <span class="muComment"># check valid decimal int</span> +<span id="L291" class="LineNr">291 </span> <span class="muComment"># parse-decimal-int-from-slice currently returns 0 on invalid parse,</span> +<span id="L292" class="LineNr">292 </span> <span class="muComment"># which isn't ideal but suffices for our purposes</span> +<span id="L293" class="LineNr">293 </span> <span class="PreProc">var</span> val/eax: int <span class="Special"><-</span> <a href='../../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> val-slice +<span id="L294" class="LineNr">294 </span> compare val, <span class="Constant">0</span> +<span id="L295" class="LineNr">295 </span> <span class="Delimiter">{</span> +<span id="L296" class="LineNr">296 </span> <span class="PreProc">break-if-></span> +<span id="L297" class="LineNr">297 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"invalid\n"</span> +<span id="L298" class="LineNr">298 </span> curr-passport-field-count <span class="Special"><-</span> copy <span class="Constant">8</span> +<span id="L299" class="LineNr">299 </span> <span class="Delimiter">}</span> +<span id="L300" class="LineNr">300 </span> <span class="Delimiter">}</span> +<span id="L301" class="LineNr">301 </span> <span class="PreProc">loop</span> +<span id="L302" class="LineNr">302 </span> <span class="Delimiter">}</span> +<span id="L303" class="LineNr">303 </span> <span class="PreProc">loop</span> +<span id="L304" class="LineNr">304 </span> <span class="Delimiter">}</span> +<span id="L305" class="LineNr">305 </span> <span class="muComment"># process final passport</span> +<span id="L306" class="LineNr">306 </span> compare curr-passport-field-count, <span class="Constant">7</span> +<span id="L307" class="LineNr">307 </span> <span class="Delimiter">{</span> +<span id="L308" class="LineNr">308 </span> <span class="PreProc">break-if-!=</span> +<span id="L309" class="LineNr">309 </span> valid-passport-count <span class="Special"><-</span> increment +<span id="L310" class="LineNr">310 </span> <span class="Delimiter">}</span> +<span id="L311" class="LineNr">311 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, valid-passport-count +<span id="L312" class="LineNr">312 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L313" class="LineNr">313 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L314" class="LineNr">314 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/5a.mu.html b/html/linux/apps/advent2020/5a.mu.html new file mode 100644 index 00000000..3c3b535d --- /dev/null +++ b/html/linux/apps/advent2020/5a.mu.html @@ -0,0 +1,146 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/5a.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.Special { color: #ff6060; } +.muRegEdx { color: #af5f00; } +.LineNr { } +.CommentedCode { color: #8a8a8a; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEcx { color: #870000; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/5a.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/5a.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/5">https://adventofcode.com/2020/day/5</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/5a.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr">10 </span> +<span id="L11" class="LineNr">11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='5a.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x10</span>) <span class="muComment"># 16 bytes is enough</span> +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L14" class="LineNr">14 </span> <span class="PreProc">var</span> max-seat-id/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L15" class="LineNr">15 </span> <span class="Delimiter">{</span> +<span id="L16" class="LineNr">16 </span> <span class="muComment"># read line from stdin</span> +<span id="L17" class="LineNr">17 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L18" class="LineNr">18 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L19" class="LineNr">19 </span> <a href='../../304screen.subx.html#L147'>print-stream-to-real-screen</a> line +<span id="L20" class="LineNr">20 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L21" class="LineNr">21 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L22" class="LineNr">22 </span> compare done?, <span class="Constant">0</span>/false +<span id="L23" class="LineNr">23 </span> <span class="PreProc">break-if-!=</span> +<span id="L24" class="LineNr">24 </span> <span class="muComment"># process line</span> +<span id="L25" class="LineNr">25 </span> <span class="PreProc">var</span> seat-id/eax: int <span class="Special"><-</span> <a href='5a.mu.html#L38'>convert-from-binary</a> line +<span id="L26" class="LineNr">26 </span> compare seat-id, max-seat-id +<span id="L27" class="LineNr">27 </span> <span class="Delimiter">{</span> +<span id="L28" class="LineNr">28 </span> <span class="PreProc">break-if-<=</span> +<span id="L29" class="LineNr">29 </span> max-seat-id <span class="Special"><-</span> copy seat-id +<span id="L30" class="LineNr">30 </span> <span class="Delimiter">}</span> +<span id="L31" class="LineNr">31 </span> <span class="PreProc">loop</span> +<span id="L32" class="LineNr">32 </span> <span class="Delimiter">}</span> +<span id="L33" class="LineNr">33 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, max-seat-id +<span id="L34" class="LineNr">34 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L35" class="LineNr">35 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L36" class="LineNr">36 </span><span class="Delimiter">}</span> +<span id="L37" class="LineNr">37 </span> +<span id="L38" class="LineNr">38 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='5a.mu.html#L38'>convert-from-binary</a></span> in: (addr stream byte)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L39" class="LineNr">39 </span> <span class="PreProc">var</span> result/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L40" class="LineNr">40 </span> <span class="PreProc">var</span> i/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">9</span> <span class="muComment"># loop counter and also exponent</span> +<span id="L41" class="LineNr">41 </span> <span class="Delimiter">{</span> +<span id="L42" class="LineNr">42 </span> compare i, <span class="Constant">0</span> +<span id="L43" class="LineNr">43 </span> <span class="PreProc">break-if-<</span> +<span id="L44" class="LineNr">44 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> in +<span id="L45" class="LineNr">45 </span><span class="CommentedCode">#? print-string 0, "char: "</span> +<span id="L46" class="LineNr">46 </span><span class="CommentedCode">#? {</span> +<span id="L47" class="LineNr">47 </span><span class="CommentedCode">#? var c2/eax: int <- copy c</span> +<span id="L48" class="LineNr">48 </span><span class="CommentedCode">#? print-int32-hex 0, c2</span> +<span id="L49" class="LineNr">49 </span><span class="CommentedCode">#? }</span> +<span id="L50" class="LineNr">50 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L51" class="LineNr">51 </span> <span class="PreProc">var</span> bit/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L52" class="LineNr">52 </span> <span class="Delimiter">{</span> +<span id="L53" class="LineNr">53 </span> compare c, <span class="Constant">0x42</span>/B +<span id="L54" class="LineNr">54 </span> <span class="PreProc">break-if-!=</span> +<span id="L55" class="LineNr">55 </span> bit <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L56" class="LineNr">56 </span> <span class="Delimiter">}</span> +<span id="L57" class="LineNr">57 </span> <span class="Delimiter">{</span> +<span id="L58" class="LineNr">58 </span> compare c, <span class="Constant">0x52</span>/R +<span id="L59" class="LineNr">59 </span> <span class="PreProc">break-if-!=</span> +<span id="L60" class="LineNr">60 </span> bit <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L61" class="LineNr">61 </span> <span class="Delimiter">}</span> +<span id="L62" class="LineNr">62 </span><span class="CommentedCode">#? print-string 0, "bit: "</span> +<span id="L63" class="LineNr">63 </span><span class="CommentedCode">#? print-int32-decimal 0, bit</span> +<span id="L64" class="LineNr">64 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L65" class="LineNr">65 </span> <span class="PreProc">var</span> bit-value/eax: int <span class="Special"><-</span> <a href='../../406int32.mu.html#L5'>repeated-shift-left</a> bit, i +<span id="L66" class="LineNr">66 </span><span class="CommentedCode">#? print-string 0, "bit value: "</span> +<span id="L67" class="LineNr">67 </span><span class="CommentedCode">#? print-int32-decimal 0, bit-value</span> +<span id="L68" class="LineNr">68 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L69" class="LineNr">69 </span> result <span class="Special"><-</span> add bit-value +<span id="L70" class="LineNr">70 </span><span class="CommentedCode">#? print-string 0, "result: "</span> +<span id="L71" class="LineNr">71 </span><span class="CommentedCode">#? print-int32-decimal 0, result</span> +<span id="L72" class="LineNr">72 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L73" class="LineNr">73 </span> i <span class="Special"><-</span> decrement +<span id="L74" class="LineNr">74 </span> <span class="PreProc">loop</span> +<span id="L75" class="LineNr">75 </span> <span class="Delimiter">}</span> +<span id="L76" class="LineNr">76 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, result +<span id="L77" class="LineNr">77 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L78" class="LineNr">78 </span> <span class="PreProc">return</span> result +<span id="L79" class="LineNr">79 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/advent2020/5b.mu.html b/html/linux/apps/advent2020/5b.mu.html new file mode 100644 index 00000000..b5e1892f --- /dev/null +++ b/html/linux/apps/advent2020/5b.mu.html @@ -0,0 +1,148 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/advent2020/5b.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/advent2020/5b.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/5b.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># <a href="https://adventofcode.com/2020/day/5">https://adventofcode.com/2020/day/5</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/advent2020/5b.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf < input</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You'll need to register to download the 'input' file for yourself.</span> +<span id="L10" class="LineNr">10 </span> +<span id="L11" class="LineNr">11 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='5b.mu.html#L11'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> pass-storage: (array int <span class="Constant">0x400</span>) <span class="muComment"># 1k ints</span> +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> pass/<span class="muRegEsi">esi</span>: (addr array int) <span class="Special"><-</span> address pass-storage +<span id="L14" class="LineNr">14 </span> <span class="muComment"># phase 1: populate pass array</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> line-storage: (stream byte <span class="Constant">0x10</span>) <span class="muComment"># 16 bytes is enough</span> +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> line/<span class="muRegEdx">edx</span>: (addr stream byte) <span class="Special"><-</span> address line-storage +<span id="L17" class="LineNr">17 </span> <span class="Delimiter">{</span> +<span id="L18" class="LineNr">18 </span> <span class="muComment"># read line from stdin</span> +<span id="L19" class="LineNr">19 </span> <a href='../../106stream.subx.html#L17'>clear-stream</a> line +<span id="L20" class="LineNr">20 </span> <a href='../../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> line +<span id="L21" class="LineNr">21 </span> <span class="muComment"># if line is empty (not even a newline), quit</span> +<span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../../309stream.subx.html#L6'>stream-empty?</a> line +<span id="L23" class="LineNr">23 </span> compare done?, <span class="Constant">0</span>/false +<span id="L24" class="LineNr">24 </span> <span class="PreProc">break-if-!=</span> +<span id="L25" class="LineNr">25 </span> <span class="muComment"># process line</span> +<span id="L26" class="LineNr">26 </span> <span class="PreProc">var</span> seat-id/eax: int <span class="Special"><-</span> <a href='5b.mu.html#L57'>convert-from-binary</a> line +<span id="L27" class="LineNr">27 </span> <span class="PreProc">var</span> dest/eax: (addr int) <span class="Special"><-</span> index pass, seat-id +<span id="L28" class="LineNr">28 </span> copy-to *dest, <span class="Constant">1</span> +<span id="L29" class="LineNr">29 </span> <span class="PreProc">loop</span> +<span id="L30" class="LineNr">30 </span> <span class="Delimiter">}</span> +<span id="L31" class="LineNr">31 </span> <span class="muComment"># phase 2: skip empty seats</span> +<span id="L32" class="LineNr">32 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L33" class="LineNr">33 </span> <span class="Delimiter">{</span> +<span id="L34" class="LineNr">34 </span> compare i, <span class="Constant">0x400</span> +<span id="L35" class="LineNr">35 </span> <span class="PreProc">break-if->=</span> +<span id="L36" class="LineNr">36 </span> <span class="PreProc">var</span> src/<span class="muRegEcx">ecx</span>: (addr int) <span class="Special"><-</span> index pass, i +<span id="L37" class="LineNr">37 </span> compare *src, <span class="Constant">0</span> +<span id="L38" class="LineNr">38 </span> <span class="PreProc">break-if-!=</span> +<span id="L39" class="LineNr">39 </span> i <span class="Special"><-</span> increment +<span id="L40" class="LineNr">40 </span> <span class="PreProc">loop</span> +<span id="L41" class="LineNr">41 </span> <span class="Delimiter">}</span> +<span id="L42" class="LineNr">42 </span> <span class="muComment"># phase 3: skip non-empty seats</span> +<span id="L43" class="LineNr">43 </span> <span class="Delimiter">{</span> +<span id="L44" class="LineNr">44 </span> compare i, <span class="Constant">0x400</span> +<span id="L45" class="LineNr">45 </span> <span class="PreProc">break-if->=</span> +<span id="L46" class="LineNr">46 </span> <span class="PreProc">var</span> src/<span class="muRegEcx">ecx</span>: (addr int) <span class="Special"><-</span> index pass, i +<span id="L47" class="LineNr">47 </span> compare *src, <span class="Constant">0</span> +<span id="L48" class="LineNr">48 </span> <span class="PreProc">break-if-=</span> +<span id="L49" class="LineNr">49 </span> i <span class="Special"><-</span> increment +<span id="L50" class="LineNr">50 </span> <span class="PreProc">loop</span> +<span id="L51" class="LineNr">51 </span> <span class="Delimiter">}</span> +<span id="L52" class="LineNr">52 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, i +<span id="L53" class="LineNr">53 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"\n"</span> +<span id="L54" class="LineNr">54 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L55" class="LineNr">55 </span><span class="Delimiter">}</span> +<span id="L56" class="LineNr">56 </span> +<span id="L57" class="LineNr">57 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='5b.mu.html#L57'>convert-from-binary</a></span> in: (addr stream byte)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L58" class="LineNr">58 </span> <span class="PreProc">var</span> result/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L59" class="LineNr">59 </span> <span class="PreProc">var</span> i/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">9</span> <span class="muComment"># loop counter and also exponent</span> +<span id="L60" class="LineNr">60 </span> <span class="Delimiter">{</span> +<span id="L61" class="LineNr">61 </span> compare i, <span class="Constant">0</span> +<span id="L62" class="LineNr">62 </span> <span class="PreProc">break-if-<</span> +<span id="L63" class="LineNr">63 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../../112read-byte.subx.html#L273'>read-byte</a> in +<span id="L64" class="LineNr">64 </span> <span class="PreProc">var</span> bit/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L65" class="LineNr">65 </span> <span class="Delimiter">{</span> +<span id="L66" class="LineNr">66 </span> compare c, <span class="Constant">0x42</span>/B +<span id="L67" class="LineNr">67 </span> <span class="PreProc">break-if-!=</span> +<span id="L68" class="LineNr">68 </span> bit <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L69" class="LineNr">69 </span> <span class="Delimiter">}</span> +<span id="L70" class="LineNr">70 </span> <span class="Delimiter">{</span> +<span id="L71" class="LineNr">71 </span> compare c, <span class="Constant">0x52</span>/R +<span id="L72" class="LineNr">72 </span> <span class="PreProc">break-if-!=</span> +<span id="L73" class="LineNr">73 </span> bit <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L74" class="LineNr">74 </span> <span class="Delimiter">}</span> +<span id="L75" class="LineNr">75 </span> <span class="PreProc">var</span> bit-value/eax: int <span class="Special"><-</span> <a href='../../406int32.mu.html#L5'>repeated-shift-left</a> bit, i +<span id="L76" class="LineNr">76 </span> result <span class="Special"><-</span> add bit-value +<span id="L77" class="LineNr">77 </span> i <span class="Special"><-</span> decrement +<span id="L78" class="LineNr">78 </span> <span class="PreProc">loop</span> +<span id="L79" class="LineNr">79 </span> <span class="Delimiter">}</span> +<span id="L80" class="LineNr">80 </span> <span class="PreProc">return</span> result +<span id="L81" class="LineNr">81 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/arith.mu.html b/html/linux/apps/arith.mu.html new file mode 100644 index 00000000..fb6a9194 --- /dev/null +++ b/html/linux/apps/arith.mu.html @@ -0,0 +1,326 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/arith.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/arith.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/arith.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Integer arithmetic using conventional precedence.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># Follows part 2 of Jack Crenshaw's "Let's build a compiler!"</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># <a href="https://compilers.iecc.com/crenshaw">https://compilers.iecc.com/crenshaw</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># Limitations:</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># No division yet.</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># To build:</span> +<span id="L10" class="LineNr"> 10 </span><span class="muComment"># $ ./translate apps/arith.mu</span> +<span id="L11" class="LineNr"> 11 </span><span class="muComment">#</span> +<span id="L12" class="LineNr"> 12 </span><span class="muComment"># Example session:</span> +<span id="L13" class="LineNr"> 13 </span><span class="muComment"># $ ./a.elf</span> +<span id="L14" class="LineNr"> 14 </span><span class="muComment"># press ctrl-c or ctrl-d to exit</span> +<span id="L15" class="LineNr"> 15 </span><span class="muComment"># > 1</span> +<span id="L16" class="LineNr"> 16 </span><span class="muComment"># 1</span> +<span id="L17" class="LineNr"> 17 </span><span class="muComment"># > 1+1</span> +<span id="L18" class="LineNr"> 18 </span><span class="muComment"># 2</span> +<span id="L19" class="LineNr"> 19 </span><span class="muComment"># > 1 + 1</span> +<span id="L20" class="LineNr"> 20 </span><span class="muComment"># 2</span> +<span id="L21" class="LineNr"> 21 </span><span class="muComment"># > 1+2 +3</span> +<span id="L22" class="LineNr"> 22 </span><span class="muComment"># 6</span> +<span id="L23" class="LineNr"> 23 </span><span class="muComment"># > 1+2 *3</span> +<span id="L24" class="LineNr"> 24 </span><span class="muComment"># 7</span> +<span id="L25" class="LineNr"> 25 </span><span class="muComment"># > (1+2) *3</span> +<span id="L26" class="LineNr"> 26 </span><span class="muComment"># 9</span> +<span id="L27" class="LineNr"> 27 </span><span class="muComment"># > 1 + 3*4</span> +<span id="L28" class="LineNr"> 28 </span><span class="muComment"># 13</span> +<span id="L29" class="LineNr"> 29 </span><span class="muComment"># > ^D</span> +<span id="L30" class="LineNr"> 30 </span><span class="muComment"># $</span> +<span id="L31" class="LineNr"> 31 </span><span class="muComment">#</span> +<span id="L32" class="LineNr"> 32 </span><span class="muComment"># Error handling is non-existent. This is just a prototype.</span> +<span id="L33" class="LineNr"> 33 </span> +<span id="L34" class="LineNr"> 34 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L34'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L35" class="LineNr"> 35 </span> <a href='../305keyboard.subx.html#L6'>enable-keyboard-immediate-mode</a> +<span id="L36" class="LineNr"> 36 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy <span class="Constant">0</span> <span class="muComment"># lookahead</span> +<span id="L37" class="LineNr"> 37 </span> <span class="PreProc">var</span> n/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> <span class="muComment"># result of each expression</span> +<span id="L38" class="LineNr"> 38 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"press ctrl-c or ctrl-d to exit\n"</span> +<span id="L39" class="LineNr"> 39 </span> <span class="muComment"># read-eval-print loop</span> +<span id="L40" class="LineNr"> 40 </span> <span class="Delimiter">{</span> +<span id="L41" class="LineNr"> 41 </span> <span class="muComment"># print prompt</span> +<span id="L42" class="LineNr"> 42 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"> "</span> +<span id="L43" class="LineNr"> 43 </span> <span class="muComment"># read and eval</span> +<span id="L44" class="LineNr"> 44 </span> n, look <span class="Special"><-</span> <a href='arith.mu.html#L58'>simplify</a> <span class="muComment"># we explicitly thread 'look' everywhere</span> +<span id="L45" class="LineNr"> 45 </span> <span class="muComment"># if (look == 0) break</span> +<span id="L46" class="LineNr"> 46 </span> compare look, <span class="Constant">0</span> +<span id="L47" class="LineNr"> 47 </span> <span class="PreProc">break-if-=</span> +<span id="L48" class="LineNr"> 48 </span> <span class="muComment"># print</span> +<span id="L49" class="LineNr"> 49 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, n +<span id="L50" class="LineNr"> 50 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"\n"</span> +<span id="L51" class="LineNr"> 51 </span> <span class="muComment">#</span> +<span id="L52" class="LineNr"> 52 </span> <span class="PreProc">loop</span> +<span id="L53" class="LineNr"> 53 </span> <span class="Delimiter">}</span> +<span id="L54" class="LineNr"> 54 </span> <a href='../305keyboard.subx.html#L72'>enable-keyboard-type-mode</a> +<span id="L55" class="LineNr"> 55 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L56" class="LineNr"> 56 </span><span class="Delimiter">}</span> +<span id="L57" class="LineNr"> 57 </span> +<span id="L58" class="LineNr"> 58 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L58'>simplify</a></span><span class="PreProc"> -> </span>_/eax: int, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L59" class="LineNr"> 59 </span> <span class="muComment"># prime the pump</span> +<span id="L60" class="LineNr"> 60 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> +<span id="L61" class="LineNr"> 61 </span> <span class="muComment"># do it</span> +<span id="L62" class="LineNr"> 62 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L63" class="LineNr"> 63 </span> result, look <span class="Special"><-</span> <a href='arith.mu.html#L67'>expression</a> look +<span id="L64" class="LineNr"> 64 </span> <span class="PreProc">return</span> result, look +<span id="L65" class="LineNr"> 65 </span><span class="Delimiter">}</span> +<span id="L66" class="LineNr"> 66 </span> +<span id="L67" class="LineNr"> 67 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L67'>expression</a></span> _look: grapheme<span class="PreProc"> -> </span>_/eax: int, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L68" class="LineNr"> 68 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy _look +<span id="L69" class="LineNr"> 69 </span> <span class="muComment"># read arg</span> +<span id="L70" class="LineNr"> 70 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L71" class="LineNr"> 71 </span> result, look <span class="Special"><-</span> <a href='arith.mu.html#L112'>term</a> look +<span id="L72" class="LineNr"> 72 </span> $expression:<span class="PreProc">loop</span>: <span class="Delimiter">{</span> +<span id="L73" class="LineNr"> 73 </span> <span class="muComment"># while next non-space char in ['+', '-']</span> +<span id="L74" class="LineNr"> 74 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L75" class="LineNr"> 75 </span> <span class="Delimiter">{</span> +<span id="L76" class="LineNr"> 76 </span> <span class="PreProc">var</span> continue?/eax: boolean <span class="Special"><-</span> <a href='arith.mu.html#L191'>add-or-sub?</a> look +<span id="L77" class="LineNr"> 77 </span> compare continue?, <span class="Constant">0</span>/false +<span id="L78" class="LineNr"> 78 </span> <span class="PreProc">break-if-=</span> $expression:<span class="PreProc">loop</span> +<span id="L79" class="LineNr"> 79 </span> <span class="Delimiter">}</span> +<span id="L80" class="LineNr"> 80 </span> <span class="muComment"># read operator</span> +<span id="L81" class="LineNr"> 81 </span> <span class="PreProc">var</span> op/<span class="muRegEcx">ecx</span>: grapheme <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L82" class="LineNr"> 82 </span> op, look <span class="Special"><-</span> operator look +<span id="L83" class="LineNr"> 83 </span> <span class="muComment"># read next arg</span> +<span id="L84" class="LineNr"> 84 </span> <span class="PreProc">var</span> second/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L85" class="LineNr"> 85 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L86" class="LineNr"> 86 </span> <span class="Delimiter">{</span> +<span id="L87" class="LineNr"> 87 </span> <span class="PreProc">var</span> tmp/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L88" class="LineNr"> 88 </span> tmp, look <span class="Special"><-</span> <a href='arith.mu.html#L112'>term</a> look +<span id="L89" class="LineNr"> 89 </span> second <span class="Special"><-</span> copy tmp +<span id="L90" class="LineNr"> 90 </span> <span class="Delimiter">}</span> +<span id="L91" class="LineNr"> 91 </span> <span class="muComment"># reduce</span> +<span id="L92" class="LineNr"> 92 </span> $expression:perform-op: <span class="Delimiter">{</span> +<span id="L93" class="LineNr"> 93 </span> <span class="Delimiter">{</span> +<span id="L94" class="LineNr"> 94 </span> compare op, <span class="Constant">0x2b</span>/+ +<span id="L95" class="LineNr"> 95 </span> <span class="PreProc">break-if-!=</span> +<span id="L96" class="LineNr"> 96 </span> result <span class="Special"><-</span> add second +<span id="L97" class="LineNr"> 97 </span> <span class="PreProc">break</span> $expression:perform-op +<span id="L98" class="LineNr"> 98 </span> <span class="Delimiter">}</span> +<span id="L99" class="LineNr"> 99 </span> <span class="Delimiter">{</span> +<span id="L100" class="LineNr">100 </span> compare op, <span class="Constant">0x2d</span>/minus +<span id="L101" class="LineNr">101 </span> <span class="PreProc">break-if-!=</span> +<span id="L102" class="LineNr">102 </span> result <span class="Special"><-</span> subtract second +<span id="L103" class="LineNr">103 </span> <span class="PreProc">break</span> $expression:perform-op +<span id="L104" class="LineNr">104 </span> <span class="Delimiter">}</span> +<span id="L105" class="LineNr">105 </span> <span class="Delimiter">}</span> +<span id="L106" class="LineNr">106 </span> <span class="PreProc">loop</span> +<span id="L107" class="LineNr">107 </span> <span class="Delimiter">}</span> +<span id="L108" class="LineNr">108 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L109" class="LineNr">109 </span> <span class="PreProc">return</span> result, look +<span id="L110" class="LineNr">110 </span><span class="Delimiter">}</span> +<span id="L111" class="LineNr">111 </span> +<span id="L112" class="LineNr">112 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L112'>term</a></span> _look: grapheme<span class="PreProc"> -> </span>_/eax: int, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L113" class="LineNr">113 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy _look +<span id="L114" class="LineNr">114 </span> <span class="muComment"># read arg</span> +<span id="L115" class="LineNr">115 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L116" class="LineNr">116 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L117" class="LineNr">117 </span> result, look <span class="Special"><-</span> <a href='arith.mu.html#L157'>factor</a> look +<span id="L118" class="LineNr">118 </span> $term:<span class="PreProc">loop</span>: <span class="Delimiter">{</span> +<span id="L119" class="LineNr">119 </span> <span class="muComment"># while next non-space char in ['*', '/']</span> +<span id="L120" class="LineNr">120 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L121" class="LineNr">121 </span> <span class="Delimiter">{</span> +<span id="L122" class="LineNr">122 </span> <span class="PreProc">var</span> continue?/eax: boolean <span class="Special"><-</span> <a href='arith.mu.html#L177'>mul-or-div?</a> look +<span id="L123" class="LineNr">123 </span> compare continue?, <span class="Constant">0</span>/false +<span id="L124" class="LineNr">124 </span> <span class="PreProc">break-if-=</span> $term:<span class="PreProc">loop</span> +<span id="L125" class="LineNr">125 </span> <span class="Delimiter">}</span> +<span id="L126" class="LineNr">126 </span> <span class="muComment"># read operator</span> +<span id="L127" class="LineNr">127 </span> <span class="PreProc">var</span> op/<span class="muRegEcx">ecx</span>: grapheme <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L128" class="LineNr">128 </span> op, look <span class="Special"><-</span> operator look +<span id="L129" class="LineNr">129 </span> <span class="muComment"># read next arg</span> +<span id="L130" class="LineNr">130 </span> <span class="PreProc">var</span> second/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L131" class="LineNr">131 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L132" class="LineNr">132 </span> <span class="Delimiter">{</span> +<span id="L133" class="LineNr">133 </span> <span class="PreProc">var</span> tmp/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L134" class="LineNr">134 </span> tmp, look <span class="Special"><-</span> <a href='arith.mu.html#L157'>factor</a> look +<span id="L135" class="LineNr">135 </span> second <span class="Special"><-</span> copy tmp +<span id="L136" class="LineNr">136 </span> <span class="Delimiter">}</span> +<span id="L137" class="LineNr">137 </span> <span class="muComment"># reduce</span> +<span id="L138" class="LineNr">138 </span> $term:perform-op: <span class="Delimiter">{</span> +<span id="L139" class="LineNr">139 </span> <span class="Delimiter">{</span> +<span id="L140" class="LineNr">140 </span> compare op, <span class="Constant">0x2a</span>/* +<span id="L141" class="LineNr">141 </span> <span class="PreProc">break-if-!=</span> +<span id="L142" class="LineNr">142 </span> result <span class="Special"><-</span> multiply second +<span id="L143" class="LineNr">143 </span> <span class="PreProc">break</span> $term:perform-op +<span id="L144" class="LineNr">144 </span> <span class="Delimiter">}</span> +<span id="L145" class="LineNr">145 </span><span class="CommentedCode">#? {</span> +<span id="L146" class="LineNr">146 </span><span class="CommentedCode">#? compare op, 0x2f/slash</span> +<span id="L147" class="LineNr">147 </span><span class="CommentedCode">#? break-if-!=</span> +<span id="L148" class="LineNr">148 </span><span class="CommentedCode">#? result <- divide second # not in Mu yet</span> +<span id="L149" class="LineNr">149 </span><span class="CommentedCode">#? break $term:perform-op</span> +<span id="L150" class="LineNr">150 </span><span class="CommentedCode">#? }</span> +<span id="L151" class="LineNr">151 </span> <span class="Delimiter">}</span> +<span id="L152" class="LineNr">152 </span> <span class="PreProc">loop</span> +<span id="L153" class="LineNr">153 </span> <span class="Delimiter">}</span> +<span id="L154" class="LineNr">154 </span> <span class="PreProc">return</span> result, look +<span id="L155" class="LineNr">155 </span><span class="Delimiter">}</span> +<span id="L156" class="LineNr">156 </span> +<span id="L157" class="LineNr">157 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L157'>factor</a></span> _look: grapheme<span class="PreProc"> -> </span>_/eax: int, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L158" class="LineNr">158 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy _look <span class="muComment"># should be a no-op</span> +<span id="L159" class="LineNr">159 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L160" class="LineNr">160 </span> <span class="muComment"># if next char is not '(', parse a number</span> +<span id="L161" class="LineNr">161 </span> compare look, <span class="Constant">0x28</span>/open-paren +<span id="L162" class="LineNr">162 </span> <span class="Delimiter">{</span> +<span id="L163" class="LineNr">163 </span> <span class="PreProc">break-if-=</span> +<span id="L164" class="LineNr">164 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L165" class="LineNr">165 </span> result, look <span class="Special"><-</span> <a href='arith.mu.html#L211'>num</a> look +<span id="L166" class="LineNr">166 </span> <span class="PreProc">return</span> result, look +<span id="L167" class="LineNr">167 </span> <span class="Delimiter">}</span> +<span id="L168" class="LineNr">168 </span> <span class="muComment"># otherwise recurse</span> +<span id="L169" class="LineNr">169 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> <span class="muComment"># '('</span> +<span id="L170" class="LineNr">170 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L171" class="LineNr">171 </span> result, look <span class="Special"><-</span> <a href='arith.mu.html#L67'>expression</a> look +<span id="L172" class="LineNr">172 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L237'>skip-spaces</a> look +<span id="L173" class="LineNr">173 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> <span class="muComment"># ')'</span> +<span id="L174" class="LineNr">174 </span> <span class="PreProc">return</span> result, look +<span id="L175" class="LineNr">175 </span><span class="Delimiter">}</span> +<span id="L176" class="LineNr">176 </span> +<span id="L177" class="LineNr">177 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L177'>mul-or-div?</a></span> c: grapheme<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L178" class="LineNr">178 </span> compare c, <span class="Constant">0x2a</span>/* +<span id="L179" class="LineNr">179 </span> <span class="Delimiter">{</span> +<span id="L180" class="LineNr">180 </span> <span class="PreProc">break-if-!=</span> +<span id="L181" class="LineNr">181 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L182" class="LineNr">182 </span> <span class="Delimiter">}</span> +<span id="L183" class="LineNr">183 </span> compare c, <span class="Constant">0x2f</span>/slash +<span id="L184" class="LineNr">184 </span> <span class="Delimiter">{</span> +<span id="L185" class="LineNr">185 </span> <span class="PreProc">break-if-!=</span> +<span id="L186" class="LineNr">186 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L187" class="LineNr">187 </span> <span class="Delimiter">}</span> +<span id="L188" class="LineNr">188 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L189" class="LineNr">189 </span><span class="Delimiter">}</span> +<span id="L190" class="LineNr">190 </span> +<span id="L191" class="LineNr">191 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L191'>add-or-sub?</a></span> c: grapheme<span class="PreProc"> -> </span>_/eax: boolean <span class="Delimiter">{</span> +<span id="L192" class="LineNr">192 </span> compare c, <span class="Constant">0x2b</span>/+ +<span id="L193" class="LineNr">193 </span> <span class="Delimiter">{</span> +<span id="L194" class="LineNr">194 </span> <span class="PreProc">break-if-!=</span> +<span id="L195" class="LineNr">195 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L196" class="LineNr">196 </span> <span class="Delimiter">}</span> +<span id="L197" class="LineNr">197 </span> compare c, <span class="Constant">0x2d</span>/minus +<span id="L198" class="LineNr">198 </span> <span class="Delimiter">{</span> +<span id="L199" class="LineNr">199 </span> <span class="PreProc">break-if-!=</span> +<span id="L200" class="LineNr">200 </span> <span class="PreProc">return</span> <span class="Constant">1</span>/true +<span id="L201" class="LineNr">201 </span> <span class="Delimiter">}</span> +<span id="L202" class="LineNr">202 </span> <span class="PreProc">return</span> <span class="Constant">0</span>/false +<span id="L203" class="LineNr">203 </span><span class="Delimiter">}</span> +<span id="L204" class="LineNr">204 </span> +<span id="L205" class="LineNr">205 </span><span class="PreProc">fn</span> <span class="muFunction">operator</span> _look: grapheme<span class="PreProc"> -> </span>_/<span class="muRegEcx">ecx</span>: grapheme, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L206" class="LineNr">206 </span> <span class="PreProc">var</span> op/<span class="muRegEcx">ecx</span>: grapheme <span class="Special"><-</span> copy _look +<span id="L207" class="LineNr">207 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> +<span id="L208" class="LineNr">208 </span> <span class="PreProc">return</span> op, look +<span id="L209" class="LineNr">209 </span><span class="Delimiter">}</span> +<span id="L210" class="LineNr">210 </span> +<span id="L211" class="LineNr">211 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L211'>num</a></span> _look: grapheme<span class="PreProc"> -> </span>_/eax: int, _/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L212" class="LineNr">212 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy _look +<span id="L213" class="LineNr">213 </span> <span class="PreProc">var</span> result/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L214" class="LineNr">214 </span> <span class="Delimiter">{</span> +<span id="L215" class="LineNr">215 </span> <span class="PreProc">var</span> first-digit/eax: int <span class="Special"><-</span> <a href='../126write-int-decimal.subx.html#L405'>to-decimal-digit</a> look +<span id="L216" class="LineNr">216 </span> result <span class="Special"><-</span> copy first-digit +<span id="L217" class="LineNr">217 </span> <span class="Delimiter">}</span> +<span id="L218" class="LineNr">218 </span> <span class="Delimiter">{</span> +<span id="L219" class="LineNr">219 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> +<span id="L220" class="LineNr">220 </span> <span class="muComment"># done?</span> +<span id="L221" class="LineNr">221 </span> <span class="PreProc">var</span> digit?/eax: boolean <span class="Special"><-</span> <a href='../126write-int-decimal.subx.html#L306'>decimal-digit?</a> look +<span id="L222" class="LineNr">222 </span> compare digit?, <span class="Constant">0</span>/false +<span id="L223" class="LineNr">223 </span> <span class="PreProc">break-if-=</span> +<span id="L224" class="LineNr">224 </span> <span class="muComment"># result *= 10</span> +<span id="L225" class="LineNr">225 </span> <span class="Delimiter">{</span> +<span id="L226" class="LineNr">226 </span> <span class="PreProc">var</span> ten/eax: int <span class="Special"><-</span> copy <span class="Constant">0xa</span> +<span id="L227" class="LineNr">227 </span> result <span class="Special"><-</span> multiply ten +<span id="L228" class="LineNr">228 </span> <span class="Delimiter">}</span> +<span id="L229" class="LineNr">229 </span> <span class="muComment"># result += digit(look)</span> +<span id="L230" class="LineNr">230 </span> <span class="PreProc">var</span> digit/eax: int <span class="Special"><-</span> <a href='../126write-int-decimal.subx.html#L405'>to-decimal-digit</a> look +<span id="L231" class="LineNr">231 </span> result <span class="Special"><-</span> add digit +<span id="L232" class="LineNr">232 </span> <span class="PreProc">loop</span> +<span id="L233" class="LineNr">233 </span> <span class="Delimiter">}</span> +<span id="L234" class="LineNr">234 </span> <span class="PreProc">return</span> result, look +<span id="L235" class="LineNr">235 </span><span class="Delimiter">}</span> +<span id="L236" class="LineNr">236 </span> +<span id="L237" class="LineNr">237 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L237'>skip-spaces</a></span> _look: grapheme<span class="PreProc"> -> </span>_/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L238" class="LineNr">238 </span> <span class="PreProc">var</span> look/<span class="muRegEsi">esi</span>: grapheme <span class="Special"><-</span> copy _look <span class="muComment"># should be a no-op</span> +<span id="L239" class="LineNr">239 </span> <span class="Delimiter">{</span> +<span id="L240" class="LineNr">240 </span> compare look, <span class="Constant">0x20</span> +<span id="L241" class="LineNr">241 </span> <span class="PreProc">break-if-!=</span> +<span id="L242" class="LineNr">242 </span> look <span class="Special"><-</span> <a href='arith.mu.html#L248'>get-char</a> +<span id="L243" class="LineNr">243 </span> <span class="PreProc">loop</span> +<span id="L244" class="LineNr">244 </span> <span class="Delimiter">}</span> +<span id="L245" class="LineNr">245 </span> <span class="PreProc">return</span> look +<span id="L246" class="LineNr">246 </span><span class="Delimiter">}</span> +<span id="L247" class="LineNr">247 </span> +<span id="L248" class="LineNr">248 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='arith.mu.html#L248'>get-char</a></span><span class="PreProc"> -> </span>_/<span class="muRegEsi">esi</span>: grapheme <span class="Delimiter">{</span> +<span id="L249" class="LineNr">249 </span> <span class="PreProc">var</span> look/eax: grapheme <span class="Special"><-</span> <a href='../305keyboard.subx.html#L132'>read-key-from-real-keyboard</a> +<span id="L250" class="LineNr">250 </span> <a href='../304screen.subx.html#L161'>print-grapheme-to-real-screen</a> look +<span id="L251" class="LineNr">251 </span> compare look, <span class="Constant">4</span> +<span id="L252" class="LineNr">252 </span> <span class="Delimiter">{</span> +<span id="L253" class="LineNr">253 </span> <span class="PreProc">break-if-!=</span> +<span id="L254" class="LineNr">254 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"^D\n"</span> +<span id="L255" class="LineNr">255 </span> <a href='../000init.subx.html#L18'>syscall_exit</a> +<span id="L256" class="LineNr">256 </span> <span class="Delimiter">}</span> +<span id="L257" class="LineNr">257 </span> <span class="PreProc">return</span> look +<span id="L258" class="LineNr">258 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/crenshaw2-1.subx.html b/html/linux/apps/crenshaw2-1.subx.html new file mode 100644 index 00000000..191ff721 --- /dev/null +++ b/html/linux/apps/crenshaw2-1.subx.html @@ -0,0 +1,621 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/crenshaw2-1.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +.subxTest { color: #5f8700; } +.subxH1Comment { color: #005faf; text-decoration: underline; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/crenshaw2-1.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/crenshaw2-1.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Port of <a href="https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas">https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment"># which corresponds to the section "single digits" in <a href="https://compilers.iecc.com/crenshaw/tutor2.txt">https://compilers.iecc.com/crenshaw/tutor2.txt</a></span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># except that we support hex digits.</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment">#</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># To run:</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># $ bootstrap/bootstrap translate [01]*.subx apps/crenshaw2-1.subx -o crenshaw2-1</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo '3' |bootstrap/bootstrap run crenshaw2-1</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># Expected output:</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># # syscall(exit, 3)</span> +<span id="L10" class="LineNr"> 10 </span><span class="subxComment"># bb/copy-to-ebx 3/imm32</span> +<span id="L11" class="LineNr"> 11 </span><span class="subxComment"># b8/copy-to-eax 1/imm32/exit</span> +<span id="L12" class="LineNr"> 12 </span><span class="subxComment"># cd/syscall 0x80/imm8</span> +<span id="L13" class="LineNr"> 13 </span><span class="subxComment">#</span> +<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># To run the generated output:</span> +<span id="L15" class="LineNr"> 15 </span><span class="subxComment"># $ echo '3' |bootstrap/bootstrap run crenshaw2-1 > z1.subx</span> +<span id="L16" class="LineNr"> 16 </span><span class="subxComment"># $ bootstrap/bootstrap translate z1.subx -o z1</span> +<span id="L17" class="LineNr"> 17 </span><span class="subxComment"># $ bootstrap/bootstrap run z1</span> +<span id="L18" class="LineNr"> 18 </span><span class="subxComment"># $ echo $?</span> +<span id="L19" class="LineNr"> 19 </span><span class="subxComment"># 3</span> +<span id="L20" class="LineNr"> 20 </span><span class="subxComment">#</span> +<span id="L21" class="LineNr"> 21 </span><span class="subxComment"># Stdin must contain just a single hex digit. Other input will print an error:</span> +<span id="L22" class="LineNr"> 22 </span><span class="subxComment"># $ echo 'xyz' |bootstrap/bootstrap run crenshaw2-1</span> +<span id="L23" class="LineNr"> 23 </span><span class="subxComment"># Error: integer expected</span> +<span id="L24" class="LineNr"> 24 </span><span class="subxComment">#</span> +<span id="L25" class="LineNr"> 25 </span><span class="subxComment"># Names in this file sometimes follow Crenshaw's original rather than my usual</span> +<span id="L26" class="LineNr"> 26 </span><span class="subxComment"># naming conventions.</span> +<span id="L27" class="LineNr"> 27 </span> +<span id="L28" class="LineNr"> 28 </span>== code +<span id="L29" class="LineNr"> 29 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L30" class="LineNr"> 30 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L31" class="LineNr"> 31 </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="L32" class="LineNr"> 32 </span> +<span id="L33" class="LineNr"> 33 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, call 'compile' if not</span> +<span id="L34" class="LineNr"> 34 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L35" class="LineNr"> 35 </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="L36" class="LineNr"> 36 </span> +<span id="L37" class="LineNr"> 37 </span> <span class="subxComment"># initialize heap</span> +<span id="L38" class="LineNr"> 38 </span> <span class="subxS1Comment"># . Heap = new-segment(Heap-size)</span> +<span id="L39" class="LineNr"> 39 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L40" class="LineNr"> 40 </span> 68/push <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>/imm32 +<span id="L41" class="LineNr"> 41 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span>/disp32 <span class="subxComment"># push *Heap-size</span> +<span id="L42" class="LineNr"> 42 </span> <span class="subxS2Comment"># . . call</span> +<span id="L43" class="LineNr"> 43 </span> e8/call <a href='../104new-segment.subx.html#L40'>new-segment</a>/disp32 +<span id="L44" class="LineNr"> 44 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L45" class="LineNr"> 45 </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="L46" class="LineNr"> 46 </span> +<span id="L47" class="LineNr"> 47 </span> <span class="subxH1Comment"># - if argc > 1 and argv[1] == "test", then return run_tests()</span> +<span id="L48" class="LineNr"> 48 </span> <span class="subxComment"># if (argc <= 1) goto run-main</span> +<span id="L49" class="LineNr"> 49 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/disp8 1/imm32 <span class="subxComment"># compare *ebp</span> +<span id="L50" class="LineNr"> 50 </span> 7e/jump-if-<= $run-main/disp8 +<span id="L51" class="LineNr"> 51 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) goto run-main</span> +<span id="L52" class="LineNr"> 52 </span> <span class="subxS1Comment"># . eax = kernel-string-equal?(argv[1], "test")</span> +<span id="L53" class="LineNr"> 53 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L54" class="LineNr"> 54 </span> 68/push <span class="Constant">"test"</span>/imm32 +<span id="L55" class="LineNr"> 55 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L56" class="LineNr"> 56 </span> <span class="subxS2Comment"># . . call</span> +<span id="L57" class="LineNr"> 57 </span> e8/call <a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</a>/disp32 +<span id="L58" class="LineNr"> 58 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L59" class="LineNr"> 59 </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="L60" class="LineNr"> 60 </span> <span class="subxS1Comment"># . if (eax == false) goto run-main</span> +<span id="L61" class="LineNr"> 61 </span> 3d/compare-eax-and 0/imm32/false +<span id="L62" class="LineNr"> 62 </span> 74/jump-if-= $run-main/disp8 +<span id="L63" class="LineNr"> 63 </span> <span class="subxComment"># run-tests()</span> +<span id="L64" class="LineNr"> 64 </span> e8/call run-tests/disp32 +<span id="L65" class="LineNr"> 65 </span> <span class="subxComment"># syscall(exit, *Num-test-failures)</span> +<span id="L66" class="LineNr"> 66 </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='../102test.subx.html#L89'>Num-test-failures</a></span>/disp32 <span class="subxComment"># copy *Num-test-failures to ebx</span> +<span id="L67" class="LineNr"> 67 </span> eb/jump $main:end/disp8 +<span id="L68" class="LineNr"> 68 </span><span class="Constant">$run-main</span>: +<span id="L69" class="LineNr"> 69 </span> <span class="subxH1Comment"># - otherwise read a program from stdin and emit its translation to stdout</span> +<span id="L70" class="LineNr"> 70 </span> <span class="subxS1Comment"># . compile(Stdin, 1/stdout, 2/stderr, ed)</span> +<span id="L71" class="LineNr"> 71 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L72" class="LineNr"> 72 </span> 68/push 0/imm32/exit-descriptor +<span id="L73" class="LineNr"> 73 </span> 68/push 2/imm32/stderr +<span id="L74" class="LineNr"> 74 </span> 68/push 1/imm32/stdout +<span id="L75" class="LineNr"> 75 </span> 68/push <span class="SpecialChar"><a href='../112read-byte.subx.html#L14'>Stdin</a></span>/imm32 +<span id="L76" class="LineNr"> 76 </span> <span class="subxS2Comment"># . . call</span> +<span id="L77" class="LineNr"> 77 </span> e8/call <a href='crenshaw2-1.subx.html#L86'>compile</a>/disp32 +<span id="L78" class="LineNr"> 78 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L79" class="LineNr"> 79 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L80" class="LineNr"> 80 </span> <span class="subxComment"># syscall(exit, 0)</span> +<span id="L81" class="LineNr"> 81 </span> bb/copy-to-ebx 0/imm32 +<span id="L82" class="LineNr"> 82 </span><span class="Constant">$main:end</span>: +<span id="L83" class="LineNr"> 83 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L84" class="LineNr"> 84 </span> +<span id="L85" class="LineNr"> 85 </span><span class="subxComment"># the main entry point</span> +<span id="L86" class="LineNr"> 86 </span><span class="subxFunction">compile</span>: <span class="subxComment"># in: (addr buffered-file), out: fd or (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)</span> +<span id="L87" class="LineNr"> 87 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L88" class="LineNr"> 88 </span> 55/push-ebp +<span id="L89" class="LineNr"> 89 </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="L90" class="LineNr"> 90 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L91" class="LineNr"> 91 </span> 50/push-eax +<span id="L92" class="LineNr"> 92 </span> 51/push-ecx +<span id="L93" class="LineNr"> 93 </span> <span class="subxComment"># prime the pump</span> +<span id="L94" class="LineNr"> 94 </span> <span class="subxS1Comment"># . Look = get-char(in)</span> +<span id="L95" class="LineNr"> 95 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L96" class="LineNr"> 96 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L97" class="LineNr"> 97 </span> <span class="subxS2Comment"># . . call</span> +<span id="L98" class="LineNr"> 98 </span> e8/call <a href='crenshaw2-1.subx.html#L508'>get-char</a>/disp32 +<span id="L99" class="LineNr"> 99 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L100" class="LineNr">100 </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="L101" class="LineNr">101 </span> <span class="subxComment"># var num/ecx: (stream byte 7)</span> +<span id="L102" class="LineNr">102 </span> <span class="subxComment"># Numbers can be 32 bits or 8 hex bytes long. One of them will be in 'Look', so we need space for 7 bytes.</span> +<span id="L103" class="LineNr">103 </span> <span class="subxComment"># Sizing the stream just right buys us overflow-handling for free inside 'get-num'.</span> +<span id="L104" class="LineNr">104 </span> <span class="subxComment"># Add 12 bytes for 'read', 'write' and 'size' fields, for a total of 19 bytes, or 0x13 in hex.</span> +<span id="L105" class="LineNr">105 </span> <span class="subxComment"># The stack pointer is no longer aligned, so dump_stack() can be misleading past this point.</span> +<span id="L106" class="LineNr">106 </span> 81 5/subop/subtract 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> 0x13/imm32 <span class="subxComment"># subtract from esp</span> +<span id="L107" class="LineNr">107 </span> 89/copy 3/mod/direct 1/rm32/ecx <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 ecx</span> +<span id="L108" class="LineNr">108 </span> <span class="subxComment"># initialize the stream</span> +<span id="L109" class="LineNr">109 </span> <span class="subxS1Comment"># . num->size = 7</span> +<span id="L110" class="LineNr">110 </span> c7 0/subop/copy 1/mod/*+disp8 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 7/imm32 <span class="subxComment"># copy to *(ecx+8)</span> +<span id="L111" class="LineNr">111 </span> <span class="subxS1Comment"># . clear-stream(num)</span> +<span id="L112" class="LineNr">112 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L113" class="LineNr">113 </span> 51/push-ecx +<span id="L114" class="LineNr">114 </span> <span class="subxS2Comment"># . . call</span> +<span id="L115" class="LineNr">115 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L116" class="LineNr">116 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L117" class="LineNr">117 </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="L118" class="LineNr">118 </span> <span class="subxComment"># read a digit from 'in' into 'num'</span> +<span id="L119" class="LineNr">119 </span> <span class="subxS1Comment"># . get-num(in, num, err, ed)</span> +<span id="L120" class="LineNr">120 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L121" class="LineNr">121 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L122" class="LineNr">122 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L123" class="LineNr">123 </span> 51/push-ecx/num +<span id="L124" class="LineNr">124 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L125" class="LineNr">125 </span> <span class="subxS2Comment"># . . call</span> +<span id="L126" class="LineNr">126 </span> e8/call <a href='crenshaw2-1.subx.html#L187'>get-num</a>/disp32 +<span id="L127" class="LineNr">127 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L128" class="LineNr">128 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L129" class="LineNr">129 </span> <span class="subxComment"># render 'num' into the following template on 'out':</span> +<span id="L130" class="LineNr">130 </span> <span class="subxComment"># bb/copy-to-ebx _num_</span> +<span id="L131" class="LineNr">131 </span> <span class="subxComment"># b8/copy-to-eax 1/imm32/exit</span> +<span id="L132" class="LineNr">132 </span> <span class="subxComment"># cd/syscall 0x80/imm8</span> +<span id="L133" class="LineNr">133 </span> <span class="subxComment">#</span> +<span id="L134" class="LineNr">134 </span> <span class="subxS1Comment"># . write(out, "bb/copy-to-ebx ")</span> +<span id="L135" class="LineNr">135 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L136" class="LineNr">136 </span> 68/push <span class="Constant">"bb/copy-to-ebx "</span>/imm32 +<span id="L137" class="LineNr">137 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L138" class="LineNr">138 </span> <span class="subxS2Comment"># . . call</span> +<span id="L139" class="LineNr">139 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L140" class="LineNr">140 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L141" class="LineNr">141 </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="L142" class="LineNr">142 </span> <span class="subxS1Comment"># . write-stream(out, num)</span> +<span id="L143" class="LineNr">143 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L144" class="LineNr">144 </span> 51/push-ecx/num +<span id="L145" class="LineNr">145 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L146" class="LineNr">146 </span> <span class="subxS2Comment"># . . call</span> +<span id="L147" class="LineNr">147 </span> e8/call <a href='../113write-stream.subx.html#L17'>write-stream</a>/disp32 +<span id="L148" class="LineNr">148 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L149" class="LineNr">149 </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="L150" class="LineNr">150 </span> <span class="subxS1Comment"># . write(out, Newline)</span> +<span id="L151" class="LineNr">151 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L152" class="LineNr">152 </span> 68/push <span class="SpecialChar"><a href='../102test.subx.html#L82'>Newline</a></span>/imm32 +<span id="L153" class="LineNr">153 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+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='../108write.subx.html#L24'>write</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> 8/imm32 <span class="subxComment"># add to esp</span> +<span id="L158" class="LineNr">158 </span> <span class="subxS1Comment"># . write(out, "b8/copy-to-eax 1/imm32/exit\n")</span> +<span id="L159" class="LineNr">159 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L160" class="LineNr">160 </span> 68/push <span class="Constant">"b8/copy-to-eax 1/imm32/exit\n"</span>/imm32 +<span id="L161" class="LineNr">161 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L162" class="LineNr">162 </span> <span class="subxS2Comment"># . . call</span> +<span id="L163" class="LineNr">163 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L164" class="LineNr">164 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L165" class="LineNr">165 </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="L166" class="LineNr">166 </span> <span class="subxS1Comment"># . write(out, "cd/syscall 0x80/imm8\n")</span> +<span id="L167" class="LineNr">167 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L168" class="LineNr">168 </span> 68/push <span class="Constant">"cd/syscall 0x80/imm8\n"</span>/imm32 +<span id="L169" class="LineNr">169 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L170" class="LineNr">170 </span> <span class="subxS2Comment"># . . call</span> +<span id="L171" class="LineNr">171 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L172" class="LineNr">172 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L173" class="LineNr">173 </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="L174" class="LineNr">174 </span><span class="Constant">$compile:end</span>: +<span id="L175" class="LineNr">175 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L176" class="LineNr">176 </span> 59/pop-to-ecx +<span id="L177" class="LineNr">177 </span> 58/pop-to-eax +<span id="L178" class="LineNr">178 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L179" class="LineNr">179 </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="L180" class="LineNr">180 </span> 5d/pop-to-ebp +<span id="L181" class="LineNr">181 </span> c3/return +<span id="L182" class="LineNr">182 </span> +<span id="L183" class="LineNr">183 </span><span class="subxComment"># Read a single digit into 'out'. Abort if there are none, or if there is no</span> +<span id="L184" class="LineNr">184 </span><span class="subxComment"># space in 'out'.</span> +<span id="L185" class="LineNr">185 </span><span class="subxComment"># Input comes from the global variable 'Look' (first byte) and the argument</span> +<span id="L186" class="LineNr">186 </span><span class="subxComment"># 'in' (rest). We leave the next byte from 'in' into 'Look' on exit.</span> +<span id="L187" class="LineNr">187 </span><span class="subxFunction">get-num</span>: <span class="subxComment"># in: (addr buffered-file), out: (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)</span> +<span id="L188" class="LineNr">188 </span> <span class="subxComment"># pseudocode:</span> +<span id="L189" class="LineNr">189 </span> <span class="subxComment"># if (!digit?(Look)) expected(ed, err, "integer")</span> +<span id="L190" class="LineNr">190 </span> <span class="subxComment"># if out->write >= out->size</span> +<span id="L191" class="LineNr">191 </span> <span class="subxComment"># write(err, "Error: too many digits in number\n")</span> +<span id="L192" class="LineNr">192 </span> <span class="subxComment"># stop(ed, 1)</span> +<span id="L193" class="LineNr">193 </span> <span class="subxComment"># out->data[out->write] = LSB(Look)</span> +<span id="L194" class="LineNr">194 </span> <span class="subxComment"># ++out->write</span> +<span id="L195" class="LineNr">195 </span> <span class="subxComment"># Look = get-char(in)</span> +<span id="L196" class="LineNr">196 </span> <span class="subxComment">#</span> +<span id="L197" class="LineNr">197 </span> <span class="subxComment"># registers:</span> +<span id="L198" class="LineNr">198 </span> <span class="subxComment"># in: esi</span> +<span id="L199" class="LineNr">199 </span> <span class="subxComment"># out: edi</span> +<span id="L200" class="LineNr">200 </span> <span class="subxComment"># out->write: ecx (cached copy; need to keep in sync)</span> +<span id="L201" class="LineNr">201 </span> <span class="subxComment"># out->size: edx</span> +<span id="L202" class="LineNr">202 </span> <span class="subxComment"># temporaries: eax, ebx</span> +<span id="L203" class="LineNr">203 </span> <span class="subxComment"># We can't allocate Look to a register because it gets written implicitly in</span> +<span id="L204" class="LineNr">204 </span> <span class="subxComment"># get-char in each iteration of the loop. (Thereby demonstrating that it's</span> +<span id="L205" class="LineNr">205 </span> <span class="subxComment"># not the right interface for us. But we'll keep it just to follow Crenshaw.)</span> +<span id="L206" class="LineNr">206 </span> <span class="subxComment">#</span> +<span id="L207" class="LineNr">207 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L208" class="LineNr">208 </span> 55/push-ebp +<span id="L209" class="LineNr">209 </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="L210" class="LineNr">210 </span> <span class="subxH1Comment"># - if (digit?(Look)) expected(ed, err, "integer")</span> +<span id="L211" class="LineNr">211 </span> <span class="subxS1Comment"># . eax = digit?(Look)</span> +<span id="L212" class="LineNr">212 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L213" class="LineNr">213 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='crenshaw2-1.subx.html#L553'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># push *Look</span> +<span id="L214" class="LineNr">214 </span> <span class="subxS2Comment"># . . call</span> +<span id="L215" class="LineNr">215 </span> e8/call <a href='crenshaw2-1.subx.html#L531'>digit?</a>/disp32 +<span id="L216" class="LineNr">216 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L217" class="LineNr">217 </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="L218" class="LineNr">218 </span> <span class="subxS1Comment"># . if (eax == false)</span> +<span id="L219" class="LineNr">219 </span> 3d/compare-eax-and 0/imm32/false +<span id="L220" class="LineNr">220 </span> 75/jump-if-!= $get-num:main/disp8 +<span id="L221" class="LineNr">221 </span> <span class="subxS1Comment"># . expected(ed, err, "integer")</span> +<span id="L222" class="LineNr">222 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L223" class="LineNr">223 </span> 68/push <span class="Constant">"integer"</span>/imm32 +<span id="L224" class="LineNr">224 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L225" class="LineNr">225 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L226" class="LineNr">226 </span> <span class="subxS2Comment"># . . call</span> +<span id="L227" class="LineNr">227 </span> e8/call <a href='crenshaw2-1.subx.html#L466'>expected</a>/disp32 <span class="subxComment"># never returns</span> +<span id="L228" class="LineNr">228 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L229" class="LineNr">229 </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="L230" class="LineNr">230 </span><span class="Constant">$get-num:main</span>: +<span id="L231" class="LineNr">231 </span> <span class="subxH1Comment"># - otherwise read a digit</span> +<span id="L232" class="LineNr">232 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L233" class="LineNr">233 </span> 50/push-eax +<span id="L234" class="LineNr">234 </span> 51/push-ecx +<span id="L235" class="LineNr">235 </span> 52/push-edx +<span id="L236" class="LineNr">236 </span> 53/push-ebx +<span id="L237" class="LineNr">237 </span> 56/push-esi +<span id="L238" class="LineNr">238 </span> 57/push-edi +<span id="L239" class="LineNr">239 </span> <span class="subxComment"># read necessary variables to registers</span> +<span id="L240" class="LineNr">240 </span> <span class="subxComment"># esi = in</span> +<span id="L241" class="LineNr">241 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 6/r32/esi 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to esi</span> +<span id="L242" class="LineNr">242 </span> <span class="subxComment"># edi = out</span> +<span id="L243" class="LineNr">243 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 7/r32/edi 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+12) to edi</span> +<span id="L244" class="LineNr">244 </span> <span class="subxComment"># ecx = out->write</span> +<span id="L245" class="LineNr">245 </span> 8b/copy 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *edi to ecx</span> +<span id="L246" class="LineNr">246 </span> <span class="subxComment"># edx = out->size</span> +<span id="L247" class="LineNr">247 </span> 8b/copy 1/mod/*+disp8 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(edi+8) to edx</span> +<span id="L248" class="LineNr">248 </span><span class="Constant">$get-num:loop</span>: +<span id="L249" class="LineNr">249 </span> <span class="subxComment"># if (out->write >= out->size) error</span> +<span id="L250" class="LineNr">250 </span> 39/compare 3/mod/direct 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare edx with ecx</span> +<span id="L251" class="LineNr">251 </span> 7d/jump-if-< $get-num:stage2/disp8 +<span id="L252" class="LineNr">252 </span> <span class="subxS1Comment"># . error(ed, err, msg) # TODO: show full number</span> +<span id="L253" class="LineNr">253 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L254" class="LineNr">254 </span> 68/push <span class="Constant">"get-num: too many digits in number"</span>/imm32 +<span id="L255" class="LineNr">255 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L256" class="LineNr">256 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L257" class="LineNr">257 </span> <span class="subxS2Comment"># . . call</span> +<span id="L258" class="LineNr">258 </span> e8/call <a href='../114error.subx.html#L9'>error</a>/disp32 <span class="subxComment"># never returns</span> +<span id="L259" class="LineNr">259 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L260" class="LineNr">260 </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="L261" class="LineNr">261 </span><span class="Constant">$get-num:stage2</span>: +<span id="L262" class="LineNr">262 </span> <span class="subxComment"># out->data[out->write] = LSB(Look)</span> +<span id="L263" class="LineNr">263 </span> 8d/copy-address 1/mod/*+disp8 4/rm32/sib 7/base/edi 1/index/ecx <span class="Normal"> . </span> 3/r32/ebx 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy edi+ecx+12 to ebx</span> +<span id="L264" class="LineNr">264 </span> 8b/copy 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="SpecialChar"><a href='crenshaw2-1.subx.html#L553'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># copy *Look to eax</span> +<span id="L265" class="LineNr">265 </span> 88/copy-byte 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/AL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at AL to *ebx</span> +<span id="L266" class="LineNr">266 </span> <span class="subxComment"># ++out->write</span> +<span id="L267" class="LineNr">267 </span> 41/increment-ecx +<span id="L268" class="LineNr">268 </span> <span class="subxComment"># Look = get-char(in)</span> +<span id="L269" class="LineNr">269 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L270" class="LineNr">270 </span> 56/push-esi +<span id="L271" class="LineNr">271 </span> <span class="subxS2Comment"># . . call</span> +<span id="L272" class="LineNr">272 </span> e8/call <a href='crenshaw2-1.subx.html#L508'>get-char</a>/disp32 +<span id="L273" class="LineNr">273 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L274" class="LineNr">274 </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="L275" class="LineNr">275 </span><span class="Constant">$get-num:loop-end</span>: +<span id="L276" class="LineNr">276 </span> <span class="subxComment"># persist necessary variables from registers</span> +<span id="L277" class="LineNr">277 </span> 89/copy 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ecx to *edi</span> +<span id="L278" class="LineNr">278 </span><span class="Constant">$get-num:end</span>: +<span id="L279" class="LineNr">279 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L280" class="LineNr">280 </span> 5f/pop-to-edi +<span id="L281" class="LineNr">281 </span> 5e/pop-to-esi +<span id="L282" class="LineNr">282 </span> 5b/pop-to-ebx +<span id="L283" class="LineNr">283 </span> 5a/pop-to-edx +<span id="L284" class="LineNr">284 </span> 59/pop-to-ecx +<span id="L285" class="LineNr">285 </span> 58/pop-to-eax +<span id="L286" class="LineNr">286 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L287" class="LineNr">287 </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="L288" class="LineNr">288 </span> 5d/pop-to-ebp +<span id="L289" class="LineNr">289 </span> c3/return +<span id="L290" class="LineNr">290 </span> +<span id="L291" class="LineNr">291 </span><span class="subxTest">test-get-num-reads-single-digit</span>: +<span id="L292" class="LineNr">292 </span> <span class="subxH1Comment"># - check that get-num returns first character if it's a digit</span> +<span id="L293" class="LineNr">293 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L294" class="LineNr">294 </span> 55/push-ebp +<span id="L295" class="LineNr">295 </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="L296" class="LineNr">296 </span> <span class="subxComment"># clear all streams</span> +<span id="L297" class="LineNr">297 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L298" class="LineNr">298 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L299" class="LineNr">299 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L300" class="LineNr">300 </span> <span class="subxS2Comment"># . . call</span> +<span id="L301" class="LineNr">301 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L302" class="LineNr">302 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L303" class="LineNr">303 </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="L304" class="LineNr">304 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L305" class="LineNr">305 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L306" class="LineNr">306 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L307" class="LineNr">307 </span> <span class="subxS2Comment"># . . call</span> +<span id="L308" class="LineNr">308 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L309" class="LineNr">309 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L310" class="LineNr">310 </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="L311" class="LineNr">311 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L312" class="LineNr">312 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L313" class="LineNr">313 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L314" class="LineNr">314 </span> <span class="subxS2Comment"># . . call</span> +<span id="L315" class="LineNr">315 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L316" class="LineNr">316 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L317" class="LineNr">317 </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="L318" class="LineNr">318 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L319" class="LineNr">319 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L320" class="LineNr">320 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L321" class="LineNr">321 </span> <span class="subxS2Comment"># . . call</span> +<span id="L322" class="LineNr">322 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L323" class="LineNr">323 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L324" class="LineNr">324 </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="L325" class="LineNr">325 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L326" class="LineNr">326 </span> <span class="subxS1Comment"># . write(_test-stream, "3")</span> +<span id="L327" class="LineNr">327 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L328" class="LineNr">328 </span> 68/push <span class="Constant">"3"</span>/imm32 +<span id="L329" class="LineNr">329 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L330" class="LineNr">330 </span> <span class="subxS2Comment"># . . call</span> +<span id="L331" class="LineNr">331 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L332" class="LineNr">332 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L333" class="LineNr">333 </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="L334" class="LineNr">334 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L335" class="LineNr">335 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L336" class="LineNr">336 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L337" class="LineNr">337 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L338" class="LineNr">338 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L339" class="LineNr">339 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L340" class="LineNr">340 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L341" class="LineNr">341 </span> 50/push-eax/ed +<span id="L342" class="LineNr">342 </span> <span class="subxS2Comment"># . . call</span> +<span id="L343" class="LineNr">343 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L344" class="LineNr">344 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L345" class="LineNr">345 </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="L346" class="LineNr">346 </span> <span class="subxComment"># prime the pump</span> +<span id="L347" class="LineNr">347 </span> <span class="subxS1Comment"># . get-char(_test-buffered-file)</span> +<span id="L348" class="LineNr">348 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L349" class="LineNr">349 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L350" class="LineNr">350 </span> <span class="subxS2Comment"># . . call</span> +<span id="L351" class="LineNr">351 </span> e8/call <a href='crenshaw2-1.subx.html#L508'>get-char</a>/disp32 +<span id="L352" class="LineNr">352 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L353" class="LineNr">353 </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="L354" class="LineNr">354 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L355" class="LineNr">355 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L356" class="LineNr">356 </span> 50/push-eax/ed +<span id="L357" class="LineNr">357 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L358" class="LineNr">358 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L359" class="LineNr">359 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L360" class="LineNr">360 </span> <span class="subxS2Comment"># . . call</span> +<span id="L361" class="LineNr">361 </span> e8/call <a href='crenshaw2-1.subx.html#L187'>get-num</a>/disp32 +<span id="L362" class="LineNr">362 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L363" class="LineNr">363 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L364" class="LineNr">364 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L365" class="LineNr">365 </span> <span class="subxComment"># check-ints-equal(*_test-output-stream->data, '3', msg)</span> +<span id="L366" class="LineNr">366 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L367" class="LineNr">367 </span> 68/push <span class="Constant">"F - test-get-num-reads-single-digit"</span>/imm32 +<span id="L368" class="LineNr">368 </span> 68/push 0x33/imm32 +<span id="L369" class="LineNr">369 </span> b8/copy-to-eax <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L370" class="LineNr">370 </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="L371" class="LineNr">371 </span> <span class="subxS2Comment"># . . call</span> +<span id="L372" class="LineNr">372 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L373" class="LineNr">373 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L374" class="LineNr">374 </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="L375" class="LineNr">375 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L376" class="LineNr">376 </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="L377" class="LineNr">377 </span> 5d/pop-to-ebp +<span id="L378" class="LineNr">378 </span> c3/return +<span id="L379" class="LineNr">379 </span> +<span id="L380" class="LineNr">380 </span><span class="subxTest">test-get-num-aborts-on-non-digit-in-Look</span>: +<span id="L381" class="LineNr">381 </span> <span class="subxH1Comment"># - check that get-num returns first character if it's a digit</span> +<span id="L382" class="LineNr">382 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L383" class="LineNr">383 </span> 55/push-ebp +<span id="L384" class="LineNr">384 </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="L385" class="LineNr">385 </span> <span class="subxComment"># clear all streams</span> +<span id="L386" class="LineNr">386 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L387" class="LineNr">387 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L388" class="LineNr">388 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L389" class="LineNr">389 </span> <span class="subxS2Comment"># . . call</span> +<span id="L390" class="LineNr">390 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L391" class="LineNr">391 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L392" class="LineNr">392 </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="L393" class="LineNr">393 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L394" class="LineNr">394 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L395" class="LineNr">395 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L396" class="LineNr">396 </span> <span class="subxS2Comment"># . . call</span> +<span id="L397" class="LineNr">397 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L398" class="LineNr">398 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L399" class="LineNr">399 </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="L400" class="LineNr">400 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L401" class="LineNr">401 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L402" class="LineNr">402 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L403" class="LineNr">403 </span> <span class="subxS2Comment"># . . call</span> +<span id="L404" class="LineNr">404 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L405" class="LineNr">405 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L406" class="LineNr">406 </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="L407" class="LineNr">407 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L408" class="LineNr">408 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L409" class="LineNr">409 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L410" class="LineNr">410 </span> <span class="subxS2Comment"># . . call</span> +<span id="L411" class="LineNr">411 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L412" class="LineNr">412 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L413" class="LineNr">413 </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="L414" class="LineNr">414 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L415" class="LineNr">415 </span> <span class="subxS1Comment"># . write(_test-stream, "3")</span> +<span id="L416" class="LineNr">416 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L417" class="LineNr">417 </span> 68/push <span class="Constant">"3"</span>/imm32 +<span id="L418" class="LineNr">418 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L419" class="LineNr">419 </span> <span class="subxS2Comment"># . . call</span> +<span id="L420" class="LineNr">420 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L421" class="LineNr">421 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L422" class="LineNr">422 </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="L423" class="LineNr">423 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L424" class="LineNr">424 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L425" class="LineNr">425 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L426" class="LineNr">426 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L427" class="LineNr">427 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L428" class="LineNr">428 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L429" class="LineNr">429 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L430" class="LineNr">430 </span> 50/push-eax/ed +<span id="L431" class="LineNr">431 </span> <span class="subxS2Comment"># . . call</span> +<span id="L432" class="LineNr">432 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L433" class="LineNr">433 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L434" class="LineNr">434 </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="L435" class="LineNr">435 </span> <span class="subxComment"># *don't* prime the pump</span> +<span id="L436" class="LineNr">436 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L437" class="LineNr">437 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L438" class="LineNr">438 </span> 50/push-eax/ed +<span id="L439" class="LineNr">439 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L440" class="LineNr">440 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L441" class="LineNr">441 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L442" class="LineNr">442 </span> <span class="subxS2Comment"># . . call</span> +<span id="L443" class="LineNr">443 </span> e8/call <a href='crenshaw2-1.subx.html#L187'>get-num</a>/disp32 +<span id="L444" class="LineNr">444 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L445" class="LineNr">445 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L446" class="LineNr">446 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L447" class="LineNr">447 </span> <span class="subxComment"># check that get-num tried to call exit(1)</span> +<span id="L448" class="LineNr">448 </span> <span class="subxS1Comment"># . check-ints-equal(ed->value, 2, msg) # i.e. stop was called with value 1</span> +<span id="L449" class="LineNr">449 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L450" class="LineNr">450 </span> 68/push <span class="Constant">"F - test-get-num-aborts-on-non-digit-in-Look"</span>/imm32 +<span id="L451" class="LineNr">451 </span> 68/push 2/imm32 +<span id="L452" class="LineNr">452 </span> <span class="subxS2Comment"># . . push ed->value</span> +<span id="L453" class="LineNr">453 </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> 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(eax+4)</span> +<span id="L454" class="LineNr">454 </span> <span class="subxS2Comment"># . . call</span> +<span id="L455" class="LineNr">455 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L456" class="LineNr">456 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L457" class="LineNr">457 </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="L458" class="LineNr">458 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L459" class="LineNr">459 </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="L460" class="LineNr">460 </span> 5d/pop-to-ebp +<span id="L461" class="LineNr">461 </span> c3/return +<span id="L462" class="LineNr">462 </span> +<span id="L463" class="LineNr">463 </span><span class="subxComment">## helpers</span> +<span id="L464" class="LineNr">464 </span> +<span id="L465" class="LineNr">465 </span><span class="subxComment"># write(f, "Error: "+s+" expected\n") then stop(ed, 1)</span> +<span id="L466" class="LineNr">466 </span><span class="subxFunction">expected</span>: <span class="subxComment"># ed: (addr exit-descriptor), f: fd or (addr stream byte), s: (addr array byte)</span> +<span id="L467" class="LineNr">467 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L468" class="LineNr">468 </span> 55/push-ebp +<span id="L469" class="LineNr">469 </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="L470" class="LineNr">470 </span> <span class="subxComment"># write(f, "Error: ")</span> +<span id="L471" class="LineNr">471 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L472" class="LineNr">472 </span> 68/push <span class="Constant">"Error: "</span>/imm32 +<span id="L473" class="LineNr">473 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L474" class="LineNr">474 </span> <span class="subxS2Comment"># . . call</span> +<span id="L475" class="LineNr">475 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L476" class="LineNr">476 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L477" class="LineNr">477 </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="L478" class="LineNr">478 </span> <span class="subxComment"># write(f, s)</span> +<span id="L479" class="LineNr">479 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L480" class="LineNr">480 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L481" class="LineNr">481 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L482" class="LineNr">482 </span> <span class="subxS2Comment"># . . call</span> +<span id="L483" class="LineNr">483 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L484" class="LineNr">484 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L485" class="LineNr">485 </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="L486" class="LineNr">486 </span> <span class="subxComment"># write(f, " expected\n")</span> +<span id="L487" class="LineNr">487 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L488" class="LineNr">488 </span> 68/push <span class="Constant">" expected\n"</span>/imm32 +<span id="L489" class="LineNr">489 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L490" class="LineNr">490 </span> <span class="subxS2Comment"># . . call</span> +<span id="L491" class="LineNr">491 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L492" class="LineNr">492 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L493" class="LineNr">493 </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="L494" class="LineNr">494 </span> <span class="subxComment"># stop(ed, 1)</span> +<span id="L495" class="LineNr">495 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L496" class="LineNr">496 </span> 68/push 1/imm32 +<span id="L497" class="LineNr">497 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L498" class="LineNr">498 </span> <span class="subxS2Comment"># . . call</span> +<span id="L499" class="LineNr">499 </span> e8/call <a href='../110stop.subx.html#L92'>stop</a>/disp32 +<span id="L500" class="LineNr">500 </span> <span class="subxComment"># should never get past this point</span> +<span id="L501" class="LineNr">501 </span><span class="Constant">$expected:dead-end</span>: +<span id="L502" class="LineNr">502 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L503" class="LineNr">503 </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="L504" class="LineNr">504 </span> 5d/pop-to-ebp +<span id="L505" class="LineNr">505 </span> c3/return +<span id="L506" class="LineNr">506 </span> +<span id="L507" class="LineNr">507 </span><span class="subxComment"># read a byte from 'f', and save it in 'Look'</span> +<span id="L508" class="LineNr">508 </span><span class="subxFunction">get-char</span>: <span class="subxComment"># f: (addr buffered-file)</span> +<span id="L509" class="LineNr">509 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L510" class="LineNr">510 </span> 55/push-ebp +<span id="L511" class="LineNr">511 </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="L512" class="LineNr">512 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L513" class="LineNr">513 </span> 50/push-eax +<span id="L514" class="LineNr">514 </span> <span class="subxComment"># eax = read-byte-buffered(f)</span> +<span id="L515" class="LineNr">515 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L516" class="LineNr">516 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L517" class="LineNr">517 </span> <span class="subxS2Comment"># . . call</span> +<span id="L518" class="LineNr">518 </span> e8/call <a href='../112read-byte.subx.html#L38'>read-byte-buffered</a>/disp32 +<span id="L519" class="LineNr">519 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L520" class="LineNr">520 </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="L521" class="LineNr">521 </span> <span class="subxComment"># save eax to Look</span> +<span id="L522" class="LineNr">522 </span> 89/copy 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="SpecialChar"><a href='crenshaw2-1.subx.html#L553'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># copy eax to *Look</span> +<span id="L523" class="LineNr">523 </span><span class="Constant">$get-char:end</span>: +<span id="L524" class="LineNr">524 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L525" class="LineNr">525 </span> 58/pop-to-eax +<span id="L526" class="LineNr">526 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L527" class="LineNr">527 </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="L528" class="LineNr">528 </span> 5d/pop-to-ebp +<span id="L529" class="LineNr">529 </span> c3/return +<span id="L530" class="LineNr">530 </span> +<span id="L531" class="LineNr">531 </span><span class="subxFunction">digit?</span>: <span class="subxComment"># c: int -> eax: boolean</span> +<span id="L532" class="LineNr">532 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L533" class="LineNr">533 </span> 55/push-ebp +<span id="L534" class="LineNr">534 </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="L535" class="LineNr">535 </span> <span class="subxComment"># eax = false</span> +<span id="L536" class="LineNr">536 </span> b8/copy-to-eax 0/imm32 +<span id="L537" class="LineNr">537 </span> <span class="subxComment"># if (c < '0') return false</span> +<span id="L538" class="LineNr">538 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 0x30/imm32 <span class="subxComment"># compare *(ebp+8)</span> +<span id="L539" class="LineNr">539 </span> 7c/jump-if-< $digit?:end/disp8 +<span id="L540" class="LineNr">540 </span> <span class="subxComment"># if (c > '9') return false</span> +<span id="L541" class="LineNr">541 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 0x39/imm32 <span class="subxComment"># compare *(ebp+8)</span> +<span id="L542" class="LineNr">542 </span> 7f/jump-if-> $digit?:end/disp8 +<span id="L543" class="LineNr">543 </span> <span class="subxComment"># otherwise return true</span> +<span id="L544" class="LineNr">544 </span> b8/copy-to-eax 1/imm32 +<span id="L545" class="LineNr">545 </span><span class="Constant">$digit?:end</span>: +<span id="L546" class="LineNr">546 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L547" class="LineNr">547 </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="L548" class="LineNr">548 </span> 5d/pop-to-ebp +<span id="L549" class="LineNr">549 </span> c3/return +<span id="L550" class="LineNr">550 </span> +<span id="L551" class="LineNr">551 </span>== data +<span id="L552" class="LineNr">552 </span> +<span id="L553" class="LineNr">553 </span><span class="SpecialChar">Look</span>: <span class="subxComment"># (char with some extra padding)</span> +<span id="L554" class="LineNr">554 </span> 0/imm32 +<span id="L555" class="LineNr">555 </span> +<span id="L556" class="LineNr">556 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/crenshaw2-1b.subx.html b/html/linux/apps/crenshaw2-1b.subx.html new file mode 100644 index 00000000..8a50be2c --- /dev/null +++ b/html/linux/apps/crenshaw2-1b.subx.html @@ -0,0 +1,815 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/crenshaw2-1b.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +.subxTest { color: #5f8700; } +.subxH1Comment { color: #005faf; text-decoration: underline; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/crenshaw2-1b.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/crenshaw2-1b.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Port of <a href="https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas">https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment"># which corresponds to the section "single digits" in <a href="https://compilers.iecc.com/crenshaw/tutor2.txt">https://compilers.iecc.com/crenshaw/tutor2.txt</a></span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># except that we support hex numbers of multiple digits.</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment">#</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># To run:</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># $ bootstrap/bootstrap translate [01]*.subx apps/crenshaw2-1b.subx -o crenshaw2-1b</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo '1a' |bootstrap/bootstrap run crenshaw2-1b</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># Expected output:</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># # syscall(exit, 1a)</span> +<span id="L10" class="LineNr"> 10 </span><span class="subxComment"># bb/copy-to-ebx 3/imm32</span> +<span id="L11" class="LineNr"> 11 </span><span class="subxComment"># b8/copy-to-eax 1/imm32/exit</span> +<span id="L12" class="LineNr"> 12 </span><span class="subxComment"># cd/syscall 0x80/imm8</span> +<span id="L13" class="LineNr"> 13 </span><span class="subxComment">#</span> +<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># To run the generated output:</span> +<span id="L15" class="LineNr"> 15 </span><span class="subxComment"># $ echo '1a' |bootstrap/bootstrap run crenshaw2-1b > z1.subx</span> +<span id="L16" class="LineNr"> 16 </span><span class="subxComment"># $ bootstrap/bootstrap translate z1.subx -o z1</span> +<span id="L17" class="LineNr"> 17 </span><span class="subxComment"># $ bootstrap/bootstrap run z1</span> +<span id="L18" class="LineNr"> 18 </span><span class="subxComment"># $ echo $?</span> +<span id="L19" class="LineNr"> 19 </span><span class="subxComment"># 26 # 0x1a in decimal</span> +<span id="L20" class="LineNr"> 20 </span><span class="subxComment">#</span> +<span id="L21" class="LineNr"> 21 </span><span class="subxComment"># Stdin must contain just a single hex digit. Other input will print an error:</span> +<span id="L22" class="LineNr"> 22 </span><span class="subxComment"># $ echo 'xyz' |bootstrap/bootstrap run crenshaw2-1b</span> +<span id="L23" class="LineNr"> 23 </span><span class="subxComment"># Error: integer expected</span> +<span id="L24" class="LineNr"> 24 </span><span class="subxComment">#</span> +<span id="L25" class="LineNr"> 25 </span><span class="subxComment"># Names in this file sometimes follow Crenshaw's original rather than my usual</span> +<span id="L26" class="LineNr"> 26 </span><span class="subxComment"># naming conventions.</span> +<span id="L27" class="LineNr"> 27 </span> +<span id="L28" class="LineNr"> 28 </span>== code +<span id="L29" class="LineNr"> 29 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L30" class="LineNr"> 30 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L31" class="LineNr"> 31 </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="L32" class="LineNr"> 32 </span> +<span id="L33" class="LineNr"> 33 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, call 'compile' if not</span> +<span id="L34" class="LineNr"> 34 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L35" class="LineNr"> 35 </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="L36" class="LineNr"> 36 </span> +<span id="L37" class="LineNr"> 37 </span> <span class="subxComment"># initialize heap</span> +<span id="L38" class="LineNr"> 38 </span> <span class="subxS1Comment"># . Heap = new-segment(Heap-size)</span> +<span id="L39" class="LineNr"> 39 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L40" class="LineNr"> 40 </span> 68/push <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>/imm32 +<span id="L41" class="LineNr"> 41 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span>/disp32 <span class="subxComment"># push *Heap-size</span> +<span id="L42" class="LineNr"> 42 </span> <span class="subxS2Comment"># . . call</span> +<span id="L43" class="LineNr"> 43 </span> e8/call <a href='../104new-segment.subx.html#L40'>new-segment</a>/disp32 +<span id="L44" class="LineNr"> 44 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L45" class="LineNr"> 45 </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="L46" class="LineNr"> 46 </span> +<span id="L47" class="LineNr"> 47 </span> <span class="subxH1Comment"># - if argc > 1 and argv[1] == "test", then return run_tests()</span> +<span id="L48" class="LineNr"> 48 </span> <span class="subxComment"># if (argc <= 1) goto run-main</span> +<span id="L49" class="LineNr"> 49 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/disp8 1/imm32 <span class="subxComment"># compare *ebp</span> +<span id="L50" class="LineNr"> 50 </span> 7e/jump-if-<= $run-main/disp8 +<span id="L51" class="LineNr"> 51 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) goto run-main</span> +<span id="L52" class="LineNr"> 52 </span> <span class="subxS1Comment"># . eax = kernel-string-equal?(argv[1], "test")</span> +<span id="L53" class="LineNr"> 53 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L54" class="LineNr"> 54 </span> 68/push <span class="Constant">"test"</span>/imm32 +<span id="L55" class="LineNr"> 55 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L56" class="LineNr"> 56 </span> <span class="subxS2Comment"># . . call</span> +<span id="L57" class="LineNr"> 57 </span> e8/call <a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</a>/disp32 +<span id="L58" class="LineNr"> 58 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L59" class="LineNr"> 59 </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="L60" class="LineNr"> 60 </span> <span class="subxS1Comment"># . if (eax == false) goto run-main</span> +<span id="L61" class="LineNr"> 61 </span> 3d/compare-eax-and 0/imm32/false +<span id="L62" class="LineNr"> 62 </span> 74/jump-if-= $run-main/disp8 +<span id="L63" class="LineNr"> 63 </span> <span class="subxComment"># run-tests()</span> +<span id="L64" class="LineNr"> 64 </span> e8/call run-tests/disp32 +<span id="L65" class="LineNr"> 65 </span> <span class="subxComment"># syscall(exit, *Num-test-failures)</span> +<span id="L66" class="LineNr"> 66 </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='../102test.subx.html#L89'>Num-test-failures</a></span>/disp32 <span class="subxComment"># copy *Num-test-failures to ebx</span> +<span id="L67" class="LineNr"> 67 </span> eb/jump $main:end/disp8 +<span id="L68" class="LineNr"> 68 </span><span class="Constant">$run-main</span>: +<span id="L69" class="LineNr"> 69 </span> <span class="subxH1Comment"># - otherwise read a program from stdin and emit its translation to stdout</span> +<span id="L70" class="LineNr"> 70 </span> <span class="subxS1Comment"># . compile(Stdin, 1/stdout, 2/stderr, 0)</span> +<span id="L71" class="LineNr"> 71 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L72" class="LineNr"> 72 </span> 68/push 0/imm32/exit-descriptor +<span id="L73" class="LineNr"> 73 </span> 68/push 2/imm32/stderr +<span id="L74" class="LineNr"> 74 </span> 68/push 1/imm32/stdout +<span id="L75" class="LineNr"> 75 </span> 68/push <span class="SpecialChar"><a href='../112read-byte.subx.html#L14'>Stdin</a></span>/imm32 +<span id="L76" class="LineNr"> 76 </span> <span class="subxS2Comment"># . . call</span> +<span id="L77" class="LineNr"> 77 </span> e8/call <a href='crenshaw2-1b.subx.html#L86'>compile</a>/disp32 +<span id="L78" class="LineNr"> 78 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L79" class="LineNr"> 79 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L80" class="LineNr"> 80 </span> <span class="subxComment"># syscall(exit, 0)</span> +<span id="L81" class="LineNr"> 81 </span> bb/copy-to-ebx 0/imm32 +<span id="L82" class="LineNr"> 82 </span><span class="Constant">$main:end</span>: +<span id="L83" class="LineNr"> 83 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L84" class="LineNr"> 84 </span> +<span id="L85" class="LineNr"> 85 </span><span class="subxComment"># the main entry point</span> +<span id="L86" class="LineNr"> 86 </span><span class="subxFunction">compile</span>: <span class="subxComment"># in: (addr buffered-file), out: fd or (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)</span> +<span id="L87" class="LineNr"> 87 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L88" class="LineNr"> 88 </span> 55/push-ebp +<span id="L89" class="LineNr"> 89 </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="L90" class="LineNr"> 90 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L91" class="LineNr"> 91 </span> 50/push-eax +<span id="L92" class="LineNr"> 92 </span> 51/push-ecx +<span id="L93" class="LineNr"> 93 </span> <span class="subxComment"># prime the pump</span> +<span id="L94" class="LineNr"> 94 </span> <span class="subxS1Comment"># . Look = get-char(in)</span> +<span id="L95" class="LineNr"> 95 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L96" class="LineNr"> 96 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L97" class="LineNr"> 97 </span> <span class="subxS2Comment"># . . call</span> +<span id="L98" class="LineNr"> 98 </span> e8/call <a href='crenshaw2-1b.subx.html#L702'>get-char</a>/disp32 +<span id="L99" class="LineNr"> 99 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L100" class="LineNr">100 </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="L101" class="LineNr">101 </span> <span class="subxComment"># var num/ecx: (stream byte 7)</span> +<span id="L102" class="LineNr">102 </span> <span class="subxComment"># Numbers can be 32 bits or 8 hex bytes long. One of them will be in 'Look', so we need space for 7 bytes.</span> +<span id="L103" class="LineNr">103 </span> <span class="subxComment"># Sizing the stream just right buys us overflow-handling for free inside 'get-num'.</span> +<span id="L104" class="LineNr">104 </span> <span class="subxComment"># Add 12 bytes for 'read', 'write' and 'size' fields, for a total of 19 bytes, or 0x13 in hex.</span> +<span id="L105" class="LineNr">105 </span> <span class="subxComment"># The stack pointer is no longer aligned, so dump_stack() can be misleading past this point.</span> +<span id="L106" class="LineNr">106 </span> 81 5/subop/subtract 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> 0x13/imm32 <span class="subxComment"># subtract from esp</span> +<span id="L107" class="LineNr">107 </span> 89/copy 3/mod/direct 1/rm32/ecx <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 ecx</span> +<span id="L108" class="LineNr">108 </span> <span class="subxComment"># initialize the stream</span> +<span id="L109" class="LineNr">109 </span> <span class="subxS1Comment"># . num->size = 7</span> +<span id="L110" class="LineNr">110 </span> c7 0/subop/copy 1/mod/*+disp8 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 7/imm32 <span class="subxComment"># copy to *(ecx+8)</span> +<span id="L111" class="LineNr">111 </span> <span class="subxS1Comment"># . clear-stream(num)</span> +<span id="L112" class="LineNr">112 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L113" class="LineNr">113 </span> 51/push-ecx +<span id="L114" class="LineNr">114 </span> <span class="subxS2Comment"># . . call</span> +<span id="L115" class="LineNr">115 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L116" class="LineNr">116 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L117" class="LineNr">117 </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="L118" class="LineNr">118 </span> <span class="subxComment"># read a digit from 'in' into 'num'</span> +<span id="L119" class="LineNr">119 </span> <span class="subxS1Comment"># . get-num(in, num, err, ed)</span> +<span id="L120" class="LineNr">120 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L121" class="LineNr">121 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L122" class="LineNr">122 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L123" class="LineNr">123 </span> 51/push-ecx/num +<span id="L124" class="LineNr">124 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L125" class="LineNr">125 </span> <span class="subxS2Comment"># . . call</span> +<span id="L126" class="LineNr">126 </span> e8/call <a href='crenshaw2-1b.subx.html#L187'>get-num</a>/disp32 +<span id="L127" class="LineNr">127 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L128" class="LineNr">128 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L129" class="LineNr">129 </span> <span class="subxComment"># render 'num' into the following template on 'out':</span> +<span id="L130" class="LineNr">130 </span> <span class="subxComment"># bb/copy-to-ebx _num_</span> +<span id="L131" class="LineNr">131 </span> <span class="subxComment"># b8/copy-to-eax 1/imm32/exit</span> +<span id="L132" class="LineNr">132 </span> <span class="subxComment"># cd/syscall 0x80/imm8</span> +<span id="L133" class="LineNr">133 </span> <span class="subxComment">#</span> +<span id="L134" class="LineNr">134 </span> <span class="subxS1Comment"># . write(out, "bb/copy-to-ebx ")</span> +<span id="L135" class="LineNr">135 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L136" class="LineNr">136 </span> 68/push <span class="Constant">"bb/copy-to-ebx "</span>/imm32 +<span id="L137" class="LineNr">137 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L138" class="LineNr">138 </span> <span class="subxS2Comment"># . . call</span> +<span id="L139" class="LineNr">139 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L140" class="LineNr">140 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L141" class="LineNr">141 </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="L142" class="LineNr">142 </span> <span class="subxS1Comment"># . write-stream(out, num)</span> +<span id="L143" class="LineNr">143 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L144" class="LineNr">144 </span> 51/push-ecx/num +<span id="L145" class="LineNr">145 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L146" class="LineNr">146 </span> <span class="subxS2Comment"># . . call</span> +<span id="L147" class="LineNr">147 </span> e8/call <a href='../113write-stream.subx.html#L17'>write-stream</a>/disp32 +<span id="L148" class="LineNr">148 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L149" class="LineNr">149 </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="L150" class="LineNr">150 </span> <span class="subxS1Comment"># . write(out, Newline)</span> +<span id="L151" class="LineNr">151 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L152" class="LineNr">152 </span> 68/push <span class="SpecialChar"><a href='../102test.subx.html#L82'>Newline</a></span>/imm32 +<span id="L153" class="LineNr">153 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+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='../108write.subx.html#L24'>write</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> 8/imm32 <span class="subxComment"># add to esp</span> +<span id="L158" class="LineNr">158 </span> <span class="subxS1Comment"># . write(out, "b8/copy-to-eax 1/imm32/exit\n")</span> +<span id="L159" class="LineNr">159 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L160" class="LineNr">160 </span> 68/push <span class="Constant">"b8/copy-to-eax 1/imm32/exit\n"</span>/imm32 +<span id="L161" class="LineNr">161 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L162" class="LineNr">162 </span> <span class="subxS2Comment"># . . call</span> +<span id="L163" class="LineNr">163 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L164" class="LineNr">164 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L165" class="LineNr">165 </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="L166" class="LineNr">166 </span> <span class="subxS1Comment"># . write(out, "cd/syscall 0x80/imm8\n")</span> +<span id="L167" class="LineNr">167 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L168" class="LineNr">168 </span> 68/push <span class="Constant">"cd/syscall 0x80/imm8\n"</span>/imm32 +<span id="L169" class="LineNr">169 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L170" class="LineNr">170 </span> <span class="subxS2Comment"># . . call</span> +<span id="L171" class="LineNr">171 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L172" class="LineNr">172 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L173" class="LineNr">173 </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="L174" class="LineNr">174 </span><span class="Constant">$compile:end</span>: +<span id="L175" class="LineNr">175 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L176" class="LineNr">176 </span> 59/pop-to-ecx +<span id="L177" class="LineNr">177 </span> 58/pop-to-eax +<span id="L178" class="LineNr">178 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L179" class="LineNr">179 </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="L180" class="LineNr">180 </span> 5d/pop-to-ebp +<span id="L181" class="LineNr">181 </span> c3/return +<span id="L182" class="LineNr">182 </span> +<span id="L183" class="LineNr">183 </span><span class="subxComment"># Read a sequence of digits into 'out'. Abort if there are none, or if there is</span> +<span id="L184" class="LineNr">184 </span><span class="subxComment"># no space in 'out'.</span> +<span id="L185" class="LineNr">185 </span><span class="subxComment"># Input comes from the global variable 'Look' (first byte) and the argument</span> +<span id="L186" class="LineNr">186 </span><span class="subxComment"># 'in' (rest). We leave the next byte from 'in' into 'Look' on exit.</span> +<span id="L187" class="LineNr">187 </span><span class="subxFunction">get-num</span>: <span class="subxComment"># in: (addr buffered-file), out: (addr stream byte), err: fd or (addr stream byte), ed: (addr exit-descriptor)</span> +<span id="L188" class="LineNr">188 </span> <span class="subxComment"># pseudocode:</span> +<span id="L189" class="LineNr">189 </span> <span class="subxComment"># if (!digit?(Look)) expected(ed, err, "integer")</span> +<span id="L190" class="LineNr">190 </span> <span class="subxComment"># do</span> +<span id="L191" class="LineNr">191 </span> <span class="subxComment"># if out->write >= out->size</span> +<span id="L192" class="LineNr">192 </span> <span class="subxComment"># write(err, "Error: too many digits in number\n")</span> +<span id="L193" class="LineNr">193 </span> <span class="subxComment"># stop(ed, 1)</span> +<span id="L194" class="LineNr">194 </span> <span class="subxComment"># out->data[out->write] = LSB(Look)</span> +<span id="L195" class="LineNr">195 </span> <span class="subxComment"># ++out->write</span> +<span id="L196" class="LineNr">196 </span> <span class="subxComment"># Look = get-char(in)</span> +<span id="L197" class="LineNr">197 </span> <span class="subxComment"># while digit?(Look)</span> +<span id="L198" class="LineNr">198 </span> <span class="subxComment"># This is complicated because I don't want to hard-code the error strategy in</span> +<span id="L199" class="LineNr">199 </span> <span class="subxComment"># a general helper like write-byte-buffered. Maybe I should just create a</span> +<span id="L200" class="LineNr">200 </span> <span class="subxComment"># local helper.</span> +<span id="L201" class="LineNr">201 </span> <span class="subxComment">#</span> +<span id="L202" class="LineNr">202 </span> <span class="subxComment"># within the loop we'll try to keep things in registers:</span> +<span id="L203" class="LineNr">203 </span> <span class="subxComment"># in: esi</span> +<span id="L204" class="LineNr">204 </span> <span class="subxComment"># out: edi</span> +<span id="L205" class="LineNr">205 </span> <span class="subxComment"># out->write: ecx (cached copy; need to keep in sync)</span> +<span id="L206" class="LineNr">206 </span> <span class="subxComment"># out->size: edx</span> +<span id="L207" class="LineNr">207 </span> <span class="subxComment"># temporaries: eax, ebx</span> +<span id="L208" class="LineNr">208 </span> <span class="subxComment"># We can't allocate Look to a register because it gets written implicitly in</span> +<span id="L209" class="LineNr">209 </span> <span class="subxComment"># get-char in each iteration of the loop. (Thereby demonstrating that it's</span> +<span id="L210" class="LineNr">210 </span> <span class="subxComment"># not the right interface for us. But we'll keep it just to follow Crenshaw.)</span> +<span id="L211" class="LineNr">211 </span> <span class="subxComment">#</span> +<span id="L212" class="LineNr">212 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L213" class="LineNr">213 </span> 55/push-ebp +<span id="L214" class="LineNr">214 </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="L215" class="LineNr">215 </span> <span class="subxH1Comment"># - if (digit?(Look)) expected(ed, err, "integer")</span> +<span id="L216" class="LineNr">216 </span> <span class="subxS1Comment"># . eax = digit?(Look)</span> +<span id="L217" class="LineNr">217 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L218" class="LineNr">218 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='crenshaw2-1b.subx.html#L747'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># push *Look</span> +<span id="L219" class="LineNr">219 </span> <span class="subxS2Comment"># . . call</span> +<span id="L220" class="LineNr">220 </span> e8/call <a href='crenshaw2-1b.subx.html#L725'>digit?</a>/disp32 +<span id="L221" class="LineNr">221 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L222" class="LineNr">222 </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="L223" class="LineNr">223 </span> <span class="subxS1Comment"># . if (eax == false)</span> +<span id="L224" class="LineNr">224 </span> 3d/compare-eax-and 0/imm32/false +<span id="L225" class="LineNr">225 </span> 75/jump-if-!= $get-num:main/disp8 +<span id="L226" class="LineNr">226 </span> <span class="subxS1Comment"># . expected(ed, err, "integer")</span> +<span id="L227" class="LineNr">227 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L228" class="LineNr">228 </span> 68/push <span class="Constant">"integer"</span>/imm32 +<span id="L229" class="LineNr">229 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L230" class="LineNr">230 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L231" class="LineNr">231 </span> <span class="subxS2Comment"># . . call</span> +<span id="L232" class="LineNr">232 </span> e8/call <a href='crenshaw2-1b.subx.html#L660'>expected</a>/disp32 <span class="subxComment"># never returns</span> +<span id="L233" class="LineNr">233 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L234" class="LineNr">234 </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="L235" class="LineNr">235 </span><span class="Constant">$get-num:main</span>: +<span id="L236" class="LineNr">236 </span> <span class="subxH1Comment"># - otherwise read a digit</span> +<span id="L237" class="LineNr">237 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L238" class="LineNr">238 </span> 50/push-eax +<span id="L239" class="LineNr">239 </span> 51/push-ecx +<span id="L240" class="LineNr">240 </span> 52/push-edx +<span id="L241" class="LineNr">241 </span> 53/push-ebx +<span id="L242" class="LineNr">242 </span> 56/push-esi +<span id="L243" class="LineNr">243 </span> 57/push-edi +<span id="L244" class="LineNr">244 </span> <span class="subxComment"># read necessary variables to registers</span> +<span id="L245" class="LineNr">245 </span> <span class="subxComment"># esi = in</span> +<span id="L246" class="LineNr">246 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 6/r32/esi 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to esi</span> +<span id="L247" class="LineNr">247 </span> <span class="subxComment"># edi = out</span> +<span id="L248" class="LineNr">248 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 7/r32/edi 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+12) to edi</span> +<span id="L249" class="LineNr">249 </span> <span class="subxComment"># ecx = out->write</span> +<span id="L250" class="LineNr">250 </span> 8b/copy 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *edi to ecx</span> +<span id="L251" class="LineNr">251 </span> <span class="subxComment"># edx = out->size</span> +<span id="L252" class="LineNr">252 </span> 8b/copy 1/mod/*+disp8 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(edi+8) to edx</span> +<span id="L253" class="LineNr">253 </span><span class="Constant">$get-num:loop</span>: +<span id="L254" class="LineNr">254 </span> <span class="subxComment"># if (out->write >= out->size) error</span> +<span id="L255" class="LineNr">255 </span> 39/compare 3/mod/direct 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare edx with ecx</span> +<span id="L256" class="LineNr">256 </span> 7d/jump-if-< $get-num:loop-stage2/disp8 +<span id="L257" class="LineNr">257 </span> <span class="subxS1Comment"># . error(ed, err, msg) # TODO: show full number</span> +<span id="L258" class="LineNr">258 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L259" class="LineNr">259 </span> 68/push <span class="Constant">"get-num: too many digits in number"</span>/imm32 +<span id="L260" class="LineNr">260 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L261" class="LineNr">261 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x14/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+20)</span> +<span id="L262" class="LineNr">262 </span> <span class="subxS2Comment"># . . call</span> +<span id="L263" class="LineNr">263 </span> e8/call <a href='../114error.subx.html#L9'>error</a>/disp32 <span class="subxComment"># never returns</span> +<span id="L264" class="LineNr">264 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L265" class="LineNr">265 </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="L266" class="LineNr">266 </span><span class="Constant">$get-num:loop-stage2</span>: +<span id="L267" class="LineNr">267 </span> <span class="subxComment"># out->data[out->write] = LSB(Look)</span> +<span id="L268" class="LineNr">268 </span> 8d/copy-address 1/mod/*+disp8 4/rm32/sib 7/base/edi 1/index/ecx <span class="Normal"> . </span> 3/r32/ebx 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy edi+ecx+12 to ebx</span> +<span id="L269" class="LineNr">269 </span> 8b/copy 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="SpecialChar"><a href='crenshaw2-1b.subx.html#L747'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># copy *Look to eax</span> +<span id="L270" class="LineNr">270 </span> 88/copy-byte 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/AL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at AL to *ebx</span> +<span id="L271" class="LineNr">271 </span> <span class="subxComment"># ++out->write</span> +<span id="L272" class="LineNr">272 </span> 41/increment-ecx +<span id="L273" class="LineNr">273 </span> <span class="subxComment"># Look = get-char(in)</span> +<span id="L274" class="LineNr">274 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L275" class="LineNr">275 </span> 56/push-esi +<span id="L276" class="LineNr">276 </span> <span class="subxS2Comment"># . . call</span> +<span id="L277" class="LineNr">277 </span> e8/call <a href='crenshaw2-1b.subx.html#L702'>get-char</a>/disp32 +<span id="L278" class="LineNr">278 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L279" class="LineNr">279 </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="L280" class="LineNr">280 </span> <span class="subxComment"># if (digit?(Look)) loop</span> +<span id="L281" class="LineNr">281 </span> <span class="subxS1Comment"># . eax = digit?(Look)</span> +<span id="L282" class="LineNr">282 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L283" class="LineNr">283 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='crenshaw2-1b.subx.html#L747'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># push *Look</span> +<span id="L284" class="LineNr">284 </span> <span class="subxS2Comment"># . . call</span> +<span id="L285" class="LineNr">285 </span> e8/call <a href='crenshaw2-1b.subx.html#L725'>digit?</a>/disp32 +<span id="L286" class="LineNr">286 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L287" class="LineNr">287 </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="L288" class="LineNr">288 </span> <span class="subxS1Comment"># . if (eax != false) loop</span> +<span id="L289" class="LineNr">289 </span> 3d/compare-eax-and 0/imm32/false +<span id="L290" class="LineNr">290 </span> 0f 85/jump-if-!= $get-num:<span class="Constant">loop</span>/disp32 +<span id="L291" class="LineNr">291 </span><span class="Constant">$get-num:loop-end</span>: +<span id="L292" class="LineNr">292 </span> <span class="subxComment"># persist necessary variables from registers</span> +<span id="L293" class="LineNr">293 </span> 89/copy 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ecx to *edi</span> +<span id="L294" class="LineNr">294 </span><span class="Constant">$get-num:end</span>: +<span id="L295" class="LineNr">295 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L296" class="LineNr">296 </span> 5f/pop-to-edi +<span id="L297" class="LineNr">297 </span> 5e/pop-to-esi +<span id="L298" class="LineNr">298 </span> 5b/pop-to-ebx +<span id="L299" class="LineNr">299 </span> 5a/pop-to-edx +<span id="L300" class="LineNr">300 </span> 59/pop-to-ecx +<span id="L301" class="LineNr">301 </span> 58/pop-to-eax +<span id="L302" class="LineNr">302 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L303" class="LineNr">303 </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="L304" class="LineNr">304 </span> 5d/pop-to-ebp +<span id="L305" class="LineNr">305 </span> c3/return +<span id="L306" class="LineNr">306 </span> +<span id="L307" class="LineNr">307 </span><span class="subxTest">test-get-num-reads-single-digit</span>: +<span id="L308" class="LineNr">308 </span> <span class="subxH1Comment"># - check that get-num returns first character if it's a digit</span> +<span id="L309" class="LineNr">309 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L310" class="LineNr">310 </span> 55/push-ebp +<span id="L311" class="LineNr">311 </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="L312" class="LineNr">312 </span> <span class="subxComment"># clear all streams</span> +<span id="L313" class="LineNr">313 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L314" class="LineNr">314 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L315" class="LineNr">315 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L316" class="LineNr">316 </span> <span class="subxS2Comment"># . . call</span> +<span id="L317" class="LineNr">317 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L318" class="LineNr">318 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L319" class="LineNr">319 </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="L320" class="LineNr">320 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L321" class="LineNr">321 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L322" class="LineNr">322 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L323" class="LineNr">323 </span> <span class="subxS2Comment"># . . call</span> +<span id="L324" class="LineNr">324 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L325" class="LineNr">325 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L326" class="LineNr">326 </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="L327" class="LineNr">327 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L328" class="LineNr">328 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L329" class="LineNr">329 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L330" class="LineNr">330 </span> <span class="subxS2Comment"># . . call</span> +<span id="L331" class="LineNr">331 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L332" class="LineNr">332 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L333" class="LineNr">333 </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="L334" class="LineNr">334 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L335" class="LineNr">335 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L336" class="LineNr">336 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L337" class="LineNr">337 </span> <span class="subxS2Comment"># . . call</span> +<span id="L338" class="LineNr">338 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L339" class="LineNr">339 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L340" class="LineNr">340 </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="L341" class="LineNr">341 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L342" class="LineNr">342 </span> <span class="subxS1Comment"># . write(_test-stream, "3")</span> +<span id="L343" class="LineNr">343 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L344" class="LineNr">344 </span> 68/push <span class="Constant">"3"</span>/imm32 +<span id="L345" class="LineNr">345 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L346" class="LineNr">346 </span> <span class="subxS2Comment"># . . call</span> +<span id="L347" class="LineNr">347 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L348" class="LineNr">348 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L349" class="LineNr">349 </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="L350" class="LineNr">350 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L351" class="LineNr">351 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L352" class="LineNr">352 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L353" class="LineNr">353 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L354" class="LineNr">354 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L355" class="LineNr">355 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L356" class="LineNr">356 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L357" class="LineNr">357 </span> 50/push-eax/ed +<span id="L358" class="LineNr">358 </span> <span class="subxS2Comment"># . . call</span> +<span id="L359" class="LineNr">359 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L360" class="LineNr">360 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L361" class="LineNr">361 </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="L362" class="LineNr">362 </span> <span class="subxComment"># prime the pump</span> +<span id="L363" class="LineNr">363 </span> <span class="subxS1Comment"># . get-char(_test-buffered-file)</span> +<span id="L364" class="LineNr">364 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L365" class="LineNr">365 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L366" class="LineNr">366 </span> <span class="subxS2Comment"># . . call</span> +<span id="L367" class="LineNr">367 </span> e8/call <a href='crenshaw2-1b.subx.html#L702'>get-char</a>/disp32 +<span id="L368" class="LineNr">368 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L369" class="LineNr">369 </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="L370" class="LineNr">370 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L371" class="LineNr">371 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L372" class="LineNr">372 </span> 50/push-eax/ed +<span id="L373" class="LineNr">373 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L374" class="LineNr">374 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L375" class="LineNr">375 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L376" class="LineNr">376 </span> <span class="subxS2Comment"># . . call</span> +<span id="L377" class="LineNr">377 </span> e8/call <a href='crenshaw2-1b.subx.html#L187'>get-num</a>/disp32 +<span id="L378" class="LineNr">378 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L379" class="LineNr">379 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L380" class="LineNr">380 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L381" class="LineNr">381 </span> <span class="subxComment"># check-ints-equal(*_test-output-stream->data, '3', msg)</span> +<span id="L382" class="LineNr">382 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L383" class="LineNr">383 </span> 68/push <span class="Constant">"F - test-get-num-reads-single-digit"</span>/imm32 +<span id="L384" class="LineNr">384 </span> 68/push 0x33/imm32 +<span id="L385" class="LineNr">385 </span> b8/copy-to-eax <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L386" class="LineNr">386 </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="L387" class="LineNr">387 </span> <span class="subxS2Comment"># . . call</span> +<span id="L388" class="LineNr">388 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L389" class="LineNr">389 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L390" class="LineNr">390 </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="L391" class="LineNr">391 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L392" class="LineNr">392 </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="L393" class="LineNr">393 </span> 5d/pop-to-ebp +<span id="L394" class="LineNr">394 </span> c3/return +<span id="L395" class="LineNr">395 </span> +<span id="L396" class="LineNr">396 </span><span class="subxTest">test-get-num-aborts-on-non-digit-in-Look</span>: +<span id="L397" class="LineNr">397 </span> <span class="subxH1Comment"># - check that get-num returns first character if it's a digit</span> +<span id="L398" class="LineNr">398 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L399" class="LineNr">399 </span> 55/push-ebp +<span id="L400" class="LineNr">400 </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="L401" class="LineNr">401 </span> <span class="subxComment"># clear all streams</span> +<span id="L402" class="LineNr">402 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L403" class="LineNr">403 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L404" class="LineNr">404 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L405" class="LineNr">405 </span> <span class="subxS2Comment"># . . call</span> +<span id="L406" class="LineNr">406 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L407" class="LineNr">407 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L408" class="LineNr">408 </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="L409" class="LineNr">409 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L410" class="LineNr">410 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L411" class="LineNr">411 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L412" class="LineNr">412 </span> <span class="subxS2Comment"># . . call</span> +<span id="L413" class="LineNr">413 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L414" class="LineNr">414 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L415" class="LineNr">415 </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="L416" class="LineNr">416 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L417" class="LineNr">417 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L418" class="LineNr">418 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L419" class="LineNr">419 </span> <span class="subxS2Comment"># . . call</span> +<span id="L420" class="LineNr">420 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L421" class="LineNr">421 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L422" class="LineNr">422 </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="L423" class="LineNr">423 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L424" class="LineNr">424 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L425" class="LineNr">425 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L426" class="LineNr">426 </span> <span class="subxS2Comment"># . . call</span> +<span id="L427" class="LineNr">427 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L428" class="LineNr">428 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L429" class="LineNr">429 </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="L430" class="LineNr">430 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L431" class="LineNr">431 </span> <span class="subxS1Comment"># . write(_test-stream, "3")</span> +<span id="L432" class="LineNr">432 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L433" class="LineNr">433 </span> 68/push <span class="Constant">"3"</span>/imm32 +<span id="L434" class="LineNr">434 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L435" class="LineNr">435 </span> <span class="subxS2Comment"># . . call</span> +<span id="L436" class="LineNr">436 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L437" class="LineNr">437 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L438" class="LineNr">438 </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="L439" class="LineNr">439 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L440" class="LineNr">440 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L441" class="LineNr">441 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L442" class="LineNr">442 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L443" class="LineNr">443 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L444" class="LineNr">444 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L445" class="LineNr">445 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L446" class="LineNr">446 </span> 50/push-eax/ed +<span id="L447" class="LineNr">447 </span> <span class="subxS2Comment"># . . call</span> +<span id="L448" class="LineNr">448 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L449" class="LineNr">449 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L450" class="LineNr">450 </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="L451" class="LineNr">451 </span> <span class="subxComment"># *don't* prime the pump</span> +<span id="L452" class="LineNr">452 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L453" class="LineNr">453 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L454" class="LineNr">454 </span> 50/push-eax/ed +<span id="L455" class="LineNr">455 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L456" class="LineNr">456 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L457" class="LineNr">457 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L458" class="LineNr">458 </span> <span class="subxS2Comment"># . . call</span> +<span id="L459" class="LineNr">459 </span> e8/call <a href='crenshaw2-1b.subx.html#L187'>get-num</a>/disp32 +<span id="L460" class="LineNr">460 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L461" class="LineNr">461 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L462" class="LineNr">462 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L463" class="LineNr">463 </span> <span class="subxComment"># check that get-num tried to call exit(1)</span> +<span id="L464" class="LineNr">464 </span> <span class="subxS1Comment"># . check-ints-equal(ed->value, 2, msg) # i.e. stop was called with value 1</span> +<span id="L465" class="LineNr">465 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L466" class="LineNr">466 </span> 68/push <span class="Constant">"F - test-get-num-aborts-on-non-digit-in-Look"</span>/imm32 +<span id="L467" class="LineNr">467 </span> 68/push 2/imm32 +<span id="L468" class="LineNr">468 </span> <span class="subxS2Comment"># . . push ed->value</span> +<span id="L469" class="LineNr">469 </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> 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(eax+4)</span> +<span id="L470" class="LineNr">470 </span> <span class="subxS2Comment"># . . call</span> +<span id="L471" class="LineNr">471 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L472" class="LineNr">472 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L473" class="LineNr">473 </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="L474" class="LineNr">474 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L475" class="LineNr">475 </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="L476" class="LineNr">476 </span> 5d/pop-to-ebp +<span id="L477" class="LineNr">477 </span> c3/return +<span id="L478" class="LineNr">478 </span> +<span id="L479" class="LineNr">479 </span><span class="subxTest">test-get-num-reads-multiple-digits</span>: +<span id="L480" class="LineNr">480 </span> <span class="subxH1Comment"># - check that get-num returns all initial digits until it encounters a non-digit</span> +<span id="L481" class="LineNr">481 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L482" class="LineNr">482 </span> 55/push-ebp +<span id="L483" class="LineNr">483 </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="L484" class="LineNr">484 </span> <span class="subxComment"># clear all streams</span> +<span id="L485" class="LineNr">485 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L486" class="LineNr">486 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L487" class="LineNr">487 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L488" class="LineNr">488 </span> <span class="subxS2Comment"># . . call</span> +<span id="L489" class="LineNr">489 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L490" class="LineNr">490 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L491" class="LineNr">491 </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="L492" class="LineNr">492 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L493" class="LineNr">493 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L494" class="LineNr">494 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L495" class="LineNr">495 </span> <span class="subxS2Comment"># . . call</span> +<span id="L496" class="LineNr">496 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L497" class="LineNr">497 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L498" class="LineNr">498 </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="L499" class="LineNr">499 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L500" class="LineNr">500 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L501" class="LineNr">501 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L502" class="LineNr">502 </span> <span class="subxS2Comment"># . . call</span> +<span id="L503" class="LineNr">503 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L504" class="LineNr">504 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L505" class="LineNr">505 </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="L506" class="LineNr">506 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L507" class="LineNr">507 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L508" class="LineNr">508 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L509" class="LineNr">509 </span> <span class="subxS2Comment"># . . call</span> +<span id="L510" class="LineNr">510 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L511" class="LineNr">511 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L512" class="LineNr">512 </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="L513" class="LineNr">513 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L514" class="LineNr">514 </span> <span class="subxS1Comment"># . write(_test-stream, "3456 x")</span> +<span id="L515" class="LineNr">515 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L516" class="LineNr">516 </span> 68/push <span class="Constant">"3456"</span>/imm32 +<span id="L517" class="LineNr">517 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L518" class="LineNr">518 </span> <span class="subxS2Comment"># . . call</span> +<span id="L519" class="LineNr">519 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L520" class="LineNr">520 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L521" class="LineNr">521 </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="L522" class="LineNr">522 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L523" class="LineNr">523 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L524" class="LineNr">524 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L525" class="LineNr">525 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L526" class="LineNr">526 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L527" class="LineNr">527 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L528" class="LineNr">528 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L529" class="LineNr">529 </span> 50/push-eax/ed +<span id="L530" class="LineNr">530 </span> <span class="subxS2Comment"># . . call</span> +<span id="L531" class="LineNr">531 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L532" class="LineNr">532 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L533" class="LineNr">533 </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="L534" class="LineNr">534 </span> <span class="subxComment"># prime the pump</span> +<span id="L535" class="LineNr">535 </span> <span class="subxS1Comment"># . get-char(_test-buffered-file)</span> +<span id="L536" class="LineNr">536 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L537" class="LineNr">537 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L538" class="LineNr">538 </span> <span class="subxS2Comment"># . . call</span> +<span id="L539" class="LineNr">539 </span> e8/call <a href='crenshaw2-1b.subx.html#L702'>get-char</a>/disp32 +<span id="L540" class="LineNr">540 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L541" class="LineNr">541 </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="L542" class="LineNr">542 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L543" class="LineNr">543 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L544" class="LineNr">544 </span> 50/push-eax/ed +<span id="L545" class="LineNr">545 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L546" class="LineNr">546 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L547" class="LineNr">547 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L548" class="LineNr">548 </span> <span class="subxS2Comment"># . . call</span> +<span id="L549" class="LineNr">549 </span> e8/call <a href='crenshaw2-1b.subx.html#L187'>get-num</a>/disp32 +<span id="L550" class="LineNr">550 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L551" class="LineNr">551 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L552" class="LineNr">552 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L553" class="LineNr">553 </span> <span class="subxComment"># check-ints-equal(*_test-output-stream->data, '3456', msg)</span> +<span id="L554" class="LineNr">554 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L555" class="LineNr">555 </span> 68/push <span class="Constant">"F - test-get-num-reads-multiple-digits"</span>/imm32 +<span id="L556" class="LineNr">556 </span> 68/push 0x36353433/imm32 +<span id="L557" class="LineNr">557 </span> b8/copy-to-eax <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L558" class="LineNr">558 </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="L559" class="LineNr">559 </span> <span class="subxS2Comment"># . . call</span> +<span id="L560" class="LineNr">560 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L561" class="LineNr">561 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L562" class="LineNr">562 </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="L563" class="LineNr">563 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L564" class="LineNr">564 </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="L565" class="LineNr">565 </span> 5d/pop-to-ebp +<span id="L566" class="LineNr">566 </span> c3/return +<span id="L567" class="LineNr">567 </span> +<span id="L568" class="LineNr">568 </span><span class="subxTest">test-get-num-reads-multiple-digits-followed-by-nondigit</span>: +<span id="L569" class="LineNr">569 </span> <span class="subxH1Comment"># - check that get-num returns all initial digits until it encounters a non-digit</span> +<span id="L570" class="LineNr">570 </span> <span class="subxComment"># This test uses exit-descriptors. Use ebp for setting up local variables.</span> +<span id="L571" class="LineNr">571 </span> 55/push-ebp +<span id="L572" class="LineNr">572 </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="L573" class="LineNr">573 </span> <span class="subxComment"># clear all streams</span> +<span id="L574" class="LineNr">574 </span> <span class="subxS1Comment"># . clear-stream(_test-stream)</span> +<span id="L575" class="LineNr">575 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L576" class="LineNr">576 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L577" class="LineNr">577 </span> <span class="subxS2Comment"># . . call</span> +<span id="L578" class="LineNr">578 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L579" class="LineNr">579 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L580" class="LineNr">580 </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="L581" class="LineNr">581 </span> <span class="subxS1Comment"># . clear-stream($_test-buffered-file->buffer)</span> +<span id="L582" class="LineNr">582 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L583" class="LineNr">583 </span> 68/push $_test-buffered-file->buffer/imm32 +<span id="L584" class="LineNr">584 </span> <span class="subxS2Comment"># . . call</span> +<span id="L585" class="LineNr">585 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L586" class="LineNr">586 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L587" class="LineNr">587 </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="L588" class="LineNr">588 </span> <span class="subxS1Comment"># . clear-stream(_test-output-stream)</span> +<span id="L589" class="LineNr">589 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L590" class="LineNr">590 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L591" class="LineNr">591 </span> <span class="subxS2Comment"># . . call</span> +<span id="L592" class="LineNr">592 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L593" class="LineNr">593 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L594" class="LineNr">594 </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="L595" class="LineNr">595 </span> <span class="subxS1Comment"># . clear-stream(_test-error-stream)</span> +<span id="L596" class="LineNr">596 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L597" class="LineNr">597 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L598" class="LineNr">598 </span> <span class="subxS2Comment"># . . call</span> +<span id="L599" class="LineNr">599 </span> e8/call <a href='../106stream.subx.html#L17'>clear-stream</a>/disp32 +<span id="L600" class="LineNr">600 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L601" class="LineNr">601 </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="L602" class="LineNr">602 </span> <span class="subxComment"># initialize 'in'</span> +<span id="L603" class="LineNr">603 </span> <span class="subxS1Comment"># . write(_test-stream, "3456 x")</span> +<span id="L604" class="LineNr">604 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L605" class="LineNr">605 </span> 68/push <span class="Constant">"3456 x"</span>/imm32 +<span id="L606" class="LineNr">606 </span> 68/push <a href='../108write.subx.html#L151'>_test-stream</a>/imm32 +<span id="L607" class="LineNr">607 </span> <span class="subxS2Comment"># . . call</span> +<span id="L608" class="LineNr">608 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L609" class="LineNr">609 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L610" class="LineNr">610 </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="L611" class="LineNr">611 </span> <span class="subxComment"># initialize exit-descriptor 'ed' for the call to 'get-num' below</span> +<span id="L612" class="LineNr">612 </span> <span class="subxS1Comment"># . var ed/eax: exit-descriptor</span> +<span id="L613" class="LineNr">613 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L614" class="LineNr">614 </span> 89/copy 3/mod/direct 0/rm32/eax <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 eax</span> +<span id="L615" class="LineNr">615 </span> <span class="subxS1Comment"># . tailor-exit-descriptor(ed, 16)</span> +<span id="L616" class="LineNr">616 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L617" class="LineNr">617 </span> 68/push 0x10/imm32/nbytes-of-args-for-get-num +<span id="L618" class="LineNr">618 </span> 50/push-eax/ed +<span id="L619" class="LineNr">619 </span> <span class="subxS2Comment"># . . call</span> +<span id="L620" class="LineNr">620 </span> e8/call <a href='../110stop.subx.html#L44'>tailor-exit-descriptor</a>/disp32 +<span id="L621" class="LineNr">621 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L622" class="LineNr">622 </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="L623" class="LineNr">623 </span> <span class="subxComment"># prime the pump</span> +<span id="L624" class="LineNr">624 </span> <span class="subxS1Comment"># . get-char(_test-buffered-file)</span> +<span id="L625" class="LineNr">625 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L626" class="LineNr">626 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L627" class="LineNr">627 </span> <span class="subxS2Comment"># . . call</span> +<span id="L628" class="LineNr">628 </span> e8/call <a href='crenshaw2-1b.subx.html#L702'>get-char</a>/disp32 +<span id="L629" class="LineNr">629 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L630" class="LineNr">630 </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="L631" class="LineNr">631 </span> <span class="subxComment"># get-num(in, out, err, ed)</span> +<span id="L632" class="LineNr">632 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L633" class="LineNr">633 </span> 50/push-eax/ed +<span id="L634" class="LineNr">634 </span> 68/push <a href='../115write-byte.subx.html#L438'>_test-error-stream</a>/imm32 +<span id="L635" class="LineNr">635 </span> 68/push <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L636" class="LineNr">636 </span> 68/push <a href='../112read-byte.subx.html#L318'>_test-buffered-file</a>/imm32 +<span id="L637" class="LineNr">637 </span> <span class="subxS2Comment"># . . call</span> +<span id="L638" class="LineNr">638 </span> e8/call <a href='crenshaw2-1b.subx.html#L187'>get-num</a>/disp32 +<span id="L639" class="LineNr">639 </span> <span class="subxComment"># registers except esp may be clobbered at this point</span> +<span id="L640" class="LineNr">640 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L641" class="LineNr">641 </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> 0x10/imm32 <span class="subxComment"># add to esp</span> +<span id="L642" class="LineNr">642 </span> <span class="subxComment"># check-ints-equal(*_test-output-stream->data, '3456', msg)</span> +<span id="L643" class="LineNr">643 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L644" class="LineNr">644 </span> 68/push <span class="Constant">"F - test-get-num-reads-multiple-digits-followed-by-nondigit"</span>/imm32 +<span id="L645" class="LineNr">645 </span> 68/push 0x36353433/imm32 +<span id="L646" class="LineNr">646 </span> b8/copy-to-eax <a href='../115write-byte.subx.html#L287'>_test-output-stream</a>/imm32 +<span id="L647" class="LineNr">647 </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="L648" class="LineNr">648 </span> <span class="subxS2Comment"># . . call</span> +<span id="L649" class="LineNr">649 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L650" class="LineNr">650 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L651" class="LineNr">651 </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="L652" class="LineNr">652 </span> <span class="subxS1Comment"># . reclaim locals</span> +<span id="L653" class="LineNr">653 </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="L654" class="LineNr">654 </span> 5d/pop-to-ebp +<span id="L655" class="LineNr">655 </span> c3/return +<span id="L656" class="LineNr">656 </span> +<span id="L657" class="LineNr">657 </span><span class="subxComment">## helpers</span> +<span id="L658" class="LineNr">658 </span> +<span id="L659" class="LineNr">659 </span><span class="subxComment"># write(f, "Error: "+s+" expected\n") then stop(ed, 1)</span> +<span id="L660" class="LineNr">660 </span><span class="subxFunction">expected</span>: <span class="subxComment"># ed: (addr exit-descriptor), f: fd or (addr stream byte), s: (addr array byte)</span> +<span id="L661" class="LineNr">661 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L662" class="LineNr">662 </span> 55/push-ebp +<span id="L663" class="LineNr">663 </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="L664" class="LineNr">664 </span> <span class="subxComment"># write(f, "Error: ")</span> +<span id="L665" class="LineNr">665 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L666" class="LineNr">666 </span> 68/push <span class="Constant">"Error: "</span>/imm32 +<span id="L667" class="LineNr">667 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L668" class="LineNr">668 </span> <span class="subxS2Comment"># . . call</span> +<span id="L669" class="LineNr">669 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L670" class="LineNr">670 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L671" class="LineNr">671 </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="L672" class="LineNr">672 </span> <span class="subxComment"># write(f, s)</span> +<span id="L673" class="LineNr">673 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L674" class="LineNr">674 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+16)</span> +<span id="L675" class="LineNr">675 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L676" class="LineNr">676 </span> <span class="subxS2Comment"># . . call</span> +<span id="L677" class="LineNr">677 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L678" class="LineNr">678 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L679" class="LineNr">679 </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="L680" class="LineNr">680 </span> <span class="subxComment"># write(f, " expected\n")</span> +<span id="L681" class="LineNr">681 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L682" class="LineNr">682 </span> 68/push <span class="Constant">" expected\n"</span>/imm32 +<span id="L683" class="LineNr">683 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L684" class="LineNr">684 </span> <span class="subxS2Comment"># . . call</span> +<span id="L685" class="LineNr">685 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L686" class="LineNr">686 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L687" class="LineNr">687 </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="L688" class="LineNr">688 </span> <span class="subxComment"># stop(ed, 1)</span> +<span id="L689" class="LineNr">689 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L690" class="LineNr">690 </span> 68/push 1/imm32 +<span id="L691" class="LineNr">691 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L692" class="LineNr">692 </span> <span class="subxS2Comment"># . . call</span> +<span id="L693" class="LineNr">693 </span> e8/call <a href='../110stop.subx.html#L92'>stop</a>/disp32 +<span id="L694" class="LineNr">694 </span> <span class="subxComment"># should never get past this point</span> +<span id="L695" class="LineNr">695 </span><span class="Constant">$expected:dead-end</span>: +<span id="L696" class="LineNr">696 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L697" class="LineNr">697 </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="L698" class="LineNr">698 </span> 5d/pop-to-ebp +<span id="L699" class="LineNr">699 </span> c3/return +<span id="L700" class="LineNr">700 </span> +<span id="L701" class="LineNr">701 </span><span class="subxComment"># read a byte from 'f', and save it in 'Look'</span> +<span id="L702" class="LineNr">702 </span><span class="subxFunction">get-char</span>: <span class="subxComment"># f: (addr buffered-file)</span> +<span id="L703" class="LineNr">703 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L704" class="LineNr">704 </span> 55/push-ebp +<span id="L705" class="LineNr">705 </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="L706" class="LineNr">706 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L707" class="LineNr">707 </span> 50/push-eax +<span id="L708" class="LineNr">708 </span> <span class="subxComment"># eax = read-byte-buffered(f)</span> +<span id="L709" class="LineNr">709 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L710" class="LineNr">710 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L711" class="LineNr">711 </span> <span class="subxS2Comment"># . . call</span> +<span id="L712" class="LineNr">712 </span> e8/call <a href='../112read-byte.subx.html#L38'>read-byte-buffered</a>/disp32 +<span id="L713" class="LineNr">713 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L714" class="LineNr">714 </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="L715" class="LineNr">715 </span> <span class="subxComment"># save eax to Look</span> +<span id="L716" class="LineNr">716 </span> 89/copy 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="SpecialChar"><a href='crenshaw2-1b.subx.html#L747'>Look</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># copy eax to *Look</span> +<span id="L717" class="LineNr">717 </span><span class="Constant">$get-char:end</span>: +<span id="L718" class="LineNr">718 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L719" class="LineNr">719 </span> 58/pop-to-eax +<span id="L720" class="LineNr">720 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L721" class="LineNr">721 </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="L722" class="LineNr">722 </span> 5d/pop-to-ebp +<span id="L723" class="LineNr">723 </span> c3/return +<span id="L724" class="LineNr">724 </span> +<span id="L725" class="LineNr">725 </span><span class="subxFunction">digit?</span>: <span class="subxComment"># c: int -> eax: boolean</span> +<span id="L726" class="LineNr">726 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L727" class="LineNr">727 </span> 55/push-ebp +<span id="L728" class="LineNr">728 </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="L729" class="LineNr">729 </span> <span class="subxComment"># eax = false</span> +<span id="L730" class="LineNr">730 </span> b8/copy-to-eax 0/imm32 +<span id="L731" class="LineNr">731 </span> <span class="subxComment"># if (c < '0') return false</span> +<span id="L732" class="LineNr">732 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 0x30/imm32 <span class="subxComment"># compare *(ebp+8)</span> +<span id="L733" class="LineNr">733 </span> 7c/jump-if-< $digit?:end/disp8 +<span id="L734" class="LineNr">734 </span> <span class="subxComment"># if (c > '9') return false</span> +<span id="L735" class="LineNr">735 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 0x39/imm32 <span class="subxComment"># compare *(ebp+8)</span> +<span id="L736" class="LineNr">736 </span> 7f/jump-if-> $digit?:end/disp8 +<span id="L737" class="LineNr">737 </span> <span class="subxComment"># otherwise return true</span> +<span id="L738" class="LineNr">738 </span> b8/copy-to-eax 1/imm32 +<span id="L739" class="LineNr">739 </span><span class="Constant">$digit?:end</span>: +<span id="L740" class="LineNr">740 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L741" class="LineNr">741 </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="L742" class="LineNr">742 </span> 5d/pop-to-ebp +<span id="L743" class="LineNr">743 </span> c3/return +<span id="L744" class="LineNr">744 </span> +<span id="L745" class="LineNr">745 </span>== data +<span id="L746" class="LineNr">746 </span> +<span id="L747" class="LineNr">747 </span><span class="SpecialChar">Look</span>: <span class="subxComment"># (char with some extra padding)</span> +<span id="L748" class="LineNr">748 </span> 0/imm32 +<span id="L749" class="LineNr">749 </span> +<span id="L750" class="LineNr">750 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex1.mu.html b/html/linux/apps/ex1.mu.html new file mode 100644 index 00000000..bf1644a4 --- /dev/null +++ b/html/linux/apps/ex1.mu.html @@ -0,0 +1,77 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex1.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex1.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/ex1.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># First example: return the answer to the Ultimate Question of Life, the</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment"># Universe, and Everything.</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># Same as <a href="https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html">https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># To run:</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./translate apps/ex1.mu</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># $ ./a.elf</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># Expected result:</span> +<span id="L10" class="LineNr">10 </span><span class="muComment"># $ echo $?</span> +<span id="L11" class="LineNr">11 </span><span class="muComment"># 42</span> +<span id="L12" class="LineNr">12 </span> +<span id="L13" class="LineNr">13 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='ex1.mu.html#L13'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L14" class="LineNr">14 </span> <span class="PreProc">return</span> <span class="Constant">0x2a</span> <span class="muComment"># Mu requires hexadecimal</span> +<span id="L15" class="LineNr">15 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex1.subx.html b/html/linux/apps/ex1.subx.html new file mode 100644 index 00000000..678f74c9 --- /dev/null +++ b/html/linux/apps/ex1.subx.html @@ -0,0 +1,79 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex1.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.SpecialChar { color: #d70000; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex1.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex1.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># First example: return the answer to the Ultimate Question of Life, the</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment"># Universe, and Everything.</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># Same as <a href="https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html">https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># To run:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex1.subx -o ex1</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># $ bootstrap/bootstrap run ex1</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># Expected result:</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># $ echo $?</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># 42</span> +<span id="L12" class="LineNr">12 </span> +<span id="L13" class="LineNr">13 </span>== code +<span id="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: +<span id="L16" class="LineNr">16 </span><span class="subxComment"># exit(42)</span> +<span id="L17" class="LineNr">17 </span>bb/copy-to-ebx 0x2a/imm32 <span class="subxComment"># 42 in hex</span> +<span id="L18" class="LineNr">18 </span>e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L19" class="LineNr">19 </span> +<span id="L20" class="LineNr">20 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex10.subx.html b/html/linux/apps/ex10.subx.html new file mode 100644 index 00000000..c359244c --- /dev/null +++ b/html/linux/apps/ex10.subx.html @@ -0,0 +1,132 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex10.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex10.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex10.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># String comparison: return 1 iff the two args passed in at the commandline are equal.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex10.subx -o ex10</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex10 abc abd</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 0 # false</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span>== code +<span id="L11" class="LineNr">11 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L12" class="LineNr">12 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L13" class="LineNr">13 </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="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># return argv-equal(argv[1], argv[2])</span> +<span id="L16" class="LineNr">16 </span><span class="subxComment"># At the start of a SubX program:</span> +<span id="L17" class="LineNr">17 </span><span class="subxComment"># argc: *esp</span> +<span id="L18" class="LineNr">18 </span><span class="subxComment"># argv[0]: *(esp+4)</span> +<span id="L19" class="LineNr">19 </span><span class="subxComment"># argv[1]: *(esp+8)</span> +<span id="L20" class="LineNr">20 </span><span class="subxComment"># ...</span> +<span id="L21" class="LineNr">21 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L22" class="LineNr">22 </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="L23" class="LineNr">23 </span> <span class="subxComment"># argv-equal(argv[1], argv[2])</span> +<span id="L24" class="LineNr">24 </span> <span class="subxS2Comment"># . . push argv[2]</span> +<span id="L25" class="LineNr">25 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L26" class="LineNr">26 </span> <span class="subxS2Comment"># . . push argv[1]</span> +<span id="L27" class="LineNr">27 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L28" class="LineNr">28 </span> <span class="subxS2Comment"># . . call</span> +<span id="L29" class="LineNr">29 </span> e8/call <a href='ex10.subx.html#L36'>argv-equal</a>/disp32 +<span id="L30" class="LineNr">30 </span> <span class="subxComment"># exit(eax)</span> +<span id="L31" class="LineNr">31 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L32" class="LineNr">32 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L33" class="LineNr">33 </span> +<span id="L34" class="LineNr">34 </span><span class="subxComment"># compare two null-terminated ascii strings</span> +<span id="L35" class="LineNr">35 </span><span class="subxComment"># reason for the name: the only place we should have null-terminated ascii strings is from commandline args</span> +<span id="L36" class="LineNr">36 </span><span class="subxFunction">argv-equal</span>: <span class="subxComment"># (s1, s2): null-terminated ascii strings -> eax: boolean</span> +<span id="L37" class="LineNr">37 </span> <span class="subxComment"># initialize s1 (ecx) and s2 (edx)</span> +<span id="L38" class="LineNr">38 </span> 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none <span class="Normal"> . </span> 1/r32/ecx 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(esp+4) to ecx</span> +<span id="L39" class="LineNr">39 </span> 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none <span class="Normal"> . </span> 2/r32/edx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(esp+8) to edx</span> +<span id="L40" class="LineNr">40 </span><span class="Constant">$argv-equal:loop</span>: +<span id="L41" class="LineNr">41 </span> <span class="subxComment"># c1/eax, c2/ebx = *s1, *s2</span> +<span id="L42" class="LineNr">42 </span> b8/copy-to-eax 0/imm32 +<span id="L43" class="LineNr">43 </span> 8a/copy-byte 0/mod/indirect 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/AL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *ecx to AL</span> +<span id="L44" class="LineNr">44 </span> bb/copy-to-ebx 0/imm32 +<span id="L45" class="LineNr">45 </span> 8a/copy-byte 0/mod/indirect 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/BL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *edx to BL</span> +<span id="L46" class="LineNr">46 </span> <span class="subxComment"># if (c1 == 0) break</span> +<span id="L47" class="LineNr">47 </span> 3d/compare-eax-and 0/imm32/null +<span id="L48" class="LineNr">48 </span> 74/jump-if-= $argv-equal:<span class="Constant">break</span>/disp8 +<span id="L49" class="LineNr">49 </span> <span class="subxComment"># if (c1 != c2) return false</span> +<span id="L50" class="LineNr">50 </span> 39/compare 3/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare eax and ebx</span> +<span id="L51" class="LineNr">51 </span> 75/jump-if-!= $argv-equal:false/disp8 +<span id="L52" class="LineNr">52 </span> <span class="subxComment"># ++s1, ++s2</span> +<span id="L53" class="LineNr">53 </span> 41/increment-ecx +<span id="L54" class="LineNr">54 </span> 42/increment-edx +<span id="L55" class="LineNr">55 </span> <span class="subxComment"># end while</span> +<span id="L56" class="LineNr">56 </span> eb/jump $argv-equal:<span class="Constant">loop</span>/disp8 +<span id="L57" class="LineNr">57 </span><span class="Constant">$argv-equal:break</span>: +<span id="L58" class="LineNr">58 </span> <span class="subxComment"># if (c2 == 0) return true</span> +<span id="L59" class="LineNr">59 </span> 81 7/subop/compare 3/mod/direct 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/imm32/null <span class="subxComment"># compare ebx</span> +<span id="L60" class="LineNr">60 </span> 75/jump-if-!= $argv-equal:false/disp8 +<span id="L61" class="LineNr">61 </span><span class="Constant">$argv-equal:success</span>: +<span id="L62" class="LineNr">62 </span> b8/copy-to-eax 1/imm32 +<span id="L63" class="LineNr">63 </span> c3/return +<span id="L64" class="LineNr">64 </span> <span class="subxComment"># return false</span> +<span id="L65" class="LineNr">65 </span><span class="Constant">$argv-equal:false</span>: +<span id="L66" class="LineNr">66 </span> b8/copy-to-eax 0/imm32 +<span id="L67" class="LineNr">67 </span> c3/return +<span id="L68" class="LineNr">68 </span> +<span id="L69" class="LineNr">69 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex11.subx.html b/html/linux/apps/ex11.subx.html new file mode 100644 index 00000000..f101f852 --- /dev/null +++ b/html/linux/apps/ex11.subx.html @@ -0,0 +1,421 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex11.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +.subxMinorFunction { color: #875f5f; } +.subxTest { color: #5f8700; } +.subxH1Comment { color: #005faf; text-decoration: underline; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex11.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex11.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Null-terminated vs size-prefixed ascii strings.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># By default we create strings with a 4-byte size prefix rather than a null suffix.</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># However we still need null-prefixed strings when interacting with the Linux</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># kernel in a few places. This layer implements a function for comparing</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># a null-terminated 'kernel string' with a size-prefixed 'SubX string'.</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment">#</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># To run:</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex11.subx -o ex11</span> +<span id="L10" class="LineNr"> 10 </span><span class="subxComment"># $ bootstrap/bootstrap run ex11 # runs a series of tests</span> +<span id="L11" class="LineNr"> 11 </span><span class="subxComment"># ...... # all tests pass</span> +<span id="L12" class="LineNr"> 12 </span><span class="subxComment">#</span> +<span id="L13" class="LineNr"> 13 </span><span class="subxComment"># (We can't yet run the tests when given a "test" commandline argument,</span> +<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># because checking for it would require the function being tested! Breakage</span> +<span id="L15" class="LineNr"> 15 </span><span class="subxComment"># would cause tests to not run, rather than to fail as we'd like.)</span> +<span id="L16" class="LineNr"> 16 </span> +<span id="L17" class="LineNr"> 17 </span>== code +<span id="L18" class="LineNr"> 18 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L19" class="LineNr"> 19 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L20" class="LineNr"> 20 </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="L21" class="LineNr"> 21 </span> +<span id="L22" class="LineNr"> 22 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run all tests</span> +<span id="L23" class="LineNr"> 23 </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="L24" class="LineNr"> 24 </span> <span class="subxComment"># exit(eax)</span> +<span id="L25" class="LineNr"> 25 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L26" class="LineNr"> 26 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L27" class="LineNr"> 27 </span> +<span id="L28" class="LineNr"> 28 </span><span class="subxComment"># compare a null-terminated ascii string with a more idiomatic size-prefixed byte array</span> +<span id="L29" class="LineNr"> 29 </span><span class="subxComment"># reason for the name: the only place we should have null-terminated ascii strings is from commandline args</span> +<span id="L30" class="LineNr"> 30 </span><span class="subxFunction">kernel-string-equal?</span>: <span class="subxComment"># s: null-terminated ascii string, benchmark: size-prefixed ascii string -> eax: boolean</span> +<span id="L31" class="LineNr"> 31 </span> <span class="subxComment"># pseudocode:</span> +<span id="L32" class="LineNr"> 32 </span> <span class="subxComment"># n = benchmark->size</span> +<span id="L33" class="LineNr"> 33 </span> <span class="subxComment"># s1 = s</span> +<span id="L34" class="LineNr"> 34 </span> <span class="subxComment"># s2 = benchmark->data</span> +<span id="L35" class="LineNr"> 35 </span> <span class="subxComment"># i = 0</span> +<span id="L36" class="LineNr"> 36 </span> <span class="subxComment"># while i < n</span> +<span id="L37" class="LineNr"> 37 </span> <span class="subxComment"># c1 = *s1</span> +<span id="L38" class="LineNr"> 38 </span> <span class="subxComment"># c2 = *s2</span> +<span id="L39" class="LineNr"> 39 </span> <span class="subxComment"># if (c1 == 0) return false</span> +<span id="L40" class="LineNr"> 40 </span> <span class="subxComment"># if (c1 != c2) return false</span> +<span id="L41" class="LineNr"> 41 </span> <span class="subxComment"># ++s1, ++s2, ++i</span> +<span id="L42" class="LineNr"> 42 </span> <span class="subxComment"># return *s1 == 0</span> +<span id="L43" class="LineNr"> 43 </span> <span class="subxComment">#</span> +<span id="L44" class="LineNr"> 44 </span> <span class="subxComment"># registers:</span> +<span id="L45" class="LineNr"> 45 </span> <span class="subxComment"># i: ecx</span> +<span id="L46" class="LineNr"> 46 </span> <span class="subxComment"># n: edx</span> +<span id="L47" class="LineNr"> 47 </span> <span class="subxComment"># s1: edi</span> +<span id="L48" class="LineNr"> 48 </span> <span class="subxComment"># s2: esi</span> +<span id="L49" class="LineNr"> 49 </span> <span class="subxComment"># c1: eax</span> +<span id="L50" class="LineNr"> 50 </span> <span class="subxComment"># c2: ebx</span> +<span id="L51" class="LineNr"> 51 </span> <span class="subxComment">#</span> +<span id="L52" class="LineNr"> 52 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L53" class="LineNr"> 53 </span> 55/push-ebp +<span id="L54" class="LineNr"> 54 </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="L55" class="LineNr"> 55 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L56" class="LineNr"> 56 </span> 51/push-ecx +<span id="L57" class="LineNr"> 57 </span> 52/push-edx +<span id="L58" class="LineNr"> 58 </span> 53/push-ebx +<span id="L59" class="LineNr"> 59 </span> 56/push-esi +<span id="L60" class="LineNr"> 60 </span> 57/push-edi +<span id="L61" class="LineNr"> 61 </span> <span class="subxComment"># s1/edi = s</span> +<span id="L62" class="LineNr"> 62 </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="L63" class="LineNr"> 63 </span> <span class="subxComment"># n/edx = benchmark->size</span> +<span id="L64" class="LineNr"> 64 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+12) to edx</span> +<span id="L65" class="LineNr"> 65 </span> 8b/copy 0/mod/indirect 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *edx to edx</span> +<span id="L66" class="LineNr"> 66 </span> <span class="subxComment"># s2/esi = benchmark->data</span> +<span id="L67" class="LineNr"> 67 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 6/r32/esi 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+12) to esi</span> +<span id="L68" class="LineNr"> 68 </span> 81 0/subop/add 3/mod/direct 6/rm32/esi <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 esi</span> +<span id="L69" class="LineNr"> 69 </span> <span class="subxComment"># i/ecx = c1/eax = c2/ebx = 0</span> +<span id="L70" class="LineNr"> 70 </span> b9/copy-to-ecx 0/imm32/exit +<span id="L71" class="LineNr"> 71 </span> b8/copy-to-eax 0/imm32 +<span id="L72" class="LineNr"> 72 </span> bb/copy-to-ebx 0/imm32 +<span id="L73" class="LineNr"> 73 </span><span class="Constant">$kernel-string-equal?:loop</span>: +<span id="L74" class="LineNr"> 74 </span> <span class="subxComment"># if (i >= n) break</span> +<span id="L75" class="LineNr"> 75 </span> 39/compare 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare ecx with edx</span> +<span id="L76" class="LineNr"> 76 </span> 7d/jump-if->= $kernel-string-equal?:<span class="Constant">break</span>/disp8 +<span id="L77" class="LineNr"> 77 </span> <span class="subxComment"># c1 = *s1</span> +<span id="L78" class="LineNr"> 78 </span> 8a/copy-byte 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/AL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *edi to AL</span> +<span id="L79" class="LineNr"> 79 </span> <span class="subxComment"># c2 = *s2</span> +<span id="L80" class="LineNr"> 80 </span> 8a/copy-byte 0/mod/indirect 6/rm32/esi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/BL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *esi to BL</span> +<span id="L81" class="LineNr"> 81 </span> <span class="subxComment"># if (c1 == 0) return false</span> +<span id="L82" class="LineNr"> 82 </span> 3d/compare-eax-and 0/imm32/null +<span id="L83" class="LineNr"> 83 </span> 74/jump-if-= $kernel-string-equal?:false/disp8 +<span id="L84" class="LineNr"> 84 </span> <span class="subxComment"># if (c1 != c2) return false</span> +<span id="L85" class="LineNr"> 85 </span> 39/compare 3/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare eax and ebx</span> +<span id="L86" class="LineNr"> 86 </span> 75/jump-if-!= $kernel-string-equal?:false/disp8 +<span id="L87" class="LineNr"> 87 </span> <span class="subxComment"># ++i</span> +<span id="L88" class="LineNr"> 88 </span> 41/increment-ecx +<span id="L89" class="LineNr"> 89 </span> <span class="subxComment"># ++s1</span> +<span id="L90" class="LineNr"> 90 </span> 47/increment-edi +<span id="L91" class="LineNr"> 91 </span> <span class="subxComment"># ++s2</span> +<span id="L92" class="LineNr"> 92 </span> 46/increment-esi +<span id="L93" class="LineNr"> 93 </span> eb/jump $kernel-string-equal?:<span class="Constant">loop</span>/disp8 +<span id="L94" class="LineNr"> 94 </span><span class="Constant">$kernel-string-equal?:break</span>: +<span id="L95" class="LineNr"> 95 </span> <span class="subxComment"># return *s1 == 0</span> +<span id="L96" class="LineNr"> 96 </span> 8a/copy-byte 0/mod/indirect 7/rm32/edi <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/AL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *edi to AL</span> +<span id="L97" class="LineNr"> 97 </span> 3d/compare-eax-and 0/imm32/null +<span id="L98" class="LineNr"> 98 </span> 75/jump-if-!= $kernel-string-equal?:false/disp8 +<span id="L99" class="LineNr"> 99 </span><span class="Constant">$kernel-string-equal?:true</span>: +<span id="L100" class="LineNr">100 </span> b8/copy-to-eax 1/imm32 +<span id="L101" class="LineNr">101 </span> eb/jump $kernel-string-equal?:end/disp8 +<span id="L102" class="LineNr">102 </span><span class="Constant">$kernel-string-equal?:false</span>: +<span id="L103" class="LineNr">103 </span> b8/copy-to-eax 0/imm32 +<span id="L104" class="LineNr">104 </span><span class="Constant">$kernel-string-equal?:end</span>: +<span id="L105" class="LineNr">105 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L106" class="LineNr">106 </span> 5f/pop-to-edi +<span id="L107" class="LineNr">107 </span> 5e/pop-to-esi +<span id="L108" class="LineNr">108 </span> 5b/pop-to-ebx +<span id="L109" class="LineNr">109 </span> 5a/pop-to-edx +<span id="L110" class="LineNr">110 </span> 59/pop-to-ecx +<span id="L111" class="LineNr">111 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L112" class="LineNr">112 </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="L113" class="LineNr">113 </span> 5d/pop-to-ebp +<span id="L114" class="LineNr">114 </span> c3/return +<span id="L115" class="LineNr">115 </span> +<span id="L116" class="LineNr">116 </span><span class="subxH1Comment"># - tests</span> +<span id="L117" class="LineNr">117 </span> +<span id="L118" class="LineNr">118 </span><span class="subxTest">test-compare-null-kernel-string-with-empty-array</span>: +<span id="L119" class="LineNr">119 </span> <span class="subxComment"># eax = kernel-string-equal?(Null-kernel-string, "")</span> +<span id="L120" class="LineNr">120 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L121" class="LineNr">121 </span> 68/push <span class="Constant">""</span>/imm32 +<span id="L122" class="LineNr">122 </span> 68/push <span class="SpecialChar">Null-kernel-string</span>/imm32 +<span id="L123" class="LineNr">123 </span> <span class="subxS2Comment"># . . call</span> +<span id="L124" class="LineNr">124 </span> e8/call kernel-string-equal?/disp32 +<span id="L125" class="LineNr">125 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L126" class="LineNr">126 </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="L127" class="LineNr">127 </span> <span class="subxComment"># check-ints-equal(eax, 1, msg)</span> +<span id="L128" class="LineNr">128 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L129" class="LineNr">129 </span> 68/push <span class="Constant">"F - test-compare-null-kernel-string-with-empty-array"</span>/imm32 +<span id="L130" class="LineNr">130 </span> 68/push 1/imm32/true +<span id="L131" class="LineNr">131 </span> 50/push-eax +<span id="L132" class="LineNr">132 </span> <span class="subxS2Comment"># . . call</span> +<span id="L133" class="LineNr">133 </span> e8/call check-ints-equal/disp32 +<span id="L134" class="LineNr">134 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L135" class="LineNr">135 </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="L136" class="LineNr">136 </span> c3/return +<span id="L137" class="LineNr">137 </span> +<span id="L138" class="LineNr">138 </span><span class="subxTest">test-compare-null-kernel-string-with-non-empty-array</span>: +<span id="L139" class="LineNr">139 </span> <span class="subxComment"># eax = kernel-string-equal?(Null-kernel-string, "Abc")</span> +<span id="L140" class="LineNr">140 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L141" class="LineNr">141 </span> 68/push <span class="Constant">"Abc"</span>/imm32 +<span id="L142" class="LineNr">142 </span> 68/push <span class="SpecialChar">Null-kernel-string</span>/imm32 +<span id="L143" class="LineNr">143 </span> <span class="subxS2Comment"># . . call</span> +<span id="L144" class="LineNr">144 </span> e8/call kernel-string-equal?/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> 8/imm32 <span class="subxComment"># add to esp</span> +<span id="L147" class="LineNr">147 </span> <span class="subxComment"># check-ints-equal(eax, 0, 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">"F - test-compare-null-kernel-string-with-non-empty-array"</span>/imm32 +<span id="L150" class="LineNr">150 </span> 68/push 0/imm32/false +<span id="L151" class="LineNr">151 </span> 50/push-eax +<span id="L152" class="LineNr">152 </span> <span class="subxS2Comment"># . . call</span> +<span id="L153" class="LineNr">153 </span> e8/call check-ints-equal/disp32 +<span id="L154" class="LineNr">154 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L155" class="LineNr">155 </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="L156" class="LineNr">156 </span> c3/return +<span id="L157" class="LineNr">157 </span> +<span id="L158" class="LineNr">158 </span><span class="subxTest">test-compare-kernel-string-with-equal-array</span>: +<span id="L159" class="LineNr">159 </span> <span class="subxComment"># eax = kernel-string-equal?(_test-Abc-kernel-string, "Abc")</span> +<span id="L160" class="LineNr">160 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L161" class="LineNr">161 </span> 68/push <span class="Constant">"Abc"</span>/imm32 +<span id="L162" class="LineNr">162 </span> 68/push _test-Abc-kernel-string/imm32 +<span id="L163" class="LineNr">163 </span> <span class="subxS2Comment"># . . call</span> +<span id="L164" class="LineNr">164 </span> e8/call kernel-string-equal?/disp32 +<span id="L165" class="LineNr">165 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L166" class="LineNr">166 </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="L167" class="LineNr">167 </span> <span class="subxComment"># check-ints-equal(eax, 1, msg)</span> +<span id="L168" class="LineNr">168 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L169" class="LineNr">169 </span> 68/push <span class="Constant">"F - test-compare-kernel-string-with-equal-array"</span>/imm32 +<span id="L170" class="LineNr">170 </span> 68/push 1/imm32/true +<span id="L171" class="LineNr">171 </span> 50/push-eax +<span id="L172" class="LineNr">172 </span> <span class="subxS2Comment"># . . call</span> +<span id="L173" class="LineNr">173 </span> e8/call check-ints-equal/disp32 +<span id="L174" class="LineNr">174 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L175" class="LineNr">175 </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="L176" class="LineNr">176 </span> c3/return +<span id="L177" class="LineNr">177 </span> +<span id="L178" class="LineNr">178 </span><span class="subxTest">test-compare-kernel-string-with-inequal-array</span>: +<span id="L179" class="LineNr">179 </span> <span class="subxComment"># eax = kernel-string-equal?(_test-Abc-kernel-string, "Adc")</span> +<span id="L180" class="LineNr">180 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L181" class="LineNr">181 </span> 68/push <span class="Constant">"Adc"</span>/imm32 +<span id="L182" class="LineNr">182 </span> 68/push _test-Abc-kernel-string/imm32 +<span id="L183" class="LineNr">183 </span> <span class="subxS2Comment"># . . call</span> +<span id="L184" class="LineNr">184 </span> e8/call kernel-string-equal?/disp32 +<span id="L185" class="LineNr">185 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L186" class="LineNr">186 </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="L187" class="LineNr">187 </span> <span class="subxComment"># check-ints-equal(eax, 0, msg)</span> +<span id="L188" class="LineNr">188 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L189" class="LineNr">189 </span> 68/push <span class="Constant">"F - test-compare-kernel-string-with-equal-array"</span>/imm32 +<span id="L190" class="LineNr">190 </span> 68/push 0/imm32/false +<span id="L191" class="LineNr">191 </span> 50/push-eax +<span id="L192" class="LineNr">192 </span> <span class="subxS2Comment"># . . call</span> +<span id="L193" class="LineNr">193 </span> e8/call check-ints-equal/disp32 +<span id="L194" class="LineNr">194 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L195" class="LineNr">195 </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="L196" class="LineNr">196 </span> c3/return +<span id="L197" class="LineNr">197 </span> +<span id="L198" class="LineNr">198 </span><span class="subxTest">test-compare-kernel-string-with-empty-array</span>: +<span id="L199" class="LineNr">199 </span> <span class="subxComment"># eax = kernel-string-equal?(_test-Abc-kernel-string, "")</span> +<span id="L200" class="LineNr">200 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L201" class="LineNr">201 </span> 68/push <span class="Constant">""</span>/imm32 +<span id="L202" class="LineNr">202 </span> 68/push _test-Abc-kernel-string/imm32 +<span id="L203" class="LineNr">203 </span> <span class="subxS2Comment"># . . call</span> +<span id="L204" class="LineNr">204 </span> e8/call kernel-string-equal?/disp32 +<span id="L205" class="LineNr">205 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L206" class="LineNr">206 </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="L207" class="LineNr">207 </span> <span class="subxComment"># check-ints-equal(eax, 0, msg)</span> +<span id="L208" class="LineNr">208 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L209" class="LineNr">209 </span> 68/push <span class="Constant">"F - test-compare-kernel-string-with-equal-array"</span>/imm32 +<span id="L210" class="LineNr">210 </span> 68/push 0/imm32/false +<span id="L211" class="LineNr">211 </span> 50/push-eax +<span id="L212" class="LineNr">212 </span> <span class="subxS2Comment"># . . call</span> +<span id="L213" class="LineNr">213 </span> e8/call check-ints-equal/disp32 +<span id="L214" class="LineNr">214 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L215" class="LineNr">215 </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="L216" class="LineNr">216 </span> c3/return +<span id="L217" class="LineNr">217 </span> +<span id="L218" class="LineNr">218 </span><span class="subxTest">test-compare-kernel-string-with-shorter-array</span>: +<span id="L219" class="LineNr">219 </span> <span class="subxComment"># eax = kernel-string-equal?(_test-Abc-kernel-string, "Ab")</span> +<span id="L220" class="LineNr">220 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L221" class="LineNr">221 </span> 68/push <span class="Constant">"Ab"</span>/imm32 +<span id="L222" class="LineNr">222 </span> 68/push _test-Abc-kernel-string/imm32 +<span id="L223" class="LineNr">223 </span> <span class="subxS2Comment"># . . call</span> +<span id="L224" class="LineNr">224 </span> e8/call kernel-string-equal?/disp32 +<span id="L225" class="LineNr">225 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L226" class="LineNr">226 </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="L227" class="LineNr">227 </span> <span class="subxComment"># check-ints-equal(eax, 0, msg)</span> +<span id="L228" class="LineNr">228 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L229" class="LineNr">229 </span> 68/push <span class="Constant">"F - test-compare-kernel-string-with-shorter-array"</span>/imm32 +<span id="L230" class="LineNr">230 </span> 68/push 0/imm32/false +<span id="L231" class="LineNr">231 </span> 50/push-eax +<span id="L232" class="LineNr">232 </span> <span class="subxS2Comment"># . . call</span> +<span id="L233" class="LineNr">233 </span> e8/call check-ints-equal/disp32 +<span id="L234" class="LineNr">234 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L235" class="LineNr">235 </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="L236" class="LineNr">236 </span> c3/return +<span id="L237" class="LineNr">237 </span> +<span id="L238" class="LineNr">238 </span><span class="subxTest">test-compare-kernel-string-with-longer-array</span>: +<span id="L239" class="LineNr">239 </span> <span class="subxComment"># eax = kernel-string-equal?(_test-Abc-kernel-string, "Abcd")</span> +<span id="L240" class="LineNr">240 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L241" class="LineNr">241 </span> 68/push <span class="Constant">"Abcd"</span>/imm32 +<span id="L242" class="LineNr">242 </span> 68/push _test-Abc-kernel-string/imm32 +<span id="L243" class="LineNr">243 </span> <span class="subxS2Comment"># . . call</span> +<span id="L244" class="LineNr">244 </span> e8/call kernel-string-equal?/disp32 +<span id="L245" class="LineNr">245 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L246" class="LineNr">246 </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="L247" class="LineNr">247 </span> <span class="subxComment"># check-ints-equal(eax, 0, msg)</span> +<span id="L248" class="LineNr">248 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L249" class="LineNr">249 </span> 68/push <span class="Constant">"F - test-compare-kernel-string-with-longer-array"</span>/imm32 +<span id="L250" class="LineNr">250 </span> 68/push 0/imm32/false +<span id="L251" class="LineNr">251 </span> 50/push-eax +<span id="L252" class="LineNr">252 </span> <span class="subxS2Comment"># . . call</span> +<span id="L253" class="LineNr">253 </span> e8/call check-ints-equal/disp32 +<span id="L254" class="LineNr">254 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L255" class="LineNr">255 </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="L256" class="LineNr">256 </span> c3/return +<span id="L257" class="LineNr">257 </span> +<span id="L258" class="LineNr">258 </span><span class="subxH1Comment"># - helpers</span> +<span id="L259" class="LineNr">259 </span> +<span id="L260" class="LineNr">260 </span><span class="subxComment"># print msg to stderr if a != b, otherwise print "."</span> +<span id="L261" class="LineNr">261 </span><span class="subxFunction">check-ints-equal</span>: <span class="subxComment"># (a: int, b: int, msg: (addr array byte)) -> boolean</span> +<span id="L262" class="LineNr">262 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L263" class="LineNr">263 </span> 55/push-ebp +<span id="L264" class="LineNr">264 </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="L265" class="LineNr">265 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L266" class="LineNr">266 </span> 51/push-ecx +<span id="L267" class="LineNr">267 </span> 53/push-ebx +<span id="L268" class="LineNr">268 </span> <span class="subxComment"># load args into eax, ebx and ecx</span> +<span id="L269" class="LineNr">269 </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="L270" class="LineNr">270 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx 0xc/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+12) to ebx</span> +<span id="L271" class="LineNr">271 </span> <span class="subxComment"># if (eax == b/ebx) print('.') and return</span> +<span id="L272" class="LineNr">272 </span> 39/compare 3/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># compare eax and ebx</span> +<span id="L273" class="LineNr">273 </span> 75/jump-if-unequal $check-ints-equal:else/disp8 +<span id="L274" class="LineNr">274 </span> <span class="subxS1Comment"># . write-stderr('.')</span> +<span id="L275" class="LineNr">275 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L276" class="LineNr">276 </span> 68/push <span class="Constant">"."</span>/imm32 +<span id="L277" class="LineNr">277 </span> <span class="subxS2Comment"># . . call</span> +<span id="L278" class="LineNr">278 </span> e8/call <a href='ex11.subx.html#L310'>write-stderr</a>/disp32 +<span id="L279" class="LineNr">279 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L280" class="LineNr">280 </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="L281" class="LineNr">281 </span> <span class="subxS1Comment"># . return</span> +<span id="L282" class="LineNr">282 </span> eb/jump $check-ints-equal:end/disp8 +<span id="L283" class="LineNr">283 </span> <span class="subxComment"># otherwise print(msg)</span> +<span id="L284" class="LineNr">284 </span><span class="Constant">$check-ints-equal:else</span>: +<span id="L285" class="LineNr">285 </span> <span class="subxComment"># copy msg into ecx</span> +<span id="L286" class="LineNr">286 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx 0x10/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+16) to ecx</span> +<span id="L287" class="LineNr">287 </span> <span class="subxComment"># print(ecx)</span> +<span id="L288" class="LineNr">288 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L289" class="LineNr">289 </span> 51/push-ecx +<span id="L290" class="LineNr">290 </span> <span class="subxS2Comment"># . . call</span> +<span id="L291" class="LineNr">291 </span> e8/call <a href='ex11.subx.html#L310'>write-stderr</a>/disp32 +<span id="L292" class="LineNr">292 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L293" class="LineNr">293 </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="L294" class="LineNr">294 </span> <span class="subxComment"># print newline</span> +<span id="L295" class="LineNr">295 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L296" class="LineNr">296 </span> 68/push <span class="SpecialChar">Newline</span>/imm32 +<span id="L297" class="LineNr">297 </span> <span class="subxS2Comment"># . . call</span> +<span id="L298" class="LineNr">298 </span> e8/call <a href='ex11.subx.html#L310'>write-stderr</a>/disp32 +<span id="L299" class="LineNr">299 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L300" class="LineNr">300 </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="L301" class="LineNr">301 </span><span class="Constant">$check-ints-equal:end</span>: +<span id="L302" class="LineNr">302 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L303" class="LineNr">303 </span> 5b/pop-to-ebx +<span id="L304" class="LineNr">304 </span> 59/pop-to-ecx +<span id="L305" class="LineNr">305 </span> <span class="subxComment"># end</span> +<span id="L306" class="LineNr">306 </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="L307" class="LineNr">307 </span> 5d/pop-to-ebp +<span id="L308" class="LineNr">308 </span> c3/return +<span id="L309" class="LineNr">309 </span> +<span id="L310" class="LineNr">310 </span><span class="subxFunction">write-stderr</span>: <span class="subxComment"># s: (addr array byte) -> <void></span> +<span id="L311" class="LineNr">311 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L312" class="LineNr">312 </span> 55/push-ebp +<span id="L313" class="LineNr">313 </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="L314" class="LineNr">314 </span> <span class="subxS1Comment"># . save registers</span> +<span id="L315" class="LineNr">315 </span> 50/push-eax +<span id="L316" class="LineNr">316 </span> 51/push-ecx +<span id="L317" class="LineNr">317 </span> 52/push-edx +<span id="L318" class="LineNr">318 </span> 53/push-ebx +<span id="L319" class="LineNr">319 </span> <span class="subxComment"># syscall(write, 2/stderr, (data) s+4, (size) *s)</span> +<span id="L320" class="LineNr">320 </span> <span class="subxS2Comment"># . . fd = 2 (stderr)</span> +<span id="L321" class="LineNr">321 </span> bb/copy-to-ebx 2/imm32 +<span id="L322" class="LineNr">322 </span> <span class="subxS2Comment"># . . x = s+4</span> +<span id="L323" class="LineNr">323 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to ecx</span> +<span id="L324" class="LineNr">324 </span> 81 0/subop/add 3/mod/direct 1/rm32/ecx <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 ecx</span> +<span id="L325" class="LineNr">325 </span> <span class="subxS2Comment"># . . size = *s</span> +<span id="L326" class="LineNr">326 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to edx</span> +<span id="L327" class="LineNr">327 </span> 8b/copy 0/mod/indirect 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *edx to edx</span> +<span id="L328" class="LineNr">328 </span> <span class="subxS2Comment"># . . syscall</span> +<span id="L329" class="LineNr">329 </span> e8/call <a href='../000init.subx.html#L29'>syscall_write</a>/disp32 +<span id="L330" class="LineNr">330 </span> <span class="subxS1Comment"># . restore registers</span> +<span id="L331" class="LineNr">331 </span> 5b/pop-to-ebx +<span id="L332" class="LineNr">332 </span> 5a/pop-to-edx +<span id="L333" class="LineNr">333 </span> 59/pop-to-ecx +<span id="L334" class="LineNr">334 </span> 58/pop-to-eax +<span id="L335" class="LineNr">335 </span> <span class="subxS1Comment"># . end</span> +<span id="L336" class="LineNr">336 </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="L337" class="LineNr">337 </span> 5d/pop-to-ebp +<span id="L338" class="LineNr">338 </span> c3/return +<span id="L339" class="LineNr">339 </span> +<span id="L340" class="LineNr">340 </span>== data +<span id="L341" class="LineNr">341 </span> +<span id="L342" class="LineNr">342 </span><span class="SpecialChar">Newline</span>: +<span id="L343" class="LineNr">343 </span> <span class="subxComment"># size</span> +<span id="L344" class="LineNr">344 </span> 1/imm32 +<span id="L345" class="LineNr">345 </span> <span class="subxComment"># data</span> +<span id="L346" class="LineNr">346 </span> 0a/newline +<span id="L347" class="LineNr">347 </span> +<span id="L348" class="LineNr">348 </span><span class="subxComment"># for kernel-string-equal tests</span> +<span id="L349" class="LineNr">349 </span><span class="SpecialChar">Null-kernel-string</span>: +<span id="L350" class="LineNr">350 </span> 00/null +<span id="L351" class="LineNr">351 </span> +<span id="L352" class="LineNr">352 </span><span class="subxMinorFunction">_test-Abc-kernel-string</span>: +<span id="L353" class="LineNr">353 </span> 41/A 62/b 63/c 00/null +<span id="L354" class="LineNr">354 </span> +<span id="L355" class="LineNr">355 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex12.subx.html b/html/linux/apps/ex12.subx.html new file mode 100644 index 00000000..12b09331 --- /dev/null +++ b/html/linux/apps/ex12.subx.html @@ -0,0 +1,104 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex12.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex12.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex12.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Example showing mmap syscall.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment"># Create a new segment using mmap, save the address, write to it.</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># To run:</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex12.subx -o ex12</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># $ bootstrap/bootstrap run ex12</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># You shouldn't get a segmentation fault.</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span>== code +<span id="L10" class="LineNr">10 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L11" class="LineNr">11 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L12" class="LineNr">12 </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="L13" class="LineNr">13 </span> +<span id="L14" class="LineNr">14 </span><span class="SpecialChar">Entry</span>: +<span id="L15" class="LineNr">15 </span> <span class="subxComment"># mmap(Mmap-new-segment->len)</span> +<span id="L16" class="LineNr">16 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex12.subx.html#L29'>Mmap-new-segment</a></span>/imm32 +<span id="L17" class="LineNr">17 </span> e8/call <a href='../000init.subx.html#L71'>syscall_mmap</a>/disp32 +<span id="L18" class="LineNr">18 </span> +<span id="L19" class="LineNr">19 </span> <span class="subxComment"># write to *eax to check that we have access to the newly-allocated segment</span> +<span id="L20" class="LineNr">20 </span> c7 0/subop/copy 0/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0x34/imm32 <span class="subxComment"># copy to *eax</span> +<span id="L21" class="LineNr">21 </span> +<span id="L22" class="LineNr">22 </span> <span class="subxComment"># exit(eax)</span> +<span id="L23" class="LineNr">23 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L24" class="LineNr">24 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L25" class="LineNr">25 </span> +<span id="L26" class="LineNr">26 </span>== data +<span id="L27" class="LineNr">27 </span> +<span id="L28" class="LineNr">28 </span><span class="subxComment"># various constants used here were found in the Linux sources (search for file mman-common.h)</span> +<span id="L29" class="LineNr">29 </span><span class="SpecialChar">Mmap-new-segment</span>: <span class="subxComment"># type mmap_arg_struct</span> +<span id="L30" class="LineNr">30 </span> <span class="subxComment"># addr</span> +<span id="L31" class="LineNr">31 </span> 0/imm32 +<span id="L32" class="LineNr">32 </span> <span class="subxComment"># len</span> +<span id="L33" class="LineNr">33 </span> 0x100/imm32 +<span id="L34" class="LineNr">34 </span> <span class="subxComment"># protection flags</span> +<span id="L35" class="LineNr">35 </span> 3/imm32 <span class="subxComment"># PROT_READ | PROT_WRITE</span> +<span id="L36" class="LineNr">36 </span> <span class="subxComment"># sharing flags</span> +<span id="L37" class="LineNr">37 </span> 0x22/imm32 <span class="subxComment"># MAP_PRIVATE | MAP_ANONYMOUS</span> +<span id="L38" class="LineNr">38 </span> <span class="subxComment"># fd</span> +<span id="L39" class="LineNr">39 </span> -1/imm32 <span class="subxComment"># since MAP_ANONYMOUS is specified</span> +<span id="L40" class="LineNr">40 </span> <span class="subxComment"># offset</span> +<span id="L41" class="LineNr">41 </span> 0/imm32 <span class="subxComment"># since MAP_ANONYMOUS is specified</span> +<span id="L42" class="LineNr">42 </span> +<span id="L43" class="LineNr">43 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex13.subx.html b/html/linux/apps/ex13.subx.html new file mode 100644 index 00000000..c0181b6c --- /dev/null +++ b/html/linux/apps/ex13.subx.html @@ -0,0 +1,87 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex13.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex13.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex13.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Compare 3 and 3.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex13.subx -o ex13</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex13</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 1</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span>== code +<span id="L11" class="LineNr">11 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L12" class="LineNr">12 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L13" class="LineNr">13 </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="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: +<span id="L16" class="LineNr">16 </span> b8/copy-to-eax 3/imm32 +<span id="L17" class="LineNr">17 </span> 3d/compare-eax-and 3/imm32 +<span id="L18" class="LineNr">18 </span> 0f 94/set-if-= 3/mod/direct 3/rm32/ebx <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"># set ebx to ZF</span> +<span id="L19" class="LineNr">19 </span> 81 4/subop/and 3/mod/direct 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xff/imm32 <span class="subxComment"># AND with eax</span> +<span id="L20" class="LineNr">20 </span> +<span id="L21" class="LineNr">21 </span><span class="Constant">$exit</span>: +<span id="L22" class="LineNr">22 </span> <span class="subxComment"># exit(ebx)</span> +<span id="L23" class="LineNr">23 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L24" class="LineNr">24 </span> +<span id="L25" class="LineNr">25 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex14.subx.html b/html/linux/apps/ex14.subx.html new file mode 100644 index 00000000..fd76cdc3 --- /dev/null +++ b/html/linux/apps/ex14.subx.html @@ -0,0 +1,88 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex14.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex14.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex14.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Multiply 2 numbers.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex14.subx -o ex14</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex14</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 6</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span>== code +<span id="L11" class="LineNr">11 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L12" class="LineNr">12 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L13" class="LineNr">13 </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="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: +<span id="L16" class="LineNr">16 </span> b8/copy-to-eax 1/imm32 +<span id="L17" class="LineNr">17 </span> b9/copy-to-ecx 2/imm32 +<span id="L18" class="LineNr">18 </span> bb/copy-to-ebx 3/imm32 +<span id="L19" class="LineNr">19 </span> +<span id="L20" class="LineNr">20 </span> 69/multiply 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx 3/imm32 <span class="subxComment"># ebx = ecx * 3</span> +<span id="L21" class="LineNr">21 </span> +<span id="L22" class="LineNr">22 </span><span class="Constant">$exit</span>: +<span id="L23" class="LineNr">23 </span> <span class="subxComment"># exit(ebx)</span> +<span id="L24" class="LineNr">24 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L25" class="LineNr">25 </span> +<span id="L26" class="LineNr">26 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex2.mu.html b/html/linux/apps/ex2.mu.html new file mode 100644 index 00000000..48ee0e00 --- /dev/null +++ b/html/linux/apps/ex2.mu.html @@ -0,0 +1,83 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex2.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex2.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/ex2.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Add 3 and 4, and return the result in the exit code.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/ex2.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># 7</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='ex2.mu.html#L10'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='ex2.mu.html#L15'>do-add</a> <span class="Constant">3</span> <span class="Constant">4</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">return</span> result +<span id="L13" class="LineNr">13 </span><span class="Delimiter">}</span> +<span id="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='ex2.mu.html#L15'>do-add</a></span> a: int, b: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> result/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy a +<span id="L17" class="LineNr">17 </span> result <span class="Special"><-</span> add b +<span id="L18" class="LineNr">18 </span> <span class="PreProc">return</span> result +<span id="L19" class="LineNr">19 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex2.subx.html b/html/linux/apps/ex2.subx.html new file mode 100644 index 00000000..519b4b73 --- /dev/null +++ b/html/linux/apps/ex2.subx.html @@ -0,0 +1,79 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex2.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.SpecialChar { color: #d70000; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex2.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex2.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Add 3 and 4, and return the result in the exit code.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex2.subx -o ex2</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex2</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 2</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span>== code +<span id="L11" class="LineNr">11 </span> +<span id="L12" class="LineNr">12 </span><span class="SpecialChar">Entry</span>: +<span id="L13" class="LineNr">13 </span><span class="subxComment"># ebx = 3</span> +<span id="L14" class="LineNr">14 </span>bb/copy-to-ebx 3/imm32 +<span id="L15" class="LineNr">15 </span><span class="subxComment"># add 4 to ebx</span> +<span id="L16" class="LineNr">16 </span>81 0/subop/add 3/mod/direct 3/rm32/ebx 4/imm32 +<span id="L17" class="LineNr">17 </span><span class="subxComment"># exit(ebx)</span> +<span id="L18" class="LineNr">18 </span>e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L19" class="LineNr">19 </span> +<span id="L20" class="LineNr">20 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex3.2.mu.html b/html/linux/apps/ex3.2.mu.html new file mode 100644 index 00000000..98c6a027 --- /dev/null +++ b/html/linux/apps/ex3.2.mu.html @@ -0,0 +1,98 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex3.2.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex3.2.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/ex3.2.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Unnecessarily use an array to sum 1..10</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/ex3.2.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ echo $?</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># 55</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='ex3.2.mu.html#L9'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span> <span class="muComment"># populate a</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> a: (array int <span class="Constant">0xb</span>) <span class="muComment"># 11; we waste index 0</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> i/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L13" class="LineNr">13 </span> <span class="Delimiter">{</span> +<span id="L14" class="LineNr">14 </span> compare i, <span class="Constant">0xb</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">break-if->=</span> +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> x/eax: (addr int) <span class="Special"><-</span> index a, i +<span id="L17" class="LineNr">17 </span> copy-to *x, i +<span id="L18" class="LineNr">18 </span> i <span class="Special"><-</span> increment +<span id="L19" class="LineNr">19 </span> <span class="PreProc">loop</span> +<span id="L20" class="LineNr">20 </span> <span class="Delimiter">}</span> +<span id="L21" class="LineNr">21 </span> <span class="muComment"># sum</span> +<span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> result/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L23" class="LineNr">23 </span> i <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L24" class="LineNr">24 </span> <span class="Delimiter">{</span> +<span id="L25" class="LineNr">25 </span> compare i, <span class="Constant">0xb</span> +<span id="L26" class="LineNr">26 </span> <span class="PreProc">break-if->=</span> +<span id="L27" class="LineNr">27 </span> <span class="PreProc">var</span> x/eax: (addr int) <span class="Special"><-</span> index a, i +<span id="L28" class="LineNr">28 </span> result <span class="Special"><-</span> add *x +<span id="L29" class="LineNr">29 </span> i <span class="Special"><-</span> increment +<span id="L30" class="LineNr">30 </span> <span class="PreProc">loop</span> +<span id="L31" class="LineNr">31 </span> <span class="Delimiter">}</span> +<span id="L32" class="LineNr">32 </span> <span class="PreProc">return</span> result +<span id="L33" class="LineNr">33 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex3.mu.html b/html/linux/apps/ex3.mu.html new file mode 100644 index 00000000..9da58562 --- /dev/null +++ b/html/linux/apps/ex3.mu.html @@ -0,0 +1,84 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex3.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex3.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/ex3.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Add the first 10 numbers, and return the result in the exit code.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate ex3.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># 55</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='ex3.mu.html#L10'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> result/<span class="muRegEbx">ebx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L13" class="LineNr">13 </span> <span class="Delimiter">{</span> +<span id="L14" class="LineNr">14 </span> compare i, <span class="Constant">0xa</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">break-if-></span> +<span id="L16" class="LineNr">16 </span> result <span class="Special"><-</span> add i +<span id="L17" class="LineNr">17 </span> i <span class="Special"><-</span> increment +<span id="L18" class="LineNr">18 </span> <span class="PreProc">loop</span> +<span id="L19" class="LineNr">19 </span> <span class="Delimiter">}</span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">return</span> result +<span id="L21" class="LineNr">21 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex3.subx.html b/html/linux/apps/ex3.subx.html new file mode 100644 index 00000000..0810bdc8 --- /dev/null +++ b/html/linux/apps/ex3.subx.html @@ -0,0 +1,98 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex3.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +.SpecialChar { color: #d70000; } +.Constant { color: #008787; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex3.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex3.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Add the first 10 numbers, and return the result in the exit code.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex3.subx -o ex3</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex3</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 55</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span>== code +<span id="L11" class="LineNr">11 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L12" class="LineNr">12 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L13" class="LineNr">13 </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="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: +<span id="L16" class="LineNr">16 </span> <span class="subxComment"># result: ebx = 0</span> +<span id="L17" class="LineNr">17 </span> bb/copy-to-ebx 0/imm32 +<span id="L18" class="LineNr">18 </span> <span class="subxComment"># counter: ecx = 1</span> +<span id="L19" class="LineNr">19 </span> b9/copy-to-ecx 1/imm32 +<span id="L20" class="LineNr">20 </span> +<span id="L21" class="LineNr">21 </span><span class="Constant">$loop</span>: +<span id="L22" class="LineNr">22 </span> <span class="subxComment"># if (counter > 10) break</span> +<span id="L23" class="LineNr">23 </span> 81 7/subop/compare 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xa/imm32 <span class="subxComment"># compare ecx</span> +<span id="L24" class="LineNr">24 </span> 7f/jump-if-> $exit/disp8 +<span id="L25" class="LineNr">25 </span> <span class="subxComment"># result += counter</span> +<span id="L26" class="LineNr">26 </span> 01/add 3/mod/direct 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># add ecx to ebx</span> +<span id="L27" class="LineNr">27 </span> <span class="subxComment"># ++counter</span> +<span id="L28" class="LineNr">28 </span> 41/increment-ecx +<span id="L29" class="LineNr">29 </span> <span class="subxComment"># loop</span> +<span id="L30" class="LineNr">30 </span> eb/jump $loop/disp8 +<span id="L31" class="LineNr">31 </span> +<span id="L32" class="LineNr">32 </span><span class="Constant">$exit</span>: +<span id="L33" class="LineNr">33 </span> <span class="subxComment"># exit(ebx)</span> +<span id="L34" class="LineNr">34 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L35" class="LineNr">35 </span> +<span id="L36" class="LineNr">36 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex4.subx.html b/html/linux/apps/ex4.subx.html new file mode 100644 index 00000000..1b3bd941 --- /dev/null +++ b/html/linux/apps/ex4.subx.html @@ -0,0 +1,99 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex4.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex4.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex4.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Read a character from stdin, save it to a global, write it to stdout.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex4.subx -o ex4</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex4</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 global variable we save to</span> +<span id="L10" class="LineNr">10 </span><span class="SpecialChar">X</span>: +<span id="L11" class="LineNr">11 </span> 0/imm32 <span class="subxComment"># space for read() to write to</span> +<span id="L12" class="LineNr">12 </span> +<span id="L13" class="LineNr">13 </span>== code +<span id="L14" class="LineNr">14 </span> +<span id="L15" class="LineNr">15 </span><span class="SpecialChar">Entry</span>: +<span id="L16" class="LineNr">16 </span><span class="subxComment"># read(stdin, X, 1)</span> +<span id="L17" class="LineNr">17 </span><span class="subxS1Comment"># . fd = 0 (stdin)</span> +<span id="L18" class="LineNr">18 </span>bb/copy-to-ebx 0/imm32 +<span id="L19" class="LineNr">19 </span><span class="subxS1Comment"># . data = X (location to write result to)</span> +<span id="L20" class="LineNr">20 </span>b9/copy-to-ecx <span class="SpecialChar"><a href='ex4.subx.html#L10'>X</a></span>/imm32 +<span id="L21" class="LineNr">21 </span><span class="subxS1Comment"># . size = 1 character</span> +<span id="L22" class="LineNr">22 </span>ba/copy-to-edx 1/imm32 +<span id="L23" class="LineNr">23 </span><span class="subxS1Comment"># . syscall</span> +<span id="L24" class="LineNr">24 </span>e8/call <a href='../000init.subx.html#L23'>syscall_read</a>/disp32 +<span id="L25" class="LineNr">25 </span> +<span id="L26" class="LineNr">26 </span><span class="subxComment"># write(stdout, X, 1)</span> +<span id="L27" class="LineNr">27 </span><span class="subxS1Comment"># . fd = 1 (stdout)</span> +<span id="L28" class="LineNr">28 </span>bb/copy-to-ebx 1/imm32 +<span id="L29" class="LineNr">29 </span><span class="subxS1Comment"># . initialize X (location to read from)</span> +<span id="L30" class="LineNr">30 </span>b9/copy-to-ecx <span class="SpecialChar"><a href='ex4.subx.html#L10'>X</a></span>/imm32 +<span id="L31" class="LineNr">31 </span><span class="subxS1Comment"># . size = 1 character</span> +<span id="L32" class="LineNr">32 </span>ba/copy-to-edx 1/imm32 +<span id="L33" class="LineNr">33 </span><span class="subxS1Comment"># . syscall</span> +<span id="L34" class="LineNr">34 </span>e8/call <a href='../000init.subx.html#L29'>syscall_write</a>/disp32 +<span id="L35" class="LineNr">35 </span> +<span id="L36" class="LineNr">36 </span><span class="subxComment"># exit(ebx)</span> +<span id="L37" class="LineNr">37 </span>e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L38" class="LineNr">38 </span> +<span id="L39" class="LineNr">39 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex5.subx.html b/html/linux/apps/ex5.subx.html new file mode 100644 index 00000000..949e207c --- /dev/null +++ b/html/linux/apps/ex5.subx.html @@ -0,0 +1,101 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex5.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex5.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex5.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Read a character from stdin, save it to a local on the stack, write it to stdout.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex5.subx -o ex5</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex5</span> +<span id="L6" class="LineNr"> 6 </span> +<span id="L7" class="LineNr"> 7 </span>== code +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L10" class="LineNr">10 </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="L11" class="LineNr">11 </span> +<span id="L12" class="LineNr">12 </span><span class="SpecialChar">Entry</span>: +<span id="L13" class="LineNr">13 </span> +<span id="L14" class="LineNr">14 </span> <span class="subxComment"># allocate x on the stack</span> +<span id="L15" class="LineNr">15 </span> 81 5/subop/subtract 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"># subtract from esp</span> +<span id="L16" class="LineNr">16 </span> +<span id="L17" class="LineNr">17 </span> <span class="subxComment"># read(stdin, x, 1)</span> +<span id="L18" class="LineNr">18 </span> <span class="subxS1Comment"># . fd = 0 (stdin)</span> +<span id="L19" class="LineNr">19 </span> bb/copy-to-ebx 0/imm32 +<span id="L20" class="LineNr">20 </span> <span class="subxS1Comment"># . data = x (location to write result to)</span> +<span id="L21" class="LineNr">21 </span> 8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none 1/r32/ecx 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy esp+4 to ecx</span> +<span id="L22" class="LineNr">22 </span> <span class="subxS1Comment"># . size = 1 character</span> +<span id="L23" class="LineNr">23 </span> ba/copy-to-edx 1/imm32 +<span id="L24" class="LineNr">24 </span> <span class="subxS1Comment"># . syscall</span> +<span id="L25" class="LineNr">25 </span> e8/call <a href='../000init.subx.html#L23'>syscall_read</a>/disp32 +<span id="L26" class="LineNr">26 </span> +<span id="L27" class="LineNr">27 </span> <span class="subxComment"># syscall(write, stdout, x, 1)</span> +<span id="L28" class="LineNr">28 </span> <span class="subxS1Comment"># . fd = 1 (stdout)</span> +<span id="L29" class="LineNr">29 </span> bb/copy-to-ebx 1/imm32 +<span id="L30" class="LineNr">30 </span> <span class="subxS1Comment"># . data = x (location to read from)</span> +<span id="L31" class="LineNr">31 </span> 8d/copy-address 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none 1/r32/ecx 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy esp+4 to ecx</span> +<span id="L32" class="LineNr">32 </span> <span class="subxS1Comment"># . size = 1 character</span> +<span id="L33" class="LineNr">33 </span> ba/copy-to-edx 1/imm32 +<span id="L34" class="LineNr">34 </span> <span class="subxS1Comment"># . syscall</span> +<span id="L35" class="LineNr">35 </span> e8/call <a href='../000init.subx.html#L29'>syscall_write</a>/disp32 +<span id="L36" class="LineNr">36 </span> +<span id="L37" class="LineNr">37 </span> <span class="subxComment"># exit(ebx)</span> +<span id="L38" class="LineNr">38 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L39" class="LineNr">39 </span> +<span id="L40" class="LineNr">40 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex6.subx.html b/html/linux/apps/ex6.subx.html new file mode 100644 index 00000000..5aa4fd53 --- /dev/null +++ b/html/linux/apps/ex6.subx.html @@ -0,0 +1,96 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex6.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex6.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex6.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Print out a (global variable) string to stdout.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex6.subx -o ex6</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex6</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Hello, world!</span> +<span id="L7" class="LineNr"> 7 </span> +<span id="L8" class="LineNr"> 8 </span>== code +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L11" class="LineNr">11 </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="L12" class="LineNr">12 </span> +<span id="L13" class="LineNr">13 </span><span class="SpecialChar">Entry</span>: +<span id="L14" class="LineNr">14 </span> <span class="subxComment"># write(stdout, X, Size)</span> +<span id="L15" class="LineNr">15 </span> <span class="subxS1Comment"># . fd = 1 (stdout)</span> +<span id="L16" class="LineNr">16 </span> bb/copy-to-ebx 1/imm32 +<span id="L17" class="LineNr">17 </span> <span class="subxS1Comment"># . initialize X (location to write result to)</span> +<span id="L18" class="LineNr">18 </span> b9/copy-to-ecx <span class="SpecialChar"><a href='ex6.subx.html#L31'>X</a></span>/imm32 +<span id="L19" class="LineNr">19 </span> <span class="subxS1Comment"># . initialize Size</span> +<span id="L20" class="LineNr">20 </span> 8b/copy 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx <span class="SpecialChar"><a href='ex6.subx.html#L29'>Size</a></span>/disp32 <span class="Normal"> . </span> <span class="subxComment"># copy *Size to edx</span> +<span id="L21" class="LineNr">21 </span> <span class="subxS1Comment"># . syscall</span> +<span id="L22" class="LineNr">22 </span> e8/call <a href='../000init.subx.html#L29'>syscall_write</a>/disp32 +<span id="L23" class="LineNr">23 </span> +<span id="L24" class="LineNr">24 </span> <span class="subxComment"># exit(ebx)</span> +<span id="L25" class="LineNr">25 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L26" class="LineNr">26 </span> +<span id="L27" class="LineNr">27 </span>== data +<span id="L28" class="LineNr">28 </span> +<span id="L29" class="LineNr">29 </span><span class="SpecialChar">Size</span>: <span class="subxComment"># size of string</span> +<span id="L30" class="LineNr">30 </span> 0x0d/imm32 <span class="subxComment"># 13</span> +<span id="L31" class="LineNr">31 </span><span class="SpecialChar">X</span>: <span class="subxComment"># string to print</span> +<span id="L32" class="LineNr">32 </span> 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 0a 00 +<span id="L33" class="LineNr">33 </span><span class="subxComment"># H e l l o ␣ w o r l d ! newline null</span> +<span id="L34" class="LineNr">34 </span> +<span id="L35" class="LineNr">35 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex7.subx.html b/html/linux/apps/ex7.subx.html new file mode 100644 index 00000000..b2a87263 --- /dev/null +++ b/html/linux/apps/ex7.subx.html @@ -0,0 +1,159 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex7.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex7.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex7.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Example showing file syscalls.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># Create a file, open it for writing, write a character to it, close it, open</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># it for reading, read a character from it, close it, delete it, and return</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># the character read.</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment">#</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># To run:</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex7.subx -o ex7</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># $ bootstrap/bootstrap run ex7</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># Expected result:</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># $ echo $?</span> +<span id="L12" class="LineNr">12 </span><span class="subxComment"># 97</span> +<span id="L13" class="LineNr">13 </span> +<span id="L14" class="LineNr">14 </span>== code +<span id="L15" class="LineNr">15 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L16" class="LineNr">16 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L17" class="LineNr">17 </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="L18" class="LineNr">18 </span> +<span id="L19" class="LineNr">19 </span><span class="SpecialChar">Entry</span>: +<span id="L20" class="LineNr">20 </span> <span class="subxComment"># creat(Filename)</span> +<span id="L21" class="LineNr">21 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L94'>Filename</a></span>/imm32 +<span id="L22" class="LineNr">22 </span> b9/copy-to-ecx 0x180/imm32/fixed-perms +<span id="L23" class="LineNr">23 </span> e8/call <a href='../000init.subx.html#L53'>syscall_creat</a>/disp32 +<span id="L24" class="LineNr">24 </span> +<span id="L25" class="LineNr">25 </span> <span class="subxComment"># stream = open(Filename, O_WRONLY, 0) # we can't use 'fd' because it looks like a hex byte</span> +<span id="L26" class="LineNr">26 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L94'>Filename</a></span>/imm32 +<span id="L27" class="LineNr">27 </span> b9/copy-to-ecx 1/imm32/wronly +<span id="L28" class="LineNr">28 </span> ba/copy-to-edx 0x180/imm32/fixed-perms +<span id="L29" class="LineNr">29 </span> e8/call <a href='../000init.subx.html#L35'>syscall_open</a>/disp32 +<span id="L30" class="LineNr">30 </span> <span class="subxComment"># save stream</span> +<span id="L31" class="LineNr">31 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L32" class="LineNr">32 </span> 89/copy 0/mod/indirect 3/rm32/ebx <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"># copy eax to *ebx</span> +<span id="L33" class="LineNr">33 </span> +<span id="L34" class="LineNr">34 </span> <span class="subxComment"># write(Stream, "a", 1)</span> +<span id="L35" class="LineNr">35 </span> <span class="subxS1Comment"># . load stream</span> +<span id="L36" class="LineNr">36 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L37" class="LineNr">37 </span> 8b/copy 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ebx to ebx</span> +<span id="L38" class="LineNr">38 </span> <span class="subxS1Comment"># .</span> +<span id="L39" class="LineNr">39 </span> b9/copy-to-ecx <span class="SpecialChar"><a href='ex7.subx.html#L90'>A</a></span>/imm32 +<span id="L40" class="LineNr">40 </span> ba/copy-to-edx 1/imm32/size +<span id="L41" class="LineNr">41 </span> e8/call <a href='../000init.subx.html#L29'>syscall_write</a>/disp32 +<span id="L42" class="LineNr">42 </span> +<span id="L43" class="LineNr">43 </span> <span class="subxComment"># close(Stream)</span> +<span id="L44" class="LineNr">44 </span> <span class="subxS1Comment"># . load stream</span> +<span id="L45" class="LineNr">45 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L46" class="LineNr">46 </span> 8b/copy 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ebx to ebx</span> +<span id="L47" class="LineNr">47 </span> <span class="subxS1Comment"># .</span> +<span id="L48" class="LineNr">48 </span> e8/call <a href='../000init.subx.html#L41'>syscall_close</a>/disp32 +<span id="L49" class="LineNr">49 </span> +<span id="L50" class="LineNr">50 </span> <span class="subxComment"># stream = open(Filename, O_RDONLY, 0)</span> +<span id="L51" class="LineNr">51 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L94'>Filename</a></span>/imm32 +<span id="L52" class="LineNr">52 </span> b9/copy-to-ecx 0/imm32/rdonly +<span id="L53" class="LineNr">53 </span> ba/copy-to-edx 0x180/imm32/fixed-perms +<span id="L54" class="LineNr">54 </span> e8/call <a href='../000init.subx.html#L35'>syscall_open</a>/disp32 +<span id="L55" class="LineNr">55 </span> <span class="subxS1Comment"># . save Stream</span> +<span id="L56" class="LineNr">56 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L57" class="LineNr">57 </span> 89/copy 0/mod/indirect 3/rm32/ebx <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"># copy eax to *ebx</span> +<span id="L58" class="LineNr">58 </span> +<span id="L59" class="LineNr">59 </span> <span class="subxComment"># read(Stream, B, 1)</span> +<span id="L60" class="LineNr">60 </span> <span class="subxS1Comment"># . load stream</span> +<span id="L61" class="LineNr">61 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L62" class="LineNr">62 </span> 8b/copy 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ebx to ebx</span> +<span id="L63" class="LineNr">63 </span> <span class="subxS1Comment"># .</span> +<span id="L64" class="LineNr">64 </span> b9/copy-to-ecx <span class="SpecialChar"><a href='ex7.subx.html#L92'>B</a></span>/imm32 +<span id="L65" class="LineNr">65 </span> ba/copy-to-edx 1/imm32/size +<span id="L66" class="LineNr">66 </span> e8/call <a href='../000init.subx.html#L23'>syscall_read</a>/disp32 +<span id="L67" class="LineNr">67 </span> +<span id="L68" class="LineNr">68 </span> <span class="subxComment"># close(Stream)</span> +<span id="L69" class="LineNr">69 </span> <span class="subxS1Comment"># . load stream</span> +<span id="L70" class="LineNr">70 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L88'>Stream</a></span>/imm32 +<span id="L71" class="LineNr">71 </span> 8b/copy 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ebx to ebx</span> +<span id="L72" class="LineNr">72 </span> <span class="subxComment">#</span> +<span id="L73" class="LineNr">73 </span> e8/call <a href='../000init.subx.html#L41'>syscall_close</a>/disp32 +<span id="L74" class="LineNr">74 </span> +<span id="L75" class="LineNr">75 </span> <span class="subxComment"># unlink(filename)</span> +<span id="L76" class="LineNr">76 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L94'>Filename</a></span>/imm32 +<span id="L77" class="LineNr">77 </span> e8/call <a href='../000init.subx.html#L59'>syscall_unlink</a>/disp32 +<span id="L78" class="LineNr">78 </span> +<span id="L79" class="LineNr">79 </span> <span class="subxComment"># exit(b)</span> +<span id="L80" class="LineNr">80 </span> <span class="subxS1Comment"># . load b</span> +<span id="L81" class="LineNr">81 </span> bb/copy-to-ebx <span class="SpecialChar"><a href='ex7.subx.html#L92'>B</a></span>/imm32 +<span id="L82" class="LineNr">82 </span> 8b/copy 0/mod/indirect 3/rm32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 3/r32/ebx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ebx to ebx</span> +<span id="L83" class="LineNr">83 </span> <span class="subxComment">#</span> +<span id="L84" class="LineNr">84 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L85" class="LineNr">85 </span> +<span id="L86" class="LineNr">86 </span>== data +<span id="L87" class="LineNr">87 </span> +<span id="L88" class="LineNr">88 </span><span class="SpecialChar">Stream</span>: +<span id="L89" class="LineNr">89 </span> 0/imm32 +<span id="L90" class="LineNr">90 </span><span class="SpecialChar">A</span>: +<span id="L91" class="LineNr">91 </span> 61/imm32/A +<span id="L92" class="LineNr">92 </span><span class="SpecialChar">B</span>: +<span id="L93" class="LineNr">93 </span> 0/imm32 +<span id="L94" class="LineNr">94 </span><span class="SpecialChar">Filename</span>: +<span id="L95" class="LineNr">95 </span> 2e 66 6f 6f 00 00 00 00 +<span id="L96" class="LineNr">96 </span><span class="subxComment"># . f o o null</span> +<span id="L97" class="LineNr">97 </span> +<span id="L98" class="LineNr">98 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex8.subx.html b/html/linux/apps/ex8.subx.html new file mode 100644 index 00000000..86d16571 --- /dev/null +++ b/html/linux/apps/ex8.subx.html @@ -0,0 +1,123 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex8.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex8.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex8.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Example reading commandline arguments: compute length of first arg.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex8.subx -o ex8</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run ex8 abc de fghi</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 3 # length of 'abc'</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment">#</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># At the start of a SubX program:</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># argc: *esp</span> +<span id="L12" class="LineNr">12 </span><span class="subxComment"># argv[0]: *(esp+4)</span> +<span id="L13" class="LineNr">13 </span><span class="subxComment"># argv[1]: *(esp+8)</span> +<span id="L14" class="LineNr">14 </span><span class="subxComment"># ...</span> +<span id="L15" class="LineNr">15 </span><span class="subxComment"># Locals start from esp-4 downwards.</span> +<span id="L16" class="LineNr">16 </span> +<span id="L17" class="LineNr">17 </span>== code +<span id="L18" class="LineNr">18 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L19" class="LineNr">19 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L20" class="LineNr">20 </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="L21" class="LineNr">21 </span> +<span id="L22" class="LineNr">22 </span><span class="SpecialChar">Entry</span>: +<span id="L23" class="LineNr">23 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L24" class="LineNr">24 </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="L25" class="LineNr">25 </span> <span class="subxComment"># eax = ascii-length(argv[1])</span> +<span id="L26" class="LineNr">26 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L27" class="LineNr">27 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L28" class="LineNr">28 </span> <span class="subxS2Comment"># . . call</span> +<span id="L29" class="LineNr">29 </span> e8/call <a href='ex8.subx.html#L37'>ascii-length</a>/disp32 +<span id="L30" class="LineNr">30 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L31" class="LineNr">31 </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="L32" class="LineNr">32 </span> +<span id="L33" class="LineNr">33 </span> <span class="subxComment"># exit(eax)</span> +<span id="L34" class="LineNr">34 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L35" class="LineNr">35 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L36" class="LineNr">36 </span> +<span id="L37" class="LineNr">37 </span><span class="subxFunction">ascii-length</span>: <span class="subxComment"># s: (addr array byte) -> n/eax</span> +<span id="L38" class="LineNr">38 </span> <span class="subxComment"># edx = s</span> +<span id="L39" class="LineNr">39 </span> 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none <span class="Normal"> . </span> 2/r32/edx 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(esp+4) to edx</span> +<span id="L40" class="LineNr">40 </span> <span class="subxComment"># var result/eax = 0</span> +<span id="L41" class="LineNr">41 </span> b8/copy-to-eax 0/imm32 +<span id="L42" class="LineNr">42 </span><span class="Constant">$ascii-length:loop</span>: +<span id="L43" class="LineNr">43 </span> <span class="subxComment"># var c/ecx = *s</span> +<span id="L44" class="LineNr">44 </span> 8a/copy-byte 0/mod/* 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/CL <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy byte at *edx to CL</span> +<span id="L45" class="LineNr">45 </span> <span class="subxComment"># if (c == '\0') break</span> +<span id="L46" class="LineNr">46 </span> 81 7/subop/compare 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/imm32/null <span class="subxComment"># compare ecx</span> +<span id="L47" class="LineNr">47 </span> 74/jump-if-= $ascii-length:end/disp8 +<span id="L48" class="LineNr">48 </span> <span class="subxComment"># ++s</span> +<span id="L49" class="LineNr">49 </span> 42/increment-edx +<span id="L50" class="LineNr">50 </span> <span class="subxComment"># ++result</span> +<span id="L51" class="LineNr">51 </span> 40/increment-eax +<span id="L52" class="LineNr">52 </span> <span class="subxComment"># loop</span> +<span id="L53" class="LineNr">53 </span> eb/jump $ascii-length:<span class="Constant">loop</span>/disp8 +<span id="L54" class="LineNr">54 </span><span class="Constant">$ascii-length:end</span>: +<span id="L55" class="LineNr">55 </span> <span class="subxComment"># return eax</span> +<span id="L56" class="LineNr">56 </span> c3/return +<span id="L57" class="LineNr">57 </span> +<span id="L58" class="LineNr">58 </span>== data +<span id="L59" class="LineNr">59 </span> +<span id="L60" class="LineNr">60 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/ex9.subx.html b/html/linux/apps/ex9.subx.html new file mode 100644 index 00000000..3a79d9d9 --- /dev/null +++ b/html/linux/apps/ex9.subx.html @@ -0,0 +1,114 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/ex9.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/ex9.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/ex9.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Example showing arg order on the stack.</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># Show difference between ascii codes of first letter of first arg and first</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># letter of second arg.</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># To run:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ bootstrap/bootstrap translate apps/ex9.subx -o ex9</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># $ bootstrap/bootstrap run ex9 z x</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># Expected result:</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># $ echo $?</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># 2</span> +<span id="L12" class="LineNr">12 </span><span class="subxComment">#</span> +<span id="L13" class="LineNr">13 </span><span class="subxComment"># At the start of a SubX program:</span> +<span id="L14" class="LineNr">14 </span><span class="subxComment"># argc: *esp</span> +<span id="L15" class="LineNr">15 </span><span class="subxComment"># argv[0]: *(esp+4)</span> +<span id="L16" class="LineNr">16 </span><span class="subxComment"># argv[1]: *(esp+8)</span> +<span id="L17" class="LineNr">17 </span><span class="subxComment"># ...</span> +<span id="L18" class="LineNr">18 </span><span class="subxComment"># Locals start from esp-4 downwards.</span> +<span id="L19" class="LineNr">19 </span> +<span id="L20" class="LineNr">20 </span>== code +<span id="L21" class="LineNr">21 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L22" class="LineNr">22 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L23" class="LineNr">23 </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="L24" class="LineNr">24 </span> +<span id="L25" class="LineNr">25 </span><span class="SpecialChar">Entry</span>: +<span id="L26" class="LineNr">26 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L27" class="LineNr">27 </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="L28" class="LineNr">28 </span> <span class="subxComment"># ascii-difference(argv[1], argv[2])</span> +<span id="L29" class="LineNr">29 </span> <span class="subxS2Comment"># . . push argv[2]</span> +<span id="L30" class="LineNr">30 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <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 *(ebp+12)</span> +<span id="L31" class="LineNr">31 </span> <span class="subxS2Comment"># . . push argv[1]</span> +<span id="L32" class="LineNr">32 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L33" class="LineNr">33 </span> <span class="subxS2Comment"># . . call</span> +<span id="L34" class="LineNr">34 </span> e8/call <a href='ex9.subx.html#L41'>ascii-difference</a>/disp32 +<span id="L35" class="LineNr">35 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L36" class="LineNr">36 </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="L37" class="LineNr">37 </span> <span class="subxComment"># exit(eax)</span> +<span id="L38" class="LineNr">38 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L39" class="LineNr">39 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L40" class="LineNr">40 </span> +<span id="L41" class="LineNr">41 </span><span class="subxFunction">ascii-difference</span>: <span class="subxComment"># (s1, s2): null-terminated ascii strings</span> +<span id="L42" class="LineNr">42 </span> <span class="subxComment"># a = first letter of s1 (ecx)</span> +<span id="L43" class="LineNr">43 </span> 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none <span class="Normal"> . </span> 0/r32/eax 4/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(esp+4) to eax</span> +<span id="L44" class="LineNr">44 </span> 8b/copy 0/mod/indirect 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"># copy *eax to eax</span> +<span id="L45" class="LineNr">45 </span> <span class="subxComment"># b = first letter of s2 (edx)</span> +<span id="L46" class="LineNr">46 </span> 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/esp 4/index/none <span class="Normal"> . </span> 1/r32/ecx 8/disp8 <span class="subxComment"># copy *(esp+8) to ecx</span> +<span id="L47" class="LineNr">47 </span> 8b/copy 0/mod/indirect 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ecx to ecx</span> +<span id="L48" class="LineNr">48 </span> <span class="subxComment"># a-b</span> +<span id="L49" class="LineNr">49 </span> 29/subtract 3/mod/direct 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># subtract ecx from eax</span> +<span id="L50" class="LineNr">50 </span> c3/return +<span id="L51" class="LineNr">51 </span> +<span id="L52" class="LineNr">52 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/factorial.mu.html b/html/linux/apps/factorial.mu.html new file mode 100644 index 00000000..3e626b6d --- /dev/null +++ b/html/linux/apps/factorial.mu.html @@ -0,0 +1,125 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/factorial.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEbx { color: #5f00ff; } +.muRegEcx { color: #870000; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.muTest { color: #5f8700; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.Special { color: #ff6060; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/factorial.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/factorial.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># compute the factorial of 5, and return the result in the exit code</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/factorial.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ echo $?</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># 120</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># You can also run the automated test suite:</span> +<span id="L10" class="LineNr">10 </span><span class="muComment"># $ ./a.elf test</span> +<span id="L11" class="LineNr">11 </span><span class="muComment"># Expected output:</span> +<span id="L12" class="LineNr">12 </span><span class="muComment"># ........</span> +<span id="L13" class="LineNr">13 </span><span class="muComment"># Every '.' indicates a passing test. Failing tests get a 'F'.</span> +<span id="L14" class="LineNr">14 </span><span class="muComment"># There's only one test in this file, but you'll also see tests running from</span> +<span id="L15" class="LineNr">15 </span><span class="muComment"># Mu's standard library.</span> +<span id="L16" class="LineNr">16 </span><span class="muComment">#</span> +<span id="L17" class="LineNr">17 </span><span class="muComment"># Compare factorial4.subx</span> +<span id="L18" class="LineNr">18 </span> +<span id="L19" class="LineNr">19 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='factorial.mu.html#L19'>factorial</a></span> n: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L20" class="LineNr">20 </span> compare n, <span class="Constant">1</span> +<span id="L21" class="LineNr">21 </span> <span class="muComment"># if (n <= 1) return 1</span> +<span id="L22" class="LineNr">22 </span> <span class="Delimiter">{</span> +<span id="L23" class="LineNr">23 </span> <span class="PreProc">break-if-></span> +<span id="L24" class="LineNr">24 </span> <span class="PreProc">return</span> <span class="Constant">1</span> +<span id="L25" class="LineNr">25 </span> <span class="Delimiter">}</span> +<span id="L26" class="LineNr">26 </span> <span class="muComment"># n > 1; return n * factorial(n-1)</span> +<span id="L27" class="LineNr">27 </span> <span class="PreProc">var</span> tmp/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy n +<span id="L28" class="LineNr">28 </span> tmp <span class="Special"><-</span> decrement +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='factorial.mu.html#L19'>factorial</a> tmp +<span id="L30" class="LineNr">30 </span> result <span class="Special"><-</span> multiply n +<span id="L31" class="LineNr">31 </span> <span class="PreProc">return</span> result +<span id="L32" class="LineNr">32 </span><span class="Delimiter">}</span> +<span id="L33" class="LineNr">33 </span> +<span id="L34" class="LineNr">34 </span><span class="PreProc">fn</span> <span class="muTest"><a href='factorial.mu.html#L34'>test-factorial</a></span> <span class="Delimiter">{</span> +<span id="L35" class="LineNr">35 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='factorial.mu.html#L19'>factorial</a> <span class="Constant">5</span> +<span id="L36" class="LineNr">36 </span> <a href='../102test.subx.html#L23'>check-ints-equal</a> result, <span class="Constant">0x78</span>, <span class="Constant">"F - test-factorial"</span> +<span id="L37" class="LineNr">37 </span><span class="Delimiter">}</span> +<span id="L38" class="LineNr">38 </span> +<span id="L39" class="LineNr">39 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='factorial.mu.html#L39'>main</a></span> args-on-stack: (addr array addr array byte)<span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L40" class="LineNr">40 </span> <span class="PreProc">var</span> args/eax: (addr array addr array byte) <span class="Special"><-</span> copy args-on-stack +<span id="L41" class="LineNr">41 </span> <span class="muComment"># len = length(args)</span> +<span id="L42" class="LineNr">42 </span> <span class="PreProc">var</span> len/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> length args +<span id="L43" class="LineNr">43 </span> <span class="muComment"># if (len <= 1) return factorial(5)</span> +<span id="L44" class="LineNr">44 </span> compare len, <span class="Constant">1</span> +<span id="L45" class="LineNr">45 </span> <span class="Delimiter">{</span> +<span id="L46" class="LineNr">46 </span> <span class="PreProc">break-if-></span> +<span id="L47" class="LineNr">47 </span> <span class="PreProc">var</span> exit-status/eax: int <span class="Special"><-</span> <a href='factorial.mu.html#L19'>factorial</a> <span class="Constant">5</span> +<span id="L48" class="LineNr">48 </span> <span class="PreProc">return</span> exit-status +<span id="L49" class="LineNr">49 </span> <span class="Delimiter">}</span> +<span id="L50" class="LineNr">50 </span> <span class="muComment"># if (args[1] == "test") run-tests()</span> +<span id="L51" class="LineNr">51 </span> <span class="PreProc">var</span> tmp2/<span class="muRegEcx">ecx</span>: (addr addr array byte) <span class="Special"><-</span> index args, <span class="Constant">1</span> +<span id="L52" class="LineNr">52 </span> <span class="PreProc">var</span> tmp3/eax: boolean <span class="Special"><-</span> <a href='../105string-equal.subx.html#L15'>string-equal?</a> *tmp2, <span class="Constant">"test"</span> +<span id="L53" class="LineNr">53 </span> compare tmp3, <span class="Constant">0</span> +<span id="L54" class="LineNr">54 </span> <span class="Delimiter">{</span> +<span id="L55" class="LineNr">55 </span> <span class="PreProc">break-if-=</span> +<span id="L56" class="LineNr">56 </span> run-tests +<span id="L57" class="LineNr">57 </span> <span class="muComment"># TODO: get at Num-test-failures somehow</span> +<span id="L58" class="LineNr">58 </span> <span class="Delimiter">}</span> +<span id="L59" class="LineNr">59 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L60" class="LineNr">60 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/factorial.subx.html b/html/linux/apps/factorial.subx.html new file mode 100644 index 00000000..c8f08ab6 --- /dev/null +++ b/html/linux/apps/factorial.subx.html @@ -0,0 +1,217 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/factorial.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.Constant { color: #008787; } +.SpecialChar { color: #d70000; } +.Normal { color: #000000; background-color: #ffffd7; padding-bottom: 1px; } +.subxTest { color: #5f8700; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/factorial.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/factorial.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment">## compute the factorial of 5, and print the result</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># $ bootstrap/bootstrap translate [01]*.subx apps/factorial.subx -o factorial</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># Expected result:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ echo $?</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># 120</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment">#</span> +<span id="L10" class="LineNr"> 10 </span><span class="subxComment"># You can also run the automated test suite:</span> +<span id="L11" class="LineNr"> 11 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial test</span> +<span id="L12" class="LineNr"> 12 </span><span class="subxComment"># Expected output:</span> +<span id="L13" class="LineNr"> 13 </span><span class="subxComment"># ........</span> +<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># Every '.' indicates a passing test. Failing tests get a 'F'.</span> +<span id="L15" class="LineNr"> 15 </span> +<span id="L16" class="LineNr"> 16 </span>== code +<span id="L17" class="LineNr"> 17 </span><span class="subxComment"># instruction effective address register displacement immediate</span> +<span id="L18" class="LineNr"> 18 </span><span class="subxS1Comment"># . op subop mod rm32 base index scale r32</span> +<span id="L19" class="LineNr"> 19 </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="L20" class="LineNr"> 20 </span> +<span id="L21" class="LineNr"> 21 </span><span class="subxFunction">factorial</span>: <span class="subxComment"># n: int -> _/eax: int</span> +<span id="L22" class="LineNr"> 22 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L23" class="LineNr"> 23 </span> 55/push-ebp +<span id="L24" class="LineNr"> 24 </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="L25" class="LineNr"> 25 </span> 51/push-ecx +<span id="L26" class="LineNr"> 26 </span> <span class="subxComment"># if (n <= 1) return 1</span> +<span id="L27" class="LineNr"> 27 </span> b8/copy-to-eax 1/imm32 +<span id="L28" class="LineNr"> 28 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 1/imm32 <span class="subxComment"># compare *(ebp+8)</span> +<span id="L29" class="LineNr"> 29 </span> 7e/jump-if-<= $factorial:end/disp8 +<span id="L30" class="LineNr"> 30 </span> <span class="subxComment"># var ecx: int = n-1</span> +<span id="L31" class="LineNr"> 31 </span> 8b/copy 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> 1/r32/ecx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy *(ebp+8) to ecx</span> +<span id="L32" class="LineNr"> 32 </span> 49/decrement-ecx +<span id="L33" class="LineNr"> 33 </span> <span class="subxComment"># var eax: int = factorial(n-1)</span> +<span id="L34" class="LineNr"> 34 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L35" class="LineNr"> 35 </span> 51/push-ecx +<span id="L36" class="LineNr"> 36 </span> <span class="subxS2Comment"># . . call</span> +<span id="L37" class="LineNr"> 37 </span> e8/call <a href='factorial.subx.html#L21'>factorial</a>/disp32 +<span id="L38" class="LineNr"> 38 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L39" class="LineNr"> 39 </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="L40" class="LineNr"> 40 </span> <span class="subxComment"># return n * factorial(n-1)</span> +<span id="L41" class="LineNr"> 41 </span> f7 4/subop/multiply 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># multiply *(ebp+8) into eax</span> +<span id="L42" class="LineNr"> 42 </span> <span class="subxComment"># TODO: check for overflow</span> +<span id="L43" class="LineNr"> 43 </span><span class="Constant">$factorial:end</span>: +<span id="L44" class="LineNr"> 44 </span> <span class="subxComment"># restore registers</span> +<span id="L45" class="LineNr"> 45 </span> 59/pop-to-ecx +<span id="L46" class="LineNr"> 46 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L47" class="LineNr"> 47 </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="L48" class="LineNr"> 48 </span> 5d/pop-to-ebp +<span id="L49" class="LineNr"> 49 </span> c3/return +<span id="L50" class="LineNr"> 50 </span> +<span id="L51" class="LineNr"> 51 </span><span class="subxTest">test-factorial</span>: +<span id="L52" class="LineNr"> 52 </span> <span class="subxComment"># factorial(5)</span> +<span id="L53" class="LineNr"> 53 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L54" class="LineNr"> 54 </span> 68/push 5/imm32 +<span id="L55" class="LineNr"> 55 </span> <span class="subxS2Comment"># . . call</span> +<span id="L56" class="LineNr"> 56 </span> e8/call <a href='factorial.subx.html#L21'>factorial</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="subxComment"># check-ints-equal(eax, 120, msg)</span> +<span id="L60" class="LineNr"> 60 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L61" class="LineNr"> 61 </span> 68/push <span class="Constant">"F - test-factorial"</span>/imm32 +<span id="L62" class="LineNr"> 62 </span> 68/push 0x78/imm32/expected-120 +<span id="L63" class="LineNr"> 63 </span> 50/push-eax +<span id="L64" class="LineNr"> 64 </span> <span class="subxS2Comment"># . . call</span> +<span id="L65" class="LineNr"> 65 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L66" class="LineNr"> 66 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L67" class="LineNr"> 67 </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="L68" class="LineNr"> 68 </span> <span class="subxComment"># end</span> +<span id="L69" class="LineNr"> 69 </span> c3/return +<span id="L70" class="LineNr"> 70 </span> +<span id="L71" class="LineNr"> 71 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, compute `factorial(5)` if not</span> +<span id="L72" class="LineNr"> 72 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L73" class="LineNr"> 73 </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="L74" class="LineNr"> 74 </span> +<span id="L75" class="LineNr"> 75 </span> <span class="subxComment"># initialize heap (needed by tests elsewhere)</span> +<span id="L76" class="LineNr"> 76 </span> <span class="subxS1Comment"># . Heap = new-segment(Heap-size)</span> +<span id="L77" class="LineNr"> 77 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L78" class="LineNr"> 78 </span> 68/push <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>/imm32 +<span id="L79" class="LineNr"> 79 </span> ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span>/disp32 <span class="subxComment"># push *Heap-size</span> +<span id="L80" class="LineNr"> 80 </span> <span class="subxS2Comment"># . . call</span> +<span id="L81" class="LineNr"> 81 </span> e8/call <a href='../104new-segment.subx.html#L40'>new-segment</a>/disp32 +<span id="L82" class="LineNr"> 82 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L83" class="LineNr"> 83 </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="L84" class="LineNr"> 84 </span> +<span id="L85" class="LineNr"> 85 </span> <span class="subxComment"># if (argc <= 1) return factorial(5)</span> +<span id="L86" class="LineNr"> 86 </span><span class="Constant">$run-main</span>: +<span id="L87" class="LineNr"> 87 </span> 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/disp8 1/imm32 <span class="subxComment"># compare *ebp</span> +<span id="L88" class="LineNr"> 88 </span> 7f/jump-if-> $main:run-tests/disp8 +<span id="L89" class="LineNr"> 89 </span> <span class="subxComment"># eax = factorial(5)</span> +<span id="L90" class="LineNr"> 90 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L91" class="LineNr"> 91 </span> 68/push 5/imm32 +<span id="L92" class="LineNr"> 92 </span> <span class="subxS2Comment"># . . call</span> +<span id="L93" class="LineNr"> 93 </span> e8/call <a href='factorial.subx.html#L21'>factorial</a>/disp32 +<span id="L94" class="LineNr"> 94 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L95" class="LineNr"> 95 </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="L96" class="LineNr"> 96 </span> <span class="subxComment"># var buffer/ecx: (stream byte 10) # number of decimal digits a 32-bit number can have</span> +<span id="L97" class="LineNr"> 97 </span> 81 5/subop/subtract 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> 0xa/imm32 <span class="subxComment"># subtract from esp</span> +<span id="L98" class="LineNr"> 98 </span> 68/push 0xa/imm32/decimal-digits-in-32bit-number +<span id="L99" class="LineNr"> 99 </span> 68/push 0/imm32/read +<span id="L100" class="LineNr">100 </span> 68/push 0/imm32/write +<span id="L101" class="LineNr">101 </span> 89/copy 3/mod/direct 1/rm32/ecx <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 ecx</span> +<span id="L102" class="LineNr">102 </span> <span class="subxComment"># write-int32-decimal(buffer, eax)</span> +<span id="L103" class="LineNr">103 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L104" class="LineNr">104 </span> 50/push-eax +<span id="L105" class="LineNr">105 </span> 51/push-ecx +<span id="L106" class="LineNr">106 </span> <span class="subxS2Comment"># . . call</span> +<span id="L107" class="LineNr">107 </span> e8/call <a href='../126write-int-decimal.subx.html#L8'>write-int32-decimal</a>/disp32 +<span id="L108" class="LineNr">108 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L109" class="LineNr">109 </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="L110" class="LineNr">110 </span> <span class="subxComment"># write-stream(stderr, buffer)</span> +<span id="L111" class="LineNr">111 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L112" class="LineNr">112 </span> 51/push-ecx +<span id="L113" class="LineNr">113 </span> 68/push 2/imm32/stderr +<span id="L114" class="LineNr">114 </span> <span class="subxS2Comment"># . . call</span> +<span id="L115" class="LineNr">115 </span> e8/call <a href='../113write-stream.subx.html#L17'>write-stream</a>/disp32 +<span id="L116" class="LineNr">116 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L117" class="LineNr">117 </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="L118" class="LineNr">118 </span> <span class="subxComment"># write(stderr, Newline)</span> +<span id="L119" class="LineNr">119 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L120" class="LineNr">120 </span> 68/push <span class="SpecialChar"><a href='../102test.subx.html#L82'>Newline</a></span>/imm32 +<span id="L121" class="LineNr">121 </span> 68/push 2/imm32/stderr +<span id="L122" class="LineNr">122 </span> <span class="subxS2Comment"># . . call</span> +<span id="L123" class="LineNr">123 </span> e8/call <a href='../108write.subx.html#L24'>write</a>/disp32 +<span id="L124" class="LineNr">124 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L125" class="LineNr">125 </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="L126" class="LineNr">126 </span> <span class="subxComment">#</span> +<span id="L127" class="LineNr">127 </span> 89/copy 3/mod/direct 3/rm32/ebx <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"># copy eax to ebx</span> +<span id="L128" class="LineNr">128 </span> eb/jump $main:end/disp8 +<span id="L129" class="LineNr">129 </span><span class="Constant">$main:run-tests</span>: +<span id="L130" class="LineNr">130 </span> <span class="subxComment"># otherwise if first arg is "test", then return run_tests()</span> +<span id="L131" class="LineNr">131 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) goto do-nothing</span> +<span id="L132" class="LineNr">132 </span> <span class="subxS1Comment"># . eax = kernel-string-equal?(argv[1], "test")</span> +<span id="L133" class="LineNr">133 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L134" class="LineNr">134 </span> 68/push <span class="Constant">"test"</span>/imm32 +<span id="L135" class="LineNr">135 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> +<span id="L136" class="LineNr">136 </span> <span class="subxS2Comment"># . . call</span> +<span id="L137" class="LineNr">137 </span> e8/call <a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</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="subxS1Comment"># . if (eax == false) goto do-nothing</span> +<span id="L141" class="LineNr">141 </span> 3d/compare-eax-and 0/imm32/false +<span id="L142" class="LineNr">142 </span> 74/jump-if-= $main:do-nothing/disp8 +<span id="L143" class="LineNr">143 </span> <span class="subxComment"># run-tests()</span> +<span id="L144" class="LineNr">144 </span> e8/call run-tests/disp32 +<span id="L145" class="LineNr">145 </span> <span class="subxComment"># exit(*Num-test-failures)</span> +<span id="L146" class="LineNr">146 </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='../102test.subx.html#L89'>Num-test-failures</a></span>/disp32 <span class="subxComment"># copy *Num-test-failures to ebx</span> +<span id="L147" class="LineNr">147 </span> eb/jump $main:end/disp8 +<span id="L148" class="LineNr">148 </span><span class="Constant">$main:do-nothing</span>: +<span id="L149" class="LineNr">149 </span> bb/copy-to-ebx 0/imm32 +<span id="L150" class="LineNr">150 </span><span class="Constant">$main:end</span>: +<span id="L151" class="LineNr">151 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +<span id="L152" class="LineNr">152 </span> +<span id="L153" class="LineNr">153 </span><span class="subxS2Comment"># . . vim:nowrap:textwidth=0</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/factorial2.subx.html b/html/linux/apps/factorial2.subx.html new file mode 100644 index 00000000..72331748 --- /dev/null +++ b/html/linux/apps/factorial2.subx.html @@ -0,0 +1,185 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/factorial2.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.subxS2Comment { color: #8a8a8a; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Constant { color: #008787; } +.subxTest { color: #5f8700; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/factorial2.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/factorial2.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment">## compute the factorial of 5, and return the result in the exit code</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># Uses syntax sugar for:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># rm32 operands</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># To run:</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># $ ./translate_subx init.linux [01]*.subx apps/factorial2.subx -o factorial</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># Expected result:</span> +<span id="L10" class="LineNr"> 10 </span><span class="subxComment"># $ echo $?</span> +<span id="L11" class="LineNr"> 11 </span><span class="subxComment"># 120</span> +<span id="L12" class="LineNr"> 12 </span><span class="subxComment">#</span> +<span id="L13" class="LineNr"> 13 </span><span class="subxComment"># You can also run the automated test suite:</span> +<span id="L14" class="LineNr"> 14 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial test</span> +<span id="L15" class="LineNr"> 15 </span><span class="subxComment"># Expected output:</span> +<span id="L16" class="LineNr"> 16 </span><span class="subxComment"># ........</span> +<span id="L17" class="LineNr"> 17 </span><span class="subxComment"># Every '.' indicates a passing test. Failing tests get a 'F'.</span> +<span id="L18" class="LineNr"> 18 </span><span class="subxComment">#</span> +<span id="L19" class="LineNr"> 19 </span><span class="subxComment"># Compare factorial.subx</span> +<span id="L20" class="LineNr"> 20 </span> +<span id="L21" class="LineNr"> 21 </span>== code +<span id="L22" class="LineNr"> 22 </span> +<span id="L23" class="LineNr"> 23 </span><span class="subxFunction">factorial</span>: <span class="subxComment"># n: int -> _/eax: int</span> +<span id="L24" class="LineNr"> 24 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L25" class="LineNr"> 25 </span> 55/push-ebp +<span id="L26" class="LineNr"> 26 </span> 89/<- %ebp 4/r32/esp +<span id="L27" class="LineNr"> 27 </span> <span class="subxComment"># save registers</span> +<span id="L28" class="LineNr"> 28 </span> 51/push-ecx +<span id="L29" class="LineNr"> 29 </span> <span class="subxComment"># if (n <= 1) return 1</span> +<span id="L30" class="LineNr"> 30 </span> b8/copy-to-eax 1/imm32 +<span id="L31" class="LineNr"> 31 </span> 81 7/subop/compare *(ebp+8) 1/imm32 +<span id="L32" class="LineNr"> 32 </span> 7e/jump-if-<= $factorial:end/disp8 +<span id="L33" class="LineNr"> 33 </span> <span class="subxComment"># n > 1; return n * factorial(n-1)</span> +<span id="L34" class="LineNr"> 34 </span> 8b/-> *(ebp+8) 1/r32/ecx +<span id="L35" class="LineNr"> 35 </span> 49/decrement-ecx +<span id="L36" class="LineNr"> 36 </span> <span class="subxComment"># var tmp/eax: int = factorial(n-1)</span> +<span id="L37" class="LineNr"> 37 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L38" class="LineNr"> 38 </span> 51/push-ecx +<span id="L39" class="LineNr"> 39 </span> <span class="subxS2Comment"># . . call</span> +<span id="L40" class="LineNr"> 40 </span> e8/call <a href='factorial2.subx.html#L23'>factorial</a>/disp32 +<span id="L41" class="LineNr"> 41 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L42" class="LineNr"> 42 </span> 81 0/subop/add %esp 4/imm32 +<span id="L43" class="LineNr"> 43 </span> <span class="subxComment"># return n * tmp</span> +<span id="L44" class="LineNr"> 44 </span> f7 4/subop/multiply-into-eax *(ebp+8) +<span id="L45" class="LineNr"> 45 </span> <span class="subxComment"># TODO: check for overflow</span> +<span id="L46" class="LineNr"> 46 </span><span class="Constant">$factorial:end</span>: +<span id="L47" class="LineNr"> 47 </span> <span class="subxComment"># restore registers</span> +<span id="L48" class="LineNr"> 48 </span> 59/pop-to-ecx +<span id="L49" class="LineNr"> 49 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L50" class="LineNr"> 50 </span> 89/<- %esp 5/r32/ebp +<span id="L51" class="LineNr"> 51 </span> 5d/pop-to-ebp +<span id="L52" class="LineNr"> 52 </span> c3/return +<span id="L53" class="LineNr"> 53 </span> +<span id="L54" class="LineNr"> 54 </span><span class="subxTest">test-factorial</span>: +<span id="L55" class="LineNr"> 55 </span> <span class="subxComment"># factorial(5)</span> +<span id="L56" class="LineNr"> 56 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L57" class="LineNr"> 57 </span> 68/push 5/imm32 +<span id="L58" class="LineNr"> 58 </span> <span class="subxS2Comment"># . . call</span> +<span id="L59" class="LineNr"> 59 </span> e8/call <a href='factorial2.subx.html#L23'>factorial</a>/disp32 +<span id="L60" class="LineNr"> 60 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L61" class="LineNr"> 61 </span> 81 0/subop/add %esp 4/imm32 +<span id="L62" class="LineNr"> 62 </span> <span class="subxComment"># check-ints-equal(eax, 120, msg)</span> +<span id="L63" class="LineNr"> 63 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L64" class="LineNr"> 64 </span> 68/push <span class="Constant">"F - test-factorial"</span>/imm32 +<span id="L65" class="LineNr"> 65 </span> 68/push 0x78/imm32/expected-120 +<span id="L66" class="LineNr"> 66 </span> 50/push-eax +<span id="L67" class="LineNr"> 67 </span> <span class="subxS2Comment"># . . call</span> +<span id="L68" class="LineNr"> 68 </span> e8/call <a href='../102test.subx.html#L23'>check-ints-equal</a>/disp32 +<span id="L69" class="LineNr"> 69 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L70" class="LineNr"> 70 </span> 81 0/subop/add %esp 0xc/imm32 +<span id="L71" class="LineNr"> 71 </span> <span class="subxComment"># end</span> +<span id="L72" class="LineNr"> 72 </span> c3/return +<span id="L73" class="LineNr"> 73 </span> +<span id="L74" class="LineNr"> 74 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, compute `factorial(5)` if not</span> +<span id="L75" class="LineNr"> 75 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L76" class="LineNr"> 76 </span> 89/<- %ebp 4/r32/esp +<span id="L77" class="LineNr"> 77 </span> +<span id="L78" class="LineNr"> 78 </span> <span class="subxComment"># initialize heap (needed by tests elsewhere)</span> +<span id="L79" class="LineNr"> 79 </span> <span class="subxS1Comment"># . Heap = new-segment(Heap-size)</span> +<span id="L80" class="LineNr"> 80 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L81" class="LineNr"> 81 </span> 68/push <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>/imm32 +<span id="L82" class="LineNr"> 82 </span> ff 6/subop/push *<span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span> +<span id="L83" class="LineNr"> 83 </span> <span class="subxS2Comment"># . . call</span> +<span id="L84" class="LineNr"> 84 </span> e8/call <a href='../104new-segment.subx.html#L40'>new-segment</a>/disp32 +<span id="L85" class="LineNr"> 85 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L86" class="LineNr"> 86 </span> 81 0/subop/add %esp 8/imm32 +<span id="L87" class="LineNr"> 87 </span> +<span id="L88" class="LineNr"> 88 </span> <span class="subxComment"># if (argc <= 1) return factorial(5)</span> +<span id="L89" class="LineNr"> 89 </span> 81 7/subop/compare *ebp 1/imm32 +<span id="L90" class="LineNr"> 90 </span> 7f/jump-if-> $main:run-tests/disp8 +<span id="L91" class="LineNr"> 91 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L92" class="LineNr"> 92 </span> 68/push 5/imm32 +<span id="L93" class="LineNr"> 93 </span> <span class="subxS2Comment"># . . call</span> +<span id="L94" class="LineNr"> 94 </span> e8/call <a href='factorial2.subx.html#L23'>factorial</a>/disp32 +<span id="L95" class="LineNr"> 95 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L96" class="LineNr"> 96 </span> 81 0/subop/add %esp 4/imm32 +<span id="L97" class="LineNr"> 97 </span> <span class="subxS1Comment"># .</span> +<span id="L98" class="LineNr"> 98 </span> 89/<- %ebx 0/r32/eax +<span id="L99" class="LineNr"> 99 </span> eb/jump $main:end/disp8 +<span id="L100" class="LineNr">100 </span><span class="Constant">$main:run-tests</span>: +<span id="L101" class="LineNr">101 </span> <span class="subxComment"># otherwise if first arg is "test", then return run_tests()</span> +<span id="L102" class="LineNr">102 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) goto do-nothing</span> +<span id="L103" class="LineNr">103 </span> <span class="subxS1Comment"># . eax = kernel-string-equal?(argv[1], "test")</span> +<span id="L104" class="LineNr">104 </span> <span class="subxS2Comment"># . . push args</span> +<span id="L105" class="LineNr">105 </span> 68/push <span class="Constant">"test"</span>/imm32 +<span id="L106" class="LineNr">106 </span> ff 6/subop/push *(ebp+8) +<span id="L107" class="LineNr">107 </span> <span class="subxS2Comment"># . . call</span> +<span id="L108" class="LineNr">108 </span> e8/call <a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</a>/disp32 +<span id="L109" class="LineNr">109 </span> <span class="subxS2Comment"># . . discard args</span> +<span id="L110" class="LineNr">110 </span> 81 0/subop/add %esp 8/imm32 +<span id="L111" class="LineNr">111 </span> <span class="subxS1Comment"># . if (eax == false) goto do-nothing</span> +<span id="L112" class="LineNr">112 </span> 3d/compare-eax-and 0/imm32/false +<span id="L113" class="LineNr">113 </span> 74/jump-if-= $main:do-nothing/disp8 +<span id="L114" class="LineNr">114 </span> <span class="subxComment"># run-tests()</span> +<span id="L115" class="LineNr">115 </span> e8/call run-tests/disp32 +<span id="L116" class="LineNr">116 </span> <span class="subxComment"># exit(*Num-test-failures)</span> +<span id="L117" class="LineNr">117 </span> 8b/-> *<span class="SpecialChar"><a href='../102test.subx.html#L89'>Num-test-failures</a></span> 3/r32/ebx +<span id="L118" class="LineNr">118 </span> eb/jump $main:end/disp8 +<span id="L119" class="LineNr">119 </span><span class="Constant">$main:do-nothing</span>: +<span id="L120" class="LineNr">120 </span> bb/copy-to-ebx 0/imm32 +<span id="L121" class="LineNr">121 </span><span class="Constant">$main:end</span>: +<span id="L122" class="LineNr">122 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/factorial3.subx.html b/html/linux/apps/factorial3.subx.html new file mode 100644 index 00000000..93cf4e55 --- /dev/null +++ b/html/linux/apps/factorial3.subx.html @@ -0,0 +1,142 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/factorial3.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Constant { color: #008787; } +.subxTest { color: #5f8700; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/factorial3.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/factorial3.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment">## compute the factorial of 5, and return the result in the exit code</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># Uses syntax sugar for:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># rm32 operands</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># function calls</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment">#</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment"># To run:</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># $ ./translate_subx init.linux [01]*.subx apps/factorial3.subx -o factorial</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># Expected result:</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># $ echo $?</span> +<span id="L12" class="LineNr">12 </span><span class="subxComment"># 120</span> +<span id="L13" class="LineNr">13 </span><span class="subxComment">#</span> +<span id="L14" class="LineNr">14 </span><span class="subxComment"># You can also run the automated test suite:</span> +<span id="L15" class="LineNr">15 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial test</span> +<span id="L16" class="LineNr">16 </span><span class="subxComment"># Expected output:</span> +<span id="L17" class="LineNr">17 </span><span class="subxComment"># ........</span> +<span id="L18" class="LineNr">18 </span><span class="subxComment"># Every '.' indicates a passing test. Failing tests get a 'F'.</span> +<span id="L19" class="LineNr">19 </span><span class="subxComment">#</span> +<span id="L20" class="LineNr">20 </span><span class="subxComment"># Compare factorial2.subx</span> +<span id="L21" class="LineNr">21 </span> +<span id="L22" class="LineNr">22 </span>== code +<span id="L23" class="LineNr">23 </span> +<span id="L24" class="LineNr">24 </span><span class="subxFunction">factorial</span>: <span class="subxComment"># n: int -> _/eax: int</span> +<span id="L25" class="LineNr">25 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L26" class="LineNr">26 </span> 55/push-ebp +<span id="L27" class="LineNr">27 </span> 89/<- %ebp 4/r32/esp +<span id="L28" class="LineNr">28 </span> <span class="subxComment"># save registers</span> +<span id="L29" class="LineNr">29 </span> 51/push-ecx +<span id="L30" class="LineNr">30 </span> <span class="subxComment"># if (n <= 1) return 1</span> +<span id="L31" class="LineNr">31 </span> b8/copy-to-eax 1/imm32 +<span id="L32" class="LineNr">32 </span> 81 7/subop/compare *(ebp+8) 1/imm32 +<span id="L33" class="LineNr">33 </span> 7e/jump-if-<= $factorial:end/disp8 +<span id="L34" class="LineNr">34 </span> <span class="subxComment"># n > 1; return n * factorial(n-1)</span> +<span id="L35" class="LineNr">35 </span> 8b/-> *(ebp+8) 1/r32/ecx +<span id="L36" class="LineNr">36 </span> 49/decrement-ecx +<span id="L37" class="LineNr">37 </span> (<a href='factorial3.subx.html#L24'>factorial</a> %ecx) <span class="subxComment"># => eax</span> +<span id="L38" class="LineNr">38 </span> f7 4/subop/multiply-into-eax *(ebp+8) +<span id="L39" class="LineNr">39 </span> <span class="subxComment"># TODO: check for overflow</span> +<span id="L40" class="LineNr">40 </span><span class="Constant">$factorial:end</span>: +<span id="L41" class="LineNr">41 </span> <span class="subxComment"># restore registers</span> +<span id="L42" class="LineNr">42 </span> 59/pop-to-ecx +<span id="L43" class="LineNr">43 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L44" class="LineNr">44 </span> 89/<- %esp 5/r32/ebp +<span id="L45" class="LineNr">45 </span> 5d/pop-to-ebp +<span id="L46" class="LineNr">46 </span> c3/return +<span id="L47" class="LineNr">47 </span> +<span id="L48" class="LineNr">48 </span><span class="subxTest">test-factorial</span>: +<span id="L49" class="LineNr">49 </span> (<a href='factorial3.subx.html#L24'>factorial</a> 5) +<span id="L50" class="LineNr">50 </span> (<a href='../102test.subx.html#L23'>check-ints-equal</a> %eax 0x78 <span class="Constant">"F - test-factorial"</span>) +<span id="L51" class="LineNr">51 </span> c3/return +<span id="L52" class="LineNr">52 </span> +<span id="L53" class="LineNr">53 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, compute `factorial(5)` if not</span> +<span id="L54" class="LineNr">54 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L55" class="LineNr">55 </span> 89/<- %ebp 4/r32/esp +<span id="L56" class="LineNr">56 </span> +<span id="L57" class="LineNr">57 </span> <span class="subxComment"># initialize heap (needed by tests elsewhere)</span> +<span id="L58" class="LineNr">58 </span> (<a href='../104new-segment.subx.html#L40'>new-segment</a> *<span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span> <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>) +<span id="L59" class="LineNr">59 </span> +<span id="L60" class="LineNr">60 </span> <span class="subxComment"># if (argc <= 1) return factorial(5)</span> +<span id="L61" class="LineNr">61 </span> 81 7/subop/compare *ebp 1/imm32 +<span id="L62" class="LineNr">62 </span> 7f/jump-if-> $main:run-tests/disp8 +<span id="L63" class="LineNr">63 </span> (<a href='factorial3.subx.html#L24'>factorial</a> 5) <span class="subxComment"># => eax</span> +<span id="L64" class="LineNr">64 </span> 89/<- %ebx 0/r32/eax +<span id="L65" class="LineNr">65 </span> eb/jump $main:end/disp8 +<span id="L66" class="LineNr">66 </span><span class="Constant">$main:run-tests</span>: +<span id="L67" class="LineNr">67 </span> <span class="subxComment"># otherwise if first arg is "test", then return run_tests()</span> +<span id="L68" class="LineNr">68 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) goto do-nothing</span> +<span id="L69" class="LineNr">69 </span> (<a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</a> *(ebp+8) <span class="Constant">"test"</span>) <span class="subxComment"># => eax</span> +<span id="L70" class="LineNr">70 </span> 3d/compare-eax-and 0/imm32/false +<span id="L71" class="LineNr">71 </span> 74/jump-if-= $main:do-nothing/disp8 +<span id="L72" class="LineNr">72 </span> <span class="subxComment">#</span> +<span id="L73" class="LineNr">73 </span> (run-tests) +<span id="L74" class="LineNr">74 </span> <span class="subxComment"># exit(*Num-test-failures)</span> +<span id="L75" class="LineNr">75 </span> 8b/-> *<span class="SpecialChar"><a href='../102test.subx.html#L89'>Num-test-failures</a></span> 3/r32/ebx +<span id="L76" class="LineNr">76 </span> eb/jump $main:end/disp8 +<span id="L77" class="LineNr">77 </span><span class="Constant">$main:do-nothing</span>: +<span id="L78" class="LineNr">78 </span> bb/copy-to-ebx 0/imm32 +<span id="L79" class="LineNr">79 </span><span class="Constant">$main:end</span>: +<span id="L80" class="LineNr">80 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/factorial4.subx.html b/html/linux/apps/factorial4.subx.html new file mode 100644 index 00000000..879a7a44 --- /dev/null +++ b/html/linux/apps/factorial4.subx.html @@ -0,0 +1,150 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/factorial4.subx</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.subxComment { color: #005faf; } +.LineNr { } +.subxFunction { color: #af5f00; text-decoration: underline; } +.subxS1Comment { color: #0000af; } +.SpecialChar { color: #d70000; } +.Constant { color: #008787; } +.subxTest { color: #5f8700; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/factorial4.subx'>https://github.com/akkartik/mu/blob/main/linux/apps/factorial4.subx</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="subxComment">## compute the factorial of 5, and return the result in the exit code</span> +<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># Uses syntax sugar for:</span> +<span id="L4" class="LineNr"> 4 </span><span class="subxComment"># rm32 operands</span> +<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># function calls</span> +<span id="L6" class="LineNr"> 6 </span><span class="subxComment"># control flow</span> +<span id="L7" class="LineNr"> 7 </span><span class="subxComment">#</span> +<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># To run:</span> +<span id="L9" class="LineNr"> 9 </span><span class="subxComment"># $ ./translate_subx init.linux [01]*.subx apps/factorial4.subx -o factorial</span> +<span id="L10" class="LineNr">10 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial</span> +<span id="L11" class="LineNr">11 </span><span class="subxComment"># Expected result:</span> +<span id="L12" class="LineNr">12 </span><span class="subxComment"># $ echo $?</span> +<span id="L13" class="LineNr">13 </span><span class="subxComment"># 120</span> +<span id="L14" class="LineNr">14 </span><span class="subxComment">#</span> +<span id="L15" class="LineNr">15 </span><span class="subxComment"># You can also run the automated test suite:</span> +<span id="L16" class="LineNr">16 </span><span class="subxComment"># $ bootstrap/bootstrap run factorial test</span> +<span id="L17" class="LineNr">17 </span><span class="subxComment"># Expected output:</span> +<span id="L18" class="LineNr">18 </span><span class="subxComment"># ........</span> +<span id="L19" class="LineNr">19 </span><span class="subxComment"># Every '.' indicates a passing test. Failing tests get a 'F'.</span> +<span id="L20" class="LineNr">20 </span><span class="subxComment">#</span> +<span id="L21" class="LineNr">21 </span><span class="subxComment"># Compare factorial3.subx</span> +<span id="L22" class="LineNr">22 </span> +<span id="L23" class="LineNr">23 </span>== code +<span id="L24" class="LineNr">24 </span> +<span id="L25" class="LineNr">25 </span><span class="subxFunction">factorial</span>: <span class="subxComment"># n: int -> _/eax: int</span> +<span id="L26" class="LineNr">26 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L27" class="LineNr">27 </span> 55/push-ebp +<span id="L28" class="LineNr">28 </span> 89/<- %ebp 4/r32/esp +<span id="L29" class="LineNr">29 </span> <span class="subxComment"># save registers</span> +<span id="L30" class="LineNr">30 </span> 51/push-ecx +<span id="L31" class="LineNr">31 </span> <span class="subxComment"># if (n <= 1) return 1</span> +<span id="L32" class="LineNr">32 </span> 81 7/subop/compare *(ebp+8) 1/imm32 +<span id="L33" class="LineNr">33 </span> { +<span id="L34" class="LineNr">34 </span> 7f/jump-if-> <span class="Constant">break</span>/disp8 +<span id="L35" class="LineNr">35 </span> b8/copy-to-eax 1/imm32 +<span id="L36" class="LineNr">36 </span> eb/jump $factorial:end/disp8 +<span id="L37" class="LineNr">37 </span> } +<span id="L38" class="LineNr">38 </span> <span class="subxComment"># n > 1; return n * factorial(n-1)</span> +<span id="L39" class="LineNr">39 </span> 8b/-> *(ebp+8) 1/r32/ecx +<span id="L40" class="LineNr">40 </span> 49/decrement-ecx +<span id="L41" class="LineNr">41 </span> (<a href='factorial4.subx.html#L25'>factorial</a> %ecx) <span class="subxComment"># => eax</span> +<span id="L42" class="LineNr">42 </span> f7 4/subop/multiply-into-eax *(ebp+8) +<span id="L43" class="LineNr">43 </span> <span class="subxComment"># TODO: check for overflow</span> +<span id="L44" class="LineNr">44 </span><span class="Constant">$factorial:end</span>: +<span id="L45" class="LineNr">45 </span> <span class="subxComment"># restore registers</span> +<span id="L46" class="LineNr">46 </span> 59/pop-to-ecx +<span id="L47" class="LineNr">47 </span> <span class="subxS1Comment"># . epilogue</span> +<span id="L48" class="LineNr">48 </span> 89/<- %esp 5/r32/ebp +<span id="L49" class="LineNr">49 </span> 5d/pop-to-ebp +<span id="L50" class="LineNr">50 </span> c3/return +<span id="L51" class="LineNr">51 </span> +<span id="L52" class="LineNr">52 </span><span class="subxTest">test-factorial</span>: +<span id="L53" class="LineNr">53 </span> (<a href='factorial4.subx.html#L25'>factorial</a> 5) +<span id="L54" class="LineNr">54 </span> (<a href='../102test.subx.html#L23'>check-ints-equal</a> %eax 0x78 <span class="Constant">"F - test-factorial"</span>) +<span id="L55" class="LineNr">55 </span> c3/return +<span id="L56" class="LineNr">56 </span> +<span id="L57" class="LineNr">57 </span><span class="SpecialChar">Entry</span>: <span class="subxComment"># run tests if necessary, compute `factorial(5)` if not</span> +<span id="L58" class="LineNr">58 </span> <span class="subxS1Comment"># . prologue</span> +<span id="L59" class="LineNr">59 </span> 89/<- %ebp 4/r32/esp +<span id="L60" class="LineNr">60 </span> +<span id="L61" class="LineNr">61 </span> <span class="subxComment"># initialize heap (needed by tests elsewhere)</span> +<span id="L62" class="LineNr">62 </span> (<a href='../104new-segment.subx.html#L40'>new-segment</a> *<span class="SpecialChar"><a href='../120allocate.subx.html#L34'>Heap-size</a></span> <span class="SpecialChar"><a href='../120allocate.subx.html#L27'>Heap</a></span>) +<span id="L63" class="LineNr">63 </span> +<span id="L64" class="LineNr">64 </span> <span class="subxComment"># if (argc <= 1) return factorial(5)</span> +<span id="L65" class="LineNr">65 </span> { +<span id="L66" class="LineNr">66 </span> <span class="subxComment"># if (argc > 1) break</span> +<span id="L67" class="LineNr">67 </span> 81 7/subop/compare *ebp 1/imm32 +<span id="L68" class="LineNr">68 </span> 7f/jump-if-> <span class="Constant">break</span>/disp8 +<span id="L69" class="LineNr">69 </span> <span class="subxComment"># ebx = factorial(5)</span> +<span id="L70" class="LineNr">70 </span> (<a href='factorial4.subx.html#L25'>factorial</a> 5) <span class="subxComment"># => eax</span> +<span id="L71" class="LineNr">71 </span> 89/<- %ebx 0/r32/eax +<span id="L72" class="LineNr">72 </span> eb/jump $main:end/disp8 +<span id="L73" class="LineNr">73 </span> } +<span id="L74" class="LineNr">74 </span> <span class="subxComment"># otherwise if first arg is "test", then return run_tests()</span> +<span id="L75" class="LineNr">75 </span> { +<span id="L76" class="LineNr">76 </span> <span class="subxComment"># if (!kernel-string-equal?(argv[1], "test")) break</span> +<span id="L77" class="LineNr">77 </span> (<a href='../103kernel-string-equal.subx.html#L31'>kernel-string-equal?</a> *(ebp+8) <span class="Constant">"test"</span>) <span class="subxComment"># => eax</span> +<span id="L78" class="LineNr">78 </span> 3d/compare-eax-and 0/imm32/false +<span id="L79" class="LineNr">79 </span> 74/jump-if-= <span class="Constant">break</span>/disp8 +<span id="L80" class="LineNr">80 </span> <span class="subxComment">#</span> +<span id="L81" class="LineNr">81 </span> (run-tests) +<span id="L82" class="LineNr">82 </span> <span class="subxComment"># exit(*Num-test-failures)</span> +<span id="L83" class="LineNr">83 </span> 8b/-> *<span class="SpecialChar"><a href='../102test.subx.html#L89'>Num-test-failures</a></span> 3/r32/ebx +<span id="L84" class="LineNr">84 </span> eb/jump $main:end/disp8 +<span id="L85" class="LineNr">85 </span> } +<span id="L86" class="LineNr">86 </span> bb/copy-to-ebx 0/imm32 +<span id="L87" class="LineNr">87 </span><span class="Constant">$main:end</span>: +<span id="L88" class="LineNr">88 </span> e8/call <a href='../000init.subx.html#L18'>syscall_exit</a>/disp32 +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/hello.mu.html b/html/linux/apps/hello.mu.html new file mode 100644 index 00000000..b7033551 --- /dev/null +++ b/html/linux/apps/hello.mu.html @@ -0,0 +1,72 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/hello.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/hello.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/hello.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Meaningless conventional example.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/hello.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span> +<span id="L7" class="LineNr"> 7 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='hello.mu.html#L7'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L8" class="LineNr"> 8 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"Hello world!\n"</span> +<span id="L9" class="LineNr"> 9 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L10" class="LineNr">10 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/parse-int.mu.html b/html/linux/apps/parse-int.mu.html new file mode 100644 index 00000000..0ba861a7 --- /dev/null +++ b/html/linux/apps/parse-int.mu.html @@ -0,0 +1,114 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/parse-int.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/parse-int.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/parse-int.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># parse a decimal int at the commandline</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/parse-int.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf 123</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ echo $?</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># 123</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='parse-int.mu.html#L9'>main</a></span> _args: (addr array addr array byte)<span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span> <span class="muComment"># if no args, print a message and exit</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> args/<span class="muRegEsi">esi</span>: (addr array addr array byte) <span class="Special"><-</span> copy _args +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> n/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> length args +<span id="L13" class="LineNr">13 </span> compare n, <span class="Constant">1</span> +<span id="L14" class="LineNr">14 </span> <span class="Delimiter">{</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">break-if-></span> +<span id="L16" class="LineNr">16 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"usage: <a href='parse-int.mu.html#L25'>parse-int</a> <integer>\n"</span> +<span id="L17" class="LineNr">17 </span> <span class="PreProc">return</span> <span class="Constant">1</span> +<span id="L18" class="LineNr">18 </span> <span class="Delimiter">}</span> +<span id="L19" class="LineNr">19 </span> <span class="muComment"># otherwise parse the first arg as an integer</span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> in/<span class="muRegEcx">ecx</span>: (addr addr array byte) <span class="Special"><-</span> index args, <span class="Constant">1</span> +<span id="L21" class="LineNr">21 </span> <span class="PreProc">var</span> out/eax: int <span class="Special"><-</span> <a href='parse-int.mu.html#L25'>parse-int</a> *in +<span id="L22" class="LineNr">22 </span> <span class="PreProc">return</span> out +<span id="L23" class="LineNr">23 </span><span class="Delimiter">}</span> +<span id="L24" class="LineNr">24 </span> +<span id="L25" class="LineNr">25 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='parse-int.mu.html#L25'>parse-int</a></span> _in: (addr array byte)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L26" class="LineNr">26 </span> <span class="PreProc">var</span> in/<span class="muRegEsi">esi</span>: (addr array byte) <span class="Special"><-</span> copy _in +<span id="L27" class="LineNr">27 </span> <span class="PreProc">var</span> len/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> length in +<span id="L28" class="LineNr">28 </span> <span class="PreProc">var</span> i/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> result/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L30" class="LineNr">30 </span> <span class="Delimiter">{</span> +<span id="L31" class="LineNr">31 </span> compare i, len +<span id="L32" class="LineNr">32 </span> <span class="PreProc">break-if->=</span> +<span id="L33" class="LineNr">33 </span> <span class="muComment"># result *= 10</span> +<span id="L34" class="LineNr">34 </span> <span class="PreProc">var</span> ten/eax: int <span class="Special"><-</span> copy <span class="Constant">0xa</span> +<span id="L35" class="LineNr">35 </span> result <span class="Special"><-</span> multiply ten +<span id="L36" class="LineNr">36 </span> <span class="muComment"># c = in[i]</span> +<span id="L37" class="LineNr">37 </span> <span class="PreProc">var</span> tmp/<span class="muRegEbx">ebx</span>: (addr byte) <span class="Special"><-</span> index in, i +<span id="L38" class="LineNr">38 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> copy-byte *tmp +<span id="L39" class="LineNr">39 </span> <span class="muComment">#</span> +<span id="L40" class="LineNr">40 </span> <span class="PreProc">var</span> g/eax: grapheme <span class="Special"><-</span> copy c +<span id="L41" class="LineNr">41 </span> <span class="PreProc">var</span> digit/eax: int <span class="Special"><-</span> <a href='../126write-int-decimal.subx.html#L405'>to-decimal-digit</a> g +<span id="L42" class="LineNr">42 </span> result <span class="Special"><-</span> add digit +<span id="L43" class="LineNr">43 </span> i <span class="Special"><-</span> increment +<span id="L44" class="LineNr">44 </span> <span class="PreProc">loop</span> +<span id="L45" class="LineNr">45 </span> <span class="Delimiter">}</span> +<span id="L46" class="LineNr">46 </span> <span class="PreProc">return</span> result +<span id="L47" class="LineNr">47 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/print-file.mu.html b/html/linux/apps/print-file.mu.html new file mode 100644 index 00000000..21dfc9d2 --- /dev/null +++ b/html/linux/apps/print-file.mu.html @@ -0,0 +1,104 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/print-file.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/print-file.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/print-file.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># accept a filename on the commandline, read it and print it out to screen</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment"># only ascii right now, just like the rest of Mu</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># To run:</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./translate apps/print-file.mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ echo abc > x</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf x</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># abc</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='print-file.mu.html#L10'>main</a></span> _args: (addr array addr array byte)<span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> args/eax: (addr array addr array byte) <span class="Special"><-</span> copy _args +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> n/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> length args +<span id="L13" class="LineNr">13 </span> compare n, <span class="Constant">1</span> +<span id="L14" class="LineNr">14 </span> <span class="Delimiter">{</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">break-if-></span> +<span id="L16" class="LineNr">16 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"usage: cat <filename>\n"</span> +<span id="L17" class="LineNr">17 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L18" class="LineNr">18 </span> <span class="Delimiter">}</span> +<span id="L19" class="LineNr">19 </span> <span class="Delimiter">{</span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">break-if-<=</span> +<span id="L21" class="LineNr">21 </span> <span class="PreProc">var</span> filename/<span class="muRegEdx">edx</span>: (addr addr array byte) <span class="Special"><-</span> index args <span class="Constant">1</span> +<span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> in: (handle buffered-file) +<span id="L23" class="LineNr">23 </span> <span class="Delimiter">{</span> +<span id="L24" class="LineNr">24 </span> <span class="PreProc">var</span> addr-in/eax: (addr handle buffered-file) <span class="Special"><-</span> address in +<span id="L25" class="LineNr">25 </span> <a href='../306files.subx.html#L9'>open</a> *filename, <span class="Constant">0</span>/read-only, addr-in +<span id="L26" class="LineNr">26 </span> <span class="Delimiter">}</span> +<span id="L27" class="LineNr">27 </span> <span class="PreProc">var</span> _in-addr/eax: (addr buffered-file) <span class="Special"><-</span> <a href='../120allocate.subx.html#L258'>lookup</a> in +<span id="L28" class="LineNr">28 </span> <span class="PreProc">var</span> in-addr/<span class="muRegEcx">ecx</span>: (addr buffered-file) <span class="Special"><-</span> copy _in-addr +<span id="L29" class="LineNr">29 </span> <span class="Delimiter">{</span> +<span id="L30" class="LineNr">30 </span> <span class="PreProc">var</span> c/eax: byte <span class="Special"><-</span> <a href='../112read-byte.subx.html#L38'>read-byte-buffered</a> in-addr +<span id="L31" class="LineNr">31 </span> compare c, <span class="Constant">0xffffffff</span>/end-of-file +<span id="L32" class="LineNr">32 </span> <span class="PreProc">break-if-=</span> +<span id="L33" class="LineNr">33 </span> <span class="PreProc">var</span> g/eax: grapheme <span class="Special"><-</span> copy c +<span id="L34" class="LineNr">34 </span> <a href='../405screen.mu.html#L214'>print-grapheme</a> <span class="Constant">0</span>/screen, g +<span id="L35" class="LineNr">35 </span> <span class="PreProc">loop</span> +<span id="L36" class="LineNr">36 </span> <span class="Delimiter">}</span> +<span id="L37" class="LineNr">37 </span> <span class="Delimiter">}</span> +<span id="L38" class="LineNr">38 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L39" class="LineNr">39 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/1.mu.html b/html/linux/apps/raytracing/1.mu.html new file mode 100644 index 00000000..f7464218 --- /dev/null +++ b/html/linux/apps/raytracing/1.mu.html @@ -0,0 +1,95 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/1.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/1.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/1.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Listing 1 of <a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">https://raytracing.github.io/books/RayTracingInOneWeekend.html</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment"># (simplified)</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment">#</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># To run (on Linux):</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ cd mu/linux</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./translate apps/raytracing/1.mu</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># $ ./a.elf > 1.ppm</span> +<span id="L9" class="LineNr"> 9 </span> +<span id="L10" class="LineNr">10 </span><span class="PreProc">fn</span> <span class="muFunction">main</span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L11" class="LineNr">11 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"P3\n256 256\n255\n"</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> j/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L13" class="LineNr">13 </span> <span class="Delimiter">{</span> +<span id="L14" class="LineNr">14 </span> compare j, <span class="Constant">0</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">break-if-<</span> +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L17" class="LineNr">17 </span> <span class="Delimiter">{</span> +<span id="L18" class="LineNr">18 </span> compare i, <span class="Constant">0xff</span> +<span id="L19" class="LineNr">19 </span> <span class="PreProc">break-if-></span> +<span id="L20" class="LineNr">20 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, i +<span id="L21" class="LineNr">21 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" "</span> +<span id="L22" class="LineNr">22 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>, j +<span id="L23" class="LineNr">23 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">" 64\n"</span> +<span id="L24" class="LineNr">24 </span> i <span class="Special"><-</span> increment +<span id="L25" class="LineNr">25 </span> <span class="PreProc">loop</span> +<span id="L26" class="LineNr">26 </span> <span class="Delimiter">}</span> +<span id="L27" class="LineNr">27 </span> j <span class="Special"><-</span> decrement +<span id="L28" class="LineNr">28 </span> <span class="PreProc">loop</span> +<span id="L29" class="LineNr">29 </span> <span class="Delimiter">}</span> +<span id="L30" class="LineNr">30 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L31" class="LineNr">31 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/2.mu.html b/html/linux/apps/raytracing/2.mu.html new file mode 100644 index 00000000..6ae05c81 --- /dev/null +++ b/html/linux/apps/raytracing/2.mu.html @@ -0,0 +1,153 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/2.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEsi { color: #005faf; } +.Special { color: #ff6060; } +.muRegEdx { color: #af5f00; } +.LineNr { } +.CommentedCode { color: #8a8a8a; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEcx { color: #870000; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/2.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/2.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Listing 7 of <a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">https://raytracing.github.io/books/RayTracingInOneWeekend.html</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu/linux</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/raytracing/2.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf > 2.ppm</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction">main</span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"P3\n256 256\n255\n"</span> +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> _four/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">4</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> four/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> convert _four +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> one-fourth/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> reciprocal four +<span id="L14" class="LineNr">14 </span> <span class="PreProc">var</span> max/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> image-size/<span class="Constant">xmm2</span>: float <span class="Special"><-</span> convert max +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> j/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L17" class="LineNr">17 </span> <span class="Delimiter">{</span> +<span id="L18" class="LineNr">18 </span> compare j, <span class="Constant">0</span> +<span id="L19" class="LineNr">19 </span> <span class="PreProc">break-if-<</span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L21" class="LineNr">21 </span> <span class="Delimiter">{</span> +<span id="L22" class="LineNr">22 </span> compare i, <span class="Constant">0xff</span> +<span id="L23" class="LineNr">23 </span> <span class="PreProc">break-if-></span> +<span id="L24" class="LineNr">24 </span> <span class="PreProc">var</span> c: rgb +<span id="L25" class="LineNr">25 </span> <span class="muComment"># compute r</span> +<span id="L26" class="LineNr">26 </span> <span class="PreProc">var</span> tmp/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> convert i +<span id="L27" class="LineNr">27 </span> tmp <span class="Special"><-</span> divide image-size +<span id="L28" class="LineNr">28 </span> <span class="PreProc">var</span> r-addr/<span class="muRegEdx">edx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, r +<span id="L29" class="LineNr">29 </span> copy-to *r-addr, tmp +<span id="L30" class="LineNr">30 </span><span class="CommentedCode">#? var tmp2/ebx: int <- reinterpret *r-addr</span> +<span id="L31" class="LineNr">31 </span><span class="CommentedCode">#? print-int32-hex 0, tmp2</span> +<span id="L32" class="LineNr">32 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L33" class="LineNr">33 </span> <span class="muComment"># compute g</span> +<span id="L34" class="LineNr">34 </span> tmp <span class="Special"><-</span> convert j +<span id="L35" class="LineNr">35 </span> tmp <span class="Special"><-</span> divide image-size +<span id="L36" class="LineNr">36 </span> <span class="PreProc">var</span> g-addr/<span class="muRegEdx">edx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, g +<span id="L37" class="LineNr">37 </span> copy-to *g-addr, tmp +<span id="L38" class="LineNr">38 </span> <span class="muComment"># compute b</span> +<span id="L39" class="LineNr">39 </span> <span class="PreProc">var</span> b-addr/<span class="muRegEdx">edx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, b +<span id="L40" class="LineNr">40 </span> copy-to *b-addr, one-fourth +<span id="L41" class="LineNr">41 </span> <span class="muComment"># emit</span> +<span id="L42" class="LineNr">42 </span> <span class="PreProc">var</span> c-addr/<span class="muRegEdx">edx</span>: (addr rgb) <span class="Special"><-</span> address c +<span id="L43" class="LineNr">43 </span> print-rgb <span class="Constant">0</span>, c-addr +<span id="L44" class="LineNr">44 </span> i <span class="Special"><-</span> increment +<span id="L45" class="LineNr">45 </span> <span class="PreProc">loop</span> +<span id="L46" class="LineNr">46 </span> <span class="Delimiter">}</span> +<span id="L47" class="LineNr">47 </span> j <span class="Special"><-</span> decrement +<span id="L48" class="LineNr">48 </span> <span class="PreProc">loop</span> +<span id="L49" class="LineNr">49 </span> <span class="Delimiter">}</span> +<span id="L50" class="LineNr">50 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L51" class="LineNr">51 </span><span class="Delimiter">}</span> +<span id="L52" class="LineNr">52 </span> +<span id="L53" class="LineNr">53 </span><span class="PreProc">type</span> rgb <span class="Delimiter">{</span> +<span id="L54" class="LineNr">54 </span> <span class="muComment"># components normalized to within [0.0, 1.0]</span> +<span id="L55" class="LineNr">55 </span> r: float +<span id="L56" class="LineNr">56 </span> g: float +<span id="L57" class="LineNr">57 </span> b: float +<span id="L58" class="LineNr">58 </span><span class="Delimiter">}</span> +<span id="L59" class="LineNr">59 </span> +<span id="L60" class="LineNr">60 </span><span class="muComment"># print translating to [0, 256)</span> +<span id="L61" class="LineNr">61 </span><span class="PreProc">fn</span> <span class="muFunction">print-rgb</span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _c: (addr rgb) <span class="Delimiter">{</span> +<span id="L62" class="LineNr">62 </span> <span class="PreProc">var</span> c/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _c +<span id="L63" class="LineNr">63 </span> <span class="PreProc">var</span> n/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> <span class="muComment"># turns out 255 works just as well as 255.999, which is lucky because we don't have floating-point literals</span> +<span id="L64" class="LineNr">64 </span> <span class="PreProc">var</span> xn/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> convert n +<span id="L65" class="LineNr">65 </span> <span class="muComment"># print 255 * c->r</span> +<span id="L66" class="LineNr">66 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy xn +<span id="L67" class="LineNr">67 </span> <span class="PreProc">var</span> src-addr/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, r +<span id="L68" class="LineNr">68 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L69" class="LineNr">69 </span> <span class="PreProc">var</span> result-int/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> convert result +<span id="L70" class="LineNr">70 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L71" class="LineNr">71 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L72" class="LineNr">72 </span> <span class="muComment"># print 255 * c->g</span> +<span id="L73" class="LineNr">73 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, g +<span id="L74" class="LineNr">74 </span> result <span class="Special"><-</span> copy xn +<span id="L75" class="LineNr">75 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L76" class="LineNr">76 </span> result-int <span class="Special"><-</span> convert result +<span id="L77" class="LineNr">77 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L78" class="LineNr">78 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L79" class="LineNr">79 </span> <span class="muComment"># print 255 * c->b</span> +<span id="L80" class="LineNr">80 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, b +<span id="L81" class="LineNr">81 </span> result <span class="Special"><-</span> copy xn +<span id="L82" class="LineNr">82 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L83" class="LineNr">83 </span> result-int <span class="Special"><-</span> convert result +<span id="L84" class="LineNr">84 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L85" class="LineNr">85 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"\n"</span> +<span id="L86" class="LineNr">86 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/3.mu.html b/html/linux/apps/raytracing/3.mu.html new file mode 100644 index 00000000..c43df3a4 --- /dev/null +++ b/html/linux/apps/raytracing/3.mu.html @@ -0,0 +1,554 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/3.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.LineNr { } +.Delimiter { color: #c000c0; } +.muRegEcx { color: #870000; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.Special { color: #ff6060; } +.PreProc { color: #c000c0; } +.muFunction { color: #af5f00; text-decoration: underline; } +.muComment { color: #005faf; } +.muS1Comment { color: #0000af; } +.muS2Comment { color: #8a8a8a; } +.CommentedCode { color: #8a8a8a; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/3.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/3.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Listing 9 of <a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">https://raytracing.github.io/books/RayTracingInOneWeekend.html</a></span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu/linux</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/raytracing/3.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf > 3.ppm</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3.mu.html#L9'>ray-color</a></span> _in: (addr ray), _out: (addr rgb) <span class="Delimiter">{</span> +<span id="L10" class="LineNr"> 10 </span> <span class="PreProc">var</span> in/<span class="muRegEsi">esi</span>: (addr ray) <span class="Special"><-</span> copy _in +<span id="L11" class="LineNr"> 11 </span> <span class="PreProc">var</span> out/<span class="muRegEdi">edi</span>: (addr rgb) <span class="Special"><-</span> copy _out +<span id="L12" class="LineNr"> 12 </span> <span class="PreProc">var</span> dir/eax: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> in, dir +<span id="L13" class="LineNr"> 13 </span><span class="CommentedCode">#? print-string 0, "r.dir: "</span> +<span id="L14" class="LineNr"> 14 </span><span class="CommentedCode">#? print-vec3 0, dir</span> +<span id="L15" class="LineNr"> 15 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L16" class="LineNr"> 16 </span> <span class="PreProc">var</span> unit-storage: vec3 +<span id="L17" class="LineNr"> 17 </span> <span class="PreProc">var</span> unit/<span class="muRegEcx">ecx</span>: (addr vec3) <span class="Special"><-</span> address unit-storage +<span id="L18" class="LineNr"> 18 </span> vec3-unit dir, unit +<span id="L19" class="LineNr"> 19 </span><span class="CommentedCode">#? print-string 0, "r.dir normalized: "</span> +<span id="L20" class="LineNr"> 20 </span><span class="CommentedCode">#? print-vec3 0, unit</span> +<span id="L21" class="LineNr"> 21 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> y-addr/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> unit, y +<span id="L23" class="LineNr"> 23 </span> <span class="muComment"># t = (dir.y + 1.0) / 2.0</span> +<span id="L24" class="LineNr"> 24 </span> <span class="PreProc">var</span> t/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *y-addr +<span id="L25" class="LineNr"> 25 </span> <span class="PreProc">var</span> one/eax: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L26" class="LineNr"> 26 </span> <span class="PreProc">var</span> one-f/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> convert one +<span id="L27" class="LineNr"> 27 </span> t <span class="Special"><-</span> add one-f +<span id="L28" class="LineNr"> 28 </span> <span class="PreProc">var</span> two/eax: int <span class="Special"><-</span> copy <span class="Constant">2</span> +<span id="L29" class="LineNr"> 29 </span> <span class="PreProc">var</span> two-f/<span class="Constant">xmm2</span>: float <span class="Special"><-</span> convert two +<span id="L30" class="LineNr"> 30 </span> t <span class="Special"><-</span> divide two-f +<span id="L31" class="LineNr"> 31 </span><span class="CommentedCode">#? print-string 0, "t: "</span> +<span id="L32" class="LineNr"> 32 </span><span class="CommentedCode">#? print-float-hex 0, t</span> +<span id="L33" class="LineNr"> 33 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L34" class="LineNr"> 34 </span> <span class="muComment"># whitening = (1.0 - t) * white</span> +<span id="L35" class="LineNr"> 35 </span> <span class="PreProc">var</span> whitening-storage: rgb +<span id="L36" class="LineNr"> 36 </span> <span class="PreProc">var</span> whitening/<span class="muRegEcx">ecx</span>: (addr rgb) <span class="Special"><-</span> address whitening-storage +<span id="L37" class="LineNr"> 37 </span> <a href='3.mu.html#L302'>rgb-white</a> whitening +<span id="L38" class="LineNr"> 38 </span> <span class="PreProc">var</span> one-minus-t/<span class="Constant">xmm3</span>: float <span class="Special"><-</span> copy one-f +<span id="L39" class="LineNr"> 39 </span> one-minus-t <span class="Special"><-</span> subtract t +<span id="L40" class="LineNr"> 40 </span> <a href='3.mu.html#L337'>rgb-scale-up</a> whitening, one-minus-t +<span id="L41" class="LineNr"> 41 </span><span class="CommentedCode">#? print-string 0, "whitening: "</span> +<span id="L42" class="LineNr"> 42 </span><span class="CommentedCode">#? print-rgb-raw 0, whitening</span> +<span id="L43" class="LineNr"> 43 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L44" class="LineNr"> 44 </span> <span class="muComment"># out = t * (0.5, 0.7, 1.0)</span> +<span id="L45" class="LineNr"> 45 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> out, r +<span id="L46" class="LineNr"> 46 </span> <a href='../../408float.mu.html#L3'>fill-in-rational</a> dest, <span class="Constant">5</span>, <span class="Constant">0xa</span> +<span id="L47" class="LineNr"> 47 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> out, g +<span id="L48" class="LineNr"> 48 </span> <a href='../../408float.mu.html#L3'>fill-in-rational</a> dest, <span class="Constant">7</span>, <span class="Constant">0xa</span> +<span id="L49" class="LineNr"> 49 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> out, b +<span id="L50" class="LineNr"> 50 </span> copy-to *dest, one-f +<span id="L51" class="LineNr"> 51 </span> <a href='3.mu.html#L337'>rgb-scale-up</a> out, t +<span id="L52" class="LineNr"> 52 </span><span class="CommentedCode">#? print-string 0, "base: "</span> +<span id="L53" class="LineNr"> 53 </span><span class="CommentedCode">#? print-rgb-raw 0, out</span> +<span id="L54" class="LineNr"> 54 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L55" class="LineNr"> 55 </span> <span class="muComment"># blend with whitening</span> +<span id="L56" class="LineNr"> 56 </span> <a href='3.mu.html#L314'>rgb-add-to</a> out, whitening +<span id="L57" class="LineNr"> 57 </span><span class="CommentedCode">#? print-string 0, "result: "</span> +<span id="L58" class="LineNr"> 58 </span><span class="CommentedCode">#? print-rgb-raw 0, out</span> +<span id="L59" class="LineNr"> 59 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L60" class="LineNr"> 60 </span><span class="Delimiter">}</span> +<span id="L61" class="LineNr"> 61 </span> +<span id="L62" class="LineNr"> 62 </span><span class="PreProc">fn</span> <span class="muFunction">main</span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L63" class="LineNr"> 63 </span> +<span id="L64" class="LineNr"> 64 </span> <span class="muComment"># image</span> +<span id="L65" class="LineNr"> 65 </span> <span class="muComment"># width = 400</span> +<span id="L66" class="LineNr"> 66 </span> <span class="muComment"># height = 400 * 9/16 = 225</span> +<span id="L67" class="LineNr"> 67 </span> <span class="PreProc">var</span> aspect: float +<span id="L68" class="LineNr"> 68 </span> <span class="PreProc">var</span> aspect-addr/eax: (addr float) <span class="Special"><-</span> address aspect +<span id="L69" class="LineNr"> 69 </span> <a href='../../408float.mu.html#L3'>fill-in-rational</a> aspect-addr, <span class="Constant">0x10</span>, <span class="Constant">9</span> <span class="muComment"># 16/9</span> +<span id="L70" class="LineNr"> 70 </span><span class="CommentedCode">#? print-string 0, "aspect ratio: "</span> +<span id="L71" class="LineNr"> 71 </span><span class="CommentedCode">#? print-float-hex 0, aspect</span> +<span id="L72" class="LineNr"> 72 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L73" class="LineNr"> 73 </span><span class="CommentedCode">#? {</span> +<span id="L74" class="LineNr"> 74 </span><span class="CommentedCode">#? var foo2/ebx: int <- reinterpret aspect</span> +<span id="L75" class="LineNr"> 75 </span><span class="CommentedCode">#? print-int32-hex 0, foo2</span> +<span id="L76" class="LineNr"> 76 </span><span class="CommentedCode">#? }</span> +<span id="L77" class="LineNr"> 77 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L78" class="LineNr"> 78 </span> +<span id="L79" class="LineNr"> 79 </span> <span class="muComment"># camera</span> +<span id="L80" class="LineNr"> 80 </span> +<span id="L81" class="LineNr"> 81 </span> <span class="muComment"># viewport-height = 2.0</span> +<span id="L82" class="LineNr"> 82 </span> <span class="PreProc">var</span> tmp/eax: int <span class="Special"><-</span> copy <span class="Constant">2</span> +<span id="L83" class="LineNr"> 83 </span> <span class="PreProc">var</span> two-f/<span class="Constant">xmm4</span>: float <span class="Special"><-</span> convert tmp +<span id="L84" class="LineNr"> 84 </span> <span class="PreProc">var</span> viewport-height/<span class="Constant">xmm7</span>: float <span class="Special"><-</span> copy two-f +<span id="L85" class="LineNr"> 85 </span><span class="CommentedCode">#? print-string 0, "viewport height: "</span> +<span id="L86" class="LineNr"> 86 </span><span class="CommentedCode">#? print-float-hex 0, viewport-height</span> +<span id="L87" class="LineNr"> 87 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L88" class="LineNr"> 88 </span><span class="CommentedCode">#? {</span> +<span id="L89" class="LineNr"> 89 </span><span class="CommentedCode">#? var foo: float</span> +<span id="L90" class="LineNr"> 90 </span><span class="CommentedCode">#? copy-to foo, viewport-height</span> +<span id="L91" class="LineNr"> 91 </span><span class="CommentedCode">#? var foo2/ebx: int <- reinterpret foo</span> +<span id="L92" class="LineNr"> 92 </span><span class="CommentedCode">#? print-int32-hex 0, foo2</span> +<span id="L93" class="LineNr"> 93 </span><span class="CommentedCode">#? }</span> +<span id="L94" class="LineNr"> 94 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L95" class="LineNr"> 95 </span> <span class="muComment"># viewport-width = aspect * viewport-height</span> +<span id="L96" class="LineNr"> 96 </span> <span class="PreProc">var</span> viewport-width/<span class="Constant">xmm6</span>: float <span class="Special"><-</span> convert tmp +<span id="L97" class="LineNr"> 97 </span> viewport-width <span class="Special"><-</span> multiply aspect +<span id="L98" class="LineNr"> 98 </span><span class="CommentedCode">#? print-string 0, "viewport width: "</span> +<span id="L99" class="LineNr"> 99 </span><span class="CommentedCode">#? print-float-hex 0, viewport-width</span> +<span id="L100" class="LineNr">100 </span><span class="CommentedCode">#? print-string 0, " "</span> +<span id="L101" class="LineNr">101 </span><span class="CommentedCode">#? {</span> +<span id="L102" class="LineNr">102 </span><span class="CommentedCode">#? var foo: float</span> +<span id="L103" class="LineNr">103 </span><span class="CommentedCode">#? copy-to foo, viewport-width</span> +<span id="L104" class="LineNr">104 </span><span class="CommentedCode">#? var foo2/ebx: int <- reinterpret foo</span> +<span id="L105" class="LineNr">105 </span><span class="CommentedCode">#? print-int32-hex 0, foo2</span> +<span id="L106" class="LineNr">106 </span><span class="CommentedCode">#? }</span> +<span id="L107" class="LineNr">107 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L108" class="LineNr">108 </span> <span class="muComment"># focal-length = 1.0</span> +<span id="L109" class="LineNr">109 </span> tmp <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L110" class="LineNr">110 </span> <span class="PreProc">var</span> focal-length/<span class="Constant">xmm5</span>: float <span class="Special"><-</span> convert tmp +<span id="L111" class="LineNr">111 </span> +<span id="L112" class="LineNr">112 </span> <span class="muComment"># origin = point3(0, 0, 0)</span> +<span id="L113" class="LineNr">113 </span> <span class="PreProc">var</span> origin-storage: vec3 +<span id="L114" class="LineNr">114 </span> <span class="PreProc">var</span> origin/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> address origin-storage +<span id="L115" class="LineNr">115 </span> <span class="muComment"># horizontal = vec3(viewport-width, 0, 0)</span> +<span id="L116" class="LineNr">116 </span> <span class="PreProc">var</span> horizontal-storage: vec3 +<span id="L117" class="LineNr">117 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> horizontal-storage, x +<span id="L118" class="LineNr">118 </span> copy-to *dest, viewport-width +<span id="L119" class="LineNr">119 </span> <span class="PreProc">var</span> horizontal/<span class="muRegEbx">ebx</span>: (addr vec3) <span class="Special"><-</span> address horizontal-storage +<span id="L120" class="LineNr">120 </span> <span class="muComment"># vertical = vec3(0, viewport-height, 0)</span> +<span id="L121" class="LineNr">121 </span> <span class="PreProc">var</span> vertical-storage: vec3 +<span id="L122" class="LineNr">122 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> vertical-storage, y +<span id="L123" class="LineNr">123 </span> copy-to *dest, viewport-height +<span id="L124" class="LineNr">124 </span> <span class="PreProc">var</span> vertical/<span class="muRegEdx">edx</span>: (addr vec3) <span class="Special"><-</span> address vertical-storage +<span id="L125" class="LineNr">125 </span> <span class="muComment"># lower-left-corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal-length)</span> +<span id="L126" class="LineNr">126 </span> <span class="muS1Comment"># . lower-left-corner = origin</span> +<span id="L127" class="LineNr">127 </span> <span class="PreProc">var</span> lower-left-corner-storage: vec3 +<span id="L128" class="LineNr">128 </span> <span class="PreProc">var</span> lower-left-corner/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> address lower-left-corner-storage +<span id="L129" class="LineNr">129 </span> copy-object origin, lower-left-corner +<span id="L130" class="LineNr">130 </span> <span class="muS1Comment"># . lower-left-corner -= horizontal/2</span> +<span id="L131" class="LineNr">131 </span> <span class="PreProc">var</span> tmp2: vec3 +<span id="L132" class="LineNr">132 </span> <span class="PreProc">var</span> tmp2-addr/eax: (addr vec3) <span class="Special"><-</span> address tmp2 +<span id="L133" class="LineNr">133 </span> copy-object horizontal, tmp2-addr +<span id="L134" class="LineNr">134 </span> vec3-scale-down tmp2-addr, two-f +<span id="L135" class="LineNr">135 </span> vec3-subtract-from lower-left-corner, tmp2-addr +<span id="L136" class="LineNr">136 </span> <span class="muS1Comment"># . lower-left-corner -= vertical/2</span> +<span id="L137" class="LineNr">137 </span> copy-object vertical, tmp2-addr +<span id="L138" class="LineNr">138 </span> vec3-scale-down tmp2-addr, two-f +<span id="L139" class="LineNr">139 </span> vec3-subtract-from lower-left-corner, tmp2-addr +<span id="L140" class="LineNr">140 </span> <span class="muS1Comment"># . lower-left-corner -= vec3(0, 0, focal-length)</span> +<span id="L141" class="LineNr">141 </span> <span class="PreProc">var</span> dest2/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> lower-left-corner, z +<span id="L142" class="LineNr">142 </span> <span class="PreProc">var</span> tmp3/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest2 +<span id="L143" class="LineNr">143 </span> tmp3 <span class="Special"><-</span> subtract focal-length +<span id="L144" class="LineNr">144 </span> copy-to *dest2, tmp3 +<span id="L145" class="LineNr">145 </span> <span class="muComment"># phew!</span> +<span id="L146" class="LineNr">146 </span> +<span id="L147" class="LineNr">147 </span> <span class="muComment"># render</span> +<span id="L148" class="LineNr">148 </span> +<span id="L149" class="LineNr">149 </span> <span class="muComment"># live variables at this point:</span> +<span id="L150" class="LineNr">150 </span> <span class="muComment"># origin (edi)</span> +<span id="L151" class="LineNr">151 </span> <span class="muComment"># lower-left-corner (esi)</span> +<span id="L152" class="LineNr">152 </span> <span class="muComment"># horizontal (ebx)</span> +<span id="L153" class="LineNr">153 </span> <span class="muComment"># vertical (edx)</span> +<span id="L154" class="LineNr">154 </span> <span class="muComment"># floating-point registers are all free</span> +<span id="L155" class="LineNr">155 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>, <span class="Constant">"P3\n400 225\n255\n"</span> <span class="muComment"># 225 = image height</span> +<span id="L156" class="LineNr">156 </span> <span class="PreProc">var</span> tmp/eax: int <span class="Special"><-</span> copy <span class="Constant">0x18f</span> <span class="muComment"># image width - 1</span> +<span id="L157" class="LineNr">157 </span> <span class="PreProc">var</span> image-width-1/<span class="Constant">xmm7</span>: float <span class="Special"><-</span> convert tmp +<span id="L158" class="LineNr">158 </span> tmp <span class="Special"><-</span> copy <span class="Constant">0xe0</span> <span class="muComment"># image height - 1</span> +<span id="L159" class="LineNr">159 </span> <span class="PreProc">var</span> image-height-1/<span class="Constant">xmm6</span>: float <span class="Special"><-</span> convert tmp +<span id="L160" class="LineNr">160 </span> <span class="muComment">#</span> +<span id="L161" class="LineNr">161 </span> <span class="PreProc">var</span> j/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0xe0</span> <span class="muComment"># 224</span> +<span id="L162" class="LineNr">162 </span> <span class="Delimiter">{</span> +<span id="L163" class="LineNr">163 </span> compare j, <span class="Constant">0</span> +<span id="L164" class="LineNr">164 </span> <span class="PreProc">break-if-<</span> +<span id="L165" class="LineNr">165 </span> <span class="PreProc">var</span> i/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L166" class="LineNr">166 </span> <span class="Delimiter">{</span> +<span id="L167" class="LineNr">167 </span> compare i, <span class="Constant">0x190</span> <span class="muComment"># 400 = image width</span> +<span id="L168" class="LineNr">168 </span> <span class="PreProc">break-if->=</span> +<span id="L169" class="LineNr">169 </span> <span class="muComment"># u = i / (image-width - 1)</span> +<span id="L170" class="LineNr">170 </span> <span class="PreProc">var</span> u/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> convert i +<span id="L171" class="LineNr">171 </span> u <span class="Special"><-</span> divide image-width-1 +<span id="L172" class="LineNr">172 </span><span class="CommentedCode">#? print-string 0, "u: "</span> +<span id="L173" class="LineNr">173 </span><span class="CommentedCode">#? print-float-hex 0, u</span> +<span id="L174" class="LineNr">174 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L175" class="LineNr">175 </span> <span class="muComment"># v = j / (image-height - 1)</span> +<span id="L176" class="LineNr">176 </span> <span class="PreProc">var</span> v/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> convert j +<span id="L177" class="LineNr">177 </span> v <span class="Special"><-</span> divide image-height-1 +<span id="L178" class="LineNr">178 </span> <span class="muComment"># r = ray(origin, lower-left-corner + u*horizontal + v*vertical - origin)</span> +<span id="L179" class="LineNr">179 </span> <span class="PreProc">var</span> r-storage: ray +<span id="L180" class="LineNr">180 </span> <span class="muS2Comment"># . . we're running out of int registers now,</span> +<span id="L181" class="LineNr">181 </span> <span class="muS2Comment"># . . but luckily we don't need i and j in the rest of this loop iteration,</span> +<span id="L182" class="LineNr">182 </span> <span class="muS2Comment"># . . so we'll just spill them in a block</span> +<span id="L183" class="LineNr">183 </span> <span class="Delimiter">{</span> +<span id="L184" class="LineNr">184 </span> <span class="muS1Comment"># . r.orig = origin</span> +<span id="L185" class="LineNr">185 </span> <span class="PreProc">var</span> r/eax: (addr ray) <span class="Special"><-</span> address r-storage +<span id="L186" class="LineNr">186 </span> <span class="PreProc">var</span> dest/<span class="muRegEcx">ecx</span>: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> r, orig +<span id="L187" class="LineNr">187 </span> copy-object origin, dest +<span id="L188" class="LineNr">188 </span> <span class="muS1Comment"># . r.dir = lower-left-corner</span> +<span id="L189" class="LineNr">189 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> r, dir +<span id="L190" class="LineNr">190 </span> copy-object lower-left-corner, dest +<span id="L191" class="LineNr">191 </span> <span class="muS1Comment"># . r.dir += horizontal*u</span> +<span id="L192" class="LineNr">192 </span> <span class="PreProc">var</span> tmp-vec3: vec3 +<span id="L193" class="LineNr">193 </span> <span class="PreProc">var</span> tmp/eax: (addr vec3) <span class="Special"><-</span> address tmp-vec3 +<span id="L194" class="LineNr">194 </span> copy-object horizontal, tmp +<span id="L195" class="LineNr">195 </span> vec3-scale-up tmp, u +<span id="L196" class="LineNr">196 </span> vec3-add-to dest, tmp +<span id="L197" class="LineNr">197 </span> <span class="muS1Comment"># . r.dir += vertical*v</span> +<span id="L198" class="LineNr">198 </span> copy-object vertical, tmp +<span id="L199" class="LineNr">199 </span> vec3-scale-up tmp, v +<span id="L200" class="LineNr">200 </span> vec3-add-to dest, tmp +<span id="L201" class="LineNr">201 </span> <span class="muS1Comment"># . r.dir -= origin</span> +<span id="L202" class="LineNr">202 </span> vec3-subtract-from dest, origin +<span id="L203" class="LineNr">203 </span><span class="CommentedCode">#? print-string 0, "ray direction: "</span> +<span id="L204" class="LineNr">204 </span><span class="CommentedCode">#? print-vec3 0, dest</span> +<span id="L205" class="LineNr">205 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L206" class="LineNr">206 </span> <span class="Delimiter">}</span> +<span id="L207" class="LineNr">207 </span> <span class="muComment"># pixel-color = ray-color(r)</span> +<span id="L208" class="LineNr">208 </span> <span class="PreProc">var</span> c-storage: rgb +<span id="L209" class="LineNr">209 </span> <span class="PreProc">var</span> c/<span class="muRegEcx">ecx</span>: (addr rgb) <span class="Special"><-</span> address c-storage +<span id="L210" class="LineNr">210 </span> <span class="Delimiter">{</span> +<span id="L211" class="LineNr">211 </span> <span class="PreProc">var</span> r/eax: (addr ray) <span class="Special"><-</span> address r-storage +<span id="L212" class="LineNr">212 </span> <a href='3.mu.html#L9'>ray-color</a> r, c +<span id="L213" class="LineNr">213 </span> <span class="muComment"># write color</span> +<span id="L214" class="LineNr">214 </span> print-rgb <span class="Constant">0</span>, c +<span id="L215" class="LineNr">215 </span><span class="CommentedCode">#? print-rgb-raw 0, c</span> +<span id="L216" class="LineNr">216 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L217" class="LineNr">217 </span> <span class="Delimiter">}</span> +<span id="L218" class="LineNr">218 </span> i <span class="Special"><-</span> increment +<span id="L219" class="LineNr">219 </span> <span class="PreProc">loop</span> +<span id="L220" class="LineNr">220 </span> <span class="Delimiter">}</span> +<span id="L221" class="LineNr">221 </span> j <span class="Special"><-</span> decrement +<span id="L222" class="LineNr">222 </span> <span class="PreProc">loop</span> +<span id="L223" class="LineNr">223 </span> <span class="Delimiter">}</span> +<span id="L224" class="LineNr">224 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L225" class="LineNr">225 </span><span class="Delimiter">}</span> +<span id="L226" class="LineNr">226 </span> +<span id="L227" class="LineNr">227 </span><span class="PreProc">type</span> ray <span class="Delimiter">{</span> +<span id="L228" class="LineNr">228 </span> orig: vec3 <span class="muComment"># point</span> +<span id="L229" class="LineNr">229 </span> dir: vec3 +<span id="L230" class="LineNr">230 </span><span class="Delimiter">}</span> +<span id="L231" class="LineNr">231 </span> +<span id="L232" class="LineNr">232 </span><span class="muComment"># A little different from the constructor at <a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">https://raytracing.github.io/books/RayTracingInOneWeekend.html</a></span> +<span id="L233" class="LineNr">233 </span><span class="muComment"># We immediately normalize the direction vector so we don't have to keep doing</span> +<span id="L234" class="LineNr">234 </span><span class="muComment"># so.</span> +<span id="L235" class="LineNr">235 </span><span class="PreProc">fn</span> <span class="muFunction">initialize-ray</span> _self: (addr ray), o: (addr vec3), d: (addr vec3) <span class="Delimiter">{</span> +<span id="L236" class="LineNr">236 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr ray) <span class="Special"><-</span> copy _self +<span id="L237" class="LineNr">237 </span> <span class="PreProc">var</span> dest/eax: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, orig +<span id="L238" class="LineNr">238 </span> copy-object o, dest +<span id="L239" class="LineNr">239 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, dir +<span id="L240" class="LineNr">240 </span> vec3-unit d, dest +<span id="L241" class="LineNr">241 </span><span class="Delimiter">}</span> +<span id="L242" class="LineNr">242 </span> +<span id="L243" class="LineNr">243 </span><span class="PreProc">fn</span> <span class="muFunction">ray-at</span> _self: (addr ray), t: float, out: (addr vec3) <span class="Delimiter">{</span> +<span id="L244" class="LineNr">244 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr ray) <span class="Special"><-</span> copy _self +<span id="L245" class="LineNr">245 </span> <span class="PreProc">var</span> src/eax: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, dir +<span id="L246" class="LineNr">246 </span> copy-object src, out +<span id="L247" class="LineNr">247 </span> vec3-scale-up out, t +<span id="L248" class="LineNr">248 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, orig +<span id="L249" class="LineNr">249 </span> vec3-add-to out, src +<span id="L250" class="LineNr">250 </span><span class="Delimiter">}</span> +<span id="L251" class="LineNr">251 </span> +<span id="L252" class="LineNr">252 </span><span class="PreProc">type</span> rgb <span class="Delimiter">{</span> +<span id="L253" class="LineNr">253 </span> <span class="muComment"># components normalized to within [0.0, 1.0]</span> +<span id="L254" class="LineNr">254 </span> r: float +<span id="L255" class="LineNr">255 </span> g: float +<span id="L256" class="LineNr">256 </span> b: float +<span id="L257" class="LineNr">257 </span><span class="Delimiter">}</span> +<span id="L258" class="LineNr">258 </span> +<span id="L259" class="LineNr">259 </span><span class="muComment"># print translating to [0, 256)</span> +<span id="L260" class="LineNr">260 </span><span class="PreProc">fn</span> <span class="muFunction">print-rgb</span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _c: (addr rgb) <span class="Delimiter">{</span> +<span id="L261" class="LineNr">261 </span> <span class="PreProc">var</span> c/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _c +<span id="L262" class="LineNr">262 </span> <span class="PreProc">var</span> xn: float +<span id="L263" class="LineNr">263 </span> <span class="PreProc">var</span> xn-addr/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> address xn +<span id="L264" class="LineNr">264 </span> <a href='../../408float.mu.html#L3'>fill-in-rational</a> xn-addr, <span class="Constant">0x3e7ff</span>, <span class="Constant">0x3e8</span> <span class="muComment"># 255999 / 1000</span> +<span id="L265" class="LineNr">265 </span> <span class="muComment"># print 255.999 * c->r</span> +<span id="L266" class="LineNr">266 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy xn +<span id="L267" class="LineNr">267 </span> <span class="PreProc">var</span> src-addr/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, r +<span id="L268" class="LineNr">268 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L269" class="LineNr">269 </span> <span class="PreProc">var</span> result-int/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> truncate result +<span id="L270" class="LineNr">270 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L271" class="LineNr">271 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L272" class="LineNr">272 </span> <span class="muComment"># print 255.999 * c->g</span> +<span id="L273" class="LineNr">273 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, g +<span id="L274" class="LineNr">274 </span> result <span class="Special"><-</span> copy xn +<span id="L275" class="LineNr">275 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L276" class="LineNr">276 </span> result-int <span class="Special"><-</span> truncate result +<span id="L277" class="LineNr">277 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L278" class="LineNr">278 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L279" class="LineNr">279 </span> <span class="muComment"># print 255.999 * c->b</span> +<span id="L280" class="LineNr">280 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, b +<span id="L281" class="LineNr">281 </span> result <span class="Special"><-</span> copy xn +<span id="L282" class="LineNr">282 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L283" class="LineNr">283 </span> result-int <span class="Special"><-</span> truncate result +<span id="L284" class="LineNr">284 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L285" class="LineNr">285 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"\n"</span> +<span id="L286" class="LineNr">286 </span><span class="Delimiter">}</span> +<span id="L287" class="LineNr">287 </span> +<span id="L288" class="LineNr">288 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3.mu.html#L288'>print-rgb-raw</a></span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _v: (addr rgb) <span class="Delimiter">{</span> +<span id="L289" class="LineNr">289 </span> <span class="PreProc">var</span> v/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _v +<span id="L290" class="LineNr">290 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"("</span> +<span id="L291" class="LineNr">291 </span> <span class="PreProc">var</span> tmp/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, r +<span id="L292" class="LineNr">292 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L293" class="LineNr">293 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L294" class="LineNr">294 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, g +<span id="L295" class="LineNr">295 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L296" class="LineNr">296 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L297" class="LineNr">297 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, b +<span id="L298" class="LineNr">298 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L299" class="LineNr">299 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">")"</span> +<span id="L300" class="LineNr">300 </span><span class="Delimiter">}</span> +<span id="L301" class="LineNr">301 </span> +<span id="L302" class="LineNr">302 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3.mu.html#L302'>rgb-white</a></span> _c: (addr rgb) <span class="Delimiter">{</span> +<span id="L303" class="LineNr">303 </span> <span class="PreProc">var</span> c/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _c +<span id="L304" class="LineNr">304 </span> <span class="PreProc">var</span> one/eax: int <span class="Special"><-</span> copy <span class="Constant">1</span> +<span id="L305" class="LineNr">305 </span> <span class="PreProc">var</span> one-f/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> convert one +<span id="L306" class="LineNr">306 </span> <span class="PreProc">var</span> dest/<span class="muRegEdi">edi</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, r +<span id="L307" class="LineNr">307 </span> copy-to *dest, one-f +<span id="L308" class="LineNr">308 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, g +<span id="L309" class="LineNr">309 </span> copy-to *dest, one-f +<span id="L310" class="LineNr">310 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, b +<span id="L311" class="LineNr">311 </span> copy-to *dest, one-f +<span id="L312" class="LineNr">312 </span><span class="Delimiter">}</span> +<span id="L313" class="LineNr">313 </span> +<span id="L314" class="LineNr">314 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3.mu.html#L314'>rgb-add-to</a></span> _c1: (addr rgb), _c2: (addr rgb) <span class="Delimiter">{</span> +<span id="L315" class="LineNr">315 </span> <span class="PreProc">var</span> c1/<span class="muRegEdi">edi</span>: (addr rgb) <span class="Special"><-</span> copy _c1 +<span id="L316" class="LineNr">316 </span> <span class="PreProc">var</span> c2/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _c2 +<span id="L317" class="LineNr">317 </span> <span class="muComment"># c1.r += c2.r</span> +<span id="L318" class="LineNr">318 </span> <span class="PreProc">var</span> arg1/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, r +<span id="L319" class="LineNr">319 </span> <span class="PreProc">var</span> arg2/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c2, r +<span id="L320" class="LineNr">320 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *arg1 +<span id="L321" class="LineNr">321 </span> result <span class="Special"><-</span> add *arg2 +<span id="L322" class="LineNr">322 </span> copy-to *arg1, result +<span id="L323" class="LineNr">323 </span> <span class="muComment"># c1.g += c2.g</span> +<span id="L324" class="LineNr">324 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, g +<span id="L325" class="LineNr">325 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c2, g +<span id="L326" class="LineNr">326 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L327" class="LineNr">327 </span> result <span class="Special"><-</span> add *arg2 +<span id="L328" class="LineNr">328 </span> copy-to *arg1, result +<span id="L329" class="LineNr">329 </span> <span class="muComment"># c1.b += c2.b</span> +<span id="L330" class="LineNr">330 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, b +<span id="L331" class="LineNr">331 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c2, b +<span id="L332" class="LineNr">332 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L333" class="LineNr">333 </span> result <span class="Special"><-</span> add *arg2 +<span id="L334" class="LineNr">334 </span> copy-to *arg1, result +<span id="L335" class="LineNr">335 </span><span class="Delimiter">}</span> +<span id="L336" class="LineNr">336 </span> +<span id="L337" class="LineNr">337 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='3.mu.html#L337'>rgb-scale-up</a></span> _c1: (addr rgb), f: float <span class="Delimiter">{</span> +<span id="L338" class="LineNr">338 </span> <span class="PreProc">var</span> c1/<span class="muRegEdi">edi</span>: (addr rgb) <span class="Special"><-</span> copy _c1 +<span id="L339" class="LineNr">339 </span> <span class="muComment"># c1.r *= f</span> +<span id="L340" class="LineNr">340 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, r +<span id="L341" class="LineNr">341 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest +<span id="L342" class="LineNr">342 </span> result <span class="Special"><-</span> multiply f +<span id="L343" class="LineNr">343 </span> copy-to *dest, result +<span id="L344" class="LineNr">344 </span> <span class="muComment"># c1.g *= f</span> +<span id="L345" class="LineNr">345 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, g +<span id="L346" class="LineNr">346 </span> result <span class="Special"><-</span> copy *dest +<span id="L347" class="LineNr">347 </span> result <span class="Special"><-</span> multiply f +<span id="L348" class="LineNr">348 </span> copy-to *dest, result +<span id="L349" class="LineNr">349 </span> <span class="muComment"># c1.b *= f</span> +<span id="L350" class="LineNr">350 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c1, b +<span id="L351" class="LineNr">351 </span> result <span class="Special"><-</span> copy *dest +<span id="L352" class="LineNr">352 </span> result <span class="Special"><-</span> multiply f +<span id="L353" class="LineNr">353 </span> copy-to *dest, result +<span id="L354" class="LineNr">354 </span><span class="Delimiter">}</span> +<span id="L355" class="LineNr">355 </span> +<span id="L356" class="LineNr">356 </span><span class="PreProc">type</span> vec3 <span class="Delimiter">{</span> +<span id="L357" class="LineNr">357 </span> x: float +<span id="L358" class="LineNr">358 </span> y: float +<span id="L359" class="LineNr">359 </span> z: float +<span id="L360" class="LineNr">360 </span><span class="Delimiter">}</span> +<span id="L361" class="LineNr">361 </span> +<span id="L362" class="LineNr">362 </span><span class="PreProc">fn</span> <span class="muFunction">print-vec3</span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _v: (addr vec3) <span class="Delimiter">{</span> +<span id="L363" class="LineNr">363 </span> <span class="PreProc">var</span> v/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L364" class="LineNr">364 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"("</span> +<span id="L365" class="LineNr">365 </span> <span class="PreProc">var</span> tmp/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L366" class="LineNr">366 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L367" class="LineNr">367 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L368" class="LineNr">368 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L369" class="LineNr">369 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L370" class="LineNr">370 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L371" class="LineNr">371 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L372" class="LineNr">372 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L373" class="LineNr">373 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">")"</span> +<span id="L374" class="LineNr">374 </span><span class="Delimiter">}</span> +<span id="L375" class="LineNr">375 </span> +<span id="L376" class="LineNr">376 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-add-to</span> _v1: (addr vec3), _v2: (addr vec3) <span class="Delimiter">{</span> +<span id="L377" class="LineNr">377 </span> <span class="PreProc">var</span> v1/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v1 +<span id="L378" class="LineNr">378 </span> <span class="PreProc">var</span> v2/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v2 +<span id="L379" class="LineNr">379 </span> <span class="muComment"># v1.x += v2.x</span> +<span id="L380" class="LineNr">380 </span> <span class="PreProc">var</span> arg1/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, x +<span id="L381" class="LineNr">381 </span> <span class="PreProc">var</span> arg2/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, x +<span id="L382" class="LineNr">382 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *arg1 +<span id="L383" class="LineNr">383 </span> result <span class="Special"><-</span> add *arg2 +<span id="L384" class="LineNr">384 </span> copy-to *arg1, result +<span id="L385" class="LineNr">385 </span> <span class="muComment"># v1.y += v2.y</span> +<span id="L386" class="LineNr">386 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, y +<span id="L387" class="LineNr">387 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, y +<span id="L388" class="LineNr">388 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L389" class="LineNr">389 </span> result <span class="Special"><-</span> add *arg2 +<span id="L390" class="LineNr">390 </span> copy-to *arg1, result +<span id="L391" class="LineNr">391 </span> <span class="muComment"># v1.z += v2.z</span> +<span id="L392" class="LineNr">392 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, z +<span id="L393" class="LineNr">393 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, z +<span id="L394" class="LineNr">394 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L395" class="LineNr">395 </span> result <span class="Special"><-</span> add *arg2 +<span id="L396" class="LineNr">396 </span> copy-to *arg1, result +<span id="L397" class="LineNr">397 </span><span class="Delimiter">}</span> +<span id="L398" class="LineNr">398 </span> +<span id="L399" class="LineNr">399 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-subtract-from</span> v1: (addr vec3), v2: (addr vec3) <span class="Delimiter">{</span> +<span id="L400" class="LineNr">400 </span> <span class="PreProc">var</span> tmp-storage: vec3 +<span id="L401" class="LineNr">401 </span> <span class="PreProc">var</span> tmp/eax: (addr vec3) <span class="Special"><-</span> address tmp-storage +<span id="L402" class="LineNr">402 </span> copy-object v2, tmp +<span id="L403" class="LineNr">403 </span> vec3-negate tmp +<span id="L404" class="LineNr">404 </span> vec3-add-to v1, tmp +<span id="L405" class="LineNr">405 </span><span class="Delimiter">}</span> +<span id="L406" class="LineNr">406 </span> +<span id="L407" class="LineNr">407 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-negate</span> v: (addr vec3) <span class="Delimiter">{</span> +<span id="L408" class="LineNr">408 </span> <span class="PreProc">var</span> negative-one/eax: int <span class="Special"><-</span> copy <span class="Constant">-1</span> +<span id="L409" class="LineNr">409 </span> <span class="PreProc">var</span> negative-one-f/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> convert negative-one +<span id="L410" class="LineNr">410 </span> vec3-scale-up v, negative-one-f +<span id="L411" class="LineNr">411 </span><span class="Delimiter">}</span> +<span id="L412" class="LineNr">412 </span> +<span id="L413" class="LineNr">413 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-scale-up</span> _v: (addr vec3), f: float <span class="Delimiter">{</span> +<span id="L414" class="LineNr">414 </span> <span class="PreProc">var</span> v/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L415" class="LineNr">415 </span> <span class="muComment"># v.x *= f</span> +<span id="L416" class="LineNr">416 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L417" class="LineNr">417 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest +<span id="L418" class="LineNr">418 </span> result <span class="Special"><-</span> multiply f +<span id="L419" class="LineNr">419 </span> copy-to *dest, result +<span id="L420" class="LineNr">420 </span> <span class="muComment"># v.y *= f</span> +<span id="L421" class="LineNr">421 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L422" class="LineNr">422 </span> result <span class="Special"><-</span> copy *dest +<span id="L423" class="LineNr">423 </span> result <span class="Special"><-</span> multiply f +<span id="L424" class="LineNr">424 </span> copy-to *dest, result +<span id="L425" class="LineNr">425 </span> <span class="muComment"># v.z *= f</span> +<span id="L426" class="LineNr">426 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L427" class="LineNr">427 </span> result <span class="Special"><-</span> copy *dest +<span id="L428" class="LineNr">428 </span> result <span class="Special"><-</span> multiply f +<span id="L429" class="LineNr">429 </span> copy-to *dest, result +<span id="L430" class="LineNr">430 </span><span class="Delimiter">}</span> +<span id="L431" class="LineNr">431 </span> +<span id="L432" class="LineNr">432 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-scale-down</span> _v: (addr vec3), f: float <span class="Delimiter">{</span> +<span id="L433" class="LineNr">433 </span> <span class="PreProc">var</span> v/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L434" class="LineNr">434 </span> <span class="muComment"># v.x /= f</span> +<span id="L435" class="LineNr">435 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L436" class="LineNr">436 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest +<span id="L437" class="LineNr">437 </span> result <span class="Special"><-</span> divide f +<span id="L438" class="LineNr">438 </span> copy-to *dest, result +<span id="L439" class="LineNr">439 </span> <span class="muComment"># v.y /= f</span> +<span id="L440" class="LineNr">440 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L441" class="LineNr">441 </span> result <span class="Special"><-</span> copy *dest +<span id="L442" class="LineNr">442 </span> result <span class="Special"><-</span> divide f +<span id="L443" class="LineNr">443 </span> copy-to *dest, result +<span id="L444" class="LineNr">444 </span> <span class="muComment"># v.z /= f</span> +<span id="L445" class="LineNr">445 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L446" class="LineNr">446 </span> result <span class="Special"><-</span> copy *dest +<span id="L447" class="LineNr">447 </span> result <span class="Special"><-</span> divide f +<span id="L448" class="LineNr">448 </span> copy-to *dest, result +<span id="L449" class="LineNr">449 </span><span class="Delimiter">}</span> +<span id="L450" class="LineNr">450 </span> +<span id="L451" class="LineNr">451 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-unit</span> in: (addr vec3), out: (addr vec3) <span class="Delimiter">{</span> +<span id="L452" class="LineNr">452 </span> <span class="PreProc">var</span> len/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> vec3-length in +<span id="L453" class="LineNr">453 </span><span class="CommentedCode">#? print-string 0, "len: "</span> +<span id="L454" class="LineNr">454 </span><span class="CommentedCode">#? print-float-hex 0, len</span> +<span id="L455" class="LineNr">455 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L456" class="LineNr">456 </span> copy-object in, out +<span id="L457" class="LineNr">457 </span> vec3-scale-down out, len +<span id="L458" class="LineNr">458 </span><span class="Delimiter">}</span> +<span id="L459" class="LineNr">459 </span> +<span id="L460" class="LineNr">460 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-length</span> v: (addr vec3)<span class="PreProc"> -> </span>_/<span class="Constant">xmm0</span>: float <span class="Delimiter">{</span> +<span id="L461" class="LineNr">461 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> vec3-length-squared v +<span id="L462" class="LineNr">462 </span> result <span class="Special"><-</span> square-root result +<span id="L463" class="LineNr">463 </span> <span class="PreProc">return</span> result +<span id="L464" class="LineNr">464 </span><span class="Delimiter">}</span> +<span id="L465" class="LineNr">465 </span> +<span id="L466" class="LineNr">466 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-length-squared</span> _v: (addr vec3)<span class="PreProc"> -> </span>_/<span class="Constant">xmm0</span>: float <span class="Delimiter">{</span> +<span id="L467" class="LineNr">467 </span> <span class="PreProc">var</span> v/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L468" class="LineNr">468 </span> <span class="muComment"># result = v.x * v.x</span> +<span id="L469" class="LineNr">469 </span> <span class="PreProc">var</span> src/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L470" class="LineNr">470 </span> <span class="PreProc">var</span> tmp/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> copy *src +<span id="L471" class="LineNr">471 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L472" class="LineNr">472 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy tmp +<span id="L473" class="LineNr">473 </span> <span class="muComment"># result += v.y * v.y</span> +<span id="L474" class="LineNr">474 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L475" class="LineNr">475 </span> tmp <span class="Special"><-</span> copy *src +<span id="L476" class="LineNr">476 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L477" class="LineNr">477 </span> result <span class="Special"><-</span> add tmp +<span id="L478" class="LineNr">478 </span> <span class="muComment"># result += v.z * v.z</span> +<span id="L479" class="LineNr">479 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L480" class="LineNr">480 </span> tmp <span class="Special"><-</span> copy *src +<span id="L481" class="LineNr">481 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L482" class="LineNr">482 </span> result <span class="Special"><-</span> add tmp +<span id="L483" class="LineNr">483 </span> <span class="PreProc">return</span> result +<span id="L484" class="LineNr">484 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/color.mu.html b/html/linux/apps/raytracing/color.mu.html new file mode 100644 index 00000000..dc60a456 --- /dev/null +++ b/html/linux/apps/raytracing/color.mu.html @@ -0,0 +1,100 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/color.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.muRegEdx { color: #af5f00; } +.LineNr { } +.muRegEsi { color: #005faf; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.Special { color: #ff6060; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/color.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/color.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="PreProc">type</span> rgb <span class="Delimiter">{</span> +<span id="L2" class="LineNr"> 2 </span> <span class="muComment"># components normalized to within [0.0, 1.0]</span> +<span id="L3" class="LineNr"> 3 </span> r: float +<span id="L4" class="LineNr"> 4 </span> g: float +<span id="L5" class="LineNr"> 5 </span> b: float +<span id="L6" class="LineNr"> 6 </span><span class="Delimiter">}</span> +<span id="L7" class="LineNr"> 7 </span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># print translating to [0, 256)</span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction">print-rgb</span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _c: (addr rgb) <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span> <span class="PreProc">var</span> c/<span class="muRegEsi">esi</span>: (addr rgb) <span class="Special"><-</span> copy _c +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> xn: float +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> xn-addr/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> address xn +<span id="L13" class="LineNr">13 </span> <a href='../../408float.mu.html#L3'>fill-in-rational</a> xn-addr, <span class="Constant">0x3e7ff</span>, <span class="Constant">0x3e8</span> <span class="muComment"># 255999 / 1000</span> +<span id="L14" class="LineNr">14 </span> <span class="muComment"># print 255.999 * c->r</span> +<span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy xn +<span id="L16" class="LineNr">16 </span> <span class="PreProc">var</span> src-addr/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, r +<span id="L17" class="LineNr">17 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L18" class="LineNr">18 </span> <span class="PreProc">var</span> result-int/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> truncate result +<span id="L19" class="LineNr">19 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L20" class="LineNr">20 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L21" class="LineNr">21 </span> <span class="muComment"># print 255.999 * c->g</span> +<span id="L22" class="LineNr">22 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, g +<span id="L23" class="LineNr">23 </span> result <span class="Special"><-</span> copy xn +<span id="L24" class="LineNr">24 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L25" class="LineNr">25 </span> result-int <span class="Special"><-</span> truncate result +<span id="L26" class="LineNr">26 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L27" class="LineNr">27 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">" "</span> +<span id="L28" class="LineNr">28 </span> <span class="muComment"># print 255.999 * c->b</span> +<span id="L29" class="LineNr">29 </span> src-addr <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> c, b +<span id="L30" class="LineNr">30 </span> result <span class="Special"><-</span> copy xn +<span id="L31" class="LineNr">31 </span> result <span class="Special"><-</span> multiply *src-addr +<span id="L32" class="LineNr">32 </span> result-int <span class="Special"><-</span> truncate result +<span id="L33" class="LineNr">33 </span> <a href='../../405screen.mu.html#L484'>print-int32-decimal</a> <a href='../../405screen.mu.html#L9'>screen</a>, result-int +<span id="L34" class="LineNr">34 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"\n"</span> +<span id="L35" class="LineNr">35 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/ray.mu.html b/html/linux/apps/raytracing/ray.mu.html new file mode 100644 index 00000000..5400c1f2 --- /dev/null +++ b/html/linux/apps/raytracing/ray.mu.html @@ -0,0 +1,86 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/ray.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.Special { color: #ff6060; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/ray.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/ray.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="PreProc">type</span> ray <span class="Delimiter">{</span> +<span id="L2" class="LineNr"> 2 </span> orig: vec3 <span class="muComment"># point</span> +<span id="L3" class="LineNr"> 3 </span> dir: vec3 +<span id="L4" class="LineNr"> 4 </span><span class="Delimiter">}</span> +<span id="L5" class="LineNr"> 5 </span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># A little different from the constructor at <a href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">https://raytracing.github.io/books/RayTracingInOneWeekend.html</a></span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># We immediately normalize the direction vector so we don't have to keep doing</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment"># so.</span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction">initialize-ray</span> _self: (addr ray), o: (addr vec3), d: (addr vec3) <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr ray) <span class="Special"><-</span> copy _self +<span id="L11" class="LineNr">11 </span> <span class="PreProc">var</span> dest/eax: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, orig +<span id="L12" class="LineNr">12 </span> copy-object o, dest +<span id="L13" class="LineNr">13 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, dir +<span id="L14" class="LineNr">14 </span> vec3-unit d, dest +<span id="L15" class="LineNr">15 </span><span class="Delimiter">}</span> +<span id="L16" class="LineNr">16 </span> +<span id="L17" class="LineNr">17 </span><span class="PreProc">fn</span> <span class="muFunction">ray-at</span> _self: (addr ray), t: float, out: (addr vec3) <span class="Delimiter">{</span> +<span id="L18" class="LineNr">18 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr ray) <span class="Special"><-</span> copy _self +<span id="L19" class="LineNr">19 </span> <span class="PreProc">var</span> src/eax: (addr vec3) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, dir +<span id="L20" class="LineNr">20 </span> copy-object src, out +<span id="L21" class="LineNr">21 </span> vec3-scale-up out, t +<span id="L22" class="LineNr">22 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> self, orig +<span id="L23" class="LineNr">23 </span> vec3-add-to out, src +<span id="L24" class="LineNr">24 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/raytracing/vec.mu.html b/html/linux/apps/raytracing/vec.mu.html new file mode 100644 index 00000000..9fe5b990 --- /dev/null +++ b/html/linux/apps/raytracing/vec.mu.html @@ -0,0 +1,201 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/raytracing/vec.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.CommentedCode { color: #8a8a8a; } +.muRegEcx { color: #870000; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.Special { color: #ff6060; } +.muComment { color: #005faf; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/raytracing/vec.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/raytracing/vec.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="PreProc">type</span> vec3 <span class="Delimiter">{</span> +<span id="L2" class="LineNr"> 2 </span> x: float +<span id="L3" class="LineNr"> 3 </span> y: float +<span id="L4" class="LineNr"> 4 </span> z: float +<span id="L5" class="LineNr"> 5 </span><span class="Delimiter">}</span> +<span id="L6" class="LineNr"> 6 </span> +<span id="L7" class="LineNr"> 7 </span><span class="PreProc">fn</span> <span class="muFunction">print-vec3</span> <a href='../../405screen.mu.html#L9'>screen</a>: (addr <a href='../../405screen.mu.html#L9'>screen</a>), _v: (addr vec3) <span class="Delimiter">{</span> +<span id="L8" class="LineNr"> 8 </span> <span class="PreProc">var</span> v/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L9" class="LineNr"> 9 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">"("</span> +<span id="L10" class="LineNr"> 10 </span> <span class="PreProc">var</span> tmp/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L11" class="LineNr"> 11 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L12" class="LineNr"> 12 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L13" class="LineNr"> 13 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L14" class="LineNr"> 14 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L15" class="LineNr"> 15 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">", "</span> +<span id="L16" class="LineNr"> 16 </span> tmp <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L17" class="LineNr"> 17 </span> <a href='../../409print-float-hex.mu.html#L118'>print-float-hex</a> <a href='../../405screen.mu.html#L9'>screen</a>, *tmp +<span id="L18" class="LineNr"> 18 </span> <a href='../../405screen.mu.html#L169'>print-string</a> <a href='../../405screen.mu.html#L9'>screen</a>, <span class="Constant">")"</span> +<span id="L19" class="LineNr"> 19 </span><span class="Delimiter">}</span> +<span id="L20" class="LineNr"> 20 </span> +<span id="L21" class="LineNr"> 21 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-add-to</span> _v1: (addr vec3), _v2: (addr vec3) <span class="Delimiter">{</span> +<span id="L22" class="LineNr"> 22 </span> <span class="PreProc">var</span> v1/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v1 +<span id="L23" class="LineNr"> 23 </span> <span class="PreProc">var</span> v2/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v2 +<span id="L24" class="LineNr"> 24 </span> <span class="muComment"># v1.x += v2.x</span> +<span id="L25" class="LineNr"> 25 </span> <span class="PreProc">var</span> arg1/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, x +<span id="L26" class="LineNr"> 26 </span> <span class="PreProc">var</span> arg2/<span class="muRegEcx">ecx</span>: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, x +<span id="L27" class="LineNr"> 27 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *arg1 +<span id="L28" class="LineNr"> 28 </span> result <span class="Special"><-</span> add *arg2 +<span id="L29" class="LineNr"> 29 </span> copy-to *arg1, result +<span id="L30" class="LineNr"> 30 </span> <span class="muComment"># v1.y += v2.y</span> +<span id="L31" class="LineNr"> 31 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, y +<span id="L32" class="LineNr"> 32 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, y +<span id="L33" class="LineNr"> 33 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L34" class="LineNr"> 34 </span> result <span class="Special"><-</span> add *arg2 +<span id="L35" class="LineNr"> 35 </span> copy-to *arg1, result +<span id="L36" class="LineNr"> 36 </span> <span class="muComment"># v1.z += v2.z</span> +<span id="L37" class="LineNr"> 37 </span> arg1 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v1, z +<span id="L38" class="LineNr"> 38 </span> arg2 <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v2, z +<span id="L39" class="LineNr"> 39 </span> result <span class="Special"><-</span> copy *arg1 +<span id="L40" class="LineNr"> 40 </span> result <span class="Special"><-</span> add *arg2 +<span id="L41" class="LineNr"> 41 </span> copy-to *arg1, result +<span id="L42" class="LineNr"> 42 </span><span class="Delimiter">}</span> +<span id="L43" class="LineNr"> 43 </span> +<span id="L44" class="LineNr"> 44 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-subtract-from</span> v1: (addr vec3), v2: (addr vec3) <span class="Delimiter">{</span> +<span id="L45" class="LineNr"> 45 </span> <span class="PreProc">var</span> tmp-storage: vec3 +<span id="L46" class="LineNr"> 46 </span> <span class="PreProc">var</span> tmp/eax: (addr vec3) <span class="Special"><-</span> address tmp-storage +<span id="L47" class="LineNr"> 47 </span> copy-object v2, tmp +<span id="L48" class="LineNr"> 48 </span> vec3-negate tmp +<span id="L49" class="LineNr"> 49 </span> vec3-add-to v1, tmp +<span id="L50" class="LineNr"> 50 </span><span class="Delimiter">}</span> +<span id="L51" class="LineNr"> 51 </span> +<span id="L52" class="LineNr"> 52 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-negate</span> v: (addr vec3) <span class="Delimiter">{</span> +<span id="L53" class="LineNr"> 53 </span> <span class="PreProc">var</span> negative-one/eax: int <span class="Special"><-</span> copy <span class="Constant">-1</span> +<span id="L54" class="LineNr"> 54 </span> <span class="PreProc">var</span> negative-one-f/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> convert negative-one +<span id="L55" class="LineNr"> 55 </span> vec3-scale-up v, negative-one-f +<span id="L56" class="LineNr"> 56 </span><span class="Delimiter">}</span> +<span id="L57" class="LineNr"> 57 </span> +<span id="L58" class="LineNr"> 58 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-scale-up</span> _v: (addr vec3), f: float <span class="Delimiter">{</span> +<span id="L59" class="LineNr"> 59 </span> <span class="PreProc">var</span> v/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L60" class="LineNr"> 60 </span> <span class="muComment"># v.x *= f</span> +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L62" class="LineNr"> 62 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest +<span id="L63" class="LineNr"> 63 </span> result <span class="Special"><-</span> multiply f +<span id="L64" class="LineNr"> 64 </span> copy-to *dest, result +<span id="L65" class="LineNr"> 65 </span> <span class="muComment"># v.y *= f</span> +<span id="L66" class="LineNr"> 66 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L67" class="LineNr"> 67 </span> result <span class="Special"><-</span> copy *dest +<span id="L68" class="LineNr"> 68 </span> result <span class="Special"><-</span> multiply f +<span id="L69" class="LineNr"> 69 </span> copy-to *dest, result +<span id="L70" class="LineNr"> 70 </span> <span class="muComment"># v.z *= f</span> +<span id="L71" class="LineNr"> 71 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L72" class="LineNr"> 72 </span> result <span class="Special"><-</span> copy *dest +<span id="L73" class="LineNr"> 73 </span> result <span class="Special"><-</span> multiply f +<span id="L74" class="LineNr"> 74 </span> copy-to *dest, result +<span id="L75" class="LineNr"> 75 </span><span class="Delimiter">}</span> +<span id="L76" class="LineNr"> 76 </span> +<span id="L77" class="LineNr"> 77 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-scale-down</span> _v: (addr vec3), f: float <span class="Delimiter">{</span> +<span id="L78" class="LineNr"> 78 </span> <span class="PreProc">var</span> v/<span class="muRegEdi">edi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L79" class="LineNr"> 79 </span> <span class="muComment"># v.x /= f</span> +<span id="L80" class="LineNr"> 80 </span> <span class="PreProc">var</span> dest/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L81" class="LineNr"> 81 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy *dest +<span id="L82" class="LineNr"> 82 </span> result <span class="Special"><-</span> divide f +<span id="L83" class="LineNr"> 83 </span> copy-to *dest, result +<span id="L84" class="LineNr"> 84 </span> <span class="muComment"># v.y /= f</span> +<span id="L85" class="LineNr"> 85 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L86" class="LineNr"> 86 </span> result <span class="Special"><-</span> copy *dest +<span id="L87" class="LineNr"> 87 </span> result <span class="Special"><-</span> divide f +<span id="L88" class="LineNr"> 88 </span> copy-to *dest, result +<span id="L89" class="LineNr"> 89 </span> <span class="muComment"># v.z /= f</span> +<span id="L90" class="LineNr"> 90 </span> dest <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L91" class="LineNr"> 91 </span> result <span class="Special"><-</span> copy *dest +<span id="L92" class="LineNr"> 92 </span> result <span class="Special"><-</span> divide f +<span id="L93" class="LineNr"> 93 </span> copy-to *dest, result +<span id="L94" class="LineNr"> 94 </span><span class="Delimiter">}</span> +<span id="L95" class="LineNr"> 95 </span> +<span id="L96" class="LineNr"> 96 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-unit</span> in: (addr vec3), out: (addr vec3) <span class="Delimiter">{</span> +<span id="L97" class="LineNr"> 97 </span> <span class="PreProc">var</span> len/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> vec3-length in +<span id="L98" class="LineNr"> 98 </span><span class="CommentedCode">#? print-string 0, "len: "</span> +<span id="L99" class="LineNr"> 99 </span><span class="CommentedCode">#? print-float-hex 0, len</span> +<span id="L100" class="LineNr">100 </span><span class="CommentedCode">#? print-string 0, "\n"</span> +<span id="L101" class="LineNr">101 </span> copy-object in, out +<span id="L102" class="LineNr">102 </span> vec3-scale-down out, len +<span id="L103" class="LineNr">103 </span><span class="Delimiter">}</span> +<span id="L104" class="LineNr">104 </span> +<span id="L105" class="LineNr">105 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-length</span> v: (addr vec3)<span class="PreProc"> -> </span>_/<span class="Constant">xmm0</span>: float <span class="Delimiter">{</span> +<span id="L106" class="LineNr">106 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> vec3-length-squared v +<span id="L107" class="LineNr">107 </span> result <span class="Special"><-</span> square-root result +<span id="L108" class="LineNr">108 </span> <span class="PreProc">return</span> result +<span id="L109" class="LineNr">109 </span><span class="Delimiter">}</span> +<span id="L110" class="LineNr">110 </span> +<span id="L111" class="LineNr">111 </span><span class="PreProc">fn</span> <span class="muFunction">vec3-length-squared</span> _v: (addr vec3)<span class="PreProc"> -> </span>_/<span class="Constant">xmm0</span>: float <span class="Delimiter">{</span> +<span id="L112" class="LineNr">112 </span> <span class="PreProc">var</span> v/<span class="muRegEsi">esi</span>: (addr vec3) <span class="Special"><-</span> copy _v +<span id="L113" class="LineNr">113 </span> <span class="muComment"># result = v.x * v.x</span> +<span id="L114" class="LineNr">114 </span> <span class="PreProc">var</span> src/eax: (addr float) <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, x +<span id="L115" class="LineNr">115 </span> <span class="PreProc">var</span> tmp/<span class="Constant">xmm1</span>: float <span class="Special"><-</span> copy *src +<span id="L116" class="LineNr">116 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L117" class="LineNr">117 </span> <span class="PreProc">var</span> result/<span class="Constant">xmm0</span>: float <span class="Special"><-</span> copy tmp +<span id="L118" class="LineNr">118 </span> <span class="muComment"># result += v.y * v.y</span> +<span id="L119" class="LineNr">119 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, y +<span id="L120" class="LineNr">120 </span> tmp <span class="Special"><-</span> copy *src +<span id="L121" class="LineNr">121 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L122" class="LineNr">122 </span> result <span class="Special"><-</span> add tmp +<span id="L123" class="LineNr">123 </span> <span class="muComment"># result += v.z * v.z</span> +<span id="L124" class="LineNr">124 </span> src <span class="Special"><-</span> <a href='../../131table.subx.html#L26'>get</a> v, z +<span id="L125" class="LineNr">125 </span> tmp <span class="Special"><-</span> copy *src +<span id="L126" class="LineNr">126 </span> tmp <span class="Special"><-</span> multiply tmp +<span id="L127" class="LineNr">127 </span> result <span class="Special"><-</span> add tmp +<span id="L128" class="LineNr">128 </span> <span class="PreProc">return</span> result +<span id="L129" class="LineNr">129 </span><span class="Delimiter">}</span> +<span id="L130" class="LineNr">130 </span> +<span id="L131" class="LineNr">131 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='vec.mu.html#L131'>vec3-dot</a></span> _v1: (addr vec3), _v2: (addr vec3)<span class="PreProc"> -> </span>_/<span class="Constant">xmm0</span>: float <span class="Delimiter">{</span> +<span id="L132" class="LineNr">132 </span><span class="Delimiter">}</span> +<span id="L133" class="LineNr">133 </span> +<span id="L134" class="LineNr">134 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='vec.mu.html#L134'>vec3-cross</a></span> _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) <span class="Delimiter">{</span> +<span id="L135" class="LineNr">135 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/rpn.mu.html b/html/linux/apps/rpn.mu.html new file mode 100644 index 00000000..b5aba508 --- /dev/null +++ b/html/linux/apps/rpn.mu.html @@ -0,0 +1,215 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/rpn.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.muRegEsi { color: #005faf; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/rpn.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/rpn.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Integer arithmetic using postfix notation</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># Limitations:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># No division yet.</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment">#</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># To build:</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./translate apps/rpn.mu</span> +<span id="L8" class="LineNr"> 8 </span><span class="muComment">#</span> +<span id="L9" class="LineNr"> 9 </span><span class="muComment"># Example session:</span> +<span id="L10" class="LineNr"> 10 </span><span class="muComment"># $ ./a.elf</span> +<span id="L11" class="LineNr"> 11 </span><span class="muComment"># press ctrl-c or ctrl-d to exit</span> +<span id="L12" class="LineNr"> 12 </span><span class="muComment"># > 1</span> +<span id="L13" class="LineNr"> 13 </span><span class="muComment"># 1</span> +<span id="L14" class="LineNr"> 14 </span><span class="muComment"># > 1 1 +</span> +<span id="L15" class="LineNr"> 15 </span><span class="muComment"># 2</span> +<span id="L16" class="LineNr"> 16 </span><span class="muComment"># > 1 2 3 + +</span> +<span id="L17" class="LineNr"> 17 </span><span class="muComment"># 6</span> +<span id="L18" class="LineNr"> 18 </span><span class="muComment"># > 1 2 3 * +</span> +<span id="L19" class="LineNr"> 19 </span><span class="muComment"># 7</span> +<span id="L20" class="LineNr"> 20 </span><span class="muComment"># > 1 2 + 3 *</span> +<span id="L21" class="LineNr"> 21 </span><span class="muComment"># 9</span> +<span id="L22" class="LineNr"> 22 </span><span class="muComment"># > 1 3 4 * +</span> +<span id="L23" class="LineNr"> 23 </span><span class="muComment"># 13</span> +<span id="L24" class="LineNr"> 24 </span><span class="muComment"># > ^D</span> +<span id="L25" class="LineNr"> 25 </span><span class="muComment"># $</span> +<span id="L26" class="LineNr"> 26 </span><span class="muComment">#</span> +<span id="L27" class="LineNr"> 27 </span><span class="muComment"># Error handling is non-existent. This is just a prototype.</span> +<span id="L28" class="LineNr"> 28 </span> +<span id="L29" class="LineNr"> 29 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='rpn.mu.html#L29'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L30" class="LineNr"> 30 </span> <span class="PreProc">var</span> in-storage: (stream byte <span class="Constant">0x100</span>) +<span id="L31" class="LineNr"> 31 </span> <span class="PreProc">var</span> in/<span class="muRegEsi">esi</span>: (addr stream byte) <span class="Special"><-</span> address in-storage +<span id="L32" class="LineNr"> 32 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"press ctrl-c or ctrl-d to exit\n"</span> +<span id="L33" class="LineNr"> 33 </span> <span class="muComment"># read-eval-print loop</span> +<span id="L34" class="LineNr"> 34 </span> <span class="Delimiter">{</span> +<span id="L35" class="LineNr"> 35 </span> <span class="muComment"># print prompt</span> +<span id="L36" class="LineNr"> 36 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"> "</span> +<span id="L37" class="LineNr"> 37 </span> <span class="muComment"># read line</span> +<span id="L38" class="LineNr"> 38 </span> <a href='../106stream.subx.html#L17'>clear-stream</a> in +<span id="L39" class="LineNr"> 39 </span> <a href='../305keyboard.subx.html#L158'>read-line-from-real-keyboard</a> in +<span id="L40" class="LineNr"> 40 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../309stream.subx.html#L6'>stream-empty?</a> in +<span id="L41" class="LineNr"> 41 </span> compare done?, <span class="Constant">0</span> +<span id="L42" class="LineNr"> 42 </span> <span class="PreProc">break-if-!=</span> +<span id="L43" class="LineNr"> 43 </span> <span class="muComment"># parse and eval</span> +<span id="L44" class="LineNr"> 44 </span> <span class="PreProc">var</span> out/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L59'>simplify</a> in +<span id="L45" class="LineNr"> 45 </span> <span class="muComment"># print</span> +<span id="L46" class="LineNr"> 46 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, out +<span id="L47" class="LineNr"> 47 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"\n"</span> +<span id="L48" class="LineNr"> 48 </span> <span class="muComment">#</span> +<span id="L49" class="LineNr"> 49 </span> <span class="PreProc">loop</span> +<span id="L50" class="LineNr"> 50 </span> <span class="Delimiter">}</span> +<span id="L51" class="LineNr"> 51 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L52" class="LineNr"> 52 </span><span class="Delimiter">}</span> +<span id="L53" class="LineNr"> 53 </span> +<span id="L54" class="LineNr"> 54 </span><span class="PreProc">type</span> <a href='rpn.mu.html#L54'>int-stack</a> <span class="Delimiter">{</span> +<span id="L55" class="LineNr"> 55 </span> data: (handle array int) +<span id="L56" class="LineNr"> 56 </span> <a href='../203stack.subx.html#L338'>top</a>: int +<span id="L57" class="LineNr"> 57 </span><span class="Delimiter">}</span> +<span id="L58" class="LineNr"> 58 </span> +<span id="L59" class="LineNr"> 59 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='rpn.mu.html#L59'>simplify</a></span> in: (addr stream byte)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L60" class="LineNr"> 60 </span> <span class="PreProc">var</span> word-storage: slice +<span id="L61" class="LineNr"> 61 </span> <span class="PreProc">var</span> word/<span class="muRegEcx">ecx</span>: (addr slice) <span class="Special"><-</span> address word-storage +<span id="L62" class="LineNr"> 62 </span> <span class="PreProc">var</span> stack-storage: <a href='rpn.mu.html#L54'>int-stack</a> +<span id="L63" class="LineNr"> 63 </span> <span class="PreProc">var</span> stack/<span class="muRegEsi">esi</span>: (addr <a href='rpn.mu.html#L54'>int-stack</a>) <span class="Special"><-</span> address stack-storage +<span id="L64" class="LineNr"> 64 </span> <a href='rpn.mu.html#L113'>initialize-int-stack</a> stack, <span class="Constant">0x10</span> +<span id="L65" class="LineNr"> 65 </span> $simplify:word-loop: <span class="Delimiter">{</span> +<span id="L66" class="LineNr"> 66 </span> <a href='../127next-word.subx.html#L11'>next-word</a> in, word +<span id="L67" class="LineNr"> 67 </span> <span class="PreProc">var</span> done?/eax: boolean <span class="Special"><-</span> <a href='../123slice.subx.html#L9'>slice-empty?</a> word +<span id="L68" class="LineNr"> 68 </span> compare done?, <span class="Constant">0</span> +<span id="L69" class="LineNr"> 69 </span> <span class="PreProc">break-if-!=</span> +<span id="L70" class="LineNr"> 70 </span> <span class="muComment"># if word is an operator, perform it</span> +<span id="L71" class="LineNr"> 71 </span> <span class="Delimiter">{</span> +<span id="L72" class="LineNr"> 72 </span> <span class="PreProc">var</span> add?/eax: boolean <span class="Special"><-</span> <a href='../123slice.subx.html#L120'>slice-equal?</a> word, <span class="Constant">"+"</span> +<span id="L73" class="LineNr"> 73 </span> compare add?, <span class="Constant">0</span> +<span id="L74" class="LineNr"> 74 </span> <span class="PreProc">break-if-=</span> +<span id="L75" class="LineNr"> 75 </span> <span class="PreProc">var</span> _b/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L76" class="LineNr"> 76 </span> <span class="PreProc">var</span> b/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy _b +<span id="L77" class="LineNr"> 77 </span> <span class="PreProc">var</span> a/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L78" class="LineNr"> 78 </span> a <span class="Special"><-</span> add b +<span id="L79" class="LineNr"> 79 </span> <a href='rpn.mu.html#L121'>push-int-stack</a> stack, a +<span id="L80" class="LineNr"> 80 </span> <span class="PreProc">loop</span> $simplify:word-loop +<span id="L81" class="LineNr"> 81 </span> <span class="Delimiter">}</span> +<span id="L82" class="LineNr"> 82 </span> <span class="Delimiter">{</span> +<span id="L83" class="LineNr"> 83 </span> <span class="PreProc">var</span> sub?/eax: boolean <span class="Special"><-</span> <a href='../123slice.subx.html#L120'>slice-equal?</a> word, <span class="Constant">"-"</span> +<span id="L84" class="LineNr"> 84 </span> compare sub?, <span class="Constant">0</span> +<span id="L85" class="LineNr"> 85 </span> <span class="PreProc">break-if-=</span> +<span id="L86" class="LineNr"> 86 </span> <span class="PreProc">var</span> _b/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L87" class="LineNr"> 87 </span> <span class="PreProc">var</span> b/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy _b +<span id="L88" class="LineNr"> 88 </span> <span class="PreProc">var</span> a/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L89" class="LineNr"> 89 </span> a <span class="Special"><-</span> subtract b +<span id="L90" class="LineNr"> 90 </span> <a href='rpn.mu.html#L121'>push-int-stack</a> stack, a +<span id="L91" class="LineNr"> 91 </span> <span class="PreProc">loop</span> $simplify:word-loop +<span id="L92" class="LineNr"> 92 </span> <span class="Delimiter">}</span> +<span id="L93" class="LineNr"> 93 </span> <span class="Delimiter">{</span> +<span id="L94" class="LineNr"> 94 </span> <span class="PreProc">var</span> mul?/eax: boolean <span class="Special"><-</span> <a href='../123slice.subx.html#L120'>slice-equal?</a> word, <span class="Constant">"*"</span> +<span id="L95" class="LineNr"> 95 </span> compare mul?, <span class="Constant">0</span> +<span id="L96" class="LineNr"> 96 </span> <span class="PreProc">break-if-=</span> +<span id="L97" class="LineNr"> 97 </span> <span class="PreProc">var</span> _b/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L98" class="LineNr"> 98 </span> <span class="PreProc">var</span> b/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy _b +<span id="L99" class="LineNr"> 99 </span> <span class="PreProc">var</span> a/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L100" class="LineNr">100 </span> a <span class="Special"><-</span> multiply b +<span id="L101" class="LineNr">101 </span> <a href='rpn.mu.html#L121'>push-int-stack</a> stack, a +<span id="L102" class="LineNr">102 </span> <span class="PreProc">loop</span> $simplify:word-loop +<span id="L103" class="LineNr">103 </span> <span class="Delimiter">}</span> +<span id="L104" class="LineNr">104 </span> <span class="muComment"># otherwise it's an int</span> +<span id="L105" class="LineNr">105 </span> <span class="PreProc">var</span> n/eax: int <span class="Special"><-</span> <a href='../311decimal-int.subx.html#L4'>parse-decimal-int-from-slice</a> word +<span id="L106" class="LineNr">106 </span> <a href='rpn.mu.html#L121'>push-int-stack</a> stack, n +<span id="L107" class="LineNr">107 </span> <span class="PreProc">loop</span> +<span id="L108" class="LineNr">108 </span> <span class="Delimiter">}</span> +<span id="L109" class="LineNr">109 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> <a href='rpn.mu.html#L133'>pop-int-stack</a> stack +<span id="L110" class="LineNr">110 </span> <span class="PreProc">return</span> result +<span id="L111" class="LineNr">111 </span><span class="Delimiter">}</span> +<span id="L112" class="LineNr">112 </span> +<span id="L113" class="LineNr">113 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='rpn.mu.html#L113'>initialize-int-stack</a></span> _self: (addr <a href='rpn.mu.html#L54'>int-stack</a>), n: int <span class="Delimiter">{</span> +<span id="L114" class="LineNr">114 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr <a href='rpn.mu.html#L54'>int-stack</a>) <span class="Special"><-</span> copy _self +<span id="L115" class="LineNr">115 </span> <span class="PreProc">var</span> d/<span class="muRegEdi">edi</span>: (addr handle array int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, data +<span id="L116" class="LineNr">116 </span> populate d, n +<span id="L117" class="LineNr">117 </span> <span class="PreProc">var</span> <a href='../203stack.subx.html#L338'>top</a>/eax: (addr int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, <a href='../203stack.subx.html#L338'>top</a> +<span id="L118" class="LineNr">118 </span> copy-to *top, <span class="Constant">0</span> +<span id="L119" class="LineNr">119 </span><span class="Delimiter">}</span> +<span id="L120" class="LineNr">120 </span> +<span id="L121" class="LineNr">121 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='rpn.mu.html#L121'>push-int-stack</a></span> _self: (addr <a href='rpn.mu.html#L54'>int-stack</a>), _val: int <span class="Delimiter">{</span> +<span id="L122" class="LineNr">122 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr <a href='rpn.mu.html#L54'>int-stack</a>) <span class="Special"><-</span> copy _self +<span id="L123" class="LineNr">123 </span> <span class="PreProc">var</span> top-addr/<span class="muRegEcx">ecx</span>: (addr int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, <a href='../203stack.subx.html#L338'>top</a> +<span id="L124" class="LineNr">124 </span> <span class="PreProc">var</span> data-ah/<span class="muRegEdx">edx</span>: (addr handle array int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, data +<span id="L125" class="LineNr">125 </span> <span class="PreProc">var</span> data/eax: (addr array int) <span class="Special"><-</span> <a href='../120allocate.subx.html#L258'>lookup</a> *data-ah +<span id="L126" class="LineNr">126 </span> <span class="PreProc">var</span> <a href='../203stack.subx.html#L338'>top</a>/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy *top-addr +<span id="L127" class="LineNr">127 </span> <span class="PreProc">var</span> dest-addr/<span class="muRegEdx">edx</span>: (addr int) <span class="Special"><-</span> index data, <a href='../203stack.subx.html#L338'>top</a> +<span id="L128" class="LineNr">128 </span> <span class="PreProc">var</span> val/eax: int <span class="Special"><-</span> copy _val +<span id="L129" class="LineNr">129 </span> copy-to *dest-addr, val +<span id="L130" class="LineNr">130 </span> add-to *top-addr, <span class="Constant">1</span> +<span id="L131" class="LineNr">131 </span><span class="Delimiter">}</span> +<span id="L132" class="LineNr">132 </span> +<span id="L133" class="LineNr">133 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='rpn.mu.html#L133'>pop-int-stack</a></span> _self: (addr <a href='rpn.mu.html#L54'>int-stack</a>)<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span> +<span id="L134" class="LineNr">134 </span> <span class="PreProc">var</span> self/<span class="muRegEsi">esi</span>: (addr <a href='rpn.mu.html#L54'>int-stack</a>) <span class="Special"><-</span> copy _self +<span id="L135" class="LineNr">135 </span> <span class="PreProc">var</span> top-addr/<span class="muRegEcx">ecx</span>: (addr int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, <a href='../203stack.subx.html#L338'>top</a> +<span id="L136" class="LineNr">136 </span> <span class="Delimiter">{</span> +<span id="L137" class="LineNr">137 </span> compare *top-addr, <span class="Constant">0</span> +<span id="L138" class="LineNr">138 </span> <span class="PreProc">break-if-></span> +<span id="L139" class="LineNr">139 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L140" class="LineNr">140 </span> <span class="Delimiter">}</span> +<span id="L141" class="LineNr">141 </span> subtract-from *top-addr, <span class="Constant">1</span> +<span id="L142" class="LineNr">142 </span> <span class="PreProc">var</span> data-ah/<span class="muRegEdx">edx</span>: (addr handle array int) <span class="Special"><-</span> <a href='../131table.subx.html#L26'>get</a> self, data +<span id="L143" class="LineNr">143 </span> <span class="PreProc">var</span> data/eax: (addr array int) <span class="Special"><-</span> <a href='../120allocate.subx.html#L258'>lookup</a> *data-ah +<span id="L144" class="LineNr">144 </span> <span class="PreProc">var</span> <a href='../203stack.subx.html#L338'>top</a>/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy *top-addr +<span id="L145" class="LineNr">145 </span> <span class="PreProc">var</span> result-addr/eax: (addr int) <span class="Special"><-</span> index data, <a href='../203stack.subx.html#L338'>top</a> +<span id="L146" class="LineNr">146 </span> <span class="PreProc">var</span> val/eax: int <span class="Special"><-</span> copy *result-addr +<span id="L147" class="LineNr">147 </span> <span class="PreProc">return</span> val +<span id="L148" class="LineNr">148 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/texture.mu.html b/html/linux/apps/texture.mu.html new file mode 100644 index 00000000..c3f44607 --- /dev/null +++ b/html/linux/apps/texture.mu.html @@ -0,0 +1,126 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/texture.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEsi { color: #005faf; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.CommentedCode { color: #8a8a8a; } +.muRegEdi { color: #00af00; } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEdx { color: #af5f00; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/texture.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/texture.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Playing with emitting cool textures.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run (on Linux):</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ git clone <a href="https://github.com/akkartik/mu">https://github.com/akkartik/mu</a></span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ cd mu</span> +<span id="L6" class="LineNr"> 6 </span><span class="muComment"># $ ./translate apps/texture.mu</span> +<span id="L7" class="LineNr"> 7 </span><span class="muComment"># $ ./a.elf > a.ppm</span> +<span id="L8" class="LineNr"> 8 </span> +<span id="L9" class="LineNr"> 9 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='texture.mu.html#L9'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L10" class="LineNr">10 </span><span class="CommentedCode">#? var width/esi: int <- copy 0x190 # 400</span> +<span id="L11" class="LineNr">11 </span><span class="CommentedCode">#? var height/edi: int <- copy 0xe1 # 225; aspect ratio 16:9</span> +<span id="L12" class="LineNr">12 </span> <span class="PreProc">var</span> width/<span class="muRegEsi">esi</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> height/<span class="muRegEdi">edi</span>: int <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L14" class="LineNr">14 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"P3\n"</span> +<span id="L15" class="LineNr">15 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, width +<span id="L16" class="LineNr">16 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">" "</span> +<span id="L17" class="LineNr">17 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, height +<span id="L18" class="LineNr">18 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"\n"</span> +<span id="L19" class="LineNr">19 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"255\n"</span> <span class="muComment"># color depth</span> +<span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> row/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L21" class="LineNr">21 </span> <span class="Delimiter">{</span> +<span id="L22" class="LineNr">22 </span> compare row, height +<span id="L23" class="LineNr">23 </span> <span class="PreProc">break-if->=</span> +<span id="L24" class="LineNr">24 </span> <span class="PreProc">var</span> col/<span class="muRegEdx">edx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L25" class="LineNr">25 </span> <span class="Delimiter">{</span> +<span id="L26" class="LineNr">26 </span> compare col, width +<span id="L27" class="LineNr">27 </span> <span class="PreProc">break-if->=</span> +<span id="L28" class="LineNr">28 </span> <span class="muComment"># r</span> +<span id="L29" class="LineNr">29 </span> <span class="PreProc">var</span> tmp/eax: int <span class="Special"><-</span> copy col +<span id="L30" class="LineNr">30 </span> tmp <span class="Special"><-</span> multiply row +<span id="L31" class="LineNr">31 </span> tmp <span class="Special"><-</span> and <span class="Constant">0x7f</span> +<span id="L32" class="LineNr">32 </span> tmp <span class="Special"><-</span> add <span class="Constant">0x80</span> +<span id="L33" class="LineNr">33 </span> tmp <span class="Special"><-</span> copy <span class="Constant">0xff</span> +<span id="L34" class="LineNr">34 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, tmp +<span id="L35" class="LineNr">35 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">" "</span> +<span id="L36" class="LineNr">36 </span> <span class="muComment"># g</span> +<span id="L37" class="LineNr">37 </span> tmp <span class="Special"><-</span> copy row +<span id="L38" class="LineNr">38 </span> tmp <span class="Special"><-</span> multiply col +<span id="L39" class="LineNr">39 </span> tmp <span class="Special"><-</span> and <span class="Constant">0x7f</span> +<span id="L40" class="LineNr">40 </span> tmp <span class="Special"><-</span> add <span class="Constant">0x80</span> +<span id="L41" class="LineNr">41 </span><span class="CommentedCode">#? tmp <- copy 0xcf</span> +<span id="L42" class="LineNr">42 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, tmp +<span id="L43" class="LineNr">43 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">" "</span> +<span id="L44" class="LineNr">44 </span> <span class="muComment"># b</span> +<span id="L45" class="LineNr">45 </span> tmp <span class="Special"><-</span> copy row +<span id="L46" class="LineNr">46 </span> tmp <span class="Special"><-</span> multiply col +<span id="L47" class="LineNr">47 </span> tmp <span class="Special"><-</span> and <span class="Constant">0x7f</span> +<span id="L48" class="LineNr">48 </span> tmp <span class="Special"><-</span> add <span class="Constant">0x80</span> +<span id="L49" class="LineNr">49 </span> <a href='../405screen.mu.html#L484'>print-int32-decimal</a> <span class="Constant">0</span>/screen, tmp +<span id="L50" class="LineNr">50 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"\n"</span> +<span id="L51" class="LineNr">51 </span> col <span class="Special"><-</span> increment +<span id="L52" class="LineNr">52 </span> <span class="PreProc">loop</span> +<span id="L53" class="LineNr">53 </span> <span class="Delimiter">}</span> +<span id="L54" class="LineNr">54 </span> row <span class="Special"><-</span> increment +<span id="L55" class="LineNr">55 </span> <span class="PreProc">loop</span> +<span id="L56" class="LineNr">56 </span> <span class="Delimiter">}</span> +<span id="L57" class="LineNr">57 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L58" class="LineNr">58 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> diff --git a/html/linux/apps/tui.mu.html b/html/linux/apps/tui.mu.html new file mode 100644 index 00000000..82681e6a --- /dev/null +++ b/html/linux/apps/tui.mu.html @@ -0,0 +1,98 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<title>Mu - linux/apps/tui.mu</title> +<meta name="Generator" content="Vim/8.2"> +<meta name="plugin-version" content="vim8.1_v2"> +<meta name="syntax" content="none"> +<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback"> +<meta name="colorscheme" content="minimal-light"> +<style> +<!-- +pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; } +body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; } +a { color:inherit; } +* { font-size:12pt; font-size: 1em; } +.PreProc { color: #c000c0; } +.muRegEcx { color: #870000; } +.Special { color: #ff6060; } +.LineNr { } +.Constant { color: #008787; } +.muFunction { color: #af5f00; text-decoration: underline; } +.Delimiter { color: #c000c0; } +.muComment { color: #005faf; } +.muRegEbx { color: #5f00ff; } +--> +</style> + +<script> +<!-- + +/* 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/main/linux/apps/tui.mu'>https://github.com/akkartik/mu/blob/main/linux/apps/tui.mu</a> +<pre id='vimCodeElement'> +<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Test some primitives for text-mode.</span> +<span id="L2" class="LineNr"> 2 </span><span class="muComment">#</span> +<span id="L3" class="LineNr"> 3 </span><span class="muComment"># To run:</span> +<span id="L4" class="LineNr"> 4 </span><span class="muComment"># $ ./translate apps/tui.mu</span> +<span id="L5" class="LineNr"> 5 </span><span class="muComment"># $ ./a.elf</span> +<span id="L6" class="LineNr"> 6 </span> +<span id="L7" class="LineNr"> 7 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='tui.mu.html#L7'>main</a></span><span class="PreProc"> -> </span>_/<span class="muRegEbx">ebx</span>: int <span class="Delimiter">{</span> +<span id="L8" class="LineNr"> 8 </span> <span class="PreProc">var</span> nrows/eax: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L9" class="LineNr"> 9 </span> <span class="PreProc">var</span> ncols/<span class="muRegEcx">ecx</span>: int <span class="Special"><-</span> copy <span class="Constant">0</span> +<span id="L10" class="LineNr">10 </span> nrows, ncols <span class="Special"><-</span> <a href='../405screen.mu.html#L60'>screen-size</a> <span class="Constant">0</span> +<span id="L11" class="LineNr">11 </span> <a href='../304screen.subx.html#L6'>enable-screen-grid-mode</a> +<span id="L12" class="LineNr">12 </span> <a href='../405screen.mu.html#L109'>move-cursor</a> <span class="Constant">0</span>/screen, <span class="Constant">5</span>/row, <span class="Constant">0x22</span>/col +<span id="L13" class="LineNr">13 </span> <a href='../405screen.mu.html#L523'>start-color</a> <span class="Constant">0</span>/screen, <span class="Constant">1</span>/fg, <span class="Constant">0x7a</span>/bg +<span id="L14" class="LineNr">14 </span> <a href='../405screen.mu.html#L583'>start-blinking</a> <span class="Constant">0</span>/screen +<span id="L15" class="LineNr">15 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"Hello world!"</span> +<span id="L16" class="LineNr">16 </span> <a href='../405screen.mu.html#L506'>reset-formatting</a> <span class="Constant">0</span>/screen +<span id="L17" class="LineNr">17 </span> <a href='../405screen.mu.html#L109'>move-cursor</a> <span class="Constant">0</span>/screen, <span class="Constant">6</span>/row, <span class="Constant">0x22</span>/col +<span id="L18" class="LineNr">18 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"tty dimensions: "</span> +<span id="L19" class="LineNr">19 </span> <a href='../405screen.mu.html#L440'>print-int32-hex</a> <span class="Constant">0</span>/screen, nrows +<span id="L20" class="LineNr">20 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">" rows, "</span> +<span id="L21" class="LineNr">21 </span> <a href='../405screen.mu.html#L440'>print-int32-hex</a> <span class="Constant">0</span>/screen, ncols +<span id="L22" class="LineNr">22 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">" rows\n"</span> +<span id="L23" class="LineNr">23 </span> +<span id="L24" class="LineNr">24 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"press a key to see its code: "</span> +<span id="L25" class="LineNr">25 </span> <a href='../305keyboard.subx.html#L6'>enable-keyboard-immediate-mode</a> +<span id="L26" class="LineNr">26 </span> <span class="PreProc">var</span> x/eax: grapheme <span class="Special"><-</span> <a href='../305keyboard.subx.html#L132'>read-key-from-real-keyboard</a> +<span id="L27" class="LineNr">27 </span> <a href='../305keyboard.subx.html#L72'>enable-keyboard-type-mode</a> +<span id="L28" class="LineNr">28 </span> <a href='../304screen.subx.html#L24'>enable-screen-type-mode</a> +<span id="L29" class="LineNr">29 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"You pressed "</span> +<span id="L30" class="LineNr">30 </span> <span class="PreProc">var</span> x-int/eax: int <span class="Special"><-</span> copy x +<span id="L31" class="LineNr">31 </span> <a href='../405screen.mu.html#L440'>print-int32-hex</a> <span class="Constant">0</span>/screen, x-int +<span id="L32" class="LineNr">32 </span> <a href='../405screen.mu.html#L169'>print-string</a> <span class="Constant">0</span>/screen, <span class="Constant">"\n"</span> +<span id="L33" class="LineNr">33 </span> <span class="PreProc">return</span> <span class="Constant">0</span> +<span id="L34" class="LineNr">34 </span><span class="Delimiter">}</span> +</pre> +</body> +</html> +<!-- vim: set foldmethod=manual : --> |