| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Procedures in assembly - More Raz</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Procedures in assembly - More Raz"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | <li><a href="collections.html">Lists, arrays, maps and sorting</a></li> |
| 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| 28 | </ol> |
| 29 | <h3>Standing alone</h3><ol start='7'> |
| 30 | <li><a href="static.html">A program with no run time</a></li> |
| 31 | <li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li> |
| 32 | <li><a href="lifecycle.html">Starting, ending, and signals</a></li> |
| 33 | <li><a href="targets.html">One module, one file per target</a></li> |
| 34 | </ol> |
| 35 | <h3>The machine</h3><ol start='11'> |
| 36 | <li><a href="asm.html" aria-current="page">Procedures in assembly</a></li> |
| 37 | <li><a href="syscalls.html">System calls</a></li> |
| 38 | <li><a href="listing.html">What the compiler makes</a></li> |
| 39 | </ol> |
| 40 | <h3>Shared memory</h3><ol start='14'> |
| 41 | <li><a href="volatile.html">Volatile</a></li> |
| 42 | <li><a href="atomics.html">Atomics</a></li> |
| 43 | <li><a href="ordering.html">Ordering and publication</a></li> |
| 44 | <li><a href="lockfree.html">A lock-free stack</a></li> |
| 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | </ol></aside><article><p class="kicker">The machine · lesson 11 of 18</p><h1>Procedures in assembly</h1> |
| 47 | <p>Sometimes a program needs an instruction the language has no word for: reading the cycle counter, asking the CPU what it is, counting bits in one instruction, a square root in the hardware. A procedure whose body is <code>code</code> is machine code, written by hand in the target's assembly language, one instruction to each <code>;</code>, up to <code>end</code>.</p> |
| 48 | <p>The compiler doesn't look past what the instructions say. It trusts you, and you owe it a few rules.</p> |
| 49 | <h2>A first one</h2> |
| 50 | <p>This reads the processor's time stamp counter. The result of a procedure goes in <code>rax</code>; <code>rdtsc</code> leaves the counter in <code>edx:eax</code>, so the two halves are joined first.</p> |
| 51 | <div class="file"><span>Cycles.raz</span></div><pre class="code"><code><span class="kw">module</span> Cycles; |
| 52 | <span class="kw">import</span> Out; |
| 53 | <span class="kw">import</span> SYSTEM; |
| 54 | |
| 55 | <span class="kw">proc</span> rdtsc*: <span class="ty">U64</span>; |
| 56 | <span class="kw">code</span> |
| 57 | rdtsc; |
| 58 | <span class="kw">shl</span> rdx, <span class="number">32</span>; |
| 59 | <span class="kw">or</span> rax, rdx; |
| 60 | ret; |
| 61 | <span class="kw">end</span> rdtsc; |
| 62 | |
| 63 | <span class="kw">proc</span> main*; |
| 64 | <span class="kw">var</span> a, b: <span class="ty">U64</span>; |
| 65 | <span class="kw">begin</span> |
| 66 | a := rdtsc(); |
| 67 | b := rdtsc(); |
| 68 | <span class="kw">if</span> b > a <span class="kw">then</span> Out.str(<span class="string">"the counter moves forward\n"</span>); <span class="kw">end</span>; |
| 69 | <span class="kw">end</span>; |
| 70 | |
| 71 | <span class="kw">end</span> Cycles.</code></pre> |
| 72 | <pre class="output"><code>the counter moves forward |
| 73 | </code></pre> |
| 74 | <p>A code procedure is called like any procedure. Everything in it is yours, <code>ret</code> included.</p> |
| 75 | <h2>The convention</h2> |
| 76 | <p>A code procedure follows the System V convention, as every raz procedure does:</p> |
| 77 | <ul><li>Integer and pointer arguments arrive in <code>rdi</code>, <code>rsi</code>, <code>rdx</code>, <code>rcx</code>, <code>r8</code> and <code>r9</code>; <code>F32</code> and <code>F64</code> ones in <code>xmm0</code> to <code>xmm7</code>.</li><li>The result goes in <code>rax</code> (and <code>rdx</code> for a second) or <code>xmm0</code>.</li><li>It must keep <code>rbx</code>, <code>rbp</code>, <code>rsp</code> and <code>r12</code> to <code>r15</code> as it found them. <code>rax</code>, <code>rcx</code>, <code>rdx</code>, <code>rsi</code>, <code>rdi</code> and <code>r8</code> to <code>r11</code> are free to change.</li><li>A slice is two integers, its address and its length, as System V passes a struct of two. As a parameter it takes two registers in a row; as the result its address goes in <code>rax</code> and its length in <code>rdx</code>.</li><li>Its parameters and results are therefore of the types C can take, or slices: at most six integer and eight real parameters, and two results, a slice being the only one.</li></ul> |
| 78 | <p>That is why the next one is so short: the argument is already in <code>rdi</code>.</p> |
| 79 | <div class="file"><span>Bits.raz</span></div><pre class="code"><code><span class="kw">module</span> Bits; |
| 80 | <span class="kw">import</span> Out; |
| 81 | <span class="kw">import</span> SYSTEM; |
| 82 | |
| 83 | <span class="comment">(* the bytes of x in the opposite order: bswap rax, which the assembler |
| 84 | doesn't know, written as its bytes *)</span> |
| 85 | <span class="kw">proc</span> swap64(x: <span class="ty">U64</span>): <span class="ty">U64</span>; |
| 86 | <span class="kw">code</span> |
| 87 | mov rax, rdi; |
| 88 | db <span class="number">48H</span>, <span class="number">0FH</span>, <span class="number">0C8H</span>; |
| 89 | ret; |
| 90 | <span class="kw">end</span> swap64; |
| 91 | |
| 92 | <span class="comment">(* the number of bits set in x *)</span> |
| 93 | <span class="kw">proc</span> ones(x: <span class="ty">U64</span>): <span class="ty">INT</span>; |
| 94 | <span class="kw">code</span> |
| 95 | popcnt rax, rdi; |
| 96 | ret; |
| 97 | <span class="kw">end</span> ones; |
| 98 | |
| 99 | <span class="kw">proc</span> main*; |
| 100 | <span class="kw">begin</span> |
| 101 | Out.str(<span class="string">"0x"</span>); Out.hex(swap64(<span class="number">0102030405060708H</span>), <span class="number">16</span>); Out.ln; |
| 102 | Out.int(ones(<span class="number">0FFH</span>)); Out.char(<span class="string">' '</span>); |
| 103 | Out.int(ones(<span class="number">0</span>)); Out.char(<span class="string">' '</span>); |
| 104 | Out.int(ones(<span class="number">0FFFFFFFFFFFFFFFFH</span>)); Out.ln; |
| 105 | <span class="kw">end</span>; |
| 106 | |
| 107 | <span class="kw">end</span> Bits.</code></pre> |
| 108 | <pre class="output"><code>0x0807060504030201 |
| 109 | 8 0 64 |
| 110 | </code></pre> |
| 111 | <p><code>db</code> writes any bytes, for an instruction the assembler doesn't know. Which instructions it knows is the implementation's business: this one knows <code>popcnt</code> but not <code>bswap</code>, and <code>db</code> keeps you from waiting for it to learn one. (<code>bswap r64</code> is <code>REX.W 0F C8+r</code> in the processor manuals.)</p> |
| 112 | <h2>Labels and loops</h2> |
| 113 | <p>A label, <code>name:</code>, names the place of the instruction after it, within the procedure. Jumps go to labels, and a constant of the module may stand wherever a number does. This sums a slice of <code>INT</code>s, whose address arrives in <code>rdi</code> and its length in <code>rsi</code>:</p> |
| 114 | <div class="file"><span>Sum.raz</span></div><pre class="code"><code><span class="kw">module</span> Sum; |
| 115 | <span class="kw">import</span> Out; |
| 116 | <span class="kw">import</span> SYSTEM; |
| 117 | |
| 118 | <span class="comment">(* the sum of the INTs of a *)</span> |
| 119 | <span class="kw">proc</span> sum(a: []<span class="ty">INT</span>): <span class="ty">INT</span>; |
| 120 | <span class="kw">code</span> |
| 121 | <span class="kw">xor</span> eax, eax; |
| 122 | test rsi, rsi; |
| 123 | jle done; |
| 124 | again: |
| 125 | add rax, [rdi]; |
| 126 | add rdi, <span class="number">8</span>; |
| 127 | dec rsi; |
| 128 | jnz again; |
| 129 | done: |
| 130 | ret; |
| 131 | <span class="kw">end</span> sum; |
| 132 | |
| 133 | <span class="comment">(* the same in raz, to check it against *)</span> |
| 134 | <span class="kw">proc</span> sumRaz(a: []<span class="ty">INT</span>): <span class="ty">INT</span>; |
| 135 | <span class="kw">var</span> i, t: <span class="ty">INT</span>; |
| 136 | <span class="kw">begin</span> |
| 137 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(a) - <span class="number">1</span> <span class="kw">do</span> t := t + a[i]; <span class="kw">end</span>; |
| 138 | <span class="kw">return</span> t; |
| 139 | <span class="kw">end</span>; |
| 140 | |
| 141 | <span class="kw">proc</span> main*; |
| 142 | <span class="kw">var</span> a: [<span class="number">6</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; |
| 143 | <span class="kw">begin</span> |
| 144 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span> a[i] := (i + <span class="number">1</span>) * <span class="number">100</span>; <span class="kw">end</span>; |
| 145 | Out.int(sum(a)); Out.char(<span class="string">' '</span>); |
| 146 | Out.int(sumRaz(a)); Out.char(<span class="string">' '</span>); |
| 147 | Out.int(sum(a[<span class="number">2</span>..<span class="number">4</span>])); Out.char(<span class="string">' '</span>); |
| 148 | Out.int(sum(a[<span class="number">0</span>..<span class="number">0</span>])); Out.ln; |
| 149 | <span class="kw">end</span>; |
| 150 | |
| 151 | <span class="kw">end</span> Sum.</code></pre> |
| 152 | <pre class="output"><code>2100 2100 700 0 |
| 153 | </code></pre> |
| 154 | <p>The procedure trusts the length it is given, as it must: the checks of the language end where its code begins.</p> |
| 155 | <p>A slice comes back the same way, its address in <code>rax</code> and its length in <code>rdx</code>. This one returns what is left of a string past its leading blanks:</p> |
| 156 | <div class="file"><span>Trim.raz</span></div><pre class="code"><code><span class="kw">module</span> Trim; |
| 157 | <span class="kw">import</span> Out; |
| 158 | <span class="kw">import</span> SYSTEM; |
| 159 | |
| 160 | <span class="comment">(* s past its leading blanks *)</span> |
| 161 | <span class="kw">proc</span> trim(s: []<span class="kw">const</span> <span class="ty">U8</span>): []<span class="kw">const</span> <span class="ty">U8</span>; |
| 162 | <span class="kw">code</span> |
| 163 | mov rax, rdi; |
| 164 | mov rdx, rsi; |
| 165 | again: |
| 166 | test rdx, rdx; |
| 167 | jz done; |
| 168 | cmp byte [rax], <span class="number">32</span>; |
| 169 | jne done; |
| 170 | inc rax; |
| 171 | dec rdx; |
| 172 | jmp again; |
| 173 | done: |
| 174 | ret; |
| 175 | <span class="kw">end</span> trim; |
| 176 | |
| 177 | <span class="kw">proc</span> main*; |
| 178 | <span class="kw">begin</span> |
| 179 | Out.char(<span class="string">'['</span>); Out.str(trim(<span class="string">" raz"</span>)); Out.str(<span class="string">"]\n"</span>); |
| 180 | Out.char(<span class="string">'['</span>); Out.str(trim(<span class="string">"raz"</span>)); Out.str(<span class="string">"]\n"</span>); |
| 181 | Out.char(<span class="string">'['</span>); Out.str(trim(<span class="string">" "</span>)); Out.str(<span class="string">"]\n"</span>); |
| 182 | <span class="kw">end</span>; |
| 183 | |
| 184 | <span class="kw">end</span> Trim.</code></pre> |
| 185 | <pre class="output"><code>[raz] |
| 186 | [raz] |
| 187 | [] |
| 188 | </code></pre> |
| 189 | <p>A procedure may have up to 64 labels. The check against the raz version is the habit worth keeping: write the procedure in raz first, then in assembly, and compare their results on awkward inputs.</p> |
| 190 | <h2>Two results, and reals</h2> |
| 191 | <p>A second result goes in <code>rdx</code>. That is exactly where <code>idiv</code> leaves the remainder, so <code>divmod</code> is four instructions. Reals travel in the <code>xmm</code> registers.</p> |
| 192 | <div class="file"><span>Divmod.raz</span></div><pre class="code"><code><span class="kw">module</span> Divmod; |
| 193 | <span class="kw">import</span> Out, Fmt; |
| 194 | <span class="kw">import</span> SYSTEM; |
| 195 | |
| 196 | <span class="comment">(* quotient and remainder, as idiv gives them: rax and rdx *)</span> |
| 197 | <span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| 198 | <span class="kw">code</span> |
| 199 | mov rax, rdi; |
| 200 | cqo; |
| 201 | idiv rsi; |
| 202 | ret; |
| 203 | <span class="kw">end</span> divmod; |
| 204 | |
| 205 | <span class="comment">(* a * x + b, on F64s in xmm0 to xmm2 *)</span> |
| 206 | <span class="kw">proc</span> fma(a, x, b: <span class="ty">F64</span>): <span class="ty">F64</span>; |
| 207 | <span class="kw">code</span> |
| 208 | mulsd xmm0, xmm1; |
| 209 | addsd xmm0, xmm2; |
| 210 | ret; |
| 211 | <span class="kw">end</span> fma; |
| 212 | |
| 213 | <span class="kw">proc</span> main*; |
| 214 | <span class="kw">var</span> q, r: <span class="ty">INT</span>; |
| 215 | <span class="kw">begin</span> |
| 216 | q, r := divmod(-<span class="number">17</span>, <span class="number">5</span>); |
| 217 | Out.int(q); Out.char(<span class="string">' '</span>); Out.int(r); Out.ln; |
| 218 | Fmt.print(<span class="string">"{}\n"</span>, [Fmt.real(fma(<span class="number">2.0</span>, <span class="number">3.5</span>, <span class="number">0.25</span>), <span class="number">2</span>)]); |
| 219 | <span class="kw">end</span>; |
| 220 | |
| 221 | <span class="kw">end</span> Divmod.</code></pre> |
| 222 | <pre class="output"><code>-3 -2 |
| 223 | 7.25 |
| 224 | </code></pre> |
| 225 | <p><code>idiv</code> by zero stops the program with the machine's own division fault. It isn't a check the compiler added, since the compiler doesn't look inside a code procedure.</p> |
| 226 | <h2>Keeping what isn't yours</h2> |
| 227 | <p>A procedure that uses <code>rbx</code> must put it back. <code>cpuid</code> clobbers <code>rbx</code>, so the vendor-string example saves it with <code>push</code> and restores it with <code>pop</code>, and keeps the buffer's address in <code>r8</code> while it works:</p> |
| 228 | <div class="file"><span>Vendor.raz</span></div><pre class="code"><code><span class="kw">module</span> Vendor; |
| 229 | <span class="kw">import</span> Out; |
| 230 | <span class="kw">import</span> SYSTEM; |
| 231 | |
| 232 | <span class="comment">(* the 12 bytes of the processor vendor, in buf (rdi) *)</span> |
| 233 | <span class="kw">proc</span> vendor(buf: ^<span class="ty">U8</span>); |
| 234 | <span class="kw">code</span> |
| 235 | push rbx; |
| 236 | mov r8, rdi; |
| 237 | <span class="kw">xor</span> eax, eax; |
| 238 | cpuid; |
| 239 | mov [r8], ebx; |
| 240 | mov [r8 + <span class="number">4</span>], edx; |
| 241 | mov [r8 + <span class="number">8</span>], ecx; |
| 242 | pop rbx; |
| 243 | ret; |
| 244 | <span class="kw">end</span> vendor; |
| 245 | |
| 246 | <span class="kw">proc</span> main*; |
| 247 | <span class="kw">var</span> buf: [<span class="number">12</span>]<span class="ty">U8</span>; i: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>; |
| 248 | <span class="kw">begin</span> |
| 249 | vendor(SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>])); |
| 250 | ok := <span class="ty">true</span>; |
| 251 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">11</span> <span class="kw">do</span> |
| 252 | <span class="kw">if</span> (buf[i] < <span class="string">' '</span>) <span class="kw">or</span> (buf[i] > <span class="string">'~'</span>) <span class="kw">then</span> ok := <span class="ty">false</span>; <span class="kw">end</span>; |
| 253 | <span class="kw">end</span>; |
| 254 | <span class="kw">if</span> ok <span class="kw">then</span> Out.str(<span class="string">"the vendor is 12 printable bytes\n"</span>); <span class="kw">end</span>; |
| 255 | <span class="kw">end</span>; |
| 256 | |
| 257 | <span class="kw">end</span> Vendor.</code></pre> |
| 258 | <pre class="output"><code>the vendor is 12 printable bytes |
| 259 | </code></pre> |
| 260 | <h2>What goes wrong</h2> |
| 261 | <p>A code procedure that breaks its rules breaks the program, and what follows isn't defined. Writing through a wrong pointer, a changed <code>rbx</code>, a missing <code>ret</code>, a stack left unbalanced: the language can't help, because it doesn't see inside. Keep them short, give each a comment that says what it expects in which register, test each against a raz twin, and use them for what only the machine can do.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="targets.html">← One module, one file per target</a><a href="index.html">Contents</a><a class="next" href="syscalls.html">System calls →</a></div></article></div> |
| 262 | </main> |
| 263 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 264 | </body> |
| 265 | </html> |