improved indentation and whitespace for markdown code blocks
All code blocks in markdown have been intended by at least 4 spaces. We add `--tab-stop=8` to all `pandoc(1)` executions in order to set a better tab stop indentation, especially for tab intended code blocks. Changed `bin/pandoc-wrapper.sh` VERSION from "1.5.3 2024-03-26" to "1.6 2024-03-31". Fixed `1991/brnstnd/sorta.README.md` so that the sorta code is code blocks. Removed unneeded paragraph group in copyright in `inc/footer.default.html`. Fixed excess whitespace after list Author on entry `index.html` page. Changed `bin/output-index-author.sh` VERSION from "1.5.2 2024-03-26" to "1.6 2024-03-31". Performed `make www` to perform the above and test under macOS. TODO: Fix the problem of the initial line of code being extra intended. TODO: Fix excess whitespace around code that is inline.
This commit is contained in:
1 parent
b84c27ce02
commit
a996f8d2b8
734 files changed
+16723
-16963
No files matched your search
+49
-49
@@ -1,7 +1,7 @@
|
||||
## To build:
|
||||
|
||||
```sh
|
||||
make
|
||||
make
|
||||
```
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ make
|
||||
The current status of this entry is:
|
||||
|
||||
```
|
||||
STATUS: INABIAF - please **DO NOT** fix
|
||||
STATUS: INABIAF - please **DO NOT** fix
|
||||
```
|
||||
|
||||
For more detailed information see [2020 carlini bugs](../../bugs.html#2020_carlini).
|
||||
@@ -19,21 +19,21 @@ For more detailed information see [2020 carlini bugs](../../bugs.html#2020_carli
|
||||
## To use:
|
||||
|
||||
```sh
|
||||
./prog
|
||||
./prog
|
||||
```
|
||||
|
||||
|
||||
## Try:
|
||||
|
||||
```sh
|
||||
./try.sh
|
||||
./try.sh
|
||||
```
|
||||
|
||||
Or if you actually want to play:
|
||||
|
||||
```sh
|
||||
# Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9
|
||||
./prog
|
||||
# Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9
|
||||
./prog
|
||||
```
|
||||
|
||||
|
||||
@@ -52,18 +52,18 @@ HOW ABOUT A NICE GAME OF <strike>CHESS</strike>TIC-TAC-TOE?
|
||||
### USAGE
|
||||
|
||||
```sh
|
||||
cc -o prog prog.c
|
||||
./prog
|
||||
cc -o prog prog.c
|
||||
./prog
|
||||
```
|
||||
|
||||
Alternates between P1 and P2. Enter a digit [1-9] to move:
|
||||
|
||||
```
|
||||
1 | 2 | 3
|
||||
---------
|
||||
4 | 5 | 6
|
||||
---------
|
||||
7 | 8 | 9
|
||||
1 | 2 | 3
|
||||
---------
|
||||
4 | 5 | 6
|
||||
---------
|
||||
7 | 8 | 9
|
||||
```
|
||||
|
||||
The game ends if:
|
||||
@@ -93,7 +93,7 @@ compilers unhappy, and others slow. (For example, passing GCC the flags `-Wall
|
||||
Under some terminals the program may exit early. The following should always work:
|
||||
|
||||
```sh
|
||||
echo "1 2 3 4 5 6 7" | ./prog
|
||||
echo "1 2 3 4 5 6 7" | ./prog
|
||||
```
|
||||
|
||||
### OBFUSCATION
|
||||
@@ -101,9 +101,9 @@ echo "1 2 3 4 5 6 7" | ./prog
|
||||
The entirety of the program consists of a single iterated call to `printf(3)`.
|
||||
|
||||
```c
|
||||
int main() {
|
||||
while(*d) printf(fmt, arg);
|
||||
}
|
||||
int main() {
|
||||
while(*d) printf(fmt, arg);
|
||||
}
|
||||
```
|
||||
|
||||
Here, `fmt` is a single string, and `arg` is a series of arguments to
|
||||
@@ -145,13 +145,13 @@ Format specifiers can take extra "arguments".
|
||||
For example, the following expression
|
||||
|
||||
```c
|
||||
printf("%1$.*2$d%3$hhn", 5, 10, &x)
|
||||
printf("%1$.*2$d%3$hhn", 5, 10, &x)
|
||||
```
|
||||
|
||||
will have the same effect as if we had written
|
||||
|
||||
```c
|
||||
x = 10;
|
||||
x = 10;
|
||||
```
|
||||
|
||||
because it will print out `0000000005` (5 padded to size 10) and then write the
|
||||
@@ -175,37 +175,37 @@ We can use format strings to compute the OR/NOT of arbitrary "bits".
|
||||
We'll start with the simplest, OR:
|
||||
|
||||
```c
|
||||
printf("%1$s%2$s%3$hhn", a, b, c)
|
||||
printf("%1$s%2$s%3$hhn", a, b, c)
|
||||
```
|
||||
|
||||
will compute
|
||||
|
||||
```c
|
||||
*c = strlen(a) + strlen(b)
|
||||
*c = strlen(a) + strlen(b)
|
||||
```
|
||||
|
||||
but given that `strlen(x)` is `1` for a 1-bit and `0` for a 0-bit, we have
|
||||
|
||||
```c
|
||||
*c = a | b
|
||||
*c = a | b
|
||||
```
|
||||
|
||||
Computing the NOT of a single value is also easy:
|
||||
|
||||
```c
|
||||
printf("%1$255d%1$s%hhn", a, b)
|
||||
printf("%1$255d%1$s%hhn", a, b)
|
||||
```
|
||||
|
||||
will compute
|
||||
|
||||
```c
|
||||
*b = (strlen(a)+255)%256 = strlen(a)-1
|
||||
*b = (strlen(a)+255)%256 = strlen(a)-1
|
||||
```
|
||||
|
||||
and again, because `strlen(x)` is either `1` or `0` we have
|
||||
|
||||
```c
|
||||
*c = !b
|
||||
*c = !b
|
||||
```
|
||||
|
||||
From here we can compute any binary circuit. Doing something efficient,
|
||||
@@ -222,11 +222,11 @@ pointers to three squares in a row to test, and `D` be where to save if there is
|
||||
win or not.
|
||||
|
||||
```c
|
||||
"%A$s%B$s%C$s%1$253d%11$hhn" // r11 = !(*A & *B & *C)
|
||||
ZERO
|
||||
"%11$s%1$255d%11hhn" // r11 = !r11
|
||||
ZERO
|
||||
"%11$s%D$s%D$hhn" // *D = *D | r11
|
||||
"%A$s%B$s%C$s%1$253d%11$hhn" // r11 = !(*A & *B & *C)
|
||||
ZERO
|
||||
"%11$s%1$255d%11hhn" // r11 = !r11
|
||||
ZERO
|
||||
"%11$s%D$s%D$hhn" // *D = *D | r11
|
||||
```
|
||||
|
||||
That is, we set `*D` to `1` if there is a three-in-a-row. We repeat this for all
|
||||
@@ -236,7 +236,7 @@ The ZERO macro ensures that the number of bytes written out is 0 mod 256 with
|
||||
the following expression
|
||||
|
||||
```c
|
||||
"%1$hhn%1$s" (repeated 256 times)
|
||||
"%1$hhn%1$s" (repeated 256 times)
|
||||
```
|
||||
|
||||
where argument 1 is a pointer to a temporary variable followed by a NUL byte.
|
||||
@@ -255,13 +255,13 @@ in `1$` the pointer to player 1's square, and `2$` the pointer to player 2's, an
|
||||
in `3$` the pointer to the board string, we can compute
|
||||
|
||||
```c
|
||||
"%1$s" (repeated 47 times) "%2$s" (repeated 56 times) %1$32d%3$hhn"
|
||||
"%1$s" (repeated 47 times) "%2$s" (repeated 56 times) %1$32d%3$hhn"
|
||||
```
|
||||
|
||||
which will, in effect, compute
|
||||
|
||||
```c
|
||||
*r3 = (*r1) * 47 + (*r2) * 56 + 32
|
||||
*r3 = (*r1) * 47 + (*r2) * 56 + 32
|
||||
```
|
||||
|
||||
which will output `' '` if neither are true, `'X'` if `r1` is true, or `'O'` if
|
||||
@@ -274,7 +274,7 @@ In order to be able to finally display the board, while still only using one
|
||||
`printf(3)` statement, we finish the statement with
|
||||
|
||||
```c
|
||||
"\n\033[2J\n%26$s"
|
||||
"\n\033[2J\n%26$s"
|
||||
```
|
||||
|
||||
which is the escape sequence to clear the screen, and then prints argument 26.
|
||||
@@ -285,12 +285,12 @@ tic-tac-toe board.
|
||||
After the board, we need to print one of the following strings:
|
||||
|
||||
```
|
||||
P1>_
|
||||
P2>_
|
||||
P1 WINS
|
||||
P2 WINS
|
||||
P1 TIES
|
||||
P2 TIES
|
||||
P1>_
|
||||
P2>_
|
||||
P1 WINS
|
||||
P2 WINS
|
||||
P1 TIES
|
||||
P2 TIES
|
||||
```
|
||||
|
||||
Depending on if it's P1 or P2's turn to move, the game is over and someone
|
||||
@@ -300,7 +300,7 @@ This turns out not to be as hard as it might look. Using the same trick as
|
||||
before, we set byte four to be
|
||||
|
||||
```c
|
||||
*byte4 = is_win * 'W' + is_tie * 'T'
|
||||
*byte4 = is_win * 'W' + is_tie * 'T'
|
||||
```
|
||||
|
||||
The byte `'I'` and `'S'` can always be the same, and we do the same for `'E'`/`'N'`.
|
||||
@@ -314,10 +314,10 @@ ends. It should just exit.
|
||||
One option would be to implement the logic as
|
||||
|
||||
```c
|
||||
printf()
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
printf()
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -325,10 +325,10 @@ but this would DOUBLE the number of calls to `printf(3)` we require. So instead
|
||||
implement it like this
|
||||
|
||||
```c
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
}
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
}
|
||||
```
|
||||
|
||||
(In reality we actually pass `scanf(3)` as an argument to avoid the extra
|
||||
|
||||
+50
-51
@@ -132,8 +132,7 @@
|
||||
<h2 id="author"><a name="author"></a>Author:</h2>
|
||||
<ul>
|
||||
<li>Name: <a href="../../authors.html#Nicholas_Carlini">Nicholas Carlini</a><br>
|
||||
Location: <a href="../../location.html#US">US</a> - <em>United States of America</em> (<em>United States</em>)
|
||||
<br><br></li>
|
||||
Location: <a href="../../location.html#US">US</a> - <em>United States of America</em> (<em>United States</em>)</li>
|
||||
</ul>
|
||||
|
||||
<!-- END: next line ends content generated by: bin/output-index-author.sh -->
|
||||
@@ -142,18 +141,18 @@ Location: <a href="../../location.html#US">US</a> - <em>United States of America
|
||||
<!-- START: this line starts content for HTML phase 21 by: bin/pandoc-wrapper.sh via bin/md2html.sh -->
|
||||
|
||||
<h2 id="to-build">To build:</h2>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">make</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">make</span></span></code></pre></div>
|
||||
<h3 id="bugs-and-misfeatures">Bugs and (Mis)features:</h3>
|
||||
<p>The current status of this entry is:</p>
|
||||
<pre><code>STATUS: INABIAF - please **DO NOT** fix</code></pre>
|
||||
<pre><code> STATUS: INABIAF - please **DO NOT** fix</code></pre>
|
||||
<p>For more detailed information see <a href="../../bugs.html#2020_carlini">2020 carlini bugs</a>.</p>
|
||||
<h2 id="to-use">To use:</h2>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">./prog</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<h2 id="try">Try:</h2>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">./try.sh</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a> <span class="ex">./try.sh</span></span></code></pre></div>
|
||||
<p>Or if you actually want to play:</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9</span></span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="ex">./prog</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a> <span class="co"># Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9</span></span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<h2 id="judges-remarks">Judges’ remarks:</h2>
|
||||
<p>This program consists of a single <code>printf(3)</code> statement wrapped in a <code>while</code>
|
||||
loop. You would not think that this would amount to much, but you would be very,
|
||||
@@ -162,14 +161,14 @@ ASCII art of the program source.</p>
|
||||
<h2 id="authors-remarks">Author’s remarks:</h2>
|
||||
<p>HOW ABOUT A NICE GAME OF <strike>CHESS</strike>TIC-TAC-TOE?</p>
|
||||
<h3 id="usage">USAGE</h3>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cc</span> <span class="at">-o</span> prog prog.c</span>
|
||||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="ex">./prog</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">cc</span> <span class="at">-o</span> prog prog.c</span>
|
||||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<p>Alternates between P1 and P2. Enter a digit [1-9] to move:</p>
|
||||
<pre><code>1 | 2 | 3
|
||||
---------
|
||||
4 | 5 | 6
|
||||
---------
|
||||
7 | 8 | 9</code></pre>
|
||||
<pre><code> 1 | 2 | 3
|
||||
---------
|
||||
4 | 5 | 6
|
||||
---------
|
||||
7 | 8 | 9</code></pre>
|
||||
<p>The game ends if:</p>
|
||||
<ul>
|
||||
<li>A player completes three in a row; that player wins.</li>
|
||||
@@ -188,12 +187,12 @@ patient.</p>
|
||||
<p>After macro expansion the format string is roughly 100K. This makes some
|
||||
compilers unhappy, and others slow. (For example, passing GCC the flags <code>-Wall -Wextra -pedantic</code> will result in a warning about oversized strings.)</p>
|
||||
<p>Under some terminals the program may exit early. The following should always work:</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">"1 2 3 4 5 6 7"</span> <span class="kw">|</span> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">"1 2 3 4 5 6 7"</span> <span class="kw">|</span> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<h3 id="obfuscation">OBFUSCATION</h3>
|
||||
<p>The entirety of the program consists of a single iterated call to <code>printf(3)</code>.</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span><span class="op">(*</span>d<span class="op">)</span> printf<span class="op">(</span>fmt<span class="op">,</span> arg<span class="op">);</span></span>
|
||||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a> <span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span><span class="op">(*</span>d<span class="op">)</span> printf<span class="op">(</span>fmt<span class="op">,</span> arg<span class="op">);</span></span>
|
||||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div>
|
||||
<p>Here, <code>fmt</code> is a single string, and <code>arg</code> is a series of arguments to
|
||||
<code>printf(3)</code>.</p>
|
||||
<p>While its primary purpose is to serve as The One True Debugger, <code>printf(3)</code> also happens
|
||||
@@ -219,9 +218,9 @@ this one <code>printf(3)</code> call (and a call to <code>scanf(3)</code> to rea
|
||||
<li><p><code>"%3$.*4$d"</code>: print argument 3 to <code>printf(3)</code> with as many zeros as in argument 4.</p></li>
|
||||
</ul>
|
||||
<p>For example, the following expression</p>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>printf<span class="op">(</span><span class="st">"%1$.*2$d%3$hhn"</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">10</span><span class="op">,</span> <span class="op">&</span>x<span class="op">)</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$.*2$d%3$hhn"</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">10</span><span class="op">,</span> <span class="op">&</span>x<span class="op">)</span></span></code></pre></div>
|
||||
<p>will have the same effect as if we had written</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> <span class="dv">10</span><span class="op">;</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="dv">10</span><span class="op">;</span></span></code></pre></div>
|
||||
<p>because it will print out <code>0000000005</code> (5 padded to size 10) and then write the
|
||||
number of bytes written to <code>x</code>.</p>
|
||||
<h4 id="printf3-oriented-programming"><code>printf(3)</code> Oriented Programming</h4>
|
||||
@@ -234,17 +233,17 @@ array–one bit per pair of bytes:</p>
|
||||
</ul>
|
||||
<p>We can use format strings to compute the OR/NOT of arbitrary “bits”.</p>
|
||||
<p>We’ll start with the simplest, OR:</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>printf<span class="op">(</span><span class="st">"%1$s%2$s%3$hhn"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">,</span> c<span class="op">)</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$s%2$s%3$hhn"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">,</span> c<span class="op">)</span></span></code></pre></div>
|
||||
<p>will compute</p>
|
||||
<div class="sourceCode" id="cb13"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>c <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)</span> <span class="op">+</span> strlen<span class="op">(</span>b<span class="op">)</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb13"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)</span> <span class="op">+</span> strlen<span class="op">(</span>b<span class="op">)</span></span></code></pre></div>
|
||||
<p>but given that <code>strlen(x)</code> is <code>1</code> for a 1-bit and <code>0</code> for a 0-bit, we have</p>
|
||||
<div class="sourceCode" id="cb14"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>c <span class="op">=</span> a <span class="op">|</span> b</span></code></pre></div>
|
||||
<div class="sourceCode" id="cb14"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> a <span class="op">|</span> b</span></code></pre></div>
|
||||
<p>Computing the NOT of a single value is also easy:</p>
|
||||
<div class="sourceCode" id="cb15"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>printf<span class="op">(</span><span class="st">"%1$255d%1$s</span><span class="sc">%hhn</span><span class="st">"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">)</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb15"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$255d%1$s</span><span class="sc">%hhn</span><span class="st">"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">)</span></span></code></pre></div>
|
||||
<p>will compute</p>
|
||||
<div class="sourceCode" id="cb16"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>b <span class="op">=</span> <span class="op">(</span>strlen<span class="op">(</span>a<span class="op">)+</span><span class="dv">255</span><span class="op">)%</span><span class="dv">256</span> <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)-</span><span class="dv">1</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb16"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>b <span class="op">=</span> <span class="op">(</span>strlen<span class="op">(</span>a<span class="op">)+</span><span class="dv">255</span><span class="op">)%</span><span class="dv">256</span> <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)-</span><span class="dv">1</span></span></code></pre></div>
|
||||
<p>and again, because <code>strlen(x)</code> is either <code>1</code> or <code>0</code> we have</p>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>c <span class="op">=</span> <span class="op">!</span>b</span></code></pre></div>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> <span class="op">!</span>b</span></code></pre></div>
|
||||
<p>From here we can compute any binary circuit. Doing something efficient,
|
||||
though, still takes work.</p>
|
||||
<h4 id="tic-tac-toe">Tic-Tac-Toe</h4>
|
||||
@@ -253,16 +252,16 @@ with a turn counter that alternates between player 1 and player 2.</p>
|
||||
<p>To detect who has won, we implement the following logic. Let <code>A</code>, <code>B</code>, and <code>C</code> be
|
||||
pointers to three squares in a row to test, and <code>D</code> be where to save if there is a
|
||||
win or not.</p>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="st">"</span><span class="sc">%A</span><span class="st">$s%B$s%C$s%1$253d%11$hhn"</span> <span class="co">// r11 = !(*A & *B & *C)</span></span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>ZERO</span>
|
||||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="st">"%11$s%1$255d</span><span class="sc">%11hhn</span><span class="st">"</span> <span class="co">// r11 = !r11</span></span>
|
||||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a>ZERO</span>
|
||||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="st">"%11$s%D$s%D$hhn"</span> <span class="co">// *D = *D | r11</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"</span><span class="sc">%A</span><span class="st">$s%B$s%C$s%1$253d%11$hhn"</span> <span class="co">// r11 = !(*A & *B & *C)</span></span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> ZERO</span>
|
||||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"%11$s%1$255d</span><span class="sc">%11hhn</span><span class="st">"</span> <span class="co">// r11 = !r11</span></span>
|
||||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> ZERO</span>
|
||||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"%11$s%D$s%D$hhn"</span> <span class="co">// *D = *D | r11</span></span></code></pre></div>
|
||||
<p>That is, we set <code>*D</code> to <code>1</code> if there is a three-in-a-row. We repeat this for all
|
||||
possible three-in-a-row configurations, for both players.</p>
|
||||
<p>The ZERO macro ensures that the number of bytes written out is 0 mod 256 with
|
||||
the following expression</p>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="st">"%1$hhn%1$s"</span> <span class="op">(</span>repeated <span class="dv">256</span> times<span class="op">)</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"%1$hhn%1$s"</span> <span class="op">(</span>repeated <span class="dv">256</span> times<span class="op">)</span></span></code></pre></div>
|
||||
<p>where argument 1 is a pointer to a temporary variable followed by a NUL byte.</p>
|
||||
<p>This works because if the current count is 0 mod 256, then <code>"%1$hhn"</code> will write
|
||||
zero to argument 1 and then <code>"%1$s"</code> will never emit any text. If, on the other
|
||||
@@ -274,31 +273,31 @@ times we’re eventually going to reach 0 mod 256.</p>
|
||||
bits to Xs and Os to print out. This is actually rather straightforward. Given
|
||||
in <code>1$</code> the pointer to player 1’s square, and <code>2$</code> the pointer to player 2’s, and
|
||||
in <code>3$</code> the pointer to the board string, we can compute</p>
|
||||
<div class="sourceCode" id="cb20"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="st">"%1$s"</span> <span class="op">(</span>repeated <span class="dv">47</span> times<span class="op">)</span> <span class="st">"%2$s"</span> <span class="op">(</span>repeated <span class="dv">56</span> times<span class="op">)</span> <span class="op">%</span><span class="dv">1</span>$<span class="dv">32</span><span class="er">d</span><span class="op">%</span><span class="dv">3</span>$hhn<span class="st">"</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb20"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"%1$s"</span> <span class="op">(</span>repeated <span class="dv">47</span> times<span class="op">)</span> <span class="st">"%2$s"</span> <span class="op">(</span>repeated <span class="dv">56</span> times<span class="op">)</span> <span class="op">%</span><span class="dv">1</span>$<span class="dv">32</span><span class="er">d</span><span class="op">%</span><span class="dv">3</span>$hhn<span class="st">"</span></span></code></pre></div>
|
||||
<p>which will, in effect, compute</p>
|
||||
<div class="sourceCode" id="cb21"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>r3 <span class="op">=</span> <span class="op">(*</span>r1<span class="op">)</span> <span class="op">*</span> <span class="dv">47</span> <span class="op">+</span> <span class="op">(*</span>r2<span class="op">)</span> <span class="op">*</span> <span class="dv">56</span> <span class="op">+</span> <span class="dv">32</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb21"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>r3 <span class="op">=</span> <span class="op">(*</span>r1<span class="op">)</span> <span class="op">*</span> <span class="dv">47</span> <span class="op">+</span> <span class="op">(*</span>r2<span class="op">)</span> <span class="op">*</span> <span class="dv">56</span> <span class="op">+</span> <span class="dv">32</span></span></code></pre></div>
|
||||
<p>which will output <code>' '</code> if neither are true, <code>'X'</code> if <code>r1</code> is true, or <code>'O'</code> if
|
||||
<code>r2</code> is true.</p>
|
||||
<h4 id="further-obfuscations">Further Obfuscations</h4>
|
||||
<p>In order to be able to finally display the board, while still only using one
|
||||
<code>printf(3)</code> statement, we finish the statement with</p>
|
||||
<div class="sourceCode" id="cb22"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="st">"</span><span class="sc">\n\033</span><span class="st">[2J</span><span class="sc">\n</span><span class="st">%26$s"</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb22"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"</span><span class="sc">\n\033</span><span class="st">[2J</span><span class="sc">\n</span><span class="st">%26$s"</span></span></code></pre></div>
|
||||
<p>which is the escape sequence to clear the screen, and then prints argument 26.
|
||||
Argument 26 is a pointer to a <code>char*</code> in memory, that initially is undefined,
|
||||
but within the <code>printf(3)</code> statement we will construct this string to look like a
|
||||
tic-tac-toe board.</p>
|
||||
<p>After the board, we need to print one of the following strings:</p>
|
||||
<pre><code>P1>_
|
||||
P2>_
|
||||
P1 WINS
|
||||
P2 WINS
|
||||
P1 TIES
|
||||
P2 TIES</code></pre>
|
||||
<pre><code> P1>_
|
||||
P2>_
|
||||
P1 WINS
|
||||
P2 WINS
|
||||
P1 TIES
|
||||
P2 TIES</code></pre>
|
||||
<p>Depending on if it’s P1 or P2’s turn to move, the game is over and someone
|
||||
won, or the game is over and it is a draw.</p>
|
||||
<p>This turns out not to be as hard as it might look. Using the same trick as
|
||||
before, we set byte four to be</p>
|
||||
<div class="sourceCode" id="cb24"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="op">*</span>byte4 <span class="op">=</span> is_win <span class="op">*</span> <span class="ch">'W'</span> <span class="op">+</span> is_tie <span class="op">*</span> <span class="ch">'T'</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb24"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>byte4 <span class="op">=</span> is_win <span class="op">*</span> <span class="ch">'W'</span> <span class="op">+</span> is_tie <span class="op">*</span> <span class="ch">'T'</span></span></code></pre></div>
|
||||
<p>The byte <code>'I'</code> and <code>'S'</code> can always be the same, and we do the same for <code>'E'</code>/<code>'N'</code>.</p>
|
||||
<p>We do this same on-the-fly creation of the <code>scanf(3)</code> format string, but for a
|
||||
different reason. We first want to run <code>printf(3)</code> to show the first board, and
|
||||
@@ -306,17 +305,17 @@ then alternate between runs to <code>scanf(3)</code> and <code>printf(3)</code>
|
||||
displaying moves. Importantly, we <em>do not</em> want a final <code>scanf(3)</code> when the game
|
||||
ends. It should just exit.</p>
|
||||
<p>One option would be to implement the logic as</p>
|
||||
<div class="sourceCode" id="cb25"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>printf<span class="op">()</span></span>
|
||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<div class="sourceCode" id="cb25"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">()</span></span>
|
||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
|
||||
<p>but this would DOUBLE the number of calls to <code>printf(3)</code> we require. So instead we
|
||||
implement it like this</p>
|
||||
<div class="sourceCode" id="cb26"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
|
||||
<div class="sourceCode" id="cb26"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div>
|
||||
<p>(In reality we actually pass <code>scanf(3)</code> as an argument to avoid the extra
|
||||
statement, but it has the same effect.)</p>
|
||||
<p>Notice there is now no initial <code>printf(3)</code>. In order make sure the program doesn’t
|
||||
|
||||
Reference in new issue
Block a user