<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Computing: the Details</title>
      <link>http://computingthedetails.org</link>
      <description>Lex&#x27;s blog of curious computer shenanigans</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="http://computingthedetails.org/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Wed, 18 Mar 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>He built a robot with three hands</title>
          <pubDate>Wed, 18 Mar 2026 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/he-built-a-robot-with-three-hands/</link>
          <guid>http://computingthedetails.org/blog/he-built-a-robot-with-three-hands/</guid>
          <description xml:base="http://computingthedetails.org/blog/he-built-a-robot-with-three-hands/">&lt;audio controls src=&quot;posts&amp;#x2F;three_hands&amp;#x2F;he_built_a_robot_with_three_hands.ogg&quot;&gt;&lt;&#x2F;audio&gt;
&lt;p&gt;He built tables,&lt;br &#x2F;&gt;
he built cabinets,&lt;br &#x2F;&gt;
he built chairs.&lt;br &#x2F;&gt;
He even built rocking horses for children to enjoy.&lt;&#x2F;p&gt;
&lt;p&gt;He built out of wood,&lt;br &#x2F;&gt;
he built things that were good,&lt;br &#x2F;&gt;
he&#x27;d build things all day if he could.&lt;&#x2F;p&gt;
&lt;p&gt;He was asked to make this,&lt;br &#x2F;&gt;
he was asked to make that,&lt;br &#x2F;&gt;
he was asked to make more and more.&lt;br &#x2F;&gt;
He would use his hammer, his chisel, a big pot of glue, and also his trusty saw.&lt;&#x2F;p&gt;
&lt;p&gt;He was one of the best,&lt;br &#x2F;&gt;
he was known for his craft.&lt;br &#x2F;&gt;
He could be trusted to build you a seaworthy raft.&lt;&#x2F;p&gt;
&lt;p&gt;He made great things, that many people wanted,&lt;br &#x2F;&gt;
he couldn&#x27;t keep up with demands.&lt;br &#x2F;&gt;
He one day decided, this should be automated,&lt;br &#x2F;&gt;
so he built a robot with three hands.&lt;&#x2F;p&gt;
&lt;p&gt;He often wished he had a third hand,&lt;br &#x2F;&gt;
he&#x27;d use it very often if he did.&lt;br &#x2F;&gt;
His logic was sound, or so he thought,&lt;br &#x2F;&gt;
his robot&#x27;s arms were more dexterous than his.&lt;&#x2F;p&gt;
&lt;p&gt;The robot made cuts,&lt;br &#x2F;&gt;
the robot sanded edges,&lt;br &#x2F;&gt;
the robot glued boards together.&lt;br &#x2F;&gt;
The robot did what it was told,&lt;br &#x2F;&gt;
the robot could do whatever.&lt;&#x2F;p&gt;
&lt;p&gt;He still finished each object with care and attention,&lt;br &#x2F;&gt;
he had a reputation to uphold.&lt;br &#x2F;&gt;
He said &quot;only a fool would trust this machine&quot;,&lt;br &#x2F;&gt;
he said &quot;nobody would be so bold&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;The robot left splinters,&lt;br &#x2F;&gt;
the robot left subtle gaps.&lt;br &#x2F;&gt;
The robot made toys from low-quality scraps.&lt;&#x2F;p&gt;
&lt;p&gt;The robot was large, and wasteful, and loud.&lt;br &#x2F;&gt;
The robot worked fast and made sawdust clouds.&lt;br &#x2F;&gt;
The robot once drew blood from its master,&lt;br &#x2F;&gt;
he regretted making the chisel move faster.&lt;&#x2F;p&gt;
&lt;p&gt;He said it was an accident, a one-off event.&lt;br &#x2F;&gt;
He added some controls so it couldn&#x27;t happen again.&lt;&#x2F;p&gt;
&lt;p&gt;He got an order from a customer he&#x27;d seen before,&lt;br &#x2F;&gt;
he needed to deliver two rocking horses to their door.&lt;br &#x2F;&gt;
He knew them well, the couple that lives on the corner,&lt;br &#x2F;&gt;
he was to make the toys for their twin daughters.&lt;&#x2F;p&gt;
&lt;p&gt;He was feeling unwell in a typical way,&lt;br &#x2F;&gt;
he&#x27;d caught the flu, so was resting in bed.&lt;br &#x2F;&gt;
He had a deadline, the twin&#x27;s birthday.&lt;br &#x2F;&gt;
He let the robot build the horses instead.&lt;&#x2F;p&gt;
&lt;p&gt;The robot built tables,&lt;br &#x2F;&gt;
the robot built cabinets,&lt;br &#x2F;&gt;
the robot built chairs.&lt;br &#x2F;&gt;
The robot even built rocking horses for children to enjoy.&lt;&#x2F;p&gt;
&lt;p&gt;He recovered in time to deliver the toys,&lt;br &#x2F;&gt;
he loved days like today, they were full of joys.&lt;br &#x2F;&gt;
He took the horses and put them in the right places,&lt;br &#x2F;&gt;
he saw the excitement on the young girls&#x27; faces.&lt;&#x2F;p&gt;
&lt;p&gt;He returned to his workshop, his face was gleaming.&lt;br &#x2F;&gt;
He noticed that the shop needed some cleaning.&lt;br &#x2F;&gt;
The robot was messy, but never did the sweeping.&lt;&#x2F;p&gt;
&lt;p&gt;He cleaned up the dust, and disposed of the scraps,&lt;br &#x2F;&gt;
he looked at his tools as he put them all back.&lt;br &#x2F;&gt;
His chisel was blunt, and his saw was bent,&lt;br &#x2F;&gt;
his pot of glue empty, completely spent.&lt;&#x2F;p&gt;
&lt;p&gt;The robot didn&#x27;t care, it had a job to do.&lt;br &#x2F;&gt;
The robot doesn&#x27;t know when it has run out of glue.&lt;br &#x2F;&gt;
The robot had never seen a sharpening stone.&lt;br &#x2F;&gt;
The robot is just a machine, it couldn&#x27;t have known.&lt;&#x2F;p&gt;
&lt;p&gt;The robot did everything the same every time,&lt;br &#x2F;&gt;
the robot only knew one cabinet design.&lt;br &#x2F;&gt;
The robot lacked character, experience, and care,&lt;br &#x2F;&gt;
the robot just kept making the same old chair.&lt;&#x2F;p&gt;
&lt;p&gt;He got a phone call later that day,&lt;br &#x2F;&gt;
he couldn&#x27;t believe what the caller had to say.&lt;br &#x2F;&gt;
His horses had hurt those two girls today,&lt;br &#x2F;&gt;
he felt sick again, but in a different way.&lt;&#x2F;p&gt;
&lt;p&gt;One horse collapsed, the seat broke in two,&lt;br &#x2F;&gt;
perhaps that joint really needed more glue.&lt;br &#x2F;&gt;
The other had splinters that pierced through skin,&lt;br &#x2F;&gt;
there was blood on the horse, and an upset twin.&lt;&#x2F;p&gt;
&lt;p&gt;He had a reputation for quality and detail.&lt;br &#x2F;&gt;
He didn&#x27;t want to show his face after such a big fail.&lt;br &#x2F;&gt;
He refunded the parents with cash in the mail.&lt;br &#x2F;&gt;
In the past, he had a reputation for quality and detail.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Giant LED Rubik&#x27;s Cube V2: Part 1, planning and software stuff</title>
          <pubDate>Mon, 09 Mar 2026 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/giant-led-cube-v2-part1/</link>
          <guid>http://computingthedetails.org/blog/giant-led-cube-v2-part1/</guid>
          <description xml:base="http://computingthedetails.org/blog/giant-led-cube-v2-part1/">&lt;p&gt;I built a Giant Playable LED Rubik&#x27;s Cube, and I took it to EMF Camp in 2022.&lt;&#x2F;p&gt;
&lt;p&gt;That cube had a single LED per facelet, for a total of 45 LEDs across the cube (because the bottom face is not there, and 5 times 9 is 45). They were powerful LEDs, each one was 3W. That&#x27;s big for one LED, but not that big for the amount of area covered.&lt;&#x2F;p&gt;
&lt;p&gt;Cube V1 was not playable during daylight hours. It was just a white box. It was only playable after the sun had gone, and the lights were visible. I want to fix that.&lt;&#x2F;p&gt;
&lt;p&gt;EMF 2026 is soon! Perhaps before then I can get enough LED matrix panels to make cube V2 with about 100,000 LEDs this time, rather than just 45. That might actually be capable of combating the sun if I do!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;cube.png&quot; alt=&quot;A Giant Rubik&amp;#39;s Cube in my living room&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-plan&quot;&gt;The Plan&lt;&#x2F;h3&gt;
&lt;p&gt;I have all the old parts from the version 1 cube. Some of them broke in 2022, some of them have deteriorated in the weather as they were stored outside. Some of them are still in good condition.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;paddle.png&quot; alt=&quot;One of the paddles from the original cube. Warped, rusty, and covered in diry and cobwebs&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For the structure of the cube: my plan is to build a new frame for the cube, but to reuse the edge sections. I will need to replace the switch mechanisms on all of the edges, since those were just bare plywood and they all swelled up and seized in the rain in the field in 2022.&lt;&#x2F;p&gt;
&lt;p&gt;By the way, these are very expensive, so if you want to help out then please send me some money on Patreon or YouTube (links in the sidebar). Or, even better, if you know someone who has lots of these panels spare then please tell them I want them!&lt;&#x2F;p&gt;
&lt;p&gt;For the software: most of the software will still work for the new cube, I just need to add in something to generate the video signal for the LED panels.&lt;&#x2F;p&gt;
&lt;p&gt;For the hardware: again, some parts are reusable, but I need some additional hardware for driving the LED panels.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-panels&quot;&gt;The Panels&lt;&#x2F;h3&gt;
&lt;p&gt;There&#x27;s lots of generic LED panels available, mostly from manufacturers in China, which are all fairly similar in their operation and construction. They tend to be mounted in chassis that lock together in a grid, and have sockets to daisy-chain power and data between the panels. Power is normally from a Neutrik Powercon connector, and data via a thing that is essentially ethernet, and also uses RJ45 connectors.&lt;&#x2F;p&gt;
&lt;p&gt;The LED panels I&#x27;m planning to use are square, 0.5m on an edge, and need to be rated for outdoor use.&lt;&#x2F;p&gt;
&lt;p&gt;I currently have one panel. I need 45.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;panel.jpg&quot; alt=&quot;The front of a 48x48 LED panel.&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;panel_connector.jpg&quot; alt=&quot;The back of an LED panel, showing the connectors. From left to right: Power in, data in, power out, data out.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;phase-1-software-fixes&quot;&gt;Phase 1: Software fixes&lt;&#x2F;h3&gt;
&lt;p&gt;The first thing I&#x27;m doing towards making this new cube exist is to make the software output the right thing.&lt;&#x2F;p&gt;
&lt;p&gt;I can put whatever image I like on each face of this cube, so I&#x27;m planning to have a small Linux computer with a modest graphics card to generate the graphics for the cube. I have experience with OpenGL shader programming, so I&#x27;m going to have some fancy shaders on the cube hopefully.&lt;&#x2F;p&gt;
&lt;p&gt;The software is currently in 3 parts:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Device: the software running on the Raspberry Pi Pico to read the input switches and (in V1) output data to the WS2811 LED string.&lt;&#x2F;li&gt;
&lt;li&gt;Service: basic game logic. Also handles sound output. Runs on a Linux computer. This part connects to the Device layer via serial-over-USB.&lt;&#x2F;li&gt;
&lt;li&gt;Controller: the GUI that is shown on a small PC monitor near the installation, shows the timer, and the button for starting a new solve. It talks to the Service layer over TCP.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Each layer has a copy of the state of the cube. In the case of the Controller, it displays the current cube state on the screen. In the case of the Service program, it only really needs it for if the Controller connects mid-game and requests to know the current state. For the Device layer, it needs to drive the LEDs, so it needs to know the state to be able to do that.&lt;&#x2F;p&gt;
&lt;p&gt;Because each layer maintains this state, they need to stay in sync. The Device and Service almost never get out of sync, since the USB serial connection is fairly reliable. The Controller can get out of sync if it is connecting over unreliable WiFi, for example.&lt;&#x2F;p&gt;
&lt;p&gt;Regardless of how well these all stay in sync, it&#x27;s best to make sure that whatever the player sees is considered the canonically correct state. That&#x27;s why the Device layer is what decides when the user has solved the cube. Even in the worst possible desync state, the game still works correctly.&lt;&#x2F;p&gt;
&lt;p&gt;In version 2 of the cube, it will no longer be the Device code that creates the visual output. I&#x27;ll be adding in the graphics generation feature to the Service layer, since that&#x27;s running on a Linux PC with a graphics card, it has the processing power for the task, while the Pico does not. Since it is now what&#x27;s generating the output, it will also need to become the canonically true model of the game state.&lt;&#x2F;p&gt;
&lt;p&gt;The hardware for the LED panels takes input from DVI or HDMI just like any other display, but comes with configuration software that allows you to specify where each panel is within the resulting frame buffer. I will have a fullscreen OpenGL window displaying on the output, and render the 45 tiles in a grid somewhere on that framebuffer, and then configure the drivers to extract each one to a tile.&lt;&#x2F;p&gt;
&lt;p&gt;Since it&#x27;s easy enough to do, I will likely have my normal calibration workflow include a step that can move the tiles around in the Service program, so I don&#x27;t have to deal with reconfiguring the LED hardware in the field.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ve got the basics working. Here&#x27;s an example of what the framebuffer looks like so far, along with a rendering of a frame mapped on to a cube in Blender.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;fb_shuffle.png&quot; alt=&quot;A grid of coloured squares in the top left corner of a large black rectangle.&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;giant_led_cube_v2&#x2F;fb_render.png&quot; alt=&quot;A rendering of a shuffled Rubik&amp;#39;s cube.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-s-next&quot;&gt;What&#x27;s next&lt;&#x2F;h3&gt;
&lt;p&gt;I still need to add animations, and make it look prettier. Then I need to build a case to hold the computer, and build a new frame for the LED panels, since the old frame is too flimsy.&lt;&#x2F;p&gt;
&lt;p&gt;oh, and also obtain a whole lot of LED panels.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>My Postcode Lookup Library now Supports Partial Postcodes</title>
          <pubDate>Tue, 13 Jan 2026 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/my-postcode-lookup-library-now-supports-partial-postcodes/</link>
          <guid>http://computingthedetails.org/blog/my-postcode-lookup-library-now-supports-partial-postcodes/</guid>
          <description xml:base="http://computingthedetails.org/blog/my-postcode-lookup-library-now-supports-partial-postcodes/">&lt;h2 id=&quot;new-feature-time&quot;&gt;New feature time!&lt;&#x2F;h2&gt;
&lt;p&gt;In the previous blog post in this saga, I introduced my privacy-preserving postcode lookup library that you can use on your website.&lt;&#x2F;p&gt;
&lt;p&gt;Want to use it on your website for a find-my-nearest type feature? find it &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;codeberg.org&#x2F;lexbailey&#x2F;nearmypostcode&quot;&gt;on Codeberg&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, it now also knows how to find a location from just an outward code!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;quick-recap-what-s-a-postcode&quot;&gt;Quick recap: What&#x27;s a postcode&lt;&#x2F;h2&gt;
&lt;p&gt;A postcode is two parts. Some examples:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Postcode:     | SW1A2AA | B1␣␣1RS |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Outward code: | SW1A    | B1␣␣    |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Inward code:  |     2AA |     1RS |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;␣&amp;quot; = space character&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One outward code contains many inward codes in a single geographical area.&lt;&#x2F;p&gt;
&lt;p&gt;Knowing the outward half of a postcode is normally enough to locate a rough area of a city. The Westminster postcode SW1A, for example, contains 143 inward codes at the time of writing.&lt;&#x2F;p&gt;
&lt;p&gt;Postcodes can always be rewritten in what I call the &quot;canonical&quot; format, which is always exactly 7 characters long. Shorter postcodes get padded with spaces between the outward and inward codes, so that they are always 7 characters.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-use-only-outward-codes&quot;&gt;Why use only outward codes?&lt;&#x2F;h2&gt;
&lt;p&gt;Sometimes, you don&#x27;t know an entire postcode for a location you want to search near, but you know some outward codes in the general area. For example, I don&#x27;t know any specific postcodes in Birmingham, but I do know that B1 is going to be somewhere near the centre of Birmingham. That&#x27;d be sufficient to find my nearest store if I&#x27;m using a store locator tool on some website while in Birmingham.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s a good habit to get in to to avoid handing out your full postcode, for privacy reasons. I don&#x27;t want my lookup tool to break that habit for anyone.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s a privacy win, and also a usability win!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;okay-so-just-add-them-in-why-is-it-that-hard&quot;&gt;Okay, so just add them in, why is it that hard?&lt;&#x2F;h2&gt;
&lt;p&gt;Aha! With that question, you&#x27;ve activated my trap card!&lt;&#x2F;p&gt;
&lt;p&gt;Strap in!&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so perhaps you&#x27;re thinking I just need to add an entry for each outward code. It should easily fit in the space for a postcode, since it&#x27;s smaller than a postcode. Right?&lt;&#x2F;p&gt;
&lt;p&gt;Wrong! The way that I encode a postcode in the file is very particular to the way that it is formatted. The first two characters of the postcode are used to determine which section of the file to search. This is done in the lookup table at the start of the file. This is fine, all outward codes have at least two characters.&lt;&#x2F;p&gt;
&lt;p&gt;Once the software knows the section of the file to search, it starts reading postcodes one by one, checking to see if any one matches the given code. The way it does this is not obvious.&lt;&#x2F;p&gt;
&lt;p&gt;Postcodes processed by this software until now have always been exactly 7 characters long. The last 5 characters of each one is converted in to a number in a mixed-radix.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S W 1 A  2 A A&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  | | 1  ×           1 =       1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  | 1    ×          26 =      26&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  2      ×       26×26 =    1352&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | 0         ×    10×26×26 =       0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | 27          × 37×10×26×26 = 6753240&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;6753240 + 0 + 1352 + 26 + 1 = 6754619&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;??W1A2AA&amp;quot; = 6754619&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Eeach column has a set of possible characters that it can contain, and the size of that set determines the multiplier for that column.&lt;&#x2F;p&gt;
&lt;p&gt;So, the postcode SW1A2AA is the postcode with value 6754619 (&quot;W1A2AA&quot;), in the section of the file for the &quot;SW&quot; prefix.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose now I was to encode just the outward code. What does that even mean? I can&#x27;t just fill in zeros where the missing characters are, since that would accidentally encode a different suffix. For example, I could encode the four character outward code like so:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S W 1 A&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | 0         ×  1 = 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | 27          × 37 = 999&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0 + 999 = 999&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;??1A&amp;quot; = 999&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But then how would I tell it apart from this full postcode?:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;S W A A  1 M L&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  | | 11 ×           1 =  11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  | 12   ×          26 = 312&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | |  1      ×       26×26 = 676&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | | 0         ×    10×26×26 =   0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| | 0           × 37×10×26×26 =   0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;11 + 312 + 676 = 999&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;??AA1ML&amp;quot; = 999&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Note that SWAA1ML is not actually a real postcode. It doesn&#x27;t exist. However, the format is correct according to the constraints of this encoding, so it is still valid.)&lt;&#x2F;p&gt;
&lt;p&gt;This shows that I can&#x27;t just put these values in the file without the risk that an old version of the software reads a new file and returns the coordinates for &quot;SW1A&quot; when you search for the non-existent postcode &quot;SWAA1ML&quot;. The software needs to behave correctly for postcodes that don&#x27;t exist, and the old version of the software should not try to read files that include outward-only entries.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;planning-for-backwards-compatibility&quot;&gt;Planning for backwards compatibility&lt;&#x2F;h2&gt;
&lt;p&gt;When I first defined this file format, I knew I might want to expand on it later. That&#x27;s why I added a file format version ID in the header. The header looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UKPPvvvvtttttttt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;\__&#x2F;\__&#x2F;\______&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  |   |       |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  |   |       \--&amp;gt; Last modified:  64 bits unsigned unix time (8 bytes)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  |   \----------&amp;gt; Version number: 32 bits unsigned           (4 bytes)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  \--------------&amp;gt; Magic string:   Constant: &amp;quot;UKPP&amp;quot;           (4 bytes)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The software checks the magic string and the version field when it first reads the file. If the magic string is not &quot;UKPP&quot;, or the version is greater than the maximum supported version by the software, then it refuses to read the file. This is to prevent it reading some incompatible data as if it was a valid postcode pack file.&lt;&#x2F;p&gt;
&lt;p&gt;So, if I generate a new file with the version set to 2, then I don&#x27;t need to worry about old versions of the software accidentally making up postcodes that don&#x27;t exist. The other thing I want to do is to enable the new software to read both the old and new file types without having to duplicate too much logic, or write a whole new decoder. That part required a bit more thought.&lt;&#x2F;p&gt;
&lt;p&gt;In file version 1, there&#x27;s four ways to encode a postcode entry. Each entry indicates which type it is in the first two bits of the entry.&lt;&#x2F;p&gt;
&lt;p&gt;The first byte looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[ PD | CD | extra ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |    |      \----&amp;gt; Meaning depends on PD and CD  (6 bits)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   |    \-----------&amp;gt; Coordinates are delta encoded (1 bit)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   \----------------&amp;gt; Postcode is delta encoded     (1 bit)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That &quot;extra&quot; field is where the postcode delta value is stored if the postcodes is delta encoded. You can tell when that&#x27;s the case, because PD is set to 1.&lt;&#x2F;p&gt;
&lt;p&gt;So there&#x27;s already two ways to encode a postcode: as the fully encoded number for the last 5 characters, or as a value to add to the previous one to get to this one.&lt;&#x2F;p&gt;
&lt;p&gt;This means there&#x27;s a bunch of spare space in the cases where the postcode is not delta encoded, since those extra bits are not used.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;First byte   | PD | CD | spare bits&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-------------|----|----|------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x00         |  0 |  0 | 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x40         |  0 |  1 | 6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x80 to 0xBF |  1 |  0 | 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0xC0 to 0xFF |  1 |  1 | 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Available space:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x01 to 0x3F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x41 to 0x7F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So if I don&#x27;t mind being unable to delta encode the outward-only postcodes, then I can add a new encoding for them that uses this spare space.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s not very many outward codes, so it doesn&#x27;t bloat the file too much to add them in without delta-encoding them. So that&#x27;s what I did. I now use the third bit sometimes:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;First byte   | PD | CD | entry type   | spare bits&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-------------|----|----|--------------|------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x00         |  0 |  0 | full         | 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x20         |  0 |  1 | outward-only | 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x40         |  0 |  0 | full         | 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x60         |  0 |  1 | outward-only | 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x80 to 0xBF |  1 |  0 | full         | 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0xC0 to 0xFF |  1 |  1 | full         | 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Available space:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x01 to 0x1F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x21 to 0x3F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x41 to 0x5F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0x61 to 0x7F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This remains compatible with the version 1 file format. If the new software reads an old file, then it will just look like a version 2 file that happens to contain no outward-only codes. This is because of another thing I did to help backwards compatibility. I made sure that the code that generates these files would always put a 0 in any unused bits in the file. While it doesn&#x27;t exclude the possibility that someone could craft a malicious file that would give incorrect results, it means that all of the files I generated will work correctly.&lt;&#x2F;p&gt;
&lt;p&gt;(If someone wanted to give you a maliciously broken file, they could already do that anyway, it just wouldn&#x27;t be as impressive.)&lt;&#x2F;p&gt;
&lt;p&gt;So that&#x27;s it! Update to the latest version of the library and your website will support outward-only postcode searches.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>I nearly crashed a supercomputer that hundreds of other people were using</title>
          <pubDate>Sat, 13 Dec 2025 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/i-nearly-crashed-a-supercomputer/</link>
          <guid>http://computingthedetails.org/blog/i-nearly-crashed-a-supercomputer/</guid>
          <description xml:base="http://computingthedetails.org/blog/i-nearly-crashed-a-supercomputer/">&lt;p&gt;You know that meme where there&#x27;s a sudden freeze-frame, and then Baba O&#x27;Riley by The Who starts playing, and the freeze-frame is of someone in an unfortunate position?&lt;&#x2F;p&gt;
&lt;p&gt;Well, ... &lt;em&gt;that&lt;&#x2F;em&gt; ... and with this still image:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;crashing_a_supercomputer&#x2F;server_fires.png&quot; alt=&quot;A digital drawing of Lex&amp;#39;s face with a shocked and slightly distressed facial expression. To the right of their face is a few server racks, which are on fire.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Yep, that&#x27;s me. You&#x27;re probably wondering how I got in to this situation.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-incident&quot;&gt;The Incident&lt;&#x2F;h2&gt;
&lt;p&gt;It was a normal morning. I was at my desk, starting to work on a project that I&#x27;d recently joined. I was getting fed up of the fact that the build tools for this project didn&#x27;t work nicely with my shell. I use Fish instead of Bash, and this project had a build system that expected you to be running Bash. There was a &lt;code&gt;sourceme&lt;&#x2F;code&gt; script that was written for Bash, which set up various functions and environment variables to configure the build system.&lt;&#x2F;p&gt;
&lt;p&gt;As one does, when they are fed up of an incompatibility such as this one, they fix it. So I made some configuration changes to my shell to enable me to work on this project conveniently without having to sacrifice use of my favourite interactive shell. I did this by wrapping the Bash sourceme file in a program that would generate a Fish sourceme file from it, which I could then source. Putting those two steps together, I could fully automate loading the build environment.&lt;&#x2F;p&gt;
&lt;p&gt;I was doing this on a High Performance Computing (HPC) cluster, so I was logged in to this machine remotely via ssh. To make the configuration change take effect, I decided to log out and log back in.&lt;&#x2F;p&gt;
&lt;p&gt;When I tried to log in again nothing happened for about a minute or two, and then suddenly I saw a message and the ssh session terminated...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bash: fork: retry: Resource temporarily unavailable&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hmm.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fork&lt;&#x2F;code&gt; is the system call that enables a process to start another process. There&#x27;s always a limit to how many processes a system can maintain. This is either a de-facto limit determined by the amount of memory available, or it is an explicit limit enforced by the operating system. Seeing an error when trying to fork that tells you the resource is temporarily unavailable means that the system has decided you&#x27;re not allowed to fork any more, you&#x27;ve hit your limit.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Well, that&#x27;s funny. I guess I can&#x27;t &lt;code&gt;ssh&lt;&#x2F;code&gt; in to &lt;code&gt;login4&lt;&#x2F;code&gt;.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(To use the computing cluster you would log in to one of the login nodes &lt;code&gt;login1&lt;&#x2F;code&gt; to &lt;code&gt;login12&lt;&#x2F;code&gt;)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Someone must be fork bombing &lt;code&gt;login4&lt;&#x2F;code&gt;. That&#x27;s kinda embarrasing, wouldn&#x27;t want to be whoever&#x27;s doing that.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(A fork bomb is program that repeatedly forks and restarts itself to waste system resources, usually as a prank or denial of service (DoS) attack)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Let&#x27;s try &lt;code&gt;login5&lt;&#x2F;code&gt;.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bash: fork: retry: Resource temporarily unavailable&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;They&#x27;re fork bombing this node too!? Wow I bet someone&#x27;s having a bad day today.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;...&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;...&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;OH NO! IT&#x27;S ME! I&#x27;M THE ONE HAVING THE BAD DAY!&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-immediate-aftermath&quot;&gt;The Immediate Aftermath&lt;&#x2F;h2&gt;
&lt;p&gt;So, I realised that it was me. I was the problem. I ran over to ask a colleague to try logging in to the nodes that I had tried. Strangely enough he was able to log in to &lt;code&gt;login4&lt;&#x2F;code&gt; without any issue. I was releived, I hadn&#x27;t broken it for everyone, only for me.&lt;&#x2F;p&gt;
&lt;p&gt;I asked him to show me the process tree for that node, so he ran &lt;code&gt;ps -faux&lt;&#x2F;code&gt; or something similar, and I saw something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1656 ...   \_ &#x2F;usr&#x2F;bin&#x2F;fish&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1657 ...   |   \_ &#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1658 ...   |   |   \_ &#x2F;usr&#x2F;bin&#x2F;fish &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1659 ...   |   |   |   \_ &#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1660 ...   |   |   |   |   \_ &#x2F;usr&#x2F;bin&#x2F;fish &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lex         1661 ...   |   |   |   |   |   \_ &#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So ,clearly there was something bad going on. I had to get it stopped, but I had no way to log in to stop it, and my colleague wasn&#x27;t able to stop it for me because he didn&#x27;t have permission to terminate my processes. So I had to email the IT support address and ask them to resolve it for me. I asked them to terminate all processes that belonged to me on all login nodes, and also asked them to rename my Fish config file, so that my botched configuration was no longer loaded when I logged in.&lt;&#x2F;p&gt;
&lt;p&gt;There was nothing more I could do at that point, so I went for lunch, and by the time I came back I had en email reply from IT saying the had resolved everything.&lt;&#x2F;p&gt;
&lt;p&gt;I was able to log in, and fix my mistakes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;cluster-computing&quot;&gt;Cluster Computing&lt;&#x2F;h2&gt;
&lt;p&gt;Before I can explain what went wrong, I need to start with a little explanation about how a High Performance Computing Cluster works.&lt;&#x2F;p&gt;
&lt;p&gt;The kind of machine I was using is not simply “a really big computer”. As nice as it would be to have a device that can just run the same programs as your desktop PC, but thousands of times faster, that’s not something that’s practical to build.&lt;&#x2F;p&gt;
&lt;p&gt;This machine, like most other high performance computers, is actually just lots of small computers all working together. As such, if you just run a normal program on it, then it won’t go any faster. If you want to actually make use of the resources available, then you need to write a program that is specifically designed for cluster computing.&lt;&#x2F;p&gt;
&lt;p&gt;There’s more to it than just a big pile of small computers. There’s a high speed network joining all the nodes together, and joining them to shared storage systems. There’s also a software layer on top of all of this to make it easy to deploy your program across the network, keep nodes in sync, and return the results to you after a run has completed. This software layer also tries to make sure everyone that uses the cluster has fair access to it. Anyone can add a job to the queue, and then the software decides when to deploy jobs from the queue to available nodes. Some jobs require lots of nodes, others that don’t make such good use of the cluster will only use one or two nodes.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;crashing_a_supercomputer&#x2F;diagram.png&quot; alt=&quot;A diagram of the architecture of a HPC cluster. There is a network, shown as red lines, which connects many worker nodes, and fewer login nodes. There is also one storage node connected to the network. A remote user terminal connects to a login node (indicated with a large arrow coming in to the cluster from outside). All of the nodes are running the cluster management software.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Although it was technically possible to bypass this software layer, and log in to any node you wanted to, this is not how you were supposed to use the computing cluster. The cluster I had access to had hundreds of nodes, but 12 of them were special. These 12 nodes were known as Login Nodes.&lt;&#x2F;p&gt;
&lt;p&gt;The normal workflow is to log in to a login node, queue jobs from that node, and wait for results. Your job will run on the worker nodes once it has been through the queue. It doesn’t run on the login node.&lt;&#x2F;p&gt;
&lt;p&gt;If you accidentally forgot to use the job submission command to queue your job, and just ran it directly on the login node, then you’d probably be using a lot of resources on the login node, and thus slowing it down so much that it’s hard for other people to use it. This is bad cluster user etiquette.&lt;&#x2F;p&gt;
&lt;p&gt;This architecture means that lots of people are all queueing jobs from the same 12 machines. I wrote a little script that would log in to each one in turn and tell me how many users were on each one, so that I could choose to log in to whichever one had the fewest users. It was quite handy.&lt;&#x2F;p&gt;
&lt;p&gt;If something was soaking up all of the resources on all the login nodes, someone would notice very quickly, and anyone else in the company using the cluster would be prevented from getting work done. We had 12 login nodes. If I remember correctly they averaged about 50 active user sessions each at any given time. Some of these will be the same user holding multiple sessions open, but this should give you an idea of the number of people you’d disrupt if you took these machines down.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-actually-happened&quot;&gt;What Actually Happened&lt;&#x2F;h2&gt;
&lt;p&gt;To recap the scenario: I was working on a large project with a very complicated build system that had a lot of different options that were controlled mostly by environment variables. Part of the source code for this project was a “sourceme” file that set up these variables according to some configuration files, and also defined some functions that could manipulate the variables for choosing different build options.&lt;&#x2F;p&gt;
&lt;p&gt;This “sourceme” was designed to work with Bash. Bash is the default user shell for most of the popular Linux distributions. However, I prefer to use Fish shell. Unfortunately, nobody else had provided a Fish version of the “sourceme” file, so I had to make my own.&lt;&#x2F;p&gt;
&lt;p&gt;The first option for doing this would be to make a copy of the Bash version, and then manually translate all of the Bash syntax in to Fish syntax. Unfortunately, the file was incredibly large, and I didn’t want to port all of it manually. Instead, I chose to write a small wrapper around the Bash version, that would start Bash, take a snapshot of the environment, source the sourceme file, and then take another snapshot. It would then compare these snapshots to find out what the sourceme file had actually changed, and then generate Fish code that makes the same changes. This means I don’t have to worry about all of the complexity of the Bash version, nor do I need to duplicate all of the code and maintain it separately.&lt;&#x2F;p&gt;
&lt;p&gt;An important part of the code generation step was to make sure that the contents of the variables was suitably escaped. Imagine I had a variable with the following value, and wanted to generate Fish code to set it:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(exit &amp;#39;1&amp;#39;) # &amp;quot;Failure&amp;quot; condition&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I can’t just put that string directly in the code, because it creates valid code that runs some other program. That’s no good, that’s a security risk!&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s a Fish builtin command for this&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;fish&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt; escape &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;some string here&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; or&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;some_command&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; string&lt;&#x2F;span&gt;&lt;span&gt; escape&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So obviously I&#x27;m going to use that to escape the strings from my Bash script wrapper&lt;&#x2F;p&gt;
&lt;p&gt;My Bash code would run Fish with the -c option, which means “instead of being an interactive shell, just run this one command and then exit”.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;VALUE&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; fish&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;c&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;string escape &amp;lt;&#x2F;dev&#x2F;stdin&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;(Warning, the above code is subtly wrong, explanation later...)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Hooray! I can work on this project from Fish! I no longer have to suffer with Bash!&lt;&#x2F;p&gt;
&lt;p&gt;This really did work perfectly fine, at first. I knew that I was going to be working on this project almost every day for a little while, so I decided it would be useful if this sourceme file would be sourced automatically every time I logged in. So I added it to my Fish config file, which Fish executes on startup.&lt;&#x2F;p&gt;
&lt;p&gt;This is where I made the first mistake!&lt;&#x2F;p&gt;
&lt;p&gt;After changing your shell config, it’s normally a good idea to manually start your shell from inside your current session to test that the change is good. If your shell won’t start, because of a catastrophic error in your config file, then you won’t be able to log in and start a new session because your shell program starting and ending is what determines when your session starts and ends. If your shell won’t run, you can’t log in with ssh. So the best practice is to try running it in your current session, and then fix any issues before you log out.&lt;&#x2F;p&gt;
&lt;p&gt;I didn’t do this. I thought I had already done enough testing. I decided to immediately log out and log in again to restart my session and get the new config.&lt;&#x2F;p&gt;
&lt;p&gt;I was not able to log in again.&lt;&#x2F;p&gt;
&lt;p&gt;The first clue as to what went wrong was in the error message. When I tried to log in, some program in the chain of programs that handle my ssh session had tried to use the “fork” system call, but was unable to because Linux had decided that there was not enough resources left to complete the fork. This could happen because the system had run out of memory, or because a fork limit was reached. It could be my own personal fork limit, or a global fork limit.&lt;&#x2F;p&gt;
&lt;p&gt;The second mistake that I made was assuming that I was not responsible for the system reaching a resource limit. I had assumed that someone else had accidentally run a fork bomb on the login machine.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;aside-what-s-a-fork-bomb&quot;&gt;Aside: What&#x27;s a fork bomb?&lt;&#x2F;h3&gt;
&lt;p&gt;What’s a fork bomb? Well, it’s a program that repeatedly forks in to two copies of itself. This create an exponentially growing number of processes. After just ten iterations of this, you’ve got over a thousand copies of the program. After another ten, you’ve got more than a million. Long before you get to this stage, you’ll start to see that your system is running much slower, as the Linux scheduler needs to spend more time keeping track of all of the processes that are running.&lt;&#x2F;p&gt;
&lt;p&gt;Eventually the process descriptor table becomes full, so forks start to fail, and some copies of the program will terminate without forking. As they do, this frees up space in the process descriptor table, and so some others that haven’t forked yet can now fork successfully again. You now have a system that is fully saturated with processes. Most of them are just wasting your CPU time by creating copies of themselves, so the programs you really care about don’t get as much CPU time as you’d like.&lt;&#x2F;p&gt;
&lt;p&gt;Stopping a fork bomb requires terminating all of the instances of the fork bomb simultaneously. If even one instance remains, and it hasn’t forked yet, then it is enough to restart the entire attack.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;but-i-didn-t-write-a-fork-bomb-right&quot;&gt;But I didn&#x27;t write a fork bomb, right?&lt;&#x2F;h3&gt;
&lt;p&gt;I thought I had encountered the effects of someone else’s accidental fork bomb. For this reason, I decided to try logging in to a different login node.&lt;&#x2F;p&gt;
&lt;p&gt;Once this node also presented the same error, I started to reconsider my initial conclusion.&lt;&#x2F;p&gt;
&lt;p&gt;I almost ran the script that I had, that would log in to each login node in sequence and tell me the number of active sessions on each one. This would have resulted in my accidental fork bomb running on every one of the login machines. I remember starting to type the command and thinking “but if it is me that’s the problem, this could take down all of the login nodes”.&lt;&#x2F;p&gt;
&lt;p&gt;I paused for a moment, and carefully pressed backspace to erase the command I was about to run.&lt;&#x2F;p&gt;
&lt;p&gt;My fear that I was the problem was confirmed when a colleague of mine was able to log in without any issues to the same nodes that I was trying to log in to, and could see the trail of destruction I had left in my wake.&lt;&#x2F;p&gt;
&lt;p&gt;I was the problem. I had indeed accidentally written a fork bomb that would automatically run when I tried to log in, and would prevent me from successfully logging in.&lt;&#x2F;p&gt;
&lt;p&gt;I had to email IT support to ask them to fix it.&lt;&#x2F;p&gt;
&lt;p&gt;The reason why it happened was quite simple. Fish will read and execute its config file before running a command provided to it in the -c option. So if you have something in your Fish config that runs a Fish command with fish -c, well, now whenever fish starts, it’s going to start fish, and wait for it to finish before it continues. The problem is, that new instance of fish will do the same thing.&lt;&#x2F;p&gt;
&lt;p&gt;By adding the sourceme file to the Fish config, I had changed the behaviour of the sourceme file. It was now a fork bomb. &lt;em&gt;Well, technically not really, it doesn’t expand exponentially, like a traditional fork bomb does. It is only linear. But it’s still a huge problem.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The reason I was not expecting this is because I assumed that the -c option in Fish would behave like the -c option in Bash. Bash will skip loading user config files when -c is provided. To make Fish skip config files, you need to additionally provide the -N option.&lt;&#x2F;p&gt;
&lt;p&gt;The thing that stopped this being a catastrophe for any machine that I logged in to, and the thing that allowed my colleague to log in while my session was floundering, was the per-user fork limit.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, the IT department had configured the login nodes to be fairly restrictive with per-user fork limits. They could handle one user hitting their limit and still provide a good service to the other users. This meant that the incident had a big impact on me, but almost no impact on anyone else.&lt;&#x2F;p&gt;
&lt;p&gt;It’s also possible that this was not actually forward thinking, and was a reaction to a previous incident, or maybe the OS they use has a sensible set of defaults. I think it was RedHat Enterprise Linux, but I can’t remember now.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, that’s my confession about the biggest mistake I ever made with access to a high performance computing cluster. I don’t work at that job any more, but not because of this incident. I continued to work there for a while, then moved on to something else.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A Privacy Perserving Postcode Lookup Tool</title>
          <pubDate>Fri, 27 Jun 2025 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/a-privacy-preserving-postcode-lookup-tool/</link>
          <guid>http://computingthedetails.org/blog/a-privacy-preserving-postcode-lookup-tool/</guid>
          <description xml:base="http://computingthedetails.org/blog/a-privacy-preserving-postcode-lookup-tool/">&lt;p&gt;Let’s talk about store locators.&lt;&#x2F;p&gt;
&lt;p&gt;You’ve probably used a website that has a store locator. For example, there’s a chain of supermarkets in the UK called The Coop. On their website is a store locator. You can type in a postcode and it will list a selection of your closest Coop shops.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;locators.png&quot; alt=&quot;Screenshots of store locator pages. From left to right: The Cooperative, Tesco, B&amp;amp;Q&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you have a look at the network tab in the developer tools when you use it, you will see that the postcode is sent to some web server. Before I’ve even typed the whole thing it’s made an API request to get a list of possible postcodes that I might be interested in. When I select a postcode from the list, it then sends that to a different API endpoint. That one returns a list of shops that I might be interested in, which is then presented to me on the page. I also get a map view, which potentially has other problem, but I’m not super interested in that at the moment.&lt;&#x2F;p&gt;
&lt;p&gt;The Coop site uses Google maps to provide this service. If you type your postcode in to this page then it sends that postcode to Google. You were never asked if you were happy for Google to know what postcodes you’re interested in.&lt;&#x2F;p&gt;
&lt;p&gt;Tesco offers the same thing. They use Mapbox APIs. Again, the postcode is sent to a different web server to the one that the main website came from, so your postcode searches are leaking beyond the one website that you’re using.&lt;&#x2F;p&gt;
&lt;p&gt;Plenty of other websites do exactly the same thing. B&amp;amp;Q is a chain of hardware and home improvement stores. They go a step further and guess your location based on your IP address. In most cases this is going to be a good guess. They’re probably using some API to do that too. But if you have a look at what they do with a postcode then you&#x27;ll see they use another similar API from Woosmap. They then make an API request to a site called kingfisher.com to find the nearest stores. Kingfisher is the company that runs B&amp;amp;Q along with similar stores under different brands.&lt;&#x2F;p&gt;
&lt;p&gt;All these big chains are totally happy to ask for your postcode, and then send it off to some other web service you’ve maybe never heard of, or worse: Google, and use that to locate your nearest stores for your convenience.&lt;&#x2F;p&gt;
&lt;p&gt;There are companies that make money from buying and selling information like “This person, who you can identify by this web tracking cookie, likely lives near this postcode.” They build profiles of web users as they browse across many different websites, adding information to your profile as they learn new things.&lt;&#x2F;p&gt;
&lt;p&gt;If all they did with this data was target adverts, then that might not be concerning, but maybe your life insurance company is buying that data and deciding that you’ve been looking up your nearest Fast Food Chain a little too often. You’re a greater heart-attack risk than most. Looks like you’re gonna have to pay more for your life insurance.&lt;&#x2F;p&gt;
&lt;p&gt;I’m not saying that they’re all doing this. But when you consider that this is possible, it’s better to prevent that data being collected in the first place.
A little aside: this is why the EU created a law that forbids websites from placing tracking cookies in your browser without consent. Lots of website operators decided that the appropriate way to comply with this law was to put intrusive and confusing popups on the page that make it easy to give consent, and not always as easy to deny consent. But today’s video is not really about that.&lt;&#x2F;p&gt;
&lt;p&gt;There’s lots of examples of websites that have these store locator features, but I’ve never seen one that is designed for privacy. I would love to come across a website with a store locator that verifiably did not let my postcode escape from my web browser in to some random web service. This means that the postcode can’t be allowed to get out of my web browser at all.&lt;&#x2F;p&gt;
&lt;p&gt;I would love this so much, in fact, that I decided to make it happen. I did it not just to prove that it is possible, but to provide the resulting tool for free to anyone that wants it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;uk-postcodes-what-you-need-to-know&quot;&gt;UK Postcodes - What you need to know&lt;&#x2F;h2&gt;
&lt;p&gt;There’s currently almost 1.8 million active postcodes in the UK. Each one holds a small number of addresses. Sometimes a postcode has a single address inside it. Sometimes a postcode maps to an entire street with tens of addresses.&lt;&#x2F;p&gt;
&lt;p&gt;Postcodes are deliberately constructed such that no house name or number is duplicated within a single postcode. This means that, technically, if you send a letter to 10, SW1A2AA, that will be delivered to the UK Government headquarters. You don’t need to write “Downing Street” on your letter. You don’t need to write “Westminster” or “London” on it either. The postcode and door number is sufficient.&lt;&#x2F;p&gt;
&lt;p&gt;It’s still a good idea to include those details when you send a letter or package. They provide some redundancy.&lt;&#x2F;p&gt;
&lt;p&gt;Every UK postcode is made up of two parts. The first part is called the Outward code, the second part is the Inward code.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| Postcode | Outward code | Inward code |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| B11RS    | B1           | 1RS         |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| B111RS   | B11          | 1RS         |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;| SW1A2AA  | SW1A         | 2AA         |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The inward code is the simplest part. It is always 3 characters long. The first of these is a digit, then the last two characters are letters. These tend to be allocated sequentially, but not with much consistency. They start at 0AA, and continue with 0AB, and onwards until 9ZZ. They aren’t always all used. For example, for the Outward code of AB12, the lowest currently active Inward code is 3AA. There’s some small gaps here and there until it reaches AB12 5YX, and then a sudden jump to AB12 9AL.&lt;&#x2F;p&gt;
&lt;p&gt;The Outward code is everything except the last three characters. This code can be 2, 3, or 4 characters long. For example: B1 is the 2-characters Outward code for an area in the centre of Birmingham. B11 is a different area of Birmingham, and is 3 characters long. We’ve already seen a 4-character Outward code SW1A.&lt;&#x2F;p&gt;
&lt;p&gt;That Birmingham example is interesting though. It shows something that makes it tricky to parse a postcode. B1 1RS is a postcode for a shopping area in the centre of Birmingham. B11 1RS is a very similar postcode that is nearby, but it’s in a residential area of Birmingham. If you write these without the space between the Outward and Inward codes, as many people often do, then they both start with B11. Any system that parses a postcode should therefore not assume that a postcode that starts with a known Outward code is, in fact, located in that Outward code. Instead it should separate the Inward code first, since that’s always 3 characters long, and then parse what remains as an Outward code.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;b11rs_vs_b111rs.png&quot; alt=&quot;Two postcodes plotted on a map. B11RS and B111RS. They are 2.2km apart.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Okay, I think we understand enough to make something that works, so let’s get in to engineering a privacy preserving store locator system.&lt;&#x2F;p&gt;
&lt;p&gt;The way we’re gonna make this privacy-preserving is by avoiding sending any specific postcode over the internet to lookup its coordinates. This means that we need the browser to do all of the computation about the distance to each store from the postcode. Once the page is loaded, it should be possible to completely disconnect from the internet, and still use the store locator.&lt;&#x2F;p&gt;
&lt;p&gt;That means we need some way to know the GPS coordinates of every UK postcode without doing a lookup via a web service.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-the-data&quot;&gt;Getting the data&lt;&#x2F;h2&gt;
&lt;p&gt;The key thing that we need is a complete mapping of UK postcodes to GPS coordinates. The Royal Mail has a complete mapping of postcodes to addresses. And they let you access &lt;em&gt;most&lt;&#x2F;em&gt; of it on their website. Strangely enough, when I tried looking for 10 Downing Street, it wouldn&#x27;t tell me that the postode for it is SW1A 2AA.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, this database is called Postcode Address File (PAF). If I want a copy of it then I need to buy a license that costs thousands of pounds and needs to be regularly renewed.&lt;&#x2F;p&gt;
&lt;p&gt;The Royal Mail has monetised their postcode and address database!&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, there is a public body in the UK called the Office for National Statistics (ONS), who maintains an open database of all postcodes and the GPS coordinates of their approximate centres (where appropriate).&lt;&#x2F;p&gt;
&lt;p&gt;Since they&#x27;re a public body, everything they do is open to the public. And they don&#x27;t even make you do a Freedom of Information (FOI) request to get it!&lt;&#x2F;p&gt;
&lt;p&gt;The ONS provides a download that’s updated every few months with all the UK postcodes and their GPS coordinates.&lt;&#x2F;p&gt;
&lt;p&gt;If you thought it was going to be the Ordnance Survey (OS), that was a good guess. OS does provide a similar data set called Code-Point. In fact the ONS data set is actually based on the OS data set, but the OS version doesn’t include GPS coordinates, instead it provides UK National Grid coordinates.&lt;&#x2F;p&gt;
&lt;p&gt;The National Grid is the grid system that OS uses on their maps. (Not to be confused with the National Grid, which is the UK’s electricity distribution infrastructure.) The conversion from National Grid coordinates to GPS coordinates is not required if I use the ONS data set, so that’s why I chose that one.&lt;&#x2F;p&gt;
&lt;p&gt;I should clarify: the GPS coordinates are approximate. A postcode isn’t really a single point, it’s an area. It can be quite a big area. The Royal Mail even explicitly warns us about this on their website. Regardless, any point in that area is going to be a useful approximation for locating your nearest store.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-hard-part-making-it-work&quot;&gt;The hard part (making it work)&lt;&#x2F;h2&gt;
&lt;p&gt;Okay we have the data, can we just write some javascript that fetches that file ready to use offline, and when a user types in a postcode, it looks up the coordinates and then calculates the distance to each store?&lt;&#x2F;p&gt;
&lt;p&gt;No, not &lt;em&gt;that&lt;&#x2F;em&gt; easily anyway.&lt;&#x2F;p&gt;
&lt;p&gt;This file is 1.4GB! That’s enough to wipe out someone’s entire mobile data allowance in a single page load! That’s too big!&lt;&#x2F;p&gt;
&lt;p&gt;It contains a lot of fields we don’t care about. Let’s get rid of all fields except for the postcode, longitude, and latitude. As we do that, we can also discard any postcodes that have been terminated, and any that don’t have coordinates associated with them.&lt;&#x2F;p&gt;
&lt;p&gt;The file is now 48MB. That’s better, but it would still take about 5 seconds to download that over my home internet connection. It doesn’t sound like a lot for one user, but I want this to scale well. I don’t want to add a lot of additional load to the server, or consume excessive bandwidth. There’s also energy to consider too. I think it’s often overlooked, but coding for minimal energy usage is valuable.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s take a closer look at how postcodes work. Earlier I explained how a postcode is an Outward code and an Inward code. If I want to optimise how I store them, I’m going to need to think about them as data. Below is a table of possible values for each position in the code&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Position |  1  |  2  |  3  |  4  |  5  |  6  |  7  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         | A-Z | A-Z | A-Z | A-Z |     | A-Z | A-Z |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Options  |     | 0-9 | 0-9 | 0-9 | 0-9 |     |     |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         |     |     |blank|blank|     |     |     |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;example  |  S  |  W  |  1  |  A  |  2  |  A  |  A  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# opts   | 26  | 36  | 37  | 37  | 10  | 26  | 26  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The Outward code always starts with a letter. The second character is either a letter or number, the third might not exist at all, but if it does, then it is a letter or a number. The fourth also might not exist, but can be a letter or a number.&lt;&#x2F;p&gt;
&lt;p&gt;To make parsing easier, you can always store postcodes in a fixed width format that is 7 characters long. You start with the Outward code. You then add 0, 1, or 2 space characters as padding, and finally add the Inward code.&lt;&#x2F;p&gt;
&lt;p&gt;Given this pattern, the first character has 26 possible values. The second has 36. The third has 37, if using the space-padding approach. The fourth has 37. The Inward code’s three characters have 10, 26, and 26 possible values respectively.&lt;&#x2F;p&gt;
&lt;p&gt;Now a hint of information theory. How many bits do we need?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;26 × 36 × 37 × 37 × 10 × 26 × 26 = 8,662,155,840&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;log₂(8,662,155,840) ≈ 33.01&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The total number of possible postcodes is about 8.6 Billion. That’s a super inconvenient number actually! This means you can’t quite store a postcode as a 32 bit integer. Even 33 bits is not enough, you need 34, but only just.&lt;&#x2F;p&gt;
&lt;p&gt;So far I haven’t made any assumptions based on which postcodes are actually in use, and I want to keep it that way. 8.6 Billion is the most postcodes we could have with this system. I want to avoid adding in any assumptions that reduce this space, so that this system continues to work in the future as more postcodes are added.&lt;&#x2F;p&gt;
&lt;p&gt;We’re gonna have to get creative about how to represent postcodes.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;making-a-better-storage-format&quot;&gt;Making a better storage format&lt;&#x2F;h3&gt;
&lt;p&gt;Let’s ditch the CSV format, and go for a raw binary representation. If I store each postcode as a number, then I can do that in 5 bytes. I need at least 34 bits, which is 40 bits if you round up to the next multiple of 8. 40 bits is 5 bytes, hence needing 5 bytes to store a postcode.&lt;&#x2F;p&gt;
&lt;p&gt;Given that the postcodes normally take up 7 bytes, this is a reduction of about 30%&lt;&#x2F;p&gt;
&lt;p&gt;While I’m doing that, I should also reduce the coordinates to a numerical representation too. Each one takes up about 9 characters in string form. A 64 bit floating point number would certainly be precise enough for a GPS coordinate, that costs 8 bytes. That’s not a big saving over the 9 bytes they were before. Perhaps we could use 32 bit floats, that way we need 4 bytes per number, that’s a lot better than 9! This is a reduction of 44%.&lt;&#x2F;p&gt;
&lt;p&gt;Overall, including the commas and newlines and whatnot, the postcode and coordinates in string form in a CSV file took 28 bytes per entry. With a packed numerical representation, which does not need the commas and newlines, this is reduced to 13 bytes per entry. This is less than half the initial size.&lt;&#x2F;p&gt;
&lt;p&gt;There’s a much better way to do this though.&lt;&#x2F;p&gt;
&lt;p&gt;Consider how a lookup works in this file:&lt;&#x2F;p&gt;
&lt;p&gt;The user has typed in a postcode, and wants to know if it is in the file. The search program needs to find that postcode in the file. The obvious simple algorithm to do this is to start at the beginning of the file, read the first postcode, see if it matches the typed postcode, and if not move on to the next entry in the file. Eventually you will hit either: the entry for the postcode you want, or the end of the file.&lt;&#x2F;p&gt;
&lt;p&gt;Scanning the entire file like this takes a significant amount of time. For the CSV version it’s about 740ms on average for a successful lookup. Sometimes it takes longer, sometimes it’s faster. It depends where in the file you find the postcode.&lt;&#x2F;p&gt;
&lt;p&gt;Another potential improvement we could make to the list of postcodes would be to reduce the amount of repetition in it. There’s a lot of postcodes that start with AB right at the start of the file. What if I had some way to insert information in to the file like “postcodes from this point on start with AB, only the last 5 characters are stored”. In that case I could remove the “AB” prefix, and save a little space.&lt;&#x2F;p&gt;
&lt;p&gt;One last idea to explore: A postcode does not define a point, it defines an area. The coordinates provided for a postcode are an approximate centre for the postcode area. Lossy compression is a category of compression where the data you get out of the decompression algorithm is not exactly the same as what you put in to the compression algorithm. The decompressed value is an approximation of the original value. Sometimes this is unacceptable, like when compressing text, such as the subtitles on this video perhaps. Sometimes this is acceptable, such as when compressing a photograph. If each pixel is almost the same colour as the original, you probably won’t notice, nor will you care. In the case of our coordinates, the values are approximate anyway, so perhaps some lossy compression is okay.&lt;&#x2F;p&gt;
&lt;p&gt;So here we have a few thoughts to guide the creation of a file format and algorithms for storing and looking up postcodes:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Lookup time should be minimised.&lt;&#x2F;li&gt;
&lt;li&gt;Prefix repetition could be avoided.&lt;&#x2F;li&gt;
&lt;li&gt;Lossy compression of coordinates could be okay.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s take a look at that design now.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-format-and-algorithms-in-detail&quot;&gt;The Format and Algorithms in Detail&lt;&#x2F;h3&gt;
&lt;p&gt;As the encoder program reads list of postcodes in to memory, and parses the coordinates for each one, it keeps track of the bounding box that surrounds all the postcodes. The program then pretends this is a square. It’s not actually a square, it&#x27;s also not a rectangle, it’s actually a part of the surface of a spheroid, but the maths we’re doing doesn’t actually care too much about the shape since we&#x27;re just going to do a transform that we are going to undo to get the original coordinates back later. It’s just a set of coordinates, we can visualise it as a square&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;postcode_boundingbox.png&quot; alt=&quot;A map of the UK showing some of the GPS coordinates for postcodes (each is a red triangle). The set of postcodes shown includes the etreme north, east, south, and west postcodes. A green bounding box is drawn around all the red triangles.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So now we have longitude and latitude values for the edges of our conceptual square as 64 bit floating point values. These values go directly in to the file, right at the start.&lt;&#x2F;p&gt;
&lt;p&gt;Next, the encoder iterates over the postcodes, and for each one it splits the code into two parts. The first is the two character prefix, and second is everything else. Previously I said that you need 34 bits to store the postcode as a number. With the prefix removed, that reduces dramatically, to 24 bits. This is great because it fits very efficiently in three bytes.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Postcodes with two-character prefix removed&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Position |  1  |  2  |  3  |  4  |  5  |  6  |  7  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     | A-Z | A-Z |     | A-Z | A-Z |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Options              | 0-9 | 0-9 | 0-9 |     |     |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     |blank|blank|     |     |     |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# opts   |  1  |  1  | 37  | 37  | 10  | 26  | 26  |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----------------------------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;⌈log₂(37 × 37 × 10 × 26 × 26)⌉ = 24 bits = 3 bytes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The coordinates are then compressed with a lossy compression system. Below is the formula used to compress the latitude coordinate. The minimum latitude from the bounding box calculation is subtracted from the postcode’s latitude coordinate. This calculates the distance in to the box from the bottom edge. This is then divided by the height of the box. This results in a value in the range 0 to 1. This is then scaled up by multiplying by 65535, and rounding to the nearest integer. This results in a 16 bit number that represents a latitude in the bounding box.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;latitude_formula.png&quot; alt=&quot;The formula described above, and a visual representation of how it maps the spheroid section to a square.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The longitude is processed in a similar way, and now I can store the latitude and longitude in four bytes. This means that each postcode and coordinate entry is 7 bytes long, but doesn’t have the prefix included. This is another good improvement over the 13 bytes per postcode from before, and it’s only a quarter of the size of each entry in the CSV file.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;fileformat1.png&quot; alt=&quot;Table showing packed postcode format, as specified above.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The encoder processes all the postcodes like this and keeps a list of all of them. While it does this, it also keeps track of the prefixes in a table. The postcode table is sorted, so all of the postcodes with the same two character prefix will appear together. As it scans the list, any time a new prefix is encountered, it will put an entry in the table to indicate how many postcodes there were before the first one with that prefix.&lt;&#x2F;p&gt;
&lt;p&gt;Once all the prefixes are added to the table, the total number of postcodes is added at the end of the table, and all of the prefixes that were not used also get values in the table. The value of each of these unused prefixes is the value of the next prefix after that one in the table.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Example Prefix Table:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;A0-AA:      0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;AB   :      0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;AC-AK: 17,385&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;AL   : 17,385&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;AM-B0: 25,185&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;B1   : 25,185&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;B2   : 30,800&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;B3   : 36,202&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;B4   : 42,028&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(Ranges of the same value are fully expanded in the real file,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;they are abreviated here only to fit more entries in the example)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now the encoder writes the table values in to the file, and then all of the packed postcodes in order. And that’s it! The file is ready to go!&lt;&#x2F;p&gt;
&lt;p&gt;This file is about 12MB. That’s 25% of the minimised CSV file size. But how do we use this file?&lt;&#x2F;p&gt;
&lt;p&gt;Let’s have a look at the lookup algorithm next.&lt;&#x2F;p&gt;
&lt;p&gt;The first step of this algorithm is to make sure the postcode is correctly formatted. Some people will deliberately put a space in their postcode, between the Outward and Inward codes. Some people will try to put such a space in and get it in the wrong place. Some people use no spaces at all. Some people will use upper case, others will use lower case. Rather than require the user to type the postcode in the correct format, the algorithm just does this for them.&lt;&#x2F;p&gt;
&lt;p&gt;Formatting a postcode is fairly easy. First make sure all the letters are upper case, remove all the spaces that the user put in there, and then insert spaces between the Outward and Inward codes until you have a 7 character string. Since the Inward code is always 3 characters, it’s easy to find where to insert the spaces.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, some postcodes are too malformed to recover them. If you miss some of the code, or include an invalid character, this step will cause an error.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Initial user input:       &amp;quot;b 11 rs&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1. Upper case all chars:  &amp;quot;B 11 RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2. Remove all spaces:     &amp;quot;B11RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3. Insert correct spaces: &amp;quot;B1  1RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----------------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Well formed output:       &amp;quot;B1  1RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we split the postcode up to get the two character prefix and the rest. We turn the prefix in to a number, and separately encode the rest of the postcode as a number just like the encoder did when it created the file. Now we can parse the file. It starts with the bounding box coordinates, which are easy enough to read. Then we use the prefix number to look up where to start scanning for the postcode in question. Because of the way that this part of the file was constructed, we can be sure that the next number in the table after this one represents the furthest we would ever need to scan to find a postcode with this prefix, so we make a note of that too.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Postcode: &amp;quot;B1  1RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Prefix:   &amp;quot;B1&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Suffix:   &amp;quot;  1RS&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Numbers from prefix lookup table:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Lookup start point: 25,185&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Lookup end point  : 30,800&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we multiply these numbers by 7, since that’s the length of each postcode, and now we know the byte offsets in to the file where we should start and stop scanning. Calculating the distance to the end of the table, and adding the start offset allows us to know where to seek to, skipping lots of postcodes that have other prefixes.&lt;&#x2F;p&gt;
&lt;p&gt;Now we just need to grab each postcode in sequence, and compare it to the number we calculated at the start. If the postcode doesn’t exist, then we will hit the end of the search range. If it does exist, then we will find a match, and can then reverse the calculation done on the coordinates to compress them.&lt;&#x2F;p&gt;
&lt;p&gt;The coordinates can’t be retrieved exactly, because there was some rounding involved in calculating them, and possibly some floating point errors too, but the results are typically within a few metres of the actual values. Certainly precise enough for something as approximate as a postcode.&lt;&#x2F;p&gt;
&lt;p&gt;This lookup process is able to skip over lots of postcodes without even reading them, this makes it much faster than a lookup in the CSV file. The average lookup time is reduced from  740ms to only 3.3ms. This is a huge saving! Three quarters of a second is long enough to notice, but a few milliseconds is barely perceptible. This is certainly fast enough.&lt;&#x2F;p&gt;
&lt;p&gt;Notice that this speed up is much more than you’d expect just from the file being shorter, so clearly the structure and lookup algorithm had a big impact too.&lt;&#x2F;p&gt;
&lt;p&gt;Remember those three things from earlier: Lookup time should be minimised. Prefix repetition could be avoided. Lossy compression of coordinates could be okay.&lt;&#x2F;p&gt;
&lt;p&gt;Designing with these in mind produced a much smaller and faster solution, but I’m not done yet!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;we-have-to-go-deeper-go-smaller&quot;&gt;We have to &lt;del&gt;go deeper&lt;&#x2F;del&gt; go smaller!&lt;&#x2F;h2&gt;
&lt;p&gt;That lookup time is excellent! In fact, it’s so good, that I think it would be acceptable to trade off a little bit of lookup time for an even smaller file size. 12MB is still a bit too big to expect a casual user to be okay with.&lt;&#x2F;p&gt;
&lt;p&gt;So here’s another thing to consider: The postcodes are sorted, and they come in densely packed clusters. There’s big gaps between the clusters, but within each cluster it’s easy to predict what postcode is going to come next.&lt;&#x2F;p&gt;
&lt;p&gt;This encoding scheme is what I’m calling the delta encoding. Some of the values in the file are not stored in absolute numbers, they are instead stored as a delta on top of the previous value in the file.&lt;&#x2F;p&gt;
&lt;p&gt;Previously a postcode was a fixed 7 byte structure, now it’s going to vary in length. The postcode is normally 3 bytes, and the coordinates are normally 4 bytes. If the coordinates are close enough to the previous ones, then I could instead store a pair of 8 bit signed integers that are added to the previous coordinates to get the next coordinates. The postcodes are sorted, so I never need to go backwards, so for the postcode delta I can store an unsigned number. I decided to limit this number to 6 bits, which will make sense soon, giving the delta a range of 1 to 64.&lt;&#x2F;p&gt;
&lt;p&gt;Now the structure has four different formats. The first byte determines which format is used. Bits 7 and 6 of the first byte are flags that indicate which fields are delta encoded. If bit 7 is set, then the postcode is delta encoded. If bit 6 is set then the coordinates are delta encoded. So the worst case scenario is when neither is delta encoded. The first byte just has the value 0, and then the next 7 bytes are the postcode and coordinates in the same format as before. This takes up 8 bytes, so it’s actually slightly worse than before, but that’s fine because the next three cases are far more common.&lt;&#x2F;p&gt;
&lt;p&gt;If the postcode is delta encoded, then the delta to the next postcode is stored in the last 6 bits of the first byte. The coordinates then appear immediately after this. If the coordinates are not delta encoded then this case is only 5 bytes in total.&lt;&#x2F;p&gt;
&lt;p&gt;Perhaps only the coordinates are delta encoded, in which case it’s 6 bytes in total.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the best case is when both of them are delta-encoded, for this case I only need 3 bytes to store the entire entry. It turns out that 91.9% of all the postcodes can be encoded in the 3 byte format, and only about 0.37% of them need to be stored in the fully expanded format.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;fileformat2.png&quot; alt=&quot;Table showing packed postcode format, as specified above.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This saves a lot of space! The new data file is only 5.5MB.&lt;&#x2F;p&gt;
&lt;p&gt;This encoding scheme is much more complicated, and so the lookup time does suffer a little bit. The lookup algorithm needs to be modified to handle the delta encoding. It needs to keep track of the previous postcode and coordinates as it scans the file. It needs to parse the structure to figure out how to update its internal state, and to know how far to move the cursor onwards.&lt;&#x2F;p&gt;
&lt;p&gt;Previously it was always moving by 7 bytes, now it might be 3, 5, 6, or 8.&lt;&#x2F;p&gt;
&lt;p&gt;So how big is the effect? Well the average lookup time with this method is about 6.6ms. Despite the file being less than half the size it was before, the lookup time has doubled! That’s unfortunate, but it is still really fast. It’s still in the barely perceptible range. Looking at the graph we can see that both of these formats handily beat the CSV format.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;graph_size.png&quot; alt=&quot;A bar graph comparing the size of the files for the various packing methods. For CSV, Packed, and Delta packed respectively, the sizes are 48MB, 12MB, and 5.5MB&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;a_privacy_preserving_postcode_lookup_tool&#x2F;graph_time.png&quot; alt=&quot;A bar graph comparing the average lookup time of the files for the various packing methods. For CSV, Packed, and Delta packed respectively, the average lookup times are 740ms, 3.3ms, and 6.6ms&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Getting speed gains has been much easier than getting space gains, but I’m happy with the delta encoding scheme’s size and speed.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-fun-demo&quot;&gt;A Fun Demo&lt;&#x2F;h2&gt;
&lt;p&gt;So, getting back to the problem I wanted to solve, let’s have a look at a little demo that uses this system to do a privacy preserving locator tool.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;lexbailey.github.io&#x2F;nearmypostcode&#x2F;aa_box_demo.html&quot;&gt;See a demo for yourself&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you open that page and then open the developer tools window of your browser (normally F12), then you can see the magic happen. Clear everything from the network requests tab after loading the page but before enabling the search tool. (Since it needs to download a file that is slightly abnormally big for a website, I thought it best to make sure the user is aware that they’re about to download it.)&lt;&#x2F;p&gt;
&lt;p&gt;Clicking the button, you can see that the file download shows up in the network requests tab. And now the search tool is enabled. At this point, you could totally disconnect from the internet, and this would still work. Type in a postcode, press search, and you&#x27;ll see a list of all the Automobile Association Call Boxes sorted by their distance to that postcode. Notice that no additional network requests were made, the lookup happened offline.&lt;&#x2F;p&gt;
&lt;p&gt;In a real world scenario, I might expect one additional network request to occur when the search tool loads, to load the list of locations, but that can also happen before the user ever types a postcode, and still preserve privacy and allow offline use. In this demo the list is trivially small and is loaded as part of the script on the main web page.&lt;&#x2F;p&gt;
&lt;p&gt;I’m just some person on the internet with almost zero clout, so I’m not really expecting any big corporation’s website to start using this any time soon. But if you happen to be someone who makes technical decisions for such a website, I would love to be able to say I’ve convinced you to switch to this approach.&lt;&#x2F;p&gt;
&lt;p&gt;I’ve only just finished making this work in my demo, so there’s some practical considerations that I still need to figure out for large scale deployments. If you have useful ideas or feedback regarding that, then please let me know by submitting an issue.&lt;&#x2F;p&gt;
&lt;p&gt;The code is open source and currently hosted &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;codeberg.org&#x2F;lexbailey&#x2F;nearmypostcode&quot;&gt;on Codeberg&lt;&#x2F;a&gt;. You can also find it &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;nearmypostcode&quot;&gt;on Github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;closing-thoughts&quot;&gt;Closing thoughts&lt;&#x2F;h2&gt;
&lt;p&gt;Before I go, I want to leave you with some thoughts about privacy and technology. Big tech companies that provide web based services have big incentives to sell your personal data. This isn’t something that is inherent in technology. I hope that what I’ve shown here has demonstrated that effectively.&lt;&#x2F;p&gt;
&lt;p&gt;Technology can be applied in ways that preserve privacy if the people creating that technology endeavour to make it that way. Big tech companies generally don’t do that.&lt;&#x2F;p&gt;
&lt;p&gt;I hope I’ve also demonstrated that it’s worth being frugal with energy and data. Spending that extra effort to minimise the size of a file means that the file can be delivered much quicker and with less energy. Spending the time to minimise the computation required to process it also reduces the energy used.&lt;&#x2F;p&gt;
&lt;p&gt;Sometimes it’s a small effect, but it could multiply up to something much bigger.&lt;&#x2F;p&gt;
&lt;p&gt;Maybe I’ve inspired some of you to apply a similar ethos to the way you write code. That’d be nice.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why does my car think that 38 - 2 = 34?</title>
          <pubDate>Sun, 01 Jun 2025 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/why-does-my-car-think-38-minus-2-is-34/</link>
          <guid>http://computingthedetails.org/blog/why-does-my-car-think-38-minus-2-is-34/</guid>
          <description xml:base="http://computingthedetails.org/blog/why-does-my-car-think-38-minus-2-is-34/">&lt;p&gt;I can&#x27;t explain this bug. It makes no sense to me. 38 minus 2 is not 34. It is 36. Obviously!&lt;&#x2F;p&gt;
&lt;p&gt;Suppose I have a computer program which has one variable called A. The program waits for an input of either &quot;up&quot; or &quot;down&quot;, and then adds two to A or subtracts two from A and uses that as the new value for A.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s say A starts at 20. I give it the &quot;up&quot; input, and now A is 22. I give it the &quot;up&quot; input again, and now A is 24. I give it the &quot;up&quot; input 8 more times, and A goes through 26, 28, 30, 32, 34, 36, 38, and ends up on 40. This is all fine.&lt;&#x2F;p&gt;
&lt;p&gt;But if I then started giving it the &quot;down&quot; input repeatedly from A=40, and the sequence of values of A started 40, 38, 34, then you&#x27;d be surprised. You&#x27;d be thinking &quot;hmm, this program is clearly broken. It is faulty. There is a bug here!&quot;&lt;&#x2F;p&gt;
&lt;p&gt;Suppose you keep testing the program as far as you are able to. You go up and down the full range of numbers that the program can handle. There&#x27;s no other cases where it jumps by 4 instead of by 2. It&#x27;s always a step of 2, unless you go down from 38, in which case it steps by 4 and lands on 34.&lt;&#x2F;p&gt;
&lt;p&gt;I know what you&#x27;re thinking. You&#x27;re thinking that surely nobody has ever written software that has this bug. How could anyone possibly put a bug like that in to such a simple program?&lt;&#x2F;p&gt;
&lt;p&gt;Surely this isn&#x27;t a bug that exists in the wild.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;finding-this-bug-in-the-wild&quot;&gt;Finding this bug in the wild&lt;&#x2F;h2&gt;
&lt;p&gt;The last time I owned a car, it was because I got it for free from a friend of mine. He had crashed his Smart Roadster in to the back of another car, didn&#x27;t want to repair it, and didn&#x27;t really use it much any more anyway. So I got this heap of metal on wheels for no charge. The retractable roof no longer retracted, instead it just leaked. The CD player was faulty in various ways. Some parts fell of it sometimes. It was hopelessly impractical for a lot of tasks. But it was sometimes useful, and the engine ran perfectly fine, so I decided the squeeze the remaining life out of it. I did repair the bumper, get it past one MoT safety inspection, and make use of the car for a while after that, but strategically sold it to a car recycler just before the next MoT inspection was due.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;(MoT stands for Ministry of Transport, a UK government agency that has since been renamed to the Department for Transport (DfT), but everyone still refers to the legally mandated annual vehicle safety inspections as &quot;MoT tests&quot;)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;why_does_my_car_think_38_minus_2_is_34&#x2F;crashed_roadster.jpg&quot; alt=&quot;A terrible old car (A red Smart Roadster with a smashed bumper and a filthy roof)&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That car had a speed limiter feature. I used it every time I drove the car. It&#x27;s really handy. Any time I see a speed limit sign, I set the car&#x27;s speed limiter to that limit, and then I don&#x27;t have to worry about how fast I&#x27;m going. The car won&#x27;t let me break the speed limit.&lt;&#x2F;p&gt;
&lt;p&gt;The minimum speed setting is 20mph. So, you can use the control on the stalk to increase or decrease the speed limit. Each click &quot;up&quot; adds 2 to the limit, up to a maximum of 120 miles per hour. I never need to go beyond 70mph though, since that&#x27;s the highest speed limit on UK roads. A click &quot;down&quot; normally subtracts 2 from the limit, down to a minimum of 20mph. However, as you&#x27;re probably expecting from the intro, a click down from 38mph subtracts 4 from the limit, and puts you on 34mph.&lt;&#x2F;p&gt;
&lt;p&gt;Most of the UK speed limits are multiples of ten. During most of the city driving I do I will only encounter speed limits of 20mph and 30mph, but I do often also use 40mph roads, and then there&#x27;s 50, 60, and 70mph roads when you get out of the city. Most of the time I encounter a change of speed limit, I need to do 5 clicks in some direction to get to the new speed limit. Most of the time I&#x27;m adding or subtracting 10.&lt;&#x2F;p&gt;
&lt;p&gt;Before I discovered this bug, when leaving a 40mph zone and entering a 30mph zone, I would do 5 &quot;down&quot; clicks. This always resulted in me ending up on 28mph. I would then be mildly confused by the slightly slower speed than I expected, look at the display, see that it said 28, and do one &quot;up&quot; click to correct it.&lt;&#x2F;p&gt;
&lt;p&gt;I thought this was no big deal, until I realised that I consistently have issues with going from 40mph to 30mph. At some point while I had the car parked on my driveway, I played with the controls and discovered that I was not imagining things. I was not just failing to use the controls correctly. It was not detecting more than one input for each click of the control. It consistently jumps from 38 to 34.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-do-we-explain-this&quot;&gt;How do we explain this?&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;m the kind of person that really enjoys writing computer code. I started writing code as a hobby when I was very young, and spent my entire career working in tech writing code all day. I&#x27;ve spent a significant amount of my life writing code. I have never come across a bug like this before, and I have not yet encountered one since.&lt;&#x2F;p&gt;
&lt;p&gt;I don&#x27;t have access to the source code for this system, and I decided not to extract the code from the car to reverse engineer the binary. As fun as that would be to do, it will be a lot of effort to disassemble the car to the point where I can even find the chip that the software for this is stored on. I didn&#x27;t want to take my car apart that far just for this bug.&lt;&#x2F;p&gt;
&lt;p&gt;So, let&#x27;s think of some hypothetical ways that this bug could happen. I&#x27;m going to use C code for demonstration purposes. Here&#x27;s a really simple thing that the program could be doing:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; get_limit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; return&lt;&#x2F;span&gt;&lt;span&gt; limit&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; called on up click&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;void&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; up&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 120&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 120&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; called on down click&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;void&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; down&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;-=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code works perfectly. No issues. We have some really simple code here, and it works fine. So clearly we&#x27;re missing something, because we don&#x27;t have the buggy behaviour yet, which is what we expected.&lt;&#x2F;p&gt;
&lt;p&gt;This is the first thing that anyone would write if asked to implement this. So, given that it works just fine: how did this bug get in there? Who was it that got to this point and thought “I can do better than this” and proceeded to write some code that gets basic arithmetic so obviously wrong?&lt;&#x2F;p&gt;
&lt;p&gt;If I wanted to provide some explanation for this bug and did not care about how similar my example code is to the real code, I could modify the down function to look like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; called on down click&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;void&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; down&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;-=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;==&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 36&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 34&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I add in an extra check that looks for the value 36, on the way down, and jumps to 34 instead.&lt;&#x2F;p&gt;
&lt;p&gt;This perfectly reproduces the behaviour that I&#x27;ve observed, but it is not at all satisfying as an explanation, because nobody would ever write that code. Any code that goes in to a car, and can interact with critical systems within the car, such as the accelerator, goes through a very strict process that aims to keep the quality of the code high.&lt;&#x2F;p&gt;
&lt;p&gt;I don&#x27;t believe that source code like this would ever be allowed into a critical system on any car. The bug can&#x27;t be as obvious as this. It must be something far more subtle.&lt;&#x2F;p&gt;
&lt;p&gt;So what is it? What is the subtle bug? Well, I have two ideas to explore, but I don&#x27;t like either of them as a good explanation.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bad-explanation-1-look-up-tables&quot;&gt;Bad explanation 1: Look up tables&lt;&#x2F;h3&gt;
&lt;p&gt;Idea 1 is that they didn’t use arithmetic to determine the next value, but instead had a lookup table that was manually written and contained a mistake.&lt;&#x2F;p&gt;
&lt;p&gt;This one is more plausible than just having an extra check to deliberately skip 36, but I still don’t like it. It still feels unlikely.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose for example that I still have my variable called “limit” and it still stores the limit in miles per hour. Now I have a lookup table that maps a speed limit value to the next value up, and another that maps a speed limit value to the next value down.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; get_limit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; return&lt;&#x2F;span&gt;&lt;span&gt; limit&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; uptable&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    22&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;26&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;34&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;36&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;38&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;40&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    42&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;44&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;46&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;48&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;50&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    52&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;54&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;56&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;58&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;60&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    62&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;66&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;68&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;70&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    72&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;74&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;76&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;78&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;80&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    82&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;84&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;86&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;88&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;90&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    92&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;94&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;96&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;98&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    102&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;104&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;106&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;108&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    110&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;112&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;114&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;116&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    118&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;120&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;120&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; downtable&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;26&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    32&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;34&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;34&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;38&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;40&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    42&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;44&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;46&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;48&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;50&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    52&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;54&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;56&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;58&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;60&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    62&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;64&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;66&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;68&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;70&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    72&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;74&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;76&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;78&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;80&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    82&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;84&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;86&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;88&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;90&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    92&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;94&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;96&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;98&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    102&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;104&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;106&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;108&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    110&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;112&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;114&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;116&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;    118&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;void&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; up&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt; uptable&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;void&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; down&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span&gt; limit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt; downtable&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Urgh! That looks awful! Each table needs to have a huge blank area at the start full of zeroes for all the values that are impossible to reach, and a placeholder value in between each number, since it always steps two at a time. I’ve just put 0 in these too.&lt;&#x2F;p&gt;
&lt;p&gt;Yes I could do some more address computations to avoid all those unused entries in the table, but that wouldn&#x27;t be the worst sin of this code.&lt;&#x2F;p&gt;
&lt;p&gt;Why am I even suggesting this? Well it does have some interesting properties that might be what the car manufacturer wanted to have. The advantage of this approach is that each of the up and down functions is doing exactly the same thing every time, with no branching paths in either one. That is, there is no if statement any more. It always looks up the new value in the table and stores it.&lt;&#x2F;p&gt;
&lt;p&gt;You no longer need to test if you are at the end of the range. The lookup table for going up maps 120 mph to 120 mph, and the downwards one maps 20 mph to 20 mph. There’s no need for the checks at the extremes of the range any more, they are handled by the tables.&lt;&#x2F;p&gt;
&lt;p&gt;Why is this useful? Sometimes a predictable execution time is useful. Especially in cryptography. The only problem with this theory is that it makes no sense to save that tiny bit of time spent branching. There’s no doubt a whole bunch of other code running in between the button press and the table lookup anyway.&lt;&#x2F;p&gt;
&lt;p&gt;There’s no way this code is on the critical path. Especially when you consider that it also lets you hold down the button to skip through the steps repeatedly. That must require a whole bunch of logic and timing stuff. It’s not going to be a timing constraint.&lt;&#x2F;p&gt;
&lt;p&gt;I seriously doubt that’s the reason. I also can’t think of any better reason to do it like this, and that’s why I doubt that this is the case. It’s a little more satisfying than the very deliberate bug from earlier, because the bug is really subtle this time. You have to look carefully at the tables to see it, instead of just immediately seeing something suspicious. But I&#x27;m still not convinced&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bad-explanation-2-is-it-floats&quot;&gt;Bad explanation 2: Is it floats?&lt;&#x2F;h3&gt;
&lt;p&gt;Idea 2 can be summarised as “some sequence of floating point rounding errors.&quot;&lt;&#x2F;p&gt;
&lt;p&gt;Okay, first of all. If you’re writing code for an embedded system, especially when it’s embedded in a car, you should really think carefully about if you really need floating point arithmetic before you actually use it.&lt;&#x2F;p&gt;
&lt;p&gt;Floating point rounding errors can cause nasty bugs. Embedded hardware often doesn’t even have floating point arithmetic units, and so it is implemented in software, which is much slower than hardware, and certainly much slower than integer arithmetic.&lt;&#x2F;p&gt;
&lt;p&gt;But let’s put that aside for a moment. Maybe they did use floating point. Maybe, because this is a German car, all of the software was written with the assumption that all the base units were in Kilometres, and then they added in support for places where the speeds and distances are measured in miles by having a conversion layer on top of the kilometres implementation.&lt;&#x2F;p&gt;
&lt;p&gt;Well, that raises some challenges, Suppose your underlying control software needs to have the speed limit specified in kilometers per hour and you don’t want to change that. You can’t just have the displayed number be converted to miles per hour, and leave the buttons to add and subtract 2 kph, because then you’d end up with an awful user experience far worse than this bug. Pressing up would add about 1.24 miles per hour. You need to also convert the up and down numbers.&lt;&#x2F;p&gt;
&lt;p&gt;So maybe you make the up and down buttons add and subtract about 3.22 kph, then you can keep the limit stored in kph, apply the translation only when you display the number, and the control system can stay the same. You’ll need to do some rounding to be sure everything works, but this could be okay.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, but how much rounding is too much?
Let’s try specifying everything to two decimal places, and rounding to the nearest integer whenever we display the limit. That works! How about we get even more approximate? Only one decimal place. That works too.&lt;&#x2F;p&gt;
&lt;p&gt;Okay how about everything is just the nearest integer, except for the conversion factor? That also works. You can be really approximate about this and it still works fine! It’s not a speed unit conversion problem.&lt;&#x2F;p&gt;
&lt;p&gt;But wait a minute, what’s going to happen if there is a rounding error? At these low numbers, the rounding errors are going to be incredibly small. We’re not even going to see an error big enough to cause a problem when rounding to the nearest integer. We’re not even seeing a scenario where we are off by one, how could this possibly lead to being off by two!?&lt;&#x2F;p&gt;
&lt;p&gt;Let’s be a little more systematic about this. If there is some rounding that leads to this problem, there must be a scale where one of the converted numbers rounds to 34, and the next one up rounds to 38. What would the step size need to be?&lt;&#x2F;p&gt;
&lt;p&gt;The lowest value that can round to 38 is 37.5. You can exactly represent 37.5 in a single precision float. The next float below that one would round down to 37. The greatest value that can round down to 34 is 34.99996185302734375, which is the next float value below 34.5, which would round to 35. The difference between these values, according to regular floating point arithmetic is precisely 3.000003814697265625, which is very slightly more than 3.&lt;&#x2F;p&gt;
&lt;p&gt;Well clearly that’s no use. If I was to use 3 as the step size, then I can get this sudden step from 38 to 34, but then if I go down again from there I get to 31, not 32. So certainly this isn’t what’s going on!&lt;&#x2F;p&gt;
&lt;p&gt;We need a step size that’s close to 2, and we need a rounding function that rounds to the nearest 2. We need it to round to correctly for all numbers in the range 20 to 120, except for the number one step below 38.&lt;&#x2F;p&gt;
&lt;p&gt;Turns out this is kinda possible! If you start at 20.82 and you make steps of 2.024, then by the time you get to the number that rounds to 34, you’re actually at about 34.988, and then you take one more step and you’re at about 37.012, and the closest multiple of 2 to that number is 38, so you’ve skipped over 36. The problem is that this happens in both directions. The real bug that I experienced only skipped 36 when heading down!&lt;&#x2F;p&gt;
&lt;p&gt;So you might be thinking that there’s some rounding on the stored value on each step, but then the bug would be inconsistent. In my experience it would always skip 36 on the way down regardless of what other up and down motion had happened before that. It would never skip 36 on the way up, even if I’d been up and down across that boundary many times. If it was an accumulation of rounding errors over time then I’d expect it to be less predictable than that.&lt;&#x2F;p&gt;
&lt;p&gt;I thought about doing an exhaustive search of all possible floating point rounding modes, all possible step sizes, and all possible conversion factors, to account for the underlying data maybe being in some strange units that’s neither mph nor kph. I even started to write some code for doing that, and then realised that the search space is way too big, and so I gave up.&lt;&#x2F;p&gt;
&lt;p&gt;And that’s where I’m at. I have no idea how to explain this bug. How could someone get this wrong? I don’t know.&lt;&#x2F;p&gt;
&lt;p&gt;If you are someone that worked on the speed limiter software for Smart cars in the early 2000s, and you know for sure why this bug happened, then please please please get in touch and tell me all about how that system works. I would love to know.&lt;&#x2F;p&gt;
&lt;p&gt;Sorry this rant doesn’t have a satisfying conclusion. I wish it did.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Fitting a pride flag image in 241 bytes. A ZX Spectrum Sizecoding project.</title>
          <pubDate>Mon, 17 Feb 2025 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/sizecoding-some-zx-spectrum-demos/</link>
          <guid>http://computingthedetails.org/blog/sizecoding-some-zx-spectrum-demos/</guid>
          <description xml:base="http://computingthedetails.org/blog/sizecoding-some-zx-spectrum-demos/">&lt;p&gt;I recently wrote some really tiny computer programs for an event called &lt;a href=&quot;http:&#x2F;&#x2F;computingthedetails.org&#x2F;blog&#x2F;sizecoding-some-zx-spectrum-demos&#x2F;LoveByte&quot;&gt;http:&#x2F;&#x2F;lovebyte.party&#x2F;&lt;&#x2F;a&gt;. It&#x27;s an annual online-only demoparty.&lt;&#x2F;p&gt;
&lt;p&gt;In case you&#x27;ve not heard of a demoparty before: it&#x27;s an event where programmers, musicians, and artists come together to show off things they&#x27;ve created.&lt;&#x2F;p&gt;
&lt;p&gt;Normally these creations are computer programs that render real-time animations, or visual effects, often accompanied by music or sound effects.&lt;&#x2F;p&gt;
&lt;p&gt;LoveByte specifically focuses on what you can do in that genre with a very limited program size. This is known as &quot;size-coding&quot; in the biz.&lt;&#x2F;p&gt;
&lt;p&gt;That appeals to me quite a bit, so I made a couple of entries. Here they are!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;progress.png&quot; alt=&quot;Progress pride flag rendered on a ZX Spectrum&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t8.webp&quot; alt=&quot;As the previous animation, but also with random sizes of bubbles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;One of them is a rendering of the progress pride flag in 241 bytes, and the other is a rising bubble effect in 31 bytes. Both of these run on the Sinclair ZX Spectrum.
The competitions are open to any platform, but I chose the ZX Spectrum.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s nothing too special about them, but they&#x27;re both good opportunities to talk about the ZX Spectrum and size-coding, two things I enjoy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;progress-pride-flag-in-241-bytes&quot;&gt;Progress Pride Flag in 241 bytes&lt;&#x2F;h2&gt;
&lt;p&gt;This was my entry to the Tiny Executable Graphics Competition, where you need to create a program that produces a static image in 256 bytes or less.&lt;&#x2F;p&gt;
&lt;p&gt;At first this one might not seem impressive. It&#x27;s just a static picture, right? Anyone could have drawn that!&lt;&#x2F;p&gt;
&lt;p&gt;Well, sure, but...&lt;&#x2F;p&gt;
&lt;p&gt;First of all, the size. The ZX Spectrum screen is 256 pixels wide, by 192 pixels tall, with a really thick border of a single colour all the way around.
This flag image fills the whole frame buffer. If you stored this as a standard Windows Bitmap, with a colour depth of 8 bits per pixel, you&#x27;d need about 48kb of space just for the image data. The ZX Spectrum frame buffer is much smaller than that. There&#x27;s only 48kb of RAM for the whole machine, so the ZX Spectrum plays some tricks to make the screen buffer smaller.&lt;&#x2F;p&gt;
&lt;p&gt;First of all, it only has 15 colours (well, technically 16, but bright black is the same as black).
Furthermore, it only lets you have two distinct colours in each 8x8 cell of pixels, and you have to pick both of them from the &quot;bright&quot; colours, or both of them from the &quot;dim&quot; colours, you can&#x27;t mix and match. So actually there&#x27;s only 48 to 128 different colour combinations depending on how you feel about double-counting some of them.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;zx_spec_colours.png&quot; alt=&quot;The selection of colours available on the ZX Spectrum&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The colours for a cell are stored in a byte known as the attribute byte. There is one attribute byte per cell.
Unfortunately, that&#x27;s not enough colours to make the progress pride flag. This is why I have to use dither patterns to simulate some of the colours that aren&#x27;t directly available.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;dithers.png&quot; alt=&quot;Examples of some dithered colour combinations&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This works really well for the orange and purple stripes, and lets me get a nicer shade of blue for the blue stripe, but there isn&#x27;t a good combination of colours for making a brown dither. I did the best I could, with a fairly sparse red dither on a black background.
Each cell of 8 by 8 pixels is represented by one attribute byte for the colour combination and a 1 bit-per-pixel bitmap, so a cell only takes up 9 bytes in total. There&#x27;s a grid of 32 by 24 of these cells, so the ZX Spectrum frame buffer format takes up about 7kb.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s 6912 bytes, to be precise.&lt;&#x2F;p&gt;
&lt;p&gt;So if I stored this image as a raw ZX Spectrum frame, it would be 6912 bytes.
The program that I wrote is only 241 bytes long, but generates the full 6912 bytes frame.
That&#x27;s a really good compression ratio. The program is about 3.5% of the size of the frame that it generates.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s visualise the drawing procedure with a split view of the frame buffer. I&#x27;ll show the bitmap area with all attributes set the same way, and then show a pattern of dots with the attributes as they are in the buffer.D These two combine to make the full image. Here&#x27;s what it looks like at the end:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;final_progress_split.png&quot; alt=&quot;The final render, and the raw bitmap and attribute visualisations&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s have a look at how we got to that point. There&#x27;s six steps to this drawing process, let&#x27;s see them one by one&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-1-make-it-yellow&quot;&gt;Step 1: Make it Yellow&lt;&#x2F;h3&gt;
&lt;p&gt;The first step might appear strange at first. It clears the screen to a yellow colour, then it applies the dither pattern to the first eight lines of the screen.
You can see the dither pattern in the bitmap, but the attributes are set so that every cell is yellow on yellow, so the screen still looks blank for now.
If we look at the final image, it&#x27;s actually only the orange, blue, and purple stripes that have this dither pattern on them. So why am I drawing the dither in so many places where I don&#x27;t need it? And why am I not dithering some places that do need it?
Well, that should become clear later on.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo1.webp&quot; alt=&quot;The screen turns yellow from top to bottom, left to right, one 8x8 pixel block at a time&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The ZX Spectrum has some space in the RAM that is designated for custom characters that can be printed with the normal character printing routine found in the ROM. This dither pattern is just a custom character repeated over and over again.
The code that puts it here is just a loop that prints that character 256 times.&lt;&#x2F;p&gt;
&lt;p&gt;The combination of the &quot;load b&quot; instruction and the &quot;decrement and jump if not zero&quot; instruction forms a very space-efficient loop. Only four bytes in total for the loop head and tail. The body is also fairly short. Only 3 bytes to load the A register with the number of the dither character, and then call the ROM&#x27;s print char function.
It might look like I&#x27;ve set the loop up to do zero iterations, but actually an initial loop counter of 0 means you&#x27;ll get 256 iterations. That&#x27;s the most we can do with a djnz loop, and it perfectly fills eight lines of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;It took 8 bytes to clear the screen to yellow, so we&#x27;ve executed 15 bytes so far, and we have this.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo2.webp&quot; alt=&quot;Six coloured stripes are added from left to right, top to bottom&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;At this point, the particularly astute among you are wondering: &quot;If this is the first thing the program did, then how is the custom character loaded already?&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;An excellent question! I played a fun little trick that I will reveal later, when we get to the appropriate part of the program.
I might be building that up a bit too much, it&#x27;s kinda boring and obvious when you realise whats going on here, but I&#x27;m still gonna wait to explain it later.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-2-adding-stripes&quot;&gt;Step 2: Adding Stripes&lt;&#x2F;h3&gt;
&lt;p&gt;In this step, the attribute data for the thick horizontal stripes is added.
There&#x27;s a lot more going on in this step. It uses 43 bytes of program space to do it, and this includes a 6 byte array that contains attribute values in order.
It does this one column at a time, starting at the 10th column and moving rightwards.
There&#x27;s an inner loop, which steps down in the Y direction, and an outer loop, that steps in the X direction.
The inner loop steps to the next attribute value after every four steps with the current attribute.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo3.webp&quot; alt=&quot;Six coloured stripes are added from left to right, top to bottom&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now you can see the effect that is created by the dither pattern bitmap and the attribute data in combination. You can&#x27;t see the dither pattern on the red stripe, because both the filled and unfilled pixels are set to display as red. The orange stripe has two different colours, yellow and red, and so the dither pattern is visible.
The last three stripes don&#x27;t yet look like the final image. The attributes are correct, but the bitmap data is not correct yet. That will come later.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-3-chevrons&quot;&gt;Step 3: Chevrons&lt;&#x2F;h3&gt;
&lt;p&gt;Step 3 is the one that takes up the most program space. It&#x27;s 68 bytes in total. It uses the string printing function in the ROM to generate the stripes that are at a 45 degree angle. The string that it prints has control characters in it for changing the colour attributes while printing. The characters it is printing are a combination of built-in characters and custom characters. The space character, and the filled block character are both built in to the ROM. These two angled half-filled cells are custom characters, as is the dither pattern.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo4.webp&quot; alt=&quot;The chevrons are added, but the lower half of them appears to have each 8 pixel strip vertically mirrored&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There is a function that prints this string 12 times in a row, advancing down and right by one space with each iteration.&lt;&#x2F;p&gt;
&lt;p&gt;Once the first 12 iterations are done, the program actually modifies itself, changing this decrement instruction to an increment instruction, so that the next time this loop runs, it will move down and to the left, instead of down and to the right.&lt;&#x2F;p&gt;
&lt;p&gt;It is still printing the exact same string though, and so it really doesn&#x27;t look right at the moment, but that will be fixed later. It has also introduced some new problems. There&#x27;s a couple of cells that now have incorrect data in them.o&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-4-oops-slightly-undo-step-3&quot;&gt;Step 4: Oops, slightly undo step 3&lt;&#x2F;h3&gt;
&lt;p&gt;Step 4 is purely here to fix up those issues introduced in step 3.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately it takes another 32 bytes to do this. It re-prints the dither pattern over the cell that now has the wrong bitmap data, and it fixes the attribute data to get the right colours again.
It also doesn&#x27;t entirely cover the left edge of the stripes, so the left edge of the green stripe is visible slightly. It fixes this too.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo4a.webp&quot; alt=&quot;A few small glitches are repaired&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In the middle it did something that&#x27;s not obvious yet. It stored a number in the space where the BASIC drawing and printing functions read attributes from. This will be important later.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;step-5-making-a-reflection&quot;&gt;Step 5: Making a Reflection&lt;&#x2F;h2&gt;
&lt;p&gt;In step 5 we spend 36 bytes on the most magical part of this whole program. It takes the bitmap from the top half of the screen, and mirrors it to the bottom of the screen. This copies the dither pattern in to the lower two stripes, and fixes the angled stripes too. The result now looks really similar to the final image.&lt;&#x2F;p&gt;
&lt;p&gt;The theory of the mirror code is fairly simple, but the details are less simple. In theory, it just needs to copy lines of pixels from the top, to the bottom. It should be simple logic. It should just be a loop with 96 iterations, where each one copies one line from the top to the bottom. Unfortunately it&#x27;s not quite that simple.
It starts with a pointer to the start of the first line, and calculates a pointer to the start of the last line, then copies the bitmap data for that line in to place. This works fine. Then it moves down by one line, and does the same. Except, it skips 7 lines! This is because of the unusual order of the pixel data in the ZX Spectrum&#x27;s frame buffer.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo5.webp&quot; alt=&quot;The top half of the bitmap is coped to the lower half of the screen, vertically mirrored&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Mathematically, it&#x27;s not a problem. The maths that calculates the offset from the end given the offset from the start is still correct, despite this strange ordering. It&#x27;s still just a simple subtraction operation. So this isn&#x27;t a problem, right?
Well, the problem becomes obvious when you consider the middle. If there was no special handling of the middle, then we would end up copying some lines from the lower half of the screen in to the top half of the scree, which is clearly not helpful.
The solution to this was to add in some logic that determines if the current offset is below the centre line of the screen. If it is then it will skip that line, and continue.
This means that the loop actually needs to do 128 iterations. Even though there&#x27;s only 96 lines to copy, the offset of the 96th line is 128 row-lengths past the first byte in the frame buffer. 32 lines get skipped.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-6-the-circle&quot;&gt;Step 6: The Circle&lt;&#x2F;h3&gt;
&lt;p&gt;This is where that mystery attribute value from before becomes useful. It was the right attribute value for the circle colour.
This code uses a fairly sophisticated feature of the ZX Spectrum BASIC ROM to load some constant values in to the calculator stack, and then call the circle drawing routine, which uses the calculator stack values to determine the X and Y coordinates, and the radius of the circle to draw.
The ZX Spectrum ROM calculator is a whole subject in itself, and not one that I&#x27;m going to get in to here. Think of it like a little DSL inside of the assembly language. It has its own op codes, which it interprets at run time in software. The only opcode that I use in this program is the one that stacks a constant value on the calculator stack.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;slowmo6.webp&quot; alt=&quot;A circle is drawn on the left side of the flag&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This makes the circle drawing step really compact. Only 14 bytes to draw the circle.&lt;&#x2F;p&gt;
&lt;p&gt;The last two bytes of the program are simply a jump-to-self, so that the program never returns.
After the end of the main code path, we find some additional data. First is the list of attribute values for the horizontal stripes, as we saw earlier, then the function for drawing the angled stripes, and the string value that it uses.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;but-when-do-the-custom-characters-get-loaded&quot;&gt;But when do the custom characters get loaded?&lt;&#x2F;h3&gt;
&lt;p&gt;Well, those are what comes after everything else in the file. Right at the end.&lt;&#x2F;p&gt;
&lt;p&gt;The BASIC loader loads the program at a very specific location, just a little bit before the start of the default user defined character area in the RAM. The custom characters are loaded in to place before the program even starts, simply by carefully choosing where the program starts loading in to memory.
This was the last optimisation that I made, since doing it requires knowing exactly how long the program is, and thus what address to start the program at.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, the assembler that I used for this project has an assert command that I can use to cause an error if the character data ends up in the wrong place.&lt;&#x2F;p&gt;
&lt;p&gt;So that&#x27;s it. In real time this process completes in about a second or two. with the slowest part being the circle drawing.
But by capturing frames from the emulator much more frequently than the real hardware can display new frames, I&#x27;m able to make the super slow motion sequences from this video. I quite enjoy watching the image appear in slow motion.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;bubbles-31-bytes&quot;&gt;Bubbles (31 bytes)&lt;&#x2F;h2&gt;
&lt;p&gt;The second demo I want to talk about is quite different.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t8.webp&quot; alt=&quot;As the previous animation, but also with random sizes of bubbles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This was my entry for the 32 Byte Intro Competition. It&#x27;s a pseudo-random stream of bubbles rising up the screen.
So this one is about one eighth of the size of the flag, and includes animation!&lt;&#x2F;p&gt;
&lt;p&gt;This one doesn&#x27;t do anything particularly surprising, but it&#x27;s still interesting.&lt;&#x2F;p&gt;
&lt;p&gt;I could just walk through the program as it exists in its final form, and explain how it works, like I did for the pride flag demo. But to do that would mean skipping over some important details about the process I used to develop it.
To give you an insight in to that process, I&#x27;ll walk through how it happened. I can do this here because I can actually remember the whole process.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;starting-by-experimenting&quot;&gt;Starting by Experimenting&lt;&#x2F;h3&gt;
&lt;p&gt;I deliberately set out to write a 32 byte demo, specifically for LoveByte. I didn&#x27;t have too much of an idea for what I wanted to do, but I thought it might be fun to try and find a way to use the ZX Spectrum ROM&#x27;s built in circle drawing function in a tiny demo.
As we saw in the previous demo, the circle drawing function can be accessed fairly easily from a machine code program with not too many bytes of code.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing that I did was write some code that would draw a circle somewhere on the screen, just to test things out.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t0.webp&quot; alt=&quot;A single circle drawn on to the screen&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The ZX Spectrum ROM has been disassembled by various people over the years. And thanks to these efforts, it is well documented where all the functions are in the ROM.&lt;&#x2F;p&gt;
&lt;p&gt;The function uses parameters from the calculator stack. This time I used a slightly different approach to the pride flag demo. I knew I wanted to be able to move the circles around, so I didn&#x27;t use hard-coded constants.&lt;&#x2F;p&gt;
&lt;p&gt;The circle function takes three values from the calculator stack. The first one is the radius of the circle. The second is the Y coordinate, and the third is the X coordinate.
Of course, because this is a stack, we need to push these in the reverse order to the order they&#x27;ll be used. Conveniently, there&#x27;s a function that can put the current value of the A register on the calculator stack, and so I call that function several times, to put three values on to the calculator stack.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so I have a circle. Now I want it to move. I&#x27;ve played around with the ZX Spectrum enough to know that there&#x27;s a ROM routine for scrolling the entire screen up by 8 rows. It&#x27;s normally used by the print function to move all the text up the screen if there&#x27;s more to print but you&#x27;ve reached the end of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;It works just fine for non-text things too, since it just scrolls the whole bitmap without regard for what&#x27;s in it.&lt;&#x2F;p&gt;
&lt;p&gt;So now I can draw a circle, move the screen up, and then draw another. It&#x27;s a very simple animation, but it works.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t1.webp&quot; alt=&quot;Circles being drawn on the screen and then shifted up, creating a stack of circles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;making-it-interesting&quot;&gt;Making it interesting&lt;&#x2F;h3&gt;
&lt;p&gt;Now I want this to be a little more interesting. I don&#x27;t just want one stream of circles coming from one point and overlapping in the least exciting way possible. I want them to vary in size and displacement. I want the Y values to mostly remain the same, somewhere low down on the screen, but I want to vary the X value and the Radius. Ideally I also need them to vary in different ways, so the pattern isn&#x27;t too predictable and boring.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing I try is just adding a displacement with each iteration. I have to store and recall the value of the A register with each iteration just before pushing the X value. This means that the circle centres slowly drift to the right.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t2.webp&quot; alt=&quot;Circles being drawn on the screen and then shifted up, each circle is drawn slightly further to the right of the previous one, creating a slanted stack of circles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This works as expected, but introduces an issue. When the circles reach the end of the screen, it causes an error in the circle drawing routine. The only way to avoid this is to avoid letting the values get too big. If I apply a mask to the result of the addition, then I can effectively do modulo arithmetic on it, so that the X values cycle around this short loop.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t3.webp&quot; alt=&quot;As the previous animation, but once the bubbles get too close the right edge, they loop back to the left&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This isn&#x27;t very random, however. I want more randomness. I&#x27;m able to modify the step size, and this does change the pattern a little, but I want something more chaotic.&lt;&#x2F;p&gt;
&lt;p&gt;The change that adds chaos is fairly simple. I just modify the mask value so that sometimes the mask has the effect of decreasing the number instead of just having a modulo effect.&lt;&#x2F;p&gt;
&lt;p&gt;I also applied the same effect to the circle radius. I loaded the same value from the stack that I was using for the chaotic X motion, and applied another mask and an addition to enforce a minimum size.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t4.webp&quot; alt=&quot;As the previous animation, but the starting position of the bubbles appears more random&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now I have a bunch of parameters I can tweak. There&#x27;s the minimum offset in X, the minimum circle radius, and the mask values for creating chaotic patterns in the X and radius. The mask values are more of an art than a science.&lt;&#x2F;p&gt;
&lt;p&gt;I think the values I chose create a nice pattern. There&#x27;s two clear groupings that the animation switches between, but it takes long enough to repeat the exact same group of circles that it looks suitably random.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;lovebyte_demos&#x2F;bbl_t8.webp&quot; alt=&quot;As the previous animation, but also with random sizes of bubbles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sizecoding-it&quot;&gt;Sizecoding it&lt;&#x2F;h3&gt;
&lt;p&gt;The file is 35 bytes at this point, so I had to do some optimisations.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing I did was rearrange some things so that I don&#x27;t have to initialise the A register at the start of the program. Instead, I pop some value from the stack, which now becomes a kind of random seed for the visualisation. This does technically cause the stack to be corrupt, but that doesn&#x27;t actually matter, since this would only be a problem if I wanted to return control to the BASIC interpreter, which I don&#x27;t.&lt;&#x2F;p&gt;
&lt;p&gt;This gets it down to 33 bytes, so I still need to save at least one byte.&lt;&#x2F;p&gt;
&lt;p&gt;I decided to see what happens when I removed the assignment to A after the first stack operation. The code that pushes the value of A on to the calculator stack actually consumes the value of A, and leaves something else in its place. I&#x27;m not sure what value exactly, but after trying it out, it appeared to do something that looked okay.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s always a slight risk with an optimisation like that. The risk is that this value depends on something unpredictable, and you might get a very different effect to the one you intended.&lt;&#x2F;p&gt;
&lt;p&gt;Regardless, this works fine every time I&#x27;ve tested it, with a few different timings, so I was happy enough to submit this to the competition.&lt;&#x2F;p&gt;
&lt;p&gt;It was only after writing this that I realised &quot;hey these look like bubbles&quot;, and so I decided that these are bubbles, hence the name of the demo.&lt;&#x2F;p&gt;
&lt;p&gt;Despite being some of the smallest demos I have submitted to any demo competition, these are the demos that I&#x27;ve enjoyed working on more than any other I&#x27;ve done previously.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>CFax: A fax service that compiles your C code</title>
          <pubDate>Sat, 27 Jul 2024 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/compilerfax-comile-c-code-by-fax/</link>
          <guid>http://computingthedetails.org/blog/compilerfax-comile-c-code-by-fax/</guid>
          <description xml:base="http://computingthedetails.org/blog/compilerfax-comile-c-code-by-fax/">&lt;p&gt;Yeah, it&#x27;s as silly as it sounds.&lt;&#x2F;p&gt;
&lt;p&gt;What&#x27;s that? &quot;Why?&quot;? ... don&#x27;t ask such silly questions!&lt;&#x2F;p&gt;
&lt;p&gt;Let me explain...&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-this-cursed-device&quot;&gt;What is this cursed device?&lt;&#x2F;h2&gt;
&lt;p&gt;I made a fax service that compiles C code for you.&lt;&#x2F;p&gt;
&lt;p&gt;To use it, take a sheet of paper with some C code printed on it, and feed it in to your fax machine, then dial the number for your instance of Compiler Fax (AKA CFax). For me, this was a Raspberry Pi with a USB Fax Modem attached to the PBX in York Hackspace, so I just dialled &quot;8&quot;. You should now be connected to the compiler fax service. Once you hear that the service has picked up and is making the tone to say it&#x27;s ready to initiate a connection, press the send button on your fax machine to send the C code over to the compiler service.&lt;&#x2F;p&gt;
&lt;p&gt;What&#x27;s fax? Fax, for all those lucky enough to have only ever used email. Is essentially like email before email was a big thing. It&#x27;s essentially just a system for sending a bitmap image of a scanned document to a printer that will print it. This happens over a phone line, and is much slower than email. Normally you can hear a fax being sent.&lt;&#x2F;p&gt;
&lt;p&gt;Eventually, the C compiler service will have finished processing the document and is ready to fax back the result. It calls back on the number specified in the original fax, the fax machine answers, accepts the fax, and prints it out.&lt;&#x2F;p&gt;
&lt;p&gt;You&#x27;re probably wondering what does CompilerFax actually look like? What is it, and how does it work? Can I make one for myself? Do I need a real phone line to be able to use it? What is the meaning of life? Why is the telephone number just &quot;8&quot;?&lt;&#x2F;p&gt;
&lt;p&gt;I also got some more questions in the YouTube comments for my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;pJ-25-pRhpY&quot;&gt;demo video&lt;&#x2F;a&gt;, I&#x27;ll try to answer all these questions here.&lt;&#x2F;p&gt;
&lt;p&gt;Easy first question: what does it look like?&lt;&#x2F;p&gt;
&lt;p&gt;Here it is! It&#x27;s a Raspberry Pi and a USB Fax Modem. Well, at the time of this photo it had two modems connected.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;cfax&#x2F;cfaxhardware.jpg&quot; alt=&quot;The CFax hardware. A Raspberry Pi in a black case with two USB Fax Modems attached&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Yeah, that&#x27;s all there is to it, that is all you need. This is the whole system. Well, I suppose you also need a phone line to connect it to, but that much should be obvious. Once you have this it&#x27;s just a case of installing the right software. The source code for CompilerFax is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;compilerfax&quot;&gt;on github&lt;&#x2F;a&gt;, you can build yourself one of these if you want.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-basics&quot;&gt;The Basics&lt;&#x2F;h2&gt;
&lt;p&gt;The Raspberry Pi is running Raspbian. The default choice for a Raspberry Pi really. On top of that I have installed Hylafax. Hylafax is, according to the website, &quot;an enterprise-class system for sending and receiving facsimiles…&quot;. I&#x27;ve found it to be a pain to configure, and so it certainly feels like enterprise class software.&lt;&#x2F;p&gt;
&lt;p&gt;Hylafax handles the connection to the fax modem, accepts calls and receives faxes in a receive queue, and whenever the line is not busy it attempts to transmit faxes from its outgoing fax queue. There&#x27;s a program you can run to add a fax to the outgoing fax queue. So, to make the C compiler part of this work, all I had to do was write a program that takes faxes from the incoming fax queue, processes them, and generates a report for each one, which gets added to the outgoing fax queue with the appropriate destination phone number.&lt;&#x2F;p&gt;
&lt;p&gt;But how exactly does it process them? It needs to have a C source code file to be able to compile it, but all it has is a bitmap image of the received fax. Well, the problem of extracting text from images is a well studied one, and we have readily available applications for doing just that. Tesseract is one such application. It does Optical Character Recognition (OCR).&lt;&#x2F;p&gt;
&lt;p&gt;My CompilerFax program takes the incoming fax, does some transforms on it because for some reason Hylafax provides me with a squashed version of the fax, then crops off a bit of the top and bottom because fax machines often add headers and footers and we don&#x27;t want the text in them to be in the OCR output. Then it feeds that resulting image into Tesseract, and gets a text file out.&lt;&#x2F;p&gt;
&lt;p&gt;Now, the user needs to make sure they put the return phone number in the program text somewhere. They can just add a comment with a line like this one. CompilerFax looks for this line to determine where to send the reply. If it can&#x27;t find the line, then it&#x27;s not able to reply, so it just ignores the fax and doesn&#x27;t process it any further. If it does find it though, then it needs to compile and run the code, and generate a report to send back.&lt;&#x2F;p&gt;
&lt;p&gt;When it works, it looks like this!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;cfax&#x2F;examplefax.jpg&quot; alt=&quot;An example C program printed on a sheet of A4 paper on the left, and the resulting output on the right, also printed on A4 paper. The program outputs &amp;quot;Hello World&amp;quot; ten times.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;can-i-break-this&quot;&gt;Can I break this?&lt;&#x2F;h2&gt;
&lt;p&gt;This is the point at which lots of people say &quot;woah, wait a minute, I just realised that this thing is going to run arbitrary C code sent to it by a random person with a fax machine. What if they send malicious code? An infinite loop? A fork bomb? What happens then!?&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Well, those sounded like fun, so I gave them a go…&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start with an infinite loop that just prints the same line over and over again.&lt;&#x2F;p&gt;
&lt;p&gt;CFax handles this just fine. If the report&#x27;s too long then it just gets truncated to 10 pages.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, let&#x27;s up the ante. You wanted a fork bomb, let&#x27;s try a fork bomb!&lt;&#x2F;p&gt;
&lt;p&gt;Kinda anticlimactic really, it just times out and does not reply. Fork bombs are no issue.&lt;&#x2F;p&gt;
&lt;p&gt;Clearly I put more thought into this than some people gave me credit for! I did make some mistakes though.&lt;&#x2F;p&gt;
&lt;p&gt;One thing I didn&#x27;t think about at first was: what if you tell it to reply to its own phone number? Well, initially this wasn&#x27;t a problem, because it only uses a single phone line, so it would never be able to call itself because the line would be busy, and it would give up after a few attempts. But I was planning to take this to EMF Camp and connect up two phone lines at once, so it probably could have dialled itself then. If it had done, then it would send the report to itself as a new fax, and if that report had the reply line in, which it likely would, then the next report would be the result of trying to compile the text of the report as C code. That would probably include the reply line, and thus it would send another fax, and then another, and another, and thus permanently have a fax circling around the incoming and outgoing fax queues. I added some logic to check if it&#x27;s faxing itself, and then just not fax itself. This logic is not perfect, and is probably still exploitable on some phone systems where there&#x27;s an optional prefix for the number, so watch out for that if you build one of these for yourself.&lt;&#x2F;p&gt;
&lt;p&gt;I also recently discovered a bug that means it fails to reply when it should. If you generate enough output text then you can cause a crash in the report generator code. It loads the whole output log into memory before it starts generating the report, and so if you generated enough output then it would run out of memory and crash before it completed. It did eventually recover and continue handling faxes after the crash, but it would cause a bit of a delay, and you&#x27;d never get the report for your program. I fixed this by limiting the log size to a sensible maximum.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-is-this-secure&quot;&gt;How is this secure?&lt;&#x2F;h2&gt;
&lt;p&gt;How did I make it safe to compile and run random code including fork bombs? Well, I used &quot;prlimit&quot;, and a really powerful linux utility called &quot;unshare&quot;. Unshare creates new kernel namespaces temporarily to run a given program. It can also change the root directory at the same time, with the chroot system call. Namespaces in linux are essentially separate contexts in which programs have access to a different set of resources. I used it to make sure that the compiler and the compiled program run without root access, without access to the network interfaces, without being able to communicate with other processes on the system, and they can only run inside a minimal Alpine root.&lt;&#x2F;p&gt;
&lt;p&gt;The Alpine root only has a few select packages from Alpine. It has a minimal base set of packages, plus GCC for compilation. That&#x27;s it, nothing more.&lt;&#x2F;p&gt;
&lt;p&gt;The preparation of the report is done outside the sandbox by grabbing the output file from the sandbox. It also resets the sandbox in between runs. There&#x27;s only one directory that is writeable in the sandbox, so a reset is as simple as deleting and recreating that directory.&lt;&#x2F;p&gt;
&lt;p&gt;So there&#x27;s the whole software stack. Raspbian, Hylafax, Tesseract, a hint of ImageMagick, a little bit of Alpine, and a bunch of glue code that I wrote to join it all together and generate faxable compilation reports.&lt;&#x2F;p&gt;
&lt;p&gt;I was hoping for a real stress test at EMF Camp 2024, where there would be a telephone network to connect it to, and plenty of hackers to have a go at breaking it. Here&#x27;s a picture of the advert that I got on the beam slides that show between talks. I actually mailed it to the beam team through sneakernet, the camp&#x27;s volunteer postal service. They kindly photographed the poster and put it on the screens.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;cfax&#x2F;beamslide.jpg&quot; alt=&quot;An advert for CFax, printed on paper, phorographed, and projected on to a large screen in a tent.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, it didn&#x27;t get much of a stress test. Perhaps I need to make it more reliable for EMF Camp 2026? Then I&#x27;ll get some takers!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;more-questions-answered&quot;&gt;More questions answered&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s end on some quickfire questions and answers:&lt;&#x2F;p&gt;
&lt;p&gt;Q1: Do you need a real phone line to be able to use this?
A: No! in fact, the only way to use my instance of it right now is to turn up at York Hackspace and use the internal phone system. There&#x27;s an old PBX on the wall, and it&#x27;s only connected up to local phones and fax machines, no external connections at the moment. If you have your own PBX then you can run this in your home too! If anyone knows of a cheap web host that also provides a phone number and fax modem connected to your machine, let me know, I might make a publicly accessible CompilerFax.&lt;&#x2F;p&gt;
&lt;p&gt;Q2: Why is the number just 8?
A: Partly answered by the previous question, it&#x27;s on a small private phone network in a hackspace. There&#x27;s only 8 phone lines on it, so they&#x27;re numbered 1 to 8. The fax machine I&#x27;m testing it with is on line 7.&lt;&#x2F;p&gt;
&lt;p&gt;Q3: What if you need more than one page to send all your code?
A: Multipage faxes are not supported at the moment. Patches welcome!&lt;&#x2F;p&gt;
&lt;p&gt;Q4: Is the sandbox actually secure?
A: I&#x27;ve done the best I can to secure it, but there might still be issues. Try and break it for yourself! The code is all on github.&lt;&#x2F;p&gt;
&lt;p&gt;Q5: Can you specify the compilation options? If so how?
A: No, unfortunately. I could maybe add in a way to do this, perhaps a special comment at the top of the file. For now though it will always compile with this command: gcc -lm program.c -o program
So it does link in the maths library, but that&#x27;s it. Otherwise it needs to be a self-contained single-file program that can be compiled with the default options.&lt;&#x2F;p&gt;
&lt;p&gt;I hope that clears up all the questions people had, but please do feel free to ask more!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>I built a laptop for EMF Camp 2024</title>
          <pubDate>Tue, 14 May 2024 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/i-built-a-laptop-for-emf-camp-2024/</link>
          <guid>http://computingthedetails.org/blog/i-built-a-laptop-for-emf-camp-2024/</guid>
          <description xml:base="http://computingthedetails.org/blog/i-built-a-laptop-for-emf-camp-2024/">&lt;p&gt;EMF camp is coming up soon. It&#x27;s a camping festival with talks, workshops, and fascinating installations. Attended by tech folk, craft folk, artists, and engineers.&lt;&#x2F;p&gt;
&lt;p&gt;Last time I took a giant playable LED Rubik&#x27;s cube. I&#x27;m not doing that again this time, sadly. The cube is a bit broken, and it was a lot of effort to do, so this time I&#x27;m just doing a personal thing rather than a big installation.&lt;&#x2F;p&gt;
&lt;p&gt;Because of the kind of event that EMF is, I need to take with me some kind of device that is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;portable&lt;&#x2F;li&gt;
&lt;li&gt;has a battery that will last for most of the event&lt;&#x2F;li&gt;
&lt;li&gt;is capable of displaying the timetable&lt;&#x2F;li&gt;
&lt;li&gt;is capable of displaying the camp map&lt;&#x2F;li&gt;
&lt;li&gt;can fetch updates to the timetable and camp map&lt;&#x2F;li&gt;
&lt;li&gt;and provides a convenient way to contact my friends at the camp&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So that&#x27;s why I built a ZX Spectrum based laptop with a dial up modem, telephone handset, and a cassette deck! With all that functionality, I might not even need my smartphone while I&#x27;m there!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;finished_laptop.jpg&quot; alt=&quot;The laptop, fully built, standing on a white table. The screen is lifted up, but isn&amp;#39;t displaying anything. It has the rainbow shoulder strap attached.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-phone-bit&quot;&gt;The phone bit&lt;&#x2F;h2&gt;
&lt;p&gt;There&#x27;s going to be a copper telephone network, and a DECT network at EMF camp. DECT is the standard that your cordless home phone uses to connect to the base station. Hopefully there will be some interesting ViewData services running somewhere on the phone network. So I&#x27;m planning to take this ViewData modem to access those services. It needs to be connected to a ZX Spectrum, and needs a separate telephone for dialling. The modem is the black box underneath the ZX Spectrum in this photo.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;spec_and_modem.png&quot; alt=&quot;A ZX Spectrum standing on top of a Prism VTX 5000.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;ViewData is a telephone based information access system. You dial a number, wait for an answer, and then let the modem join the call, and then you can receive what is essentially a TeleText page that contains the information you want.&lt;&#x2F;p&gt;
&lt;p&gt;In the UK, there was a ViewData service offered by the Post Office Telecoms service, which later became British Telecom (BT). Their service was called Prestel, so you might hear these modems referred to as Prestel modems. Prestel ran from 1978 to 1994. It is now thoroughly obsolete. There is nothing you can do with ViewData that you can&#x27;t do more easily with an internet connection. But, of course, I enjoy playing with esoteric old computer systems, and so do a lot of other people that attend EMF Camp. So we&#x27;re still gonna use it just for fun.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;prestel.png&quot; alt=&quot;The Prestel Logo. It is a grid of letters that spell the word Prestel in various directions, and with various colours on the letters.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I could just take this assortment of components and connect them all together, but I&#x27;m gonna be in a field, and I kinda want to be able to take it around the camp without much effort. So that&#x27;s why I decided to turn them into a laptop. All I really had to do was put all these parts into a case, and add a battery and power circuitry.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;parts.jpg&quot; alt=&quot;All of the parts of the laptop, assembled and working on a bench, as a proof of concept.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For some reason I decided to massively overcomplicate the phone part though. I bought a cheap landline phone, disassembled it, replaced the dial pad, and built a custom housing for it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;bt_handset.png&quot; alt=&quot;A generic cheap BT telephone handset.&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;phone_with_custom_keypad.jpg&quot; alt=&quot;The same handset, disassembled, with a custom keypad ready to connect to it in place of the original.&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;custom_phone_case.jpg&quot; alt=&quot;The custom phone case, partly assembled, showing a 3D printed handset, and a laser cut cradle that also houses the keypad.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The phone line is kinda interesting. I was wondering about how to connect to the camp phone network while I&#x27;m wandering the camp. It&#x27;s not like landline phones are portable, and the modem certainly doesn&#x27;t support DECT. But then someone pointed out to me that BT makes a device called a Digital Voice adapter. You see, BT is trying to switch everyone over to VoIP. They want the whole phone network to be digital. If you register for an internet and phone service from BT today then they&#x27;ll send you a &quot;hub&quot; which is a modem, wifi router, and a DECT base station all in the same package. But some customers have old style phones that they still want to use. In fact, you can still buy brand new BT branded analog telephones, which is what I did for this project. So they sell these devices for &quot;upgrading&quot; an old phone to be a DECT phone. I just need to embed one of those in my laptop.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;dv_adapter.png&quot; alt=&quot;A BT Digital Voice Adapter.&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;dv_adapter_open.jpg&quot; alt=&quot;The electronics from inside a BT Digital Voice Adapter.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With the DECT adapter, I can connect wirelessly to the phone network and have a completely mobile solution to accessing the timetable and camp map. Oh, and it will let me contact my friends while I&#x27;m there too, the phone still works as normal.&lt;&#x2F;p&gt;
&lt;p&gt;Another EMF-goer, called Ben, has put together a viewdata service for accessing the schedule information. I&#x27;ve left a link in the description to a browser based viewdata terminal emulator that he&#x27;s set up to demo his service. I&#x27;m planning to make use of it while I&#x27;m there. But, of course, just in case that doesn&#x27;t work for some reason, I have a backup plan.&lt;&#x2F;p&gt;
&lt;p&gt;You can find a demo of this on his website... &lt;em&gt;On last inspection, the site appears to no longer work, but the link is preserved here anyway&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;viewdata.microwavepizza.co.uk&#x2F;&quot;&gt;https:&#x2F;&#x2F;viewdata.microwavepizza.co.uk&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;offline-schedule-access&quot;&gt;Offline schedule access&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_mainmenu.png&quot; alt=&quot;Screenshot of the main menu for the schedule browser software&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_example_events.png&quot; alt=&quot;Screenshot of one of the pages of events for the Friday&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_event_opening.png&quot; alt=&quot;Screenshot of the event details page for the opening ceremony&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s possible that I won&#x27;t be able to connect to the DECT network, or that my modem might develop a fault, or something else I&#x27;ve not expected. So my backup plan for if I can&#x27;t access the schedule on the phone network is to just have a local copy on cassette tape, and load it directly.&lt;&#x2F;p&gt;
&lt;p&gt;I put quite a bit of effort into the software for this. It can show the timetable, and even filter it based on event type. You can view just a specific day&#x27;s events, or even search in the title or speaker name fields.&lt;&#x2F;p&gt;
&lt;p&gt;The full 2024 timetable is not released yet, so I&#x27;ve used the 2022 timetable as a placeholder. I&#x27;m hoping that the 2024 timetable is distributed in the same JSON format that the 2022 timetable was. I&#x27;m also hoping that there&#x27;s not significantly more data in the 2024 timetable, since it&#x27;s such a struggle to fit it all in the spectrum&#x27;s memory.&lt;&#x2F;p&gt;
&lt;p&gt;In fact, I already can&#x27;t fit it all in. There&#x27;s way too much text. My friend, Lissa, came up with a really clever text encoding scheme that gets the text down below 60% of its original size. She analysed a whole bunch of encoding and compression schemes before settling on this design. So, thank you Lissa! you made this a whole lot easier!&lt;&#x2F;p&gt;
&lt;p&gt;Instead of using the file as a stream of bytes, this encoding considers them to be a stream of bits. The encoding process analyses the text to create a list of characters ordered by frequency of use, and then assigns shorter encodings to the more frequently used characters. The encodings are chosen so that no short encoding is a prefix to a larger one. This is essential for the decoder to be able to identify when it has parsed a complete character.&lt;&#x2F;p&gt;
&lt;p&gt;The shortest encodings are only 3 bits long, but they get longer and longer as you have more unique characters. The encodings can get really long, and some characters are actually much larger in this encoding, but those are only the really infrequent characters, so overall you get a good space saving.&lt;&#x2F;p&gt;
&lt;p&gt;However, even with that I can&#x27;t load all the text at once. Each event on the timetable has a detailed description attached to it. This is great, and I want to make sure that my program can display it. The only problem is that even just the description fields alone is too much to load all of it at once. The encoded description text is about 94k. The Spectrum only has 48k and after loading the program, map data, and other event information, there&#x27;s only about 10k left over anyway.&lt;&#x2F;p&gt;
&lt;p&gt;The solution to this problem is to simply not load it all at once. If it&#x27;s currently saturday, then I probably don&#x27;t care too much about what happened on friday, or what&#x27;s going to happen on sunday. So I don&#x27;t need all of the descriptions for all of the events to be loaded at once. I&#x27;ve split up the descriptions into modules that can be loaded and unloaded as long as there is space in memory for them. In practice this means that you can only have one description module loaded at a time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-map&quot;&gt;The Map&lt;&#x2F;h2&gt;
&lt;p&gt;I decided it was worthwhile allocating a little bit of RAM for the map feature. This can stay loaded all the time. It does take up about 6k of RAM, but I think it will be worth it for the convenience of being able to navigate the camp in a hurry when a talk is about to start.
It&#x27;s not particularly detailed. There&#x27;s only two zoom levels. The first one is just an overview of the camp, with only the biggest landmarks visible. You can zoom in on the north or south of the camp to see a little more detail. I mainly just want to be able to find all the venues listed on the timetable.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_map.png&quot; alt=&quot;Screenshot of the map of the camp&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_map_north.png&quot; alt=&quot;Screenshot of the map of the camp, zoomed in to the North side&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;screenshot_map_south.png&quot; alt=&quot;Screenshot of the map of the camp, zoomed in to the South side&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;have-a-go&quot;&gt;Have a go!&lt;&#x2F;h2&gt;
&lt;p&gt;If you want to, you can play around with the software. Download &lt;a href=&quot;&#x2F;posts&#x2F;laptop_for_emf_camp&#x2F;emfinfo.tap&quot;&gt;the tap file&lt;&#x2F;a&gt; for use in your favourite ZX Spectrum Emulator. You can also find all the code and releases &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;emfinfo&quot;&gt;on GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ve tried to design it so that it can easily be ported to some other systems too, so that if you&#x27;re going to EMF camp and bringing your own retro laptop that isn&#x27;t ZX Spectrum based, then you can use the same software with a little bit of porting effort. I&#x27;d love to add support for MS DOS and Amiga myself, but I doubt I&#x27;ll find the time before the event. It&#x27;s only a couple of weeks away. &lt;em&gt;Update: There is now an &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;EMFInfo&#x2F;releases&#x2F;download&#x2F;Release&#x2F;EMFInfo_ms-dos.zip&quot;&gt;MS DOS version&lt;&#x2F;a&gt; and also a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;EMFInfo&#x2F;releases&#x2F;download&#x2F;Release&#x2F;EMFInfo_linux.zip&quot;&gt;Linux version&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since I&#x27;m taking this around a field, I&#x27;ve tried to make it easy to carry. The rainbow guitar strap isn&#x27;t just to make it look cool, and the screen hinge is also a sturdy carry handle. Fortunately, I even had the foresight to design this little spring-loaded telephone handset retention system, so that the phone stays in place while I have the laptop on my shoulder.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll probably be quite easy to spot in the field. If you do find me at EMF camp, and have a cassette recorder, you can take a copy of the software from my cassette tape if you like.&lt;&#x2F;p&gt;
&lt;p&gt;Or, even better: I&#x27;ve never actually used this user-to-user communication software that came with the Prism modem. I&#x27;ve only got one modem, and never known anyone else with one while I&#x27;ve also had a proper phone system to try it with. So if you have one at EMF camp, come find me! Let&#x27;s try it out!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A Very Silly CPU Monitor</title>
          <pubDate>Wed, 29 Nov 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/a-very-silly-cpu-monitor/</link>
          <guid>http://computingthedetails.org/blog/a-very-silly-cpu-monitor/</guid>
          <description xml:base="http://computingthedetails.org/blog/a-very-silly-cpu-monitor/">&lt;p&gt;We&#x27;ve all seen CPU usage indicators in Windows&#x27; Task manager, or htop, or whatever the MacOS thingy is called. But I thought it&#x27;d be cool if that wasn&#x27;t just a number on the screen, but represented physically somehow.&lt;&#x2F;p&gt;
&lt;p&gt;And what better way to monitor a bunch of numbers, than to have a bunch of analog meters? I have fourteen of them, to be exact. My desktop PC reports 12 CPU load averages, so I have one for each of those, and I added in a couple of extras for memory usage and swap space usage.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_very_silly_cpu_monitor&#x2F;cpu_monitor.jpg&quot; alt=&quot;The Monitor&quot; title=&quot;A 2x7 grid of analog meters lit up in various colours between green and red.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I was originally planning to use a bunch of old AVO multimeters for this purpose. I have eight of them, which was enough for this project &lt;em&gt;before&lt;&#x2F;em&gt; I procrastinated it for so long that I had upgraded my computer. The problem is, they&#x27;re huge, and not as easy to add coloured lighting to.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_very_silly_cpu_monitor&#x2F;avo.jpg&quot; alt=&quot;AVO meter&quot; title=&quot;An AVO Model 8 Multimeter&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;These little things are much more manageable. They&#x27;re fairly cheap, only about £2.50 each. I got the 0-5mA displays, which I can drive directly from an Arduino using PWM through a 1k resistor. Of course, the 0-5mA scale that comes with the meters is not what I want, so I printed these custom scales for the various things I wanted. 12 different CPU% scales, all individually numbered, one for percentage of memory used, and another for percentage of swap space. It&#x27;s fairly easy to add them in. The scales are printed onto little metal plates, so I just printed my replacement scales onto sticker paper and stuck them over the top.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_very_silly_cpu_monitor&#x2F;scale_plate.png&quot; alt=&quot;Scale plate&quot; title=&quot;Two hands holding a small metal plate with a milliamp scale printed on it. The scale goes from 0 to 5mA&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I was selling this as a complete product then I&#x27;d probably get the scales custom printed by the manufacturer, since that would look much neater. For a one-off though, I&#x27;m very happy with this.&lt;&#x2F;p&gt;
&lt;p&gt;I did consider making the memory and swap space meters use absolute scales, measured in gigabytes, but then it wouldn&#x27;t be very future proof. I could easily upgrade the memory or swap space in my system and that would mean I&#x27;d need to replace the scale again.&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, I also added LEDs for a little more pizzazz. By chance, I had an addressable LED strip lying around that happened to have exactly the right spacing to get two LEDs under each display with minimal spacing between the displays. I think the coloured lighting really adds a lot of charm.&lt;&#x2F;p&gt;
&lt;p&gt;The electronics for this is really simple. There&#x27;s an arduino mega in the back, along with a little power supply for the LEDs, and a big mess of wires connecting all the displays up.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_very_silly_cpu_monitor&#x2F;innards.png&quot; alt=&quot;Wiring&quot; title=&quot;Left: An arduino mega on the bench wired up to a single 0-5mA meter, driving it to approximately 3.5mA. Right: The main chasis of the complete display unit shown from the back. Lots of wires are running from the terminals on the meters.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Most of the frame is laser cut, and just slots together with little cut-outs and tabs. To make it really rigid, the main A-frame pieces are held together with some aluminium bars that I machined to length. It feels really strong, not like it&#x27;s going to twist or bend at all.&lt;&#x2F;p&gt;
&lt;p&gt;I think it looks quite neat under my monitor, just quietly working away, letting me see at a glance how hard I&#x27;m pushing my computer, and providing an entertaining display whenever I start compiling some large software project for the first time.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Printing Banners on the Sinclair ZX Spectrum (with a receipt printer)</title>
          <pubDate>Mon, 21 Aug 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/printing-banners-from-a-zx-spectrum/</link>
          <guid>http://computingthedetails.org/blog/printing-banners-from-a-zx-spectrum/</guid>
          <description xml:base="http://computingthedetails.org/blog/printing-banners-from-a-zx-spectrum/">&lt;p&gt;I wanna show you this banner printer I made! It&#x27;s not a very complicated system, except for one really specific part. Which of course means that&#x27;s the part I want to talk about.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;banner_printer&#x2F;example_banner.png&quot; alt=&quot;A pair of banners printed on till receipt paper in very large lettering. The top one says &amp;quot;DejaVu serif&amp;quot; and the bottom one says &amp;quot;The quick brown fox&amp;quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Before we get there though, let&#x27;s have a look at the setup.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-setup&quot;&gt;The Setup&lt;&#x2F;h2&gt;
&lt;p&gt;There is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a Sinclair ZX Spectrum, from 1982.&lt;&#x2F;li&gt;
&lt;li&gt;a cassette deck, of course.&lt;&#x2F;li&gt;
&lt;li&gt;a Kempston Centronics interface, for connecting to a printer, also 80s era.&lt;&#x2F;li&gt;
&lt;li&gt;And finally, a modern thermal printer.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;banner_printer&#x2F;overview.png&quot; alt=&quot;A photo of the banner printer system set up. It contains all the components listed above, and also a more modern CRT television&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now, that last one is quite an anachronism, I know.  So this isn&#x27;t quite &quot;how to print banners like it&#x27;s 1982&quot;, but Sinclair&#x27;s printer for this thing is incredibly slow, and it&#x27;s hard to get the paper for it now. Also I don&#x27;t have one anyway. So for now we get this.&lt;&#x2F;p&gt;
&lt;p&gt;I would love to see this work on a Sinclair printer, and it would be possible to do it, but actually the interesting part of making this work was interfacing with this modern printer fast enough to print banners at the printer&#x27;s maximum speed.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so why was this hard? Sounds like an easy project right? just print the right sequence for each letter. What is this mysterious complicated thing I mentioned? well, let me show you version one of this system.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-simple-big-slow-solution&quot;&gt;A simple, big, slow solution&lt;&#x2F;h2&gt;
&lt;p&gt;So I want to print the letter &#x27;A&#x27;. maybe I&#x27;ll do that with some LPRINT commands, and use spaces and hashes to make empty pixels and filled pixels.&lt;&#x2F;p&gt;
&lt;p&gt;I just have to have a subroutine that looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;BASIC&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;935 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   #                 &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;936 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ####              &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;937 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   #######           &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;938 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;     ########        &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;939 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;       #########     &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;940 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;       ##   #####    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;941 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;       ##      ##    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;942 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;       ##   #####    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;943 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;       #########     &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;944 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;     ########        &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;945 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   #######           &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;946 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ####              &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;947 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   #                 &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;948 RETURN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now I need another one, for the letter B, and another one for each other letter I want to be able to print, and all the punctuation, etc.&lt;&#x2F;p&gt;
&lt;p&gt;I did start manually writing these, but quickly realised that was a silly idea, and generated all the code for this from a font I had on my laptop. This works fine, except, it took nearly three whole minutes to load the resulting program from the tape. It&#x27;s a loooong program.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;making-it-shorter-better-faster-stronger&quot;&gt;Making it shorter, better, faster, &lt;del&gt;stronger&lt;&#x2F;del&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;So, the program takes a long time to load. It&#x27;s slow to print, and also the banners are not particularly readable from a distance. The contrast between the filled and unfilled pixels is not great. We can solve both of these problems at once.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s a lot of repetition in this file. Lots of lines that start with LPRINT, lots of long runs of spaces and long runs of hashes. It&#x27;s clearly very compressible. In particular, the only things we really care about here are the image data, and the boundaries between the images.&lt;&#x2F;p&gt;
&lt;p&gt;The current solution uses an entire byte for each pixel, but we only have two colours anyway, so we want to reduce this to just one bit per pixel. So in this section, for the letter &#x27;A&#x27;, we&#x27;ve used 369 bytes to represent the image, but at one bit per pixel, tightly packed, the bitmap would only be 35 bytes long.&lt;&#x2F;p&gt;
&lt;p&gt;Clearly this first approach is very wasteful and we should switch to a packed bitmap format. However, switching to this tightly packed format produces another problem. Previously, the image data was encoded exactly how the printer was expecting it. It was already a stream of characters that just get sent to the printer as-is. Now we need to do an extra step before we can send it. We need to convert the bitmap into a stream of printable characters.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re gonna have to do some bit manipulation on the data, to extract each pixel bit by bit and generate the characters on the fly. Unfortunately, Sinclair BASIC doesn&#x27;t have bitwise operations. They can be constructed from other operations that are present, but it&#x27;s much faster to just use machine code. Most of the program is still BASIC though, just the expanding of the font data will be done with machine code.&lt;&#x2F;p&gt;
&lt;p&gt;Since I was going to this much effort, I also decided to add some compression.&lt;&#x2F;p&gt;
&lt;p&gt;Take a look at this letter L. It&#x27;s three identical lines, and then seven identical lines.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;BASIC&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1067 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##############    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1068 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##############    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1069 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##############    &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1070 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1071 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1072 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1073 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1074 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1075 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1076 LPRINT &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;   ##                &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1077 RETURN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It would be nice to have a way to say &quot;repeat this line this number of times&quot; and save repeating the same few bytes over and over. Turns out there&#x27;s a really neat way to do this.&lt;&#x2F;p&gt;
&lt;p&gt;The width of the printer output in the mode that I&#x27;m using to print these banners is 21 characters. That means that I need 21 pixels per line. A byte has 8 bits, and therefore 8 pixels, so I need three bytes before I have enough bits for an entire line. That&#x27;s 24 bits. So I have three bits spare before the end of the third byte after I&#x27;ve encoded the first line of pixels.&lt;&#x2F;p&gt;
&lt;p&gt;I could start the next line in those three bits, if I wanted to, but actually I chose to keep all the lines aligned to 3 bytes, and to use those three spare bits for something else.&lt;&#x2F;p&gt;
&lt;p&gt;Two of these bits are going to encode a number. This number is in the range 1 to 4, and it&#x27;s the number of times to print the line. Of course, a two bit binary number normally has the range 0 to 3, and so I add one during decompression to get a number in the range 1 to 4. The last bit tells us if this is the last line of the bitmap. This lets us know where to stop printing each character.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;first byte&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|          7..0           |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|          bitmap         |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;second byte&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|          7..0           |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|          bitmap         |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;third byte&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|    7..3      | 2 | 1..0 |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|    bitmap    |stp| reps |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---------------------------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So the decompression routine builds a string representing the current line, calculates the number of times to repeat it, then sends that string to the printer the right number of times. Finally it checks if it has reached the end of the bitmap. If it has then it is finished printing this character, if not, it does the same thing again on the next three bytes.&lt;&#x2F;p&gt;
&lt;p&gt;One huge advantage of this approach is that I can use any pair of characters I want for the light and dark pixels with no extra cost. Previously I could only use characters that I could type on the spectrum keyboard. This printer has support for a character that is just a filled rectangle, it&#x27;s perfect for this purpose. I use that character, and the space character, and get a much better contrast than before.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;banner_printer&#x2F;contrast_compare.png&quot; alt=&quot;A comparison of the contrast of eavh printing method. The top banner uses the hash symbol for filled pixels, but the lower banner uses the block character. The contrast is much higher on the lower banner.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This loads soooo much faster too. We&#x27;re down from 160 seconds of load time, to just 40 seconds. And now, since the font data is just a bitmap somewhere in memory, rather than a bunch of subroutines in a BASIC program, I can easily load other data in its place. So, naturally, I converted all of the fonts on my laptop into this format, and I loaded them all on to one big cassette tape.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s a 90 minute cassette, and I did my best to fill it up, but only ended up with about 70 minutes of font data. I bet there&#x27;s not many other people in the world that have 70 minutes worth of font data stored on a compact cassette.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;banner_printer&#x2F;font_examples.png&quot; alt=&quot;A comparison of several different fonts printed using this banner printer system. Each example uses the phrase &amp;quot;The quick brown fox&amp;quot;. From top to bottom, the fonts are as follows: DejaVu Serif, DejaVu Sans, Audiowide&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I should have filtered through the fonts I had on my laptop, because actually lots of them don&#x27;t even have the characters I care about and just rendered placeholders instead. But I had space to spare on the tape anyway, so it&#x27;s not really a problem.&lt;&#x2F;p&gt;
&lt;p&gt;I also added in a name badge printing thing, because why not? And now I have possibly the silliest banner printing system there has ever been.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>terminal.doc: a Word document that is a terminal emulator</title>
          <pubDate>Mon, 29 May 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/this-word-document-is-a-terminal-emulator/</link>
          <guid>http://computingthedetails.org/blog/this-word-document-is-a-terminal-emulator/</guid>
          <description xml:base="http://computingthedetails.org/blog/this-word-document-is-a-terminal-emulator/">&lt;p&gt;I decided to make a Word document that is also a terminal emulator.
It doesn&#x27;t have any practical purpose, I just made it for fun.&lt;&#x2F;p&gt;
&lt;p&gt;The first video linked above (Part 1) is a demo of me using the terminal emulator.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to try this for yourself, you will need to be running Microsoft Word on Windows 10 or 11. You&#x27;ll then need to download the &lt;a href=&quot;&#x2F;posts&#x2F;this_word_document_is_a_terminal_emulator&#x2F;terminal.docm&quot;&gt;terminal emulator document&lt;&#x2F;a&gt;, open it in Word, and allow the macros to run.&lt;&#x2F;p&gt;
&lt;p&gt;This blog post explains how that document works.&lt;&#x2F;p&gt;
&lt;video width=&quot;1280&quot; autoplay loop&gt;
&lt;source src=&quot;&amp;#x2F;posts&amp;#x2F;terminal_dot_doc&amp;#x2F;demo.webm&quot; type=&quot;video&amp;#x2F;webm&quot;&gt;
A demo of various commands being run inside bash and CMD inside terminal.doc
&lt;&#x2F;video&gt;
&lt;h2 id=&quot;the-short-answer&quot;&gt;The short answer&lt;&#x2F;h2&gt;
&lt;p&gt;Some of you have predicted this already. It uses VBA scripts in the document, which load some kernel API functions from some Windows DLLs, and uses them to launch and interact with programs.&lt;&#x2F;p&gt;
&lt;p&gt;The long answer includes some interesting challenges though, and that&#x27;s what the rest of this blog post is about.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;terminal_dot_doc&#x2F;macros.png&quot; alt=&quot;A screenshot of the macro editor in Word, showing the macros definitions for terminal.doc&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-makes-a-terminal-emulator&quot;&gt;What makes a terminal emulator?&lt;&#x2F;h2&gt;
&lt;p&gt;There&#x27;s three important parts to a terminal emulator. There&#x27;s:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a parser for the incoming data stream, from the program that is connected to the terminal, which determines how to update the terminal&#x27;s text buffer.&lt;&#x2F;li&gt;
&lt;li&gt;a renderer to turn that buffer into something that can be displayed on the screen.&lt;&#x2F;li&gt;
&lt;li&gt;And finally there&#x27;s a keyboard encoder which takes keyboard input data and turns it into a data stream to send to the connected program.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;terminal_dot_doc&#x2F;terminal_diagram.png&quot; alt=&quot;A diagram of the interactions between the parts listed above&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The keyboard encoder part is normally very simple. Any modern OS provides keyboard input through a convenient abstraction layer.
For most key presses, the terminal emulator just passes the character from the keyboard event directly to the program.
Sometimes it needs to be a bit more sophisticated, though. For example arrow keys get turned into special control sequences.&lt;&#x2F;p&gt;
&lt;p&gt;The renderer part is the next most complicated part, as it requires interacting with graphics hardware. Again, modern OSes provide good abstraction layers for graphics output, but generally this part is still more complicated than input.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the parser for incoming data is the most complicated part. It is, in my opinion, what defines what a terminal emulator is. If you don&#x27;t do this step and just put all the text on the screen without parsing it, then you wouldn&#x27;t have a terminal emulator, you&#x27;d just have a thing that displays a raw data stream.&lt;&#x2F;p&gt;
&lt;p&gt;So these are the three things I needed to have in my terminal emulator.
Now, over to Word. I&#x27;m sure the first question some of you had after seeing the demo was &quot;how is this even possible inside a word document? Word documents are just static blobs of data, they don&#x27;t contain code!&quot;&lt;&#x2F;p&gt;
&lt;p&gt;Well, actually they can contain code. Microsoft&#x27;s Visual Basic for Applications, AKA &quot;VBA&quot;, is a lighter weight version of Visual Basic that Microsoft has embedded into many of the applications that they offer.
It enables application functionality to be augmented by the user, or by other developers, without them needing to have the source code for the application itself. This is a really powerful tool for building useful things on top of existing code.&lt;&#x2F;p&gt;
&lt;p&gt;In the case of VBA in Word: the code that you wrote would be stored inside of a Word document or a template file, and would be available to anyone using that file.
In Word, it is typically used to generate parts of documents. The classic example is mail merge, where you want to generate a bunch of letters and print them out and mail them to people given a database of names and addresses.&lt;&#x2F;p&gt;
&lt;p&gt;There is a big downside to this though. Allowing people to embed computer code into Word documents is risky. Suppose I write some VBA code that starts sending all the contents of your Documents folder over the internet to a web server that I own. Now I just need to email you a word document, and once you open it I start receiving confidential files.&lt;&#x2F;p&gt;
&lt;p&gt;By default, modern versions of Microsoft Word will not execute VBA code embedded in documents. There are modern alternatives to VBA that are much safer for the user, and so these
are preferred instead. Of course, Microsoft doesn&#x27;t want to break compatibility with older documents, so Word will ask you if you want to allow macros to run.&lt;&#x2F;p&gt;
&lt;p&gt;The reason why I&#x27;m using VBA for this instead of the modern MS Word extension API is because it&#x27;s far more entertaining to have a Word document be a terminal emulator than it is to have a Word extension that&#x27;s a terminal emulator. It&#x27;s almost too easy if it&#x27;s an extension. The VBA method also means I can share just a single Word file with everyone and it &quot;just works&quot; with no plugins to install.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-technical-details&quot;&gt;The Technical Details&lt;&#x2F;h2&gt;
&lt;p&gt;So how does this actually work?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;1-text-buffers-and-rendering&quot;&gt;1. Text Buffers and Rendering&lt;&#x2F;h3&gt;
&lt;p&gt;Normally a terminal emulator would maintain buffers in memory that contain details about the text to put on the screen. Typically there is a buffer for text, and another buffer for formatting attributes. Then the rendering code takes the contents of these buffers and produces the final image you see on the screen.&lt;&#x2F;p&gt;
&lt;p&gt;In the case of my Word document terminal, I wanted to use the existing rendering features in Word. Since Word already has its own internal buffers for keeping text and formatting information around, I wanted to simply reuse these. This is one of the things that I did to make the task deliberately more difficult. It&#x27;s more difficult because the user is able to directly interact with this buffer at the same time as my code does. If I was maintaining my own buffers then there would be much less opportunity for race conditions and mismatches in internal state.&lt;&#x2F;p&gt;
&lt;p&gt;You see, when my code needs to add to this buffer, or rather: add to the document, it needs to determine somehow where in the document to insert the new text, insert it there, set any attributes on it, and make sure that the cursor ends up in the right place.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s hard to be sure if the cursor is already in the right place. The user can move the cursor in various ways. They can use arrow keys, click somewhere else in the document, or perhaps implicitly move the cursor by deleting or pasting in text. All of these actions are handled by Word directly, and not from my code. My code just has to deal with the consequences of them. In practice, the way that I deal with this problem is by ignoring it, mostly.&lt;&#x2F;p&gt;
&lt;p&gt;While my code will move the cursor if required by a specific control sequence from the program, it otherwise just allows the user to move it, and inserts new text wherever the cursor happens to be. The reality is that this is good enough for most use cases. (As if there are any real use cases for a terminal emulator in a Word document.) This is part of the reason why vim doesn&#x27;t work very well in this terminal emulator.  Vim explicitly moves the cursor around a lot, and expects it to remain wherever it puts it until it moves it again. If the user has moved the cursor in the meantime, then vim&#x27;s rendering logic fails.&lt;&#x2F;p&gt;
&lt;p&gt;So, my terminal emulator is using the document as its buffer. This means that I get to entirely skip writing the rendering part of a terminal emulator, since Word is doing that for me.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;2-handling-keyboard-input&quot;&gt;2. Handling Keyboard Input&lt;&#x2F;h3&gt;
&lt;p&gt;Word already does a bunch of things in response to keyboard input. One thing that it doesn&#x27;t do, however, is provide VBA code with a way to listen to keyboard input events.&lt;&#x2F;p&gt;
&lt;p&gt;It is technically possible to receive keyboard events from a Form in VBA code, but that&#x27;s not applicable here because I want this to work in the normal document pane. Since I can&#x27;t receive the keyboard events from Word, I have to derive keyboard events from the effects that they had on the document.&lt;&#x2F;p&gt;
&lt;p&gt;I do this by using an infinite loop that repeatedly checks the document for changes and determines what was the most likely cause of the change.&lt;&#x2F;p&gt;
&lt;p&gt;For example: suppose in one iteration of my main loop I see that the previous state of the document was when it was 23 characters long in total, but now I see that in its current state it is 24 characters long. The document has one more character in it than it did before.  I assume that this is because the user has typed a character. It&#x27;s possible that they typed multiple characters between the times that I checked, and so perhaps it is 25 or 26 characters long. Regardless, I can determine how many new characters have appeared, and extract that number of characters from directly behind the current cursor position in the document.&lt;&#x2F;p&gt;
&lt;p&gt;Surprisingly this is actually quite effective. Similarly I can detect when the document gets shorter, and I can assume that this is the result of the user pressing backspace. I can then insert backspace characters into the input stream for the connected program.&lt;&#x2F;p&gt;
&lt;p&gt;I could also observe changes in the cursor position that don&#x27;t correspond to changes in document length, and use those to deduce when the user has pressed arrow keys to move the cursor. In practice though, this is too complicated to be worthwhile. Naturally, the user can achieve the same effect in the document via multiple different means, so I cannot perfectly map document changes onto the keyboard events that produced them.&lt;&#x2F;p&gt;
&lt;p&gt;The theme of this project was deliberately making things hard for myself, and in this case I actually made things impossible for myself. Did the user just type &quot;hello&quot; by pressing H E L L O? or did they paste in the word hello with Ctrl-V? There is no way I can know. Similarly, if a keyboard action has no effect on the document, then I can&#x27;t detect it. This is why I can&#x27;t detect Ctrl-C and send an interrupt to the program.&lt;&#x2F;p&gt;
&lt;p&gt;This is acceptable, however. The benefit of writing a tool simply for entertainment purposes is that bugs that produce comically bad user experiences are actually features.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so I kinda have keyboard input. It&#x27;s a bit janky, but it&#x27;s good enough. Now all I need is a parser for terminal escape sequences.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;3-parsing-escape-sequences&quot;&gt;3. Parsing Escape Sequences&lt;&#x2F;h3&gt;
&lt;p&gt;This is the actual terminal emulator part. And actually this is the really boring part of the whole thing. Honestly I don&#x27;t have much to say about it. It works as you would expect. The only strange thing it is doing is interacting with Word to maintain the resulting state, instead of maintaining its own buffers.&lt;&#x2F;p&gt;
&lt;p&gt;It moves the cursor around the word document instead of maintaining its own cursor position. It puts text directly into the document instead of buffering it itself. Again, there&#x27;s a bit of jankiness here, but it works well as an entertainment piece.&lt;&#x2F;p&gt;
&lt;p&gt;The cursor doesn&#x27;t move around exactly as expected. It struggles to delete the right sections of text when it needs to overwrite something that&#x27;s already there. It takes a long time to complete certain actions. But it&#x27;s fine! I could have spent more time on it and probably gotten Vim to be almost usable like this, but I decided to just leave it. It was entertaining already.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;and-then-the-other-little-annoying-bits&quot;&gt;And then the other little annoying bits&lt;&#x2F;h3&gt;
&lt;p&gt;Now onto some strange little things that came up while I was writing this.&lt;&#x2F;p&gt;
&lt;p&gt;First of all, you probably saw in the other video that Vim doesn&#x27;t believe it&#x27;s connected to a terminal. I joked about Vim being rude, but actually it&#x27;s technically correct. You see, for historical reasons that I don&#x27;t fully understand, some programs care about if they are connected to a terminal or not. Most modern OSes provide a mechanism for a program to query &quot;hey am I connected to a terminal?&quot;, and since we have terminal emulators, the OSes also have to provide a way for a terminal emulator to tell the OS &quot;hey, I&#x27;m a terminal emulator, if this program asks if it&#x27;s connected to a terminal, say yes!&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to read about how Linux handles this, have a read of the documentation for the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;ioctl_tty.2.html&quot;&gt;ioctl tty syscalls&lt;&#x2F;a&gt; (&lt;code&gt;man ioctl_tty&lt;&#x2F;code&gt;). You can also open a process on a pseudoterminal device as documented in &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.man7.org&#x2F;linux&#x2F;man-pages&#x2F;man7&#x2F;pty.7.html&quot;&gt;&lt;code&gt;man pty&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Windows handles this very differently, of course. There is a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;console&#x2F;creating-a-pseudoconsole-session&quot;&gt;guide for creating pseudoconsoles&lt;&#x2F;a&gt; which explains the normal way to do this on windows.&lt;&#x2F;p&gt;
&lt;p&gt;So why didn&#x27;t I just do that so that this worked properly? Well, I tried, but I couldn&#x27;t figure out how to do it. You see, this is my first ever VBA project, and perhaps a more experienced VBA dev would be able to make this work, but I couldn&#x27;t figure it out.&lt;&#x2F;p&gt;
&lt;p&gt;I know exactly what sequence of system calls are required, and I know what values to give them. The problem is just that these system calls have a more complicated set of arguments and I don&#x27;t really know VBA well enough to get all the right pointers to these arguments. In the end I gave up, and just went with the thing that kinda worked already.&lt;&#x2F;p&gt;
&lt;p&gt;The other effect this had was that I couldn&#x27;t escape the default config that programs use when they don&#x27;t think they are connected to a terminal. In particular: bash echos the characters it receives by default. The problem this causes for me is that Word is also adding the same characters to the document, so every letter typed appears twice. This is something that you can normally configure by setting a flag in the virtual terminal device. Since I had given up on getting that working, instead I had to workaround this. So when bash is running, my terminal emulator deletes each character that Word adds to the document, so that only bash&#x27;s echoed character remains.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Solving Advent of Code 2022 on the Sinclair ZX Spectrum: Part 3</title>
          <pubDate>Thu, 16 Mar 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part3/</link>
          <guid>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part3/</guid>
          <description xml:base="http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part3/">&lt;p&gt;If this doesn&#x27;t make sense, see part 1&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-9&quot;&gt;Day 9&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;9&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;9&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The idea for this task was that you&#x27;re tracking the end of a rope as it swings around. The rope is split into discrete segments, each segment occupies a cell in a square grid, and moves from cell to cell chasing the segment ahead of it. There&#x27;s a bunch of rules for which cell it moves to. The movement rules are a little odd, and annoying to implement, but not too much of a problem. The challenge is to track the end of the rope as the head of the rope moves around. Initially the rope is only two segments long, but then for the second part of the task it&#x27;s ten segments long.&lt;&#x2F;p&gt;
&lt;p&gt;The answer that I need to provide for each part is the number of squares that the tail of the rope visited. At first this seems easy, just keep a count of the number of squares visited so far. Each time the tail moves, increment the count. But this only works if the tail never crosses over its own path. So actually I should only count a step if it moves onto a space that&#x27;s never been used before.&lt;&#x2F;p&gt;
&lt;p&gt;As explained in part 2 of this series, I have been prototyping solutions in python. For day 9, once I had made my initial prototype, I then used it to analyse the size of the problem.&lt;&#x2F;p&gt;
&lt;p&gt;I first checked the size of the grid that the rope moves around in. This isn&#x27;t specified in the input file. All I know is where the rope starts and how it moves. I calculated the extents of the rope&#x27;s movement, and I found that the range in X is 234 units, and the range in Y is 286 units. This means that for the Y axis, the range of movement is greater than can fit in a byte. My Y coordinates need to be at least 9 bit numbers, which practically means they should be 16 bit numbers. The X coordinates could fit in one byte if I am careful with where the origin is.&lt;&#x2F;p&gt;
&lt;p&gt;Now consider that I need to track where the tail of the rope has been before, so I know what to count and what not to count. I could do this by keeping a list of all the places that the rope has been to. When I move the rope, I can check if the new tail coordinates are in that list. If they are, I know not to add to the count. Otherwise I will add one to the count.&lt;&#x2F;p&gt;
&lt;p&gt;As the list grows, it becomes more costly to lookup values in this list. The other option is to have a 2D array that is as large as I expect the grid to be, and have each element be a flag to indicate if that cell has been visited. Indexing into this grid will be fast regardless of how long the path gets.&lt;&#x2F;p&gt;
&lt;p&gt;So which of these methods should I use?&lt;&#x2F;p&gt;
&lt;p&gt;Well, first we need to consider how much memory I have available. I have 48k. I lose about 7k to the video memory. I lose about 9k to the input file. I will probably need about 2k for the program code. That leaves me with roughly 30k for any data structures that I need.&lt;&#x2F;p&gt;
&lt;p&gt;So, let&#x27;s see about keeping a list of all the squares I&#x27;ve visited. For the first part of the task, I visit about 6000 squares. For each one I need to keep the X and Y coordinates. If I use a 16 bit number for each one, that&#x27;s 4 bytes per square. This means I need about 24k for my list. That will fit in my 30k of free space. As mentioned previously, I could probably get this down to 18k since the range of X values I need will fit in one byte, allowing me to use only 3 bytes per coordinate.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so that will work, but I suspect it will be very slow. Let&#x27;s see if the other method will work.&lt;&#x2F;p&gt;
&lt;p&gt;I need a 2D array of flags. I know I need at least 234*286 flags. That&#x27;s nearly 67 thousand flags. Clearly I can&#x27;t have a byte for each flag, since that would be about 67k, and I only have 30k. However, a byte could store 8 flags quite easily. If I pack the flags in like this then I only need about 8k for all the flags. This is much more compact than the list method, and has the big advantage that it is less likely to break for larger inputs.&lt;&#x2F;p&gt;
&lt;p&gt;If I was to use a longer input file that visited more spaces, the list gets longer, and the lookup time gets longer with it. Whereas the grid of flags has a constant lookup time. The grid method limits the range of coordinates a little more, but I can future proof it a little by using that spare space I have to make the grid larger in each direction.&lt;&#x2F;p&gt;
&lt;p&gt;If I use a grid that&#x27;s 336*336 cells in size, that means I use 14k for the grid, and I have comfortable margins around what my input file uses, so hopefully it would work for lots of other input files too.&lt;&#x2F;p&gt;
&lt;p&gt;Something I would love to see in Advent of Code is more detail on the constraints that the file was generated with. I would love to know the exact limits of the generated files, so that I could be more certain about how my program will handle files other than my own.&lt;&#x2F;p&gt;
&lt;p&gt;I have to do quite a bit of arithmetic to set each flag in the grid. Since it&#x27;s just a huge bitmap, I need to calculate which byte contains the flag I want to set, calculate which bit of that byte is the right flag, set that bit without disturbing any other bits, and then write it back to the bitmap.&lt;&#x2F;p&gt;
&lt;p&gt;Once the rope stops moving, I then iterate over the whole grid and count the number of bits in each byte. I haven&#x27;t yet experimented with keeping a count as I move through the input file. I&#x27;m not sure if it will be faster or not, but I want to find out.&lt;&#x2F;p&gt;
&lt;p&gt;So that was day 9. This one feels kinda tricky because I don&#x27;t know if my program would work for all possible inputs. It works for my input data, and I&#x27;ve tried to make it support a wide range of values, but I&#x27;ll never know for sure if it supports them all.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, it works for me, so I&#x27;m calling that one complete and moving on to day 10.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day9.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-10&quot;&gt;Day 10&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;10&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;10&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Day 10&#x27;s task was curious. The first part felt like many of the other tasks that have gone before, but the second part ended with rendering a bitmap image of some text. The text is the answer. To do this you have to simulate a really simple CPU.&lt;&#x2F;p&gt;
&lt;p&gt;The simulation of the CPU is the easy part. For this task I want to talk about the bitmap image rendering part. The ZX Spectrum video RAM is split into two parts, the first contains a bitmap image with one bit per pixel. The second section encodes all the colour data, and a few other attributes. The screen is split into 8x8 pixel squares, and each square can only have two colours. The bitmap selects which of the two colours is visible for each pixel.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;ve played many games on the ZX Spectrum, you&#x27;ve probably seen the effects of this, when a player walks in front of an object on the screen that is normally a different colour, the program must choose only one pair of colours to draw, and so sometimes the player colours leak onto the object that they&#x27;re close to, or vice versa.&lt;&#x2F;p&gt;
&lt;p&gt;In the case of my solution to this task, I&#x27;m happy with the default black text on a beige background, so I needn&#x27;t change anything in the colour data section of the video RAM. I only need to update the bitmap area. There is one non-obvious thing about the bitmap data though. It&#x27;s not in the order you might expect.&lt;&#x2F;p&gt;
&lt;p&gt;It would be convenient if the pixels were all in order, left to right, top to bottom. They aren&#x27;t. They do go from left to right in order, but each time you get to the end of a line, you jump down by 8 pixels and reset to the start of the line. That is, until you get to about one third of the way down the screen, then you jump back up to line 2, and repeat this process until you&#x27;ve visited every pixel in the top third of the screen, then you do the same on the middle third of the screen, and then once more on the last third.&lt;&#x2F;p&gt;
&lt;p&gt;This might seem odd at first, but there is a good technical reason for this order. I&#x27;ll not get into the details here, but it simplifies some of the video hardware. So now I have to deal with this surprising order of lines in my code. It&#x27;s not actually too hard to do, since the last three bits of the high byte directly control the Y coordinate within a single square, and the bitmap I need to render fits in a one square high strip.&lt;&#x2F;p&gt;
&lt;p&gt;I did spend a while getting this wrong, and rendered all sorts of garbage to the screen before I finally got all the maths right. I bet this was a frustration shared by most 80s game developers who had to deal with this.&lt;&#x2F;p&gt;
&lt;p&gt;The computation was simple enough though, just requiring some careful thought about how things align when drawing the bitmap.&lt;&#x2F;p&gt;
&lt;p&gt;So that was day 10. Quite a fun one.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day10.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-11&quot;&gt;Day 11&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;11&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;11&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I didn&#x27;t like this particular task. It felt so much more tedious than everything that went before. Certainly the least fun task so far.&lt;&#x2F;p&gt;
&lt;p&gt;If we ignore all the flavour text around the task description for this one, it reduces to just applying simple arithmetic to some numbers over and over again.
There was no need for large data structures, the memory footprint is fairly low for this task.&lt;&#x2F;p&gt;
&lt;p&gt;If you just follow the instructions provided, then the numbers you will be dealing with get &lt;em&gt;really&lt;&#x2F;em&gt; big. Way too big to fit in a 32 bit number. They grow exponentially and quickly become too big to handle in a reasonable time. The insight you were supposed to find was that the arithmetic needn&#x27;t use such big numbers.&lt;&#x2F;p&gt;
&lt;p&gt;The important thing to notice is that you can do all of your addition modulo some fairly small number. Unfortunately, despite making use of this, the largest numbers I had to deal with still got wider than 32 bits, so I had to make a copy of my maths library that could handle 48 bit integers.&lt;&#x2F;p&gt;
&lt;p&gt;Now, the rest of the logic for this task is nothing worth talking about, but something that &lt;em&gt;is&lt;&#x2F;em&gt; worth talking about is the amount of time I spent debugging this one.&lt;&#x2F;p&gt;
&lt;p&gt;There is something that I had failed to take into account. Whenever I tested this program, I got the right answer for the first part, and I got a wrong answer for the second part. What was really strange was that I got a different wrong answer each time even if I hadn&#x27;t changed anything. So clearly something was very wrong.&lt;&#x2F;p&gt;
&lt;p&gt;I started debugging this and stepped through the computation in the debugger in my emulator. At each step it seemed that the computation was all correct. I stepped through the first few iterations of the algorithm, and they all worked. I jumped a few hundred steps ahead, still correct. Then I jumped a much longer way ahead and found that one of the numbers was wrong.&lt;&#x2F;p&gt;
&lt;p&gt;Hmm. How am I going to find out at which exact point the computation that I am doing in the emulator diverges from the python reference code that worked fine. Well to do this I decided to modify the emulator. I added in some custom code that would save a particular range of memory from the emulator into a file each time it reached a particular instruction that I chose because it wasn&#x27;t used anywhere else in my program or in the ROM.&lt;&#x2F;p&gt;
&lt;p&gt;I then wrote some more python code to decode this memory dump and compare it to what my python implementation was doing. I found the exact time at which the first error occurred, many minutes into the program run time. Once I knew how far to step into the program, I started the debugger again, stepped to the right place, and … it worked fine.&lt;&#x2F;p&gt;
&lt;p&gt;This was really frustrating. This was by far the most effort I had put into debugging one of these programs. All of the logic in my program was correct. I could see that even if it started with wrong values, it still did the right computation each time. Something else in the system was messing with the values in the middle of the computation.&lt;&#x2F;p&gt;
&lt;p&gt;After hours debugging, I suddenly realised what it was, and the solution was simple.&lt;&#x2F;p&gt;
&lt;p&gt;Most of the binary operations in my maths library use the IY register to point to the second operand. The IY register is also used by the BASIC ROM without saving and restoring it. This would be fine if there was no ROM code running, and you&#x27;d think that would be the case while my code is running, but actually it&#x27;s not.&lt;&#x2F;p&gt;
&lt;p&gt;I was already saving and restoring the IY register value for the purposes of calling into the ROM&#x27;s print routines, since it expects IY to be untouched. But what I had missed was that the timer interrupt handler uses it too.&lt;&#x2F;p&gt;
&lt;p&gt;The reason this hasn&#x27;t been a problem before is because none of my programs have ever needed to run for as long as this one does. It takes about 9 hours to complete. The emulator that I use can go several hundred times faster than real time, so it only takes about 90 seconds on my laptop. The fact is that the timer interrupt only uses IY if the timer counter overflows. The counter overflows approximately once every 22 minutes. Naturally that happens many times if you&#x27;re running your code for 9 hours.&lt;&#x2F;p&gt;
&lt;p&gt;I disabled interrupts, and ran my code again, it worked first time.&lt;&#x2F;p&gt;
&lt;p&gt;None of the other programs had hit this issue, because they all ran in less than 20 minutes. They did still need fixing though, since the timer is always running, and so depending on when the program starts they might hit this same issue.&lt;&#x2F;p&gt;
&lt;p&gt;I spent a long time looking for this bug. It turned out to be a really simple fix.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day11.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Solving Advent of Code 2022 on the Sinclair ZX Spectrum: Part 2</title>
          <pubDate>Sun, 08 Jan 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part2/</link>
          <guid>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part2/</guid>
          <description xml:base="http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part2/">&lt;p&gt;If this doesn&#x27;t make sense, see part 1&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-6&quot;&gt;Day 6&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;6&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;6&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This is gonna be a quick one. The task for day 6 is fairly simple. You need to scan a sliding window across a string, and detect when all the characters in that window form a unique set.&lt;&#x2F;p&gt;
&lt;p&gt;Parts 1 and 2 are the same except for the window size.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s really nothing interesting in the code for this one, none of it was tricky to do in assembly, so I won&#x27;t dwell here. Days 7 and 8 are more exciting.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day6.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-7&quot;&gt;Day 7&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;7&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;7&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;When I first read the details for this challenge, I thought it was going to be a nightmare to implement in assembly.
The input file resembles a shell session output, where a user is moving around a file system with cd, and checking the sizes of files in the directories they enter. The files and directories all have names but the full path names are never specified, only implied by the route the user takes through the file system.&lt;&#x2F;p&gt;
&lt;p&gt;I was expecting to build some kind of tree data structure, but at first I thought I was going to have to track file names, which meant dealing with strings of unknown sizes. This is easy to do in a high level language, even C style strings would feel like luxury for this purpose. But actually it turns out I don&#x27;t need to think about the file names at all.&lt;&#x2F;p&gt;
&lt;p&gt;I looked closely at the description for this task before I started writing my input parsing code. The answers I need are just the sizes of some specific directories. I never actually need to know what the name of any file is. At least, not for the purposes of getting an answer.&lt;&#x2F;p&gt;
&lt;p&gt;But perhaps I do need the file names for parsing the tree? After all, I only know what directory the user is in by its name. Well, actually I discovered that in this fantasy shell session, the user is &lt;em&gt;very&lt;&#x2F;em&gt; predictable. They always descend into directories in order, and descend all the way to the leaves of each directory before continuing on to the next directory.&lt;&#x2F;p&gt;
&lt;p&gt;With this in mind, most of the input file can be ignored, and in fact there&#x27;s several places in my parsing code where I simply skip over parts of the input.&lt;&#x2F;p&gt;
&lt;p&gt;The first line in the file tells us nothing, as far as I can tell from the examples I have seen, this line is always the same, so I can ignore it. Any line that says &quot;$ ls&quot; can just be ignored. I know that the user always does this after entering a directory. As soon as I see &quot;$ cd &amp;lt;some_dir&amp;gt;&quot;, I already know that the next line will be &quot;$ ls&quot;, so I can ignore it. Whenever I see a line that starts with a number, I only need to read the number, then I can skip over the rest of the line since the file name doesn&#x27;t matter at all. The lines that start with &quot;dir&quot; are also safe to ignore entirely. As we&#x27;ve established already: the name doesn&#x27;t matter, and that&#x27;s the only real information on those lines.&lt;&#x2F;p&gt;
&lt;p&gt;Most of the input for this task is just thrown away.&lt;&#x2F;p&gt;
&lt;p&gt;Upon discovering all of this, I first wrote some python code that implements the algorithm I need. Having seen the difficulty start to ramp up by now, I had decided that from here on I probably needed to have a working prototype in a high level language before I tried to write any assembly code. Before I show you how I solved day 7, let&#x27;s take a quick detour into how I prototyped solutions in python and then ported them to assembly.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-side-bar-about-prototyping&quot;&gt;A side bar about prototyping&lt;&#x2F;h3&gt;
&lt;p&gt;python, for me, is an ideal language for Advent of Code. It&#x27;s easy to write, and the built in data structures and string processing routines make parsing of the input really easy. The problem is that once I have a python solution, I need to translate it into assembly code. For some operations this translation is really obvious. Simple arithmetic is easy, for example. But for other things it&#x27;s not so obvious. Let me show you.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s sum a list of numbers in python.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;!&#x2F;usr&#x2F;bin&#x2F;env python3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;inp&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;123&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;234&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;345&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;456&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;567&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;678&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt;inp&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The list starts off as text, which needs to be parsed, each line is one number. Here this is done by splitting the input into lines, and then casting each one to an int. I&#x27;ve used the map function to do this, and I&#x27;ve also used the built in sum function. None of this is available to me in assembly code.&lt;&#x2F;p&gt;
&lt;p&gt;Now I&#x27;m going to split up these nested function calls.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; inp&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;strip&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;nums&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; lines&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;nums&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then I need to get rid of the string split and the int conversion. I don&#x27;t have a string split function in assembly, and actually I don&#x27;t need one, I can eliminate the need for it. I&#x27;m going to loop over each character in the input. I&#x27;m using a while loop here instead of a for loop because they look more like assembly code. I&#x27;d like to use goto really, since that&#x27;s closest to what assembly code looks like, but python doesn&#x27;t have goto, so I can&#x27;t. I&#x27;m using the name hl for the loop counter, and you&#x27;ll see why soon.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;hl&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;nums&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;while&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; True&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;inp&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;hl&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;    d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 48&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; *=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    elif&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span&gt;        nums&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;num&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;        break&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;25&lt;&#x2F;span&gt;&lt;span&gt;    hl&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;26&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;nums&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For each character, I first assume the character is a digit, convert it from ascii into numeric form, then check if it actually is a digit, as I had assumed. If it is, I multiply num by ten and add on the new digit. This is the number parsing algorithm I mentioned in the previous video. If the character is actually a newline, however, I have finished parsing the number, and I can add it to the list.&lt;&#x2F;p&gt;
&lt;p&gt;I then reset the number for the next line. If ever I find a character that&#x27;s none of these things, then I&#x27;ve finished iterating. I always deliberately make sure there&#x27;s a null character at the end of my input file, so in python I can emulate this by just adding one to the end of this string.&lt;&#x2F;p&gt;
&lt;p&gt;Now I see another opportunity to optimise this. I don&#x27;t want to construct a list in assembly code if I can avoid it, and in fact the sum operation doesn&#x27;t really need to keep a list of all the numbers to sum. Let&#x27;s just take a running total while I&#x27;m parsing the input. I&#x27;ll also move the loop counter increment statement to the start of the loop, which will make more sense when we look at the assembly code.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;hl&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;total&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;while&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; True&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    hl&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;    a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;inp&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;hl&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span&gt;    d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 48&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; *=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    elif&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span&gt;        total&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;23&lt;&#x2F;span&gt;&lt;span&gt;        num&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;    else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;        break&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;26&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;total&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now I have some python code that really doesn&#x27;t look like python any more. Normally you&#x27;d never write python code like this. It looks awful as python code, but it looks great as a prototype for some assembly code.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at the assembly that I wrote based on this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;start&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;    ld hl, inp-&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; l hl points to start of input&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;loop&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; hl &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; hl += 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (hl) &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; a = inp[hl]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 48&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;    cp &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; nc, not_a_digit &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; if a &amp;gt;= 0 and a &amp;lt; 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; num *= 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;    ld ix, num&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; multen32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; tmp = a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;    ld iy, tmp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    ld (iy), a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; num += tmp&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; add32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;not_a_digit&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;    cp (&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt; - &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;48&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; check for newline&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; nz, not_a_newline&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; total += num&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;23&lt;&#x2F;span&gt;&lt;span&gt;    ld ix, total&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;    ld iy, num&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; add32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; num = 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;27&lt;&#x2F;span&gt;&lt;span&gt;    ld (iy), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;    ld (iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;29&lt;&#x2F;span&gt;&lt;span&gt;    ld (iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;    ld (iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;31&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;32&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;not_a_newline&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;33&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; end of the file&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;34&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; convert total to string and print it...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can see why I chose the names hl and a in the python code. These are actually register names on the Z80. This really helps follow what&#x27;s going on when I translate my python code. It&#x27;s not a direct translation, as you can see, but it&#x27;s much easier to go from this python code than it is to go from the initial python code.&lt;&#x2F;p&gt;
&lt;p&gt;The assembly code makes use of my 32 bit add function and string conversion function from the math library that I wrote.&lt;&#x2F;p&gt;
&lt;p&gt;My math library does also have a number parsing function, but I avoided using it here just for demonstration purposes.&lt;&#x2F;p&gt;
&lt;p&gt;So that&#x27;s how I&#x27;ve been prototyping in python each day. There&#x27;s a bunch of other things that I do that I won&#x27;t go into detail for. Things like turning some function variables and arguments into globals, eliminating tuple returns, putting return values in globals, and sometimes even mocking up struct field access. This process made it much easier to get the assembly code right each day. I still had to do lots of debugging, but at least I had a good reference implementation to debug against, and a good starting point for writing my code.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, let&#x27;s get back to day 7&#x27;s challenge.&lt;&#x2F;p&gt;
&lt;p&gt;so, I wrote my python code, and I made it look as much like assembly as I could, then I translated the program.
For the reasons I explained earlier, there&#x27;s a bunch of places where it just skips over parts of the input. It skips over the ls lines, the dir lines, and the names of files.&lt;&#x2F;p&gt;
&lt;p&gt;The algorithm builds a tree structure, but you can see that it only stores the links to the other nodes, and a total size for the current node. As mentioned, the names are not important. There&#x27;s a cursor stack specifically for remembering how to backtrack during parsing. Once the whole tree is parsed, I can recurse the whole tree and add the sizes of child nodes on to parent nodes. Finally, since all the nodes are the same size and stored sequentially, I can then easily iterate over all nodes to compute the answers without having to use recursion.&lt;&#x2F;p&gt;
&lt;p&gt;A little while after getting all of this working, I discovered that I don&#x27;t actually need to understand the hierarchy of the file system at all. I don&#x27;t need any of the links between nodes. The only reason I had them was so that I could recurse across the tree structure after I parsed it, but actually I can do the operation that requires recursion during the parsing step. I just add the size of each child node to the parent node whenever I find a &quot;cd ..&quot; command. This means I never need recursion for this task at all, only iteration. Perhaps at some point I&#x27;ll implement this optimisation in my code, but for now it is a missed opportunity.&lt;&#x2F;p&gt;
&lt;p&gt;Day 7 was an interesting challenge with a deceptively noisy input file. So far it&#x27;s the only challenge where there&#x27;s been any redundancy in the input file. It took me a really long time to see that despite the fact that the input file represents a tree structure, I never need to recurse across it.&lt;&#x2F;p&gt;
&lt;p&gt;I don&#x27;t think I would have discovered this if I hadn&#x27;t been looking so hard for optimisations. For me this is part of the magic of challenging myself to use an assembly language for these tasks. If I had just been using python for this, I wouldn&#x27;t have bothered looking out for these optimisations.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day7.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-8&quot;&gt;Day 8&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;8&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;8&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Okay, on to day 8, last one for this post, and it should be fairly quick.&lt;&#x2F;p&gt;
&lt;p&gt;In this one you have to find particular trees in a grid. Essentially looking for a tall tree with lots of shorter trees around it. I&#x27;ve hugely oversimplified the problem description there, but the interesting part here is another optimisation that I made in the parsing step.&lt;&#x2F;p&gt;
&lt;p&gt;This is an optimisation that is obvious in hindsight, but I probably wouldn&#x27;t have thought about it if I was using a high level language to solve this problem.&lt;&#x2F;p&gt;
&lt;p&gt;For my solution to this task, there&#x27;s not really an input parsing step. Well, technically there is one, but it&#x27;s probably not doing what you think. You might expect me to parse the digits in the file, convert them from ascii values into plain numeric values, maybe even insert them into an array, and &lt;em&gt;then&lt;&#x2F;em&gt; start to process the data. But actually the absolute height of the trees, which is what these numbers represent, is not so important.&lt;&#x2F;p&gt;
&lt;p&gt;The important part is the differences between pairs of trees. I don&#x27;t need to convert the ascii characters to numbers, because ascii has quite sensibly decided to put all the digits right next to each other in order. If I subtract the ascii value for 4 from the ascii value for 7, then I get the number 3. I needn&#x27;t bother convert these numbers first, since I would get the same result here anyway.&lt;&#x2F;p&gt;
&lt;p&gt;So, I don&#x27;t have much set up effort to do before I start computing the result for this task. I need to know how wide the grid is, and how tall it is, so I know how far to iterate, and how to calculate the memory address of each tree digit. But that&#x27;s all. When it comes to the computation, the raw values from the file are just fine. I don&#x27;t need to put them into an array or list either. I can just load them directly from where the file is in memory.&lt;&#x2F;p&gt;
&lt;p&gt;As for the rest of the solution to day 8, well I&#x27;m sure you can imagine it, it&#x27;s just iterating over a grid in various ways, and performing simple arithmetic as you do so.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day8.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Solving Advent of Code 2022 on the Sinclair ZX Spectrum: Part 1</title>
          <pubDate>Tue, 03 Jan 2023 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part1/</link>
          <guid>http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part1/</guid>
          <description xml:base="http://computingthedetails.org/blog/advent-of-code-on-the-zx-spectrum-part1/">&lt;p&gt;Advent of Code is a programming challenge and competition that runs during the 25 days of Advent.&lt;&#x2F;p&gt;
&lt;p&gt;I decided that this year that my challenge to myself for Advent of Code would be to solve each day&#x27;s challenge with a Sinclair ZX Spectrum program.&lt;&#x2F;p&gt;
&lt;p&gt;The Sinclair ZX Spectrum was a popular British home computer from the early 1980s.
It has 48k of RAM. That&#x27;s not much by today&#x27;s standards.&lt;&#x2F;p&gt;
&lt;p&gt;I happen to have one in working order, and wanted an excuse to use it more. So I thought this would be a fun way to do that.&lt;&#x2F;p&gt;
&lt;p&gt;Can I do all 25 days on the ZX Spectrum? (Spoiler alert! ... I stopped before I reached the end)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;Okay, first of all let&#x27;s see what I&#x27;m in for.&lt;&#x2F;p&gt;
&lt;p&gt;Each day of AoC comes in two parts. That&#x27;s a total of 50 challenges. For each challenge you get a description of the challenge, an input file that you need to process to complete the challenge, and finally a place to submit your answer to see if you got it right.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;re aiming to get on the leaderboard then you have to be awake at the right time and code as fast as you can. Only the first 100 correct answers score points each day. That&#x27;s 5AM for me here in England. I&#x27;m not aiming for leaderboard positions this year, I&#x27;m just having fun with retro computing. So I&#x27;m gonna wake up whenever I like.&lt;&#x2F;p&gt;
&lt;p&gt;Each day has two challenges, and normally they&#x27;re very closely related. Often the first one sets you up nicely for the second one, but sometimes it doesn&#x27;t. As the days go on, you have to be more clever with how you solve the problems. There&#x27;s often an obvious solution that&#x27;s way too computationally expensive to get a result in a reasonable time, and then a more creative solution that&#x27;s quite fast.&lt;&#x2F;p&gt;
&lt;p&gt;So in summary, what we need to do each day is: parse the input file, process the parsed data to get the answer to part 1, process the input again but this time calculate the answer to part 2.&lt;&#x2F;p&gt;
&lt;p&gt;Of course we only know part 2 after doing part 1. Sometimes you can calculate both answers with one pass over the input data, but at least at first that&#x27;s not helpful.&lt;&#x2F;p&gt;
&lt;p&gt;I have quite a lot of programming experience, so to make this fun I need to make it harder. So now let&#x27;s look at how I&#x27;ve made this hard for myself.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-target-the-zx-spectrum&quot;&gt;The target (The ZX Spectrum)&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;ve made things hard for myself by choosing a difficult platform to write programs for.&lt;&#x2F;p&gt;
&lt;p&gt;The Sinclair ZX Spectrum from 1982 has only 48kb of RAM. It has a clock speed of about 3.5MHz. You&#x27;re probably watching this video on a device with a clock that&#x27;s almost 1000 times faster than this.&lt;&#x2F;p&gt;
&lt;p&gt;Okay, so it&#x27;s slow, and it hasn&#x27;t got much RAM. Doesn&#x27;t this just mean it will take longer?
Well, it &lt;em&gt;will&lt;&#x2F;em&gt; take longer, for sure. But there&#x27;s other problems too. How do I write programs for this machine?&lt;&#x2F;p&gt;
&lt;p&gt;If you start up the machine then all you get is a BASIC interpreter. I mean BASIC as in the language. But it is also basic in that it&#x27;s not a feature rich development environment that you might expect to use on a modern PC.&lt;&#x2F;p&gt;
&lt;p&gt;I could write BASIC programs for this machine, but BASIC is slow, has some annoying constraints, and also lacks certain operations that I&#x27;d have to reimplement with my own functions, which would be much slower than machine code versions. So that&#x27;s the decision essentially made for me. If I want the performance I need for some of the more intense AoC challenges, then I need to produce machine code.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ve never found a compiler for any language that I&#x27;m happy with for the Z80, which is the CPU inside the ZX Spectrum. So instead I decided to make things &lt;em&gt;extra&lt;&#x2F;em&gt; hard for myself and write all of my solution code in Z80 Assembly language. This means all the algorithms and data structures I need, I will need to implement them myself.&lt;&#x2F;p&gt;
&lt;p&gt;RAM is also a problem. I have only 48k. About 7k of that is used by the graphics system. Using this for computation would put garbage on the screen, so I&#x27;d like to avoid it if possible. I need to use some of the screen space for outputting the answers anyway. So I&#x27;m already down to a practical limit of 41k.&lt;&#x2F;p&gt;
&lt;p&gt;Before I started this adventure I had a look at the size of the input files from the previous year, and I found that most of them are less than 10k about five of them were larger than this, the largest being 20k on day 22.&lt;&#x2F;p&gt;
&lt;p&gt;The input text is unique to each player, and is generated by the server for you. Your answer is different to everyone else&#x27;s, but I&#x27;m not sure how much the input size varies. It might be possible that there&#x27;s a slight difference in size between players, I&#x27;m not sure.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, I started with the assumption that 20k will be the upper bound. I&#x27;m sure that&#x27;ll be enough, right? &lt;em&gt;(in the biz, this is known as foreshadowing)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So that leaves me with 21k of space for my program. I thought that sounded like a lot at first. But then I remembered that I also need any working data structures to fit in the remaining space. Turns out most programs fit into 2k including space for small variables, so that leaves 19k for any data structures I need. Still, that should be plenty of space, right? &lt;em&gt;(in the biz, this is known as &lt;del&gt;foreshadowing&lt;&#x2F;del&gt; repeating a joke)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Time for your spoiler warning. From here on expect some mild spoilers for the AoC challenges.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-plan&quot;&gt;The plan&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;m going to cover days one to five in this post, and then later days will be in future posts. I won&#x27;t be going into much detail for some of the easier challenges, but I will go into lots of detail for the more interesting ones that required a little bit of creativity. You&#x27;ve probably already seen other people describing their algorithms in detail if you&#x27;re into this kind of thing, so I&#x27;ll try to focus on the challenges that are unique to using a ZX Spectrum for these kinds of programs.&lt;&#x2F;p&gt;
&lt;p&gt;Days one to five were all fairly simple. We&#x27;ll start at the beginning, with day one, and the first challenge that I have to overcome.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-1&quot;&gt;Day 1&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;1&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;1&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;At the heart of the ZX Spectrum is the Zilog Z80 CPU. People often call this an 8-bit microprocessor. A pet peeve of mine is how people use phrases like these and don&#x27;t qualify them. If someone says &quot;This is an 8 bit processor&quot;, what do they actually mean? Do they mean that the address bus is 8 bits wide? Do they mean that the widest part of the data path is 8 bits? Do they mean that the general purpose registers are 8 bits wide?&lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s clarify this statement for the Z80. It has a 16 bit address bus and an 8 bit data bus, it has registers that are either 8 bits wide or 16 bits wide depending on the operation. The widest adders in the machine are 16 bits wide. So is this an 8 bit micro? Or a 16 bit micro?&lt;&#x2F;p&gt;
&lt;p&gt;Either way, the largest adder I have access to is a 16 bit adder. But actually the more convenient ones to use are the 8 bit adders. So this means the largest integer that I can use with the built in hardware is 2 to the power of 16 minus one. That&#x27;s 65,535. So here&#x27;s problem one: this is not big enough for AoC. Regular AoC players will know that the numbers often get big.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s common for lots of modern machines to have at least 32 bit adders and 32 bit registers. This lets you get numbers up to almost 4.3 billion. This  should be plenty for this task. I had hoped that it would be big enough for &lt;em&gt;all&lt;&#x2F;em&gt; of the tasks. &lt;em&gt;(in the biz, this is known as &lt;del&gt;foreshadowing&lt;&#x2F;del&gt; murdering a joke)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So, my first task was to implement 32 bit addition, and also parsing of base 10 numbers. Let&#x27;s start with adding: there&#x27;s a pair of registers on the Z80 that make this really easy to do. the IX and IY registers can point to the starting position in RAM of each of the two numbers to add, and then I can load the right bytes from each one using the load instructions that use offsets from the IX and IY registers. Below is the function I wrote for this. It works because you can do a 32 bit add by doing four 8 bit adds, as long as you remember to pass the carry output bit from each add operation to the next higher order add operation.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;add32le&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; ; (ix) = (ix) + (iy)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; af&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    or&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;    ld a,(ix)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    adc&lt;&#x2F;span&gt;&lt;span&gt; a,(iy)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix), a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    adc&lt;&#x2F;span&gt;&lt;span&gt; a,(iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    adc&lt;&#x2F;span&gt;&lt;span&gt; a,(iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    adc&lt;&#x2F;span&gt;&lt;span&gt; a,(iy+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; af&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    ret&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Okay, now for number parsing. That means I need multiplication. The algorithm I used for parsing decimal was to parse the first digit, add it to an accumulator, and then for each additional character I find, I multiply the accumulator by 10 and then add on the digit I just found until I run out of digits.&lt;&#x2F;p&gt;
&lt;p&gt;My multiplier implementation is fairly simple, it iterates over the bits of one number, multiplying by two or adding the current value each time. This is a fairly common approach and it works well for this size of number. The problem is that when I parse a number from the input file, I need to do this multiply step once for each digit in the number. There&#x27;s about 2000 numbers in the file for day 1, usually about 4 digits each. That&#x27;s 8000 multiplications I need to do. My multiplier isn&#x27;t unreasonably slow, but it&#x27;s certainly on the critical path for parsing decimal numbers for day 1.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;mul32le&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; ; (ix) = (ix) * (iy)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; bc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; de&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; Copy (ix) to (tmp2)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;    ld bc, &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; ix&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;    ld de, mul32le_tmp2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;    ldir&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; Copy (iy) to (tmp1)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;    ld bc, &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;    ld de, mul32le_tmp1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span&gt;    ldir&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; set (ix) to 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span&gt;    ld (ix+&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; 32 step multiply loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;    ld b, &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ;ld iy, mul32le_tmp2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;26&lt;&#x2F;span&gt;&lt;span&gt;    ld de, &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;mul32le_loop&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;    ld iy, mul32le_tmp1 &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;29&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; sra32le_iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;    ld iy, mul32le_tmp2 &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;31&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; c, add32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;32&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; dbl32le_iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;33&lt;&#x2F;span&gt;&lt;span&gt;    djnz mul32le_loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;34&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;35&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;36&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; de&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;37&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; bc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;38&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    ret&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;39&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;mul32le_tmp1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;40&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;41&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;mul32le_tmp2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since I know that I only need to multiply by 10 while I&#x27;m parsing a number, I can actually create an optimised multiply function that&#x27;s specifically for multiplying by ten. How does it work?&lt;&#x2F;p&gt;
&lt;p&gt;Well let&#x27;s start with multiplying by two, multiplying a number by two in binary is really easy. You know from primary school maths that if you need to multiply a decimal number by 10 then you just shift everything over by one digit and put a zero in the units place. Well, as we move from base 10 to base 2, that same shift operation turns from times-ten into times-two. Most CPUs have shift instructions, and the Z80 is no exception. I can multiply by 2 &lt;em&gt;really&lt;&#x2F;em&gt; fast. In fact it&#x27;s a part of the algorithm for arbitrary multiplication already. But 2 is not 10. So how do I use this information to make multiplying by 10 really fast?&lt;&#x2F;p&gt;
&lt;p&gt;Well, add is quite fast, so if I can build my times-ten just using shifts and adds then I can make times ten be really fast. Let&#x27;s take X, shift it once to get 2X, we&#x27;ll keep that for later. Now let&#x27;s shift 2X again, twice more, to get 8X. Now I can add 2X and 8X together to get 10X.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; Optimised multiply by 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; (ix) = (ix) * 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; decomposes 10 into sum of powers of two&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; x * 10 === (x * 2) + (x * 8) === dbl(x) + dbl(dbl(dbl(x)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;; three doubles plus an add&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;multen32le&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; bc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; de&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; (ix) = (ix) * 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; dbl32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; (tmp1) = (ix)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span&gt;    ld bc, &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    push&lt;&#x2F;span&gt;&lt;span&gt; ix&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span&gt;    ld de, multen32le_tmp1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span&gt;    ldir&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; (ix) = (ix) * 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; dbl32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; (ix) = (ix) * 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; dbl32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; (ix) = (ix) + (tmp1)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt;    ld iy, multen32le_tmp1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    call&lt;&#x2F;span&gt;&lt;span&gt; add32le&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; iy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;28&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; de&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;29&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    pop&lt;&#x2F;span&gt;&lt;span&gt; bc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    ret&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;31&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;multen32le_tmp1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;32&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    db&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;My arbitrary multiply function does 64 one-bit shifts and 32 additions. My newly optimised X10 function does 3 one-bit shifts, and 1 addition. That&#x27;s a huge improvement.&lt;&#x2F;p&gt;
&lt;p&gt;With number parsing going fast, and 32 bit addition working, day 1 becomes easy. I&#x27;ll spare you the details, it&#x27;s just adding some numbers together and deciding which one is biggest.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day1.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-2&quot;&gt;Day 2&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;2&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;2&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since I had all the infrastructure I had implemented for day 1, it was really easy to get day 2 done.&lt;&#x2F;p&gt;
&lt;p&gt;Day 2 was slightly more complicated than day 1, but not much. The challenge involved evaluating total scores from games of Rock Paper Scissors according to some scoring formula. There wasn&#x27;t anything in day 2 that was particularly difficult. The only new technique I used here was pre-computing a look up table of values for the scores. I used two different tables to look up the scores given by the outcome of each game. The scoring was different in each part of the challenge, hence I needed a different look up table for each one.&lt;&#x2F;p&gt;
&lt;p&gt;Nothing new enough or exciting enough to discus for day 2.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day2.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-3&quot;&gt;Day 3&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;3&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;3&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Day 3, still quite early on. The difficulty has not ramped up yet. This is a fairly simple task, but it&#x27;s an opportunity to talk about parsing, and how I&#x27;m actually getting the input files on to the ZX Spectrum.&lt;&#x2F;p&gt;
&lt;p&gt;Well, first of all, when I&#x27;m coding I&#x27;m actually using an emulator. I have been verifying that my code runs on real hardware too, but during development it&#x27;s much nicer to have an emulator with a debugger.&lt;&#x2F;p&gt;
&lt;p&gt;Programs are normally loaded on to the ZX Spectrum via a compact cassette, although you can actually use anything that produces a suitable signal. I have used my laptop headphone output to load programs during testing without needing a cassette. I have been using cassettes occasionally too though, for fun.&lt;&#x2F;p&gt;
&lt;p&gt;My build process for my code includes a step where the program and the input file are each turned into the format that the ZX Spectrum can load. These files can be loaded directly by an emulator, or can be converted into .wav audio files. I&#x27;ve been using the .wav method to test on real hardware and to record onto cassettes.&lt;&#x2F;p&gt;
&lt;p&gt;These files need to know where in memory they will be loaded. A third file, which is a small BASIC program, is the first file to be loaded. It runs automatically and loads the other files in the right places automatically. It loads the main program, which starts at address 0x8000, and then the input text, which is loaded at 0xA000. This gives me plenty of space for each of these. I have about 24k of space starting at 0xA000, so that should be plenty of space for even the largest of the input files that I&#x27;m anticipating (foreshadowing). The program has an 8k block to use. If the program ever gets bigger than this then it will start to overlap with the input text.&lt;&#x2F;p&gt;
&lt;p&gt;The program knows that the input will start at 0xA000, so to read the input text, it just starts reading from that part of the memory. On modern systems you often find that a program will open a file, and only load a part of it into memory at once, often it doesn&#x27;t need to store the whole thing in memory, it can stream it from the storage medium as it processes it, and discard the parts it has already processed. On the ZX Spectrum this is not an option. You have to load the whole file, or none of the file. If I wanted to process this in parts, I will have to split it into multiple different files and load one at a time.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an example of a parsing function. If &lt;code&gt;BC&lt;&#x2F;code&gt; points to the start of a line in the file, then running this code puts the length of the line in register &lt;code&gt;E&lt;&#x2F;code&gt;. Of course, it produces incorrect results for lines longer than 255 characters. It also fails for lines of length 0, in which case is returns the length of the next line in the file plus one. These limitations are known, and are not problematic for the given puzzle input.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;length_loop&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; get the length of this line&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; bc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (bc)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    cp &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; ;check for line feed&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; nz, length_loop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; e is now the line length&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another issue is that the user experience is very different for files on tapes. The user has to locate the file on the tape, or sit and wait for the tape to play all the way to the file that the program wants. The ZX Spectrum can&#x27;t control the tape player. Imagine if you had to manually control the start location of the read head on your hard disk to load a file.&lt;&#x2F;p&gt;
&lt;p&gt;My solution for day 3 is often jumping around the file while parsing it. Since it sometimes needs to scan across several lines at the same time. Having the whole thing in memory is actually really convenient for this purpose. In part 2 there&#x27;s a need to scan three lines at the same time. This function finds the starts of the next three lines in the file:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;find_lines&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; upon arriving at find_lines, hl should point to the first char in the first line&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;    ld (line1_start), hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;find_line_2&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (hl)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;    cp &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; z, part2_done&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span&gt;    cp &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; ; check for newline&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; nz, find_line_2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;    ld (line2_start), hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;find_line_3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;14&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;    ld a, (hl)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;    cp &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; ; check for newline&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    jp&lt;&#x2F;span&gt;&lt;span&gt; nz, find_line_3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;18&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;    inc&lt;&#x2F;span&gt;&lt;span&gt; hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;    ld (line3_start), hl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;found_lines&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;21&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    ; we now have three pointers, one pointing to each line in this group&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Anyway, the rest of the details for part 3 are not particularly interesting. Let&#x27;s move on.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day3.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-4&quot;&gt;Day 4&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;4&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;4&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Day 4 also wasn&#x27;t particularly interesting. It was just basic arithmetic over and over again.
It didn&#x27;t take me long to implement a solution using things I&#x27;d already experienced in the first few days.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day4.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-5&quot;&gt;Day 5&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;2022&#x2F;day&#x2F;5&quot;&gt;See the problem description on adventofcode.com&#x2F;2022&#x2F;day&#x2F;5&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Day 5 was interesting because it was the first one that required me to implement a data structure that I hadn&#x27;t given much thought to before.&lt;&#x2F;p&gt;
&lt;p&gt;In modern high level languages you get lots of things easy. Suppose you need a LIFO queue, (Last In, First Out) you can just summon one from the eather with a single line of code. I&#x27;m writing Z80 assembly, I have to bring all my own data structures.&lt;&#x2F;p&gt;
&lt;p&gt;The disadvantage of this is obvious, but the advantage is that I can make optimisations that are specific to my current use case.&lt;&#x2F;p&gt;
&lt;p&gt;For day 5 there was an interesting task to do. Lifting crates with a virtual crane, and placing them on other stacks of crates, in a particular order. Essentially you have a bunch of LIFO stacks and you have to pop and push from them in a particular order.&lt;&#x2F;p&gt;
&lt;p&gt;Parsing the input for this task was a little bit annoying, because there&#x27;s a kind of ascii art representation of the stacks at the start. It looks nice, but it&#x27;s not the most helpful thing for machine readability. Here&#x27;s the example they provide:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [D]    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[N] [C]    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[Z] [M] [P]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 1   2   3 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;move 1 from 2 to 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;move 3 from 1 to 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;move 2 from 2 to 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;move 1 from 1 to 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first thing I do for day 5 is count up all the crates. This total is going to be the biggest any of my stacks could possibly get. So I reserve that much space for each stack. Then I put the character that represents each container into my freshly allocated LIFOs in the right order.&lt;&#x2F;p&gt;
&lt;p&gt;I decided that I wouldn&#x27;t bother to track how many items were in each LIFO, since they were not going to get particularly big anyway. So what I did was make sure that the remaining elements in a LIFO were all spaces, so that I can find the top by scanning along until I hit a space.&lt;&#x2F;p&gt;
&lt;p&gt;In hindsight this wasn&#x27;t a great idea. It&#x27;s not fast at all, and it only saves about 9 bytes of memory, I had plenty to spare, actually. Anyway, the second part of the task asked me to pop multiple items at once from a LIFO. I hadn&#x27;t planned for this when I wrote the solution to part 1, so I got a bit lazy, and this made the solution &lt;em&gt;really&lt;&#x2F;em&gt; slow.&lt;&#x2F;p&gt;
&lt;p&gt;The problem was that the items that I popped from one LIFO now needed to be pushed onto the next LIFO in reverse order (or in the same order, depending on how you look at it). There&#x27;s a much better way to do this, but my solution was simply to move the items to a temporary LIFO, and then move them out of there onto the target. This one goes really slow. It takes about fifteen minutes to finish computing both results for this one. I need to go back and optimise my solution for day 5 at some point.&lt;&#x2F;p&gt;
&lt;p&gt;Full listing on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;AoC2022&#x2F;blob&#x2F;main&#x2F;day5.asm&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Echo is broken in Bash</title>
          <pubDate>Fri, 02 Dec 2022 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/echo-is-broken-in-bash/</link>
          <guid>http://computingthedetails.org/blog/echo-is-broken-in-bash/</guid>
          <description xml:base="http://computingthedetails.org/blog/echo-is-broken-in-bash/">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;If you&#x27;re reading this, you probably know what Bash is. For those that don&#x27;t: Bash is the Bourne Again SHell, an open source shell compatible with the Bourne Shell, but that probably doesn&#x27;t actually mean much if you&#x27;re not already in the know. It&#x27;s a computer program that interprets commands, launching other programs as requested, and passing data between them. It&#x27;s the basis of a whole lot of bits of glue between other unix programs you use day to day. Bash is part of many software stacks that our modern way of life depends on.&lt;&#x2F;p&gt;
&lt;p&gt;Now I&#x27;m about to tell you just how badly the echo command in Bash is broken.&lt;&#x2F;p&gt;
&lt;p&gt;... what&#x27;s that? ... I think I can hear the reader through the screen...&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&quot;But echo is such a simple command!? what could possibly be wrong with it?&quot;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Nope. It&#x27;s not simple. There&#x27;s a lot of details. Strap in!&lt;&#x2F;p&gt;
&lt;p&gt;Quick side note for beginners: it&#x27;s easy to confuse terminal emulators and shells when you first come to use them, because almost every time you use a terminal emulator, it&#x27;s because you want to use your shell. They&#x27;re two independent programs. The terminal emulator renders the output of a program to your screen. Bash is just one such program you might want to use this way. Terminal emulators don&#x27;t have to launch shells when they start up. Shells are just programs after all. You don&#x27;t have to use bash, you could be using a superior shell program. Similarly and separately: you don&#x27;t have to use the terminal emulator that came with your linux distro, you could use a superior one. But anyway, if you are a beginner, don&#x27;t let this worry you too much.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-quick-quiz&quot;&gt;A quick quiz&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s start with a nice easy quiz. How well do you know Bash?&lt;&#x2F;p&gt;
&lt;p&gt;Q1. What will this command output?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;echo foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Answer: &lt;code&gt;foo&lt;&#x2F;code&gt;, of course. That&#x27;s an easy one.&lt;&#x2F;p&gt;
&lt;p&gt;Q2. How about if you pass the echo command something that looks like a program argument? What will this command output?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;echo -a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Answer: &lt;code&gt;-a&lt;&#x2F;code&gt;, which makes sense. &lt;code&gt;-a&lt;&#x2F;code&gt; doesn&#x27;t mean anything to echo, so it just prints it out verbatim.&lt;&#x2F;p&gt;
&lt;p&gt;Q3. What about some other letters? What does this output?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;c&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Answer:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-c&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Q4. Yeah, all of those do the same. Looks like a pattern is emerging, but how about this next one?&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;echo -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, you know by the way I&#x27;m asking that the answer is different this time, so I guess this is the end of the quiz, but the right answer is &quot;it prints nothing&quot;. I would also accept &quot;Well technically it does print something. It prints one newline character.&quot; ... yes, well done, you are awarded a gold star that says &quot;pedant&quot; on it.&lt;&#x2F;p&gt;
&lt;p&gt;So, clearly echo is doing some argument parsing. It&#x27;s not just blindly reproducing what you pass to it. Let&#x27;s come back to this later.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-s-the-problem&quot;&gt;What&#x27;s the problem?&lt;&#x2F;h2&gt;
&lt;p&gt;Suppose you&#x27;re debugging a Bash script. You find that somewhere in the middle you get an error like &lt;code&gt;bash: llvm-ar: command not found&lt;&#x2F;code&gt;, but you&#x27;re really sure that you&#x27;ve got all of llvm installed, so your first debugging step is to add &lt;code&gt;echo $PATH&lt;&#x2F;code&gt; to your script just before the &lt;code&gt;llvm-ar&lt;&#x2F;code&gt; command, perhaps the path is set to find a half-baked llvm install somewhere else. We all do this. We all use echo to print out the contents of a variable sometimes. And this is fine, especially if that variable is &lt;code&gt;$PATH&lt;&#x2F;code&gt;, since it&#x27;s very unlikely to hit the issue I&#x27;m going to talk about.&lt;&#x2F;p&gt;
&lt;p&gt;Now, consider that you might be generating a list of arguments to pass to some other program. Here&#x27;s a short script that will find the text &quot;-right&quot; in the file static&#x2F;style.css. A not-unreasonable task.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;pattern&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-e -right&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;pattern&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; static&#x2F;style.css&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you run this script, you get the output anyone would expect. For example, perhaps the result looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    margin-right: 10px;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    padding-right:0.2em;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    margin-right: 0;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great. Everything is working fine, nothing to see here. But what if the script is a bit more complicated and you end up needing to debug it at some point. Perhaps you need to know what the &lt;code&gt;pattern&lt;&#x2F;code&gt; variable is set to at that point just before the grep command.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;pattern&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-e -right&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;pattern&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I tricked you! the quiz isn&#x27;t over!&lt;&#x2F;p&gt;
&lt;p&gt;Q5. What does the above script print out?&lt;&#x2F;p&gt;
&lt;p&gt;Answer:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-right&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;huh!? where is the &lt;code&gt;-e&lt;&#x2F;code&gt;? It&#x27;s obvious that the &lt;code&gt;-e&lt;&#x2F;code&gt; is missing here, but if the assignment was much earlier in the script, and the result not clear, then this could be a real headache to debug.&lt;&#x2F;p&gt;
&lt;p&gt;It doesn&#x27;t matter that the text came from a variable, it looks like &lt;code&gt;-e&lt;&#x2F;code&gt; which is special for echo, and so it does the special thing instead of echoing &lt;code&gt;-e&lt;&#x2F;code&gt;. This means that &lt;code&gt;echo $VAR&lt;&#x2F;code&gt; will not always echo the value of the variable &lt;code&gt;VAR&lt;&#x2F;code&gt;. This is the fundamental problem with echo, as implemented in bash.&lt;&#x2F;p&gt;
&lt;p&gt;You cannot reliably use echo to output the contents of a variable&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. It&#x27;s not possible, and I think that&#x27;s a bug in bash&#x27;s echo implementation.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, no amount of quoting can help you here. Here&#x27;s a few attempts that still do not echo &#x27;-e&#x27;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ export VAR=-e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo $VAR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;quot;$VAR&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;$VAR&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$VAR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;-e&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;-&amp;#39;&amp;#39;e&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The quotes are removed by bash before echo ever sees the variable contents. So quoting does not solve this problem. Quoting does solve some other problems though, and failure to quote variables properly is actually the cause of lots of bugs. Maybe I should make a post about shell injection some time?&lt;&#x2F;p&gt;
&lt;p&gt;Another thing people sometimes try is adding in dashes. A lot of unix utilities use a single or double dash to indicate the end of parsing of dash-prefixed arguments. Bash&#x27;s echo does no such thing.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo - -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -- -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-- -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Anyway, there&#x27;s workarounds for when this is a problem, I&#x27;ll get to those soon, but first let&#x27;s look at an actual bug caused by not considering this behaviour of echo.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;zipgrep-it-s-not-you-it-s-your-arch-linux-package&quot;&gt;zipgrep, it&#x27;s not you, it&#x27;s your Arch linux package&lt;&#x2F;h2&gt;
&lt;p&gt;zipgrep is a program that has a bug that exists because of this behaviour of echo. I&#x27;m sure there&#x27;s lots of others, but this is the first one I found when looking for bugs like this in common tools that you might expect to find installed by default on some linux distros.&lt;&#x2F;p&gt;
&lt;p&gt;zipgrep is a tool that will search for a particular pattern of text in all the files in a zip file and tell you which ones match.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m going to make a file called foo which contains the word bar, then I&#x27;m going to zip that up as foo.zip and if I ask the unzip program to list all the files in foo.zip, it tells me that there&#x27;s one called foo, as expected.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;bar&amp;#39; &amp;gt; foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ zip foo.zip foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  adding: foo (stored 0%)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ unzip -Z1 foo.zip&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, let&#x27;s ask &lt;code&gt;zipgrep&lt;&#x2F;code&gt; if it can find &lt;code&gt;bar&lt;&#x2F;code&gt; inside this zip file.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ zipgrep bar foo.zip&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo:bar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No surprises here, it finds that the foo file contains bar, so it tells us that. great! that worked!&lt;&#x2F;p&gt;
&lt;p&gt;Now let&#x27;s make a file called &quot;-e&quot;, which just contains the word bar, let&#x27;s put that in a zip file, I&#x27;ll call it e.zip.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;bar&amp;#39; &amp;gt; -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ zip e.zip -- -e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  adding: -e (stored 0%)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is fine, and it all worked. if I list the files found in the zip file, I see that there is one file, and it is called &quot;-e&quot;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ unzip -Z1 e.zip&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Maybe you can see where this is going.&lt;&#x2F;p&gt;
&lt;p&gt;Now I ask zipgrep to look for bar again, this time in e.zip. It should find a file called &quot;-e&quot; which contains bar. What it actually does is fail with a strange error message.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ zipgrep bar e.zip&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;caution: filename not matched: &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s have a look at why this is. The answer is fairly simple. zipgrep first generates a list of all the filenames in the zip file, then for each name in that list, it escapes the filename by echoing it into this sed command, and then extracts just that file from the zip, and finally is able to grep that file for the pattern we&#x27;re interested in.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;70&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Escape (or re-escape) shell-special characters in the archive&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;71&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; member name, &amp;quot;i&amp;quot;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;72&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;    i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;73&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;     sed&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;\\\\&#x2F;\\\\\\\\&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;|&#x2F;\\\|&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;&amp;amp;&#x2F;\\\&amp;amp;&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; `&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s just evaluate that &lt;code&gt;echo|sed&lt;&#x2F;code&gt; command step-by-step. The only file name is &lt;code&gt;-e&lt;&#x2F;code&gt; so that&#x27;s the value of the &lt;code&gt;i&lt;&#x2F;code&gt; variable initially.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; sed&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;\\\\&#x2F;\\\\\\\\&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;|&#x2F;\\\|&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;&amp;amp;&#x2F;\\\&amp;amp;&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; `&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; sed&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;\\\\&#x2F;\\\\\\\\&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;|&#x2F;\\\|&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;&amp;amp;&#x2F;\\\&amp;amp;&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; `&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; substitue value of i&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;      |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; sed&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;\\\\&#x2F;\\\\\\\\&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;|&#x2F;\\\|&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s&#x2F;&amp;amp;&#x2F;\\\&amp;amp;&#x2F;g&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; `&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; equivalent to the above&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; the sed command operates on the empty string, the result is the empty string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Later on, zipgrep uses that empty string as the filename argument to the unzip command.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;75&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Globally, send fd 4 to stdout.  In the pipeline, send normal&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;76&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; stdout to fd 4, and send grep status to fd 3.  Collect fd 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;77&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; with ``.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;78&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;    exec&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;&amp;gt;&amp;amp;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;79&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;    status_grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot; style=&quot;background-color: light-dark(#383A420C, #3E3D32);&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;80&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;     (&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; unzip&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;p-L&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;zipfile&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;81&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;     egrep&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;opt&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;pat&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;&amp;gt;&amp;amp;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; ;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; $&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;gt;&amp;amp;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;82&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;     sed&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;s|^|&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #E6DB74);&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #E6DB74);&quot;&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F8F8F2);&quot;&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #E6DB74);&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;:|&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;&amp;gt;&amp;amp;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #AE81FF);&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot; style=&quot;color: light-dark(#9D9D9F, #90908A);&quot;&gt;83&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;     )&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; `&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sure enough that&#x27;s the cause of the strange error message we see.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ unzip -p-L e.zip &amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;caution: filename not matched: &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can fix this! &lt;code&gt;printf&lt;&#x2F;code&gt; is kinda like a glorified version of echo, it behaves mostly like &lt;code&gt;printf&lt;&#x2F;code&gt; in C.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s replace this &lt;code&gt;echo&lt;&#x2F;code&gt; with a &lt;code&gt;printf&lt;&#x2F;code&gt;. Now the string will be printed exactly as it is passed to printf, it will not be interpreted as anything special.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;diff&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #F92672);&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #F92672);&quot;&gt;    i=` echo &amp;quot;$i&amp;quot; | \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #A6E22E);&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #A6E22E);&quot;&gt;    i=` printf &amp;#39;%s&amp;#39; &amp;quot;$i&amp;quot; | \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The interesting thing about this bug is that it is specific to systems that package zipgrep such that it uses Bash as the interpreter. Some other shells do not do this with their echo commands, and I suspect that zipgrep was written with one of these in mind. The Arch linux package (at the time of writing) packages zipgrep to use Bash. Debian (at the time of writing) packages zipgrep &quot;properly&quot; such that it uses &lt;code&gt;dash&lt;&#x2F;code&gt; (Debian Almquist SHell) as the interpreter, since &lt;code&gt;dash&lt;&#x2F;code&gt; is what provides &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; on Debian. Arch uses Bash as &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I think this highlights an even more fundamental issue with the design of Linux distributions, and programs that run on them. There are so many different options for what shell to use as &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt;. Which one is correct? What features can a program rely on? A program specifies what interpreter should be used to run it, but it&#x27;s up to the operating system which interpreter is actually used. When a program specifies that it is to be run with &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt;, what does that mean? Does it really not care if you use Bash, Dash, Ash, Zsh? There are linux or unix systems that use each of these shells as the default for &#x2F;bin&#x2F;sh.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;but-wait-there-s-more-fascinating-workarounds-ahead&quot;&gt;But wait! There&#x27;s more! Fascinating workarounds ahead!&lt;&#x2F;h2&gt;
&lt;p&gt;There&#x27;s another option for working around this interesting echo behaviour, one that wouldn&#x27;t work for fixing the zipgrep bug, but one that might be useful to know. To figure out what it is, let&#x27;s look at what &quot;-e&quot; and &quot;-n&quot; actually do, and how Bash&#x27;s echo implementation handles them.&lt;&#x2F;p&gt;
&lt;p&gt;The first place to look is the Bash man page, which explains its echo implementation. You can see that it mentions the optional arguments &quot;-e&quot;, &quot;-n&quot; and &quot;-E&quot;. we&#x27;re less interested in &quot;-E&quot; because on most systems it&#x27;s ignored.&lt;&#x2F;p&gt;
&lt;p&gt;&quot;-n&quot; simply prevents the final newline character being printed. You can see the effects of this in your terminal by comparing these two echos...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -n foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&quot;-e&quot; enables interpreting of escape sequences. There&#x27;s a big list of them. A common one is &lt;code&gt;\n&lt;&#x2F;code&gt;, which inserts a newline when escape sequences are enabled.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;#39;foo\nbar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo\nbar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -e &amp;#39;foo\nbar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can also remove the final newline by using &lt;code&gt;-n&lt;&#x2F;code&gt; like this...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -e -n &amp;#39;foo\nbar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bar$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that it&#x27;s fairly common for unix programs to allow multiple single-char options to be specified next to each other. This next command is exactly the same as the previous command where we have the &quot;-e&quot; and the &quot;-n&quot; separate.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -enenenen &amp;#39;foo\nbar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bar$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can see it doesn&#x27;t even matter how many times we specify each of the options. But if we add an option that isn&#x27;t recognised then suddenly all of the options in that one argument are ignored. So here, &quot;-enx&quot; gets echoed exactly as we typed it...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -enx &amp;#39;foo\nbar&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-enx foo\nbar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So if we have a variable and we want to print its value to the screen, we can take advantage of this fact, and this is how the second workaround option works: add a space to the end of the argument. We need to be careful to add double quotes so that the space becomes part of the first argument, if there is one.&lt;&#x2F;p&gt;
&lt;p&gt;So suppose &lt;code&gt;foo&lt;&#x2F;code&gt; contained &lt;code&gt;-e&lt;&#x2F;code&gt;, normally this would cause us a problem, but this time there&#x27;s also a space character at the end, which is not a valid echo option, this whole argument is treated as not-special, and is just printed as-is.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ export foo=&amp;#39;-e&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo &amp;quot;$foo &amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-e &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;there&#x27;s an additional space character at the end, but in this case we don&#x27;t actually care about that, it&#x27;s not like we can see a space character at the end of a line anyway.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Hmm what&#x27;s that? You have a terminal that renders little dots where trailing spaces are? Quiet you!&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, this wouldn&#x27;t have worked for our zipgrep fix, since we&#x27;re using the result as an argument to another program, rather than simply printing it to the screen. If there was an extra space character in there then it wouldn&#x27;t work, so printf is still the best way to do this in general.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we&#x27;re done talking about how the bash implementation of echo is tricky, I wanna take a moment to look at other echo implementations. Almost every shell has its own implementation of echo, let&#x27;s look at these shells:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;bash&lt;&#x2F;code&gt;, &lt;code&gt;dash&lt;&#x2F;code&gt;, &lt;code&gt;zsh&lt;&#x2F;code&gt;, &lt;code&gt;fish&lt;&#x2F;code&gt;, &lt;code&gt;tcsh&lt;&#x2F;code&gt;, &lt;code&gt;xonsh&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Each one of these has echo as a built in command, so let&#x27;s type the same thing into every one of these shells, and see what each one does. This is the command we will use:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;echo - - -n &amp;quot;foo\bar\\ntext``&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So let&#x27;s go! Here are the outputs of this command from each of those shells:&lt;&#x2F;p&gt;
&lt;p&gt;bash&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- - -n foo\bar\ntest&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;dash&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- - -n foar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;zsh&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;n -n foar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;fish&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- - -n foo\bar\ntest``&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;tcsh&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- - -n foar\ntest&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;xonsh&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- - -n foar\ntest``&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Remember that we&#x27;re now not just comparing the echo behaviours across shells, but we&#x27;re also allowing other shell features to have an influence here, for example string escaping, argument splitting, and command substitution.&lt;&#x2F;p&gt;
&lt;p&gt;Each of these shells does something unique with this input. That&#x27;s six different shells, and six different results.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-challenge-for-you&quot;&gt;A challenge for you&lt;&#x2F;h2&gt;
&lt;p&gt;The quiz continues! but this time I&#x27;m not telling the answer. &lt;em&gt;Update: I have actually posted some answers in the update section below&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll leave you with a challenge. The challenge is to get Bash&#x27;s echo to output the following two bytes and nothing more: &lt;code&gt;-n&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Just those two bytes. Just a dash, followed by a lower case N. You&#x27;re not allowed to have additional characters before or after them, so you can&#x27;t use the trailing space trick from before. You must only use echo, no piping the output to another program to post-process it. And since you need just those two bytes, that means no newline character at the end.&lt;&#x2F;p&gt;
&lt;p&gt;For a while I thought that it was impossible, but actually it is possible to do, and it&#x27;s especially fun to give as a challenge to friends that haven&#x27;t read this post yet, so challenge your friends!&lt;&#x2F;p&gt;
&lt;p&gt;You can check if you&#x27;ve got it right by piping the output to a hex dump utility such as &lt;code&gt;xxd&lt;&#x2F;code&gt;. A successful attempt looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo ...your.input.here... | xxd&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;00000000: 2d6e                                     -n &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h2&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
&lt;p&gt;Okay, technically that&#x27;s not true, but the answer relies on other Bash features that preprocess the string so that echo will re-process the string back in to the original.  It&#x27;s ugly: &lt;code&gt;echo -e &quot;${VAR&#x2F;&#x2F;\\&#x2F;\\\\}\n\c&quot;&lt;&#x2F;code&gt;. It works by using the &lt;code&gt;\n\c&lt;&#x2F;code&gt; escape sequence in &lt;code&gt;-e&lt;&#x2F;code&gt; mode to make sure the string is never a valid option argument, and it defuses escape sequences from the input string by escaping them with extra backslashes using Bash&#x27;s string substitution syntax. It&#x27;s not neat, it&#x27;s going to raise questions in code review, but it does technically get the job done.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h2 id=&quot;update-follow-up-q-and-a&quot;&gt;UPDATE: Follow up Q and A&lt;&#x2F;h2&gt;
&lt;p&gt;(published 2023-02-12)&lt;&#x2F;p&gt;
&lt;p&gt;You all enjoyed that post! So here&#x27;s some answers to questions you asked. Spoilers ahead for the challenge!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;non-solutions&quot;&gt;Non-solutions&lt;&#x2F;h3&gt;
&lt;p&gt;Some people suggested some things that don&#x27;t work, or appear to work visually but are doing something different.&lt;&#x2F;p&gt;
&lt;p&gt;The first category was using dashes, but that doesn&#x27;t work.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -n - -n&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;- -n$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ echo -n -- -n&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-- -n$&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I suspect that the people who thought &lt;code&gt;echo -n - -n&lt;&#x2F;code&gt; would work were accidentally using Zsh instead of Bash.&lt;&#x2F;p&gt;
&lt;p&gt;Some other people used the backspace character (or its escape sequence) as part of their solution. This is not a valid solution. It appears to work in the terminal, because of how the backspace is interpreted, but it fails the rigourous test with &lt;code&gt;xxd&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For example, you can print a backspace character at the start, which does nothing, but isn&#x27;t a valid option. The character is there even though you can&#x27;t see it.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;en&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;\b-n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another attempt is to have the backspace at the end, after a sacrificial character&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;en&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-n \b&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;solutions&quot;&gt;Solutions&lt;&#x2F;h3&gt;
&lt;p&gt;There&#x27;s a few solutions that use escape sequences to generate one or both of the characters in the sequence. These are all the same concept, they prevent echo from interpreting them as options, but are then parsed as escape sequences if &lt;code&gt;-e&lt;&#x2F;code&gt; is specified.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s a few of these:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;en&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;\u002dn&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;en&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;\055n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;en&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-\u006e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next solution is the one that I found first. This is where the special escape sequence &lt;code&gt;\c&lt;&#x2F;code&gt; is used. This escape sequence doesn&#x27;t evaluate to some other character. It actually makes echo stop outputting anything after the \c. There&#x27;s nothing there anyway, but its presence prevents the option from being valid&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-n\c&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;bending-the-rules-without-breaking-them&quot;&gt;Bending the rules without breaking them&lt;&#x2F;h3&gt;
&lt;p&gt;Some &lt;del&gt;smart arses&lt;&#x2F;del&gt; very clever people found ways to bend the rules so they technically complied with the requirements but didn&#x27;t solve the problem the way I intended. Here&#x27;s one...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; ;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; n&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Yep, it only uses echo, it prints the two characters I asked for and nothing else, it doesn&#x27;t pipe the output anywhere. Nicely done! I can&#x27;t be too mad.&lt;&#x2F;p&gt;
&lt;p&gt;Then someone did this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;n&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; n-&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And, yeah, I suppose I wasn&#x27;t explicit that they have to be in the right order, but I reject this &quot;solution&quot; regardless!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A floppy disk is technially a letter</title>
          <pubDate>Thu, 08 Sep 2022 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/a-floppy-disk-is-technically-a-letter/</link>
          <guid>http://computingthedetails.org/blog/a-floppy-disk-is-technically-a-letter/</guid>
          <description xml:base="http://computingthedetails.org/blog/a-floppy-disk-is-technically-a-letter/">&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_floppy_disk_is_technically_a_letter&#x2F;header.webp&quot; alt=&quot;A Royal mail red postbox, and a green 3.5 inch floppy disk with a red first-class postage stamp on it. There is an arrow pointing from the floppy disk to the hole in the post box.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you live in the UK then you&#x27;ll almost certainly know about Royal Mail. They probably deliver most of your mail, and you probably recognise their distinctive red-cylinder
post boxes.&lt;&#x2F;p&gt;
&lt;p&gt;These are Royal mail stamps.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_floppy_disk_is_technically_a_letter&#x2F;stamps.png&quot; alt=&quot;Nine first-class Royal mail stamps in a stamp book. They are red, with a profile of Queen Elizabeth the second in white.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see they&#x27;ve really gone for the whole Royal theme. This is of course an instagram photo of the queen taken with a &quot;make me look younger&quot; filter. It has done a splendid job.&lt;&#x2F;p&gt;
&lt;p&gt;Each one of these proves that I have paid for postage of one letter. Or at least they do until they start enforcing use of the new barcode stamps. I guess these ones are just too easy to counterfeit.&lt;&#x2F;p&gt;
&lt;p&gt;Now, this stamp book is about two years old and I&#x27;ve used three of them. I have only a few months to use up the last nine of these. If I continue to use stamps at my current rate, then by the time they become invalid
I&#x27;ll still have approximately ... &lt;em&gt;nine&lt;&#x2F;em&gt; stamps. I&#x27;m gonna have to drastically increase the rate at which I use stamps, but I never send letters, so what can I do?&lt;&#x2F;p&gt;
&lt;p&gt;Well, I noticed something about what these stamps are valid for. According to Royal Mail, a Letter is anything that fits inside a volume of 240x165x5mm, and up to a weight of 100g.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;a_floppy_disk_is_technically_a_letter&#x2F;royal_mail_requirements.png&quot; alt=&quot;A screenshot of the royal mail site section that explains the letter restrictions as above&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So, I checked a standard 3.5 inch floppy disk, and it does indeed fit quite comfortably in to that specification. This sounds like a much more fun way to use my old stamps.&lt;&#x2F;p&gt;
&lt;p&gt;Actually, you can comfortably fit &lt;em&gt;two&lt;&#x2F;em&gt; floppy disks in the size of one letter. If you tape two together, you can send them both at once!
&lt;img src=&quot;&#x2F;posts&#x2F;a_floppy_disk_is_technically_a_letter&#x2F;size_visual.png&quot; alt=&quot;A rendering of two blue floppy disks inside a transparent visualisation of the maximum letter size. There is lots of spare space around the disks&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I decided to send a friend of mine a floppy disk with something silly on it.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s a Microsoft Word 97 dot-doc file containing a stream of hex codes, which when interpreted as ASCII text will give you a base64 encoding of a gzip file, which when unzipped will give you a PNG file, which when rendered presents a QR code that encodes a link to ... Rick Astley&#x27;s &quot;Never Gonna Give you up&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s a high-effort rick roll.&lt;&#x2F;p&gt;
&lt;p&gt;I have a particular friend who has a fondness for older versions of Microsoft Word. I decided to send her this floppy disk to see if she would figure out that it was me that sent it.&lt;&#x2F;p&gt;
&lt;p&gt;And the answer is... Yes! She knew it was me. She received the floppy disk, as sent, and she quickly predicted that it contained a Rick roll.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>I found a ZX Spectrum demo in a box of old tapes</title>
          <pubDate>Sat, 02 Jul 2022 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/rachels-skill-program/</link>
          <guid>http://computingthedetails.org/blog/rachels-skill-program/</guid>
          <description xml:base="http://computingthedetails.org/blog/rachels-skill-program/">&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot1.png&quot; alt=&quot;screenshot of the first screen of RACHEL&amp;#39;s SKILL PROGRAM. the words are printed in a very large font, in red, cyan, and green&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Many years ago I bought a job lot of compact casette tapes on ebay. They all contained ZX spectrum software. The lot that I bought also came with a bunch of other things too, and it was those things that I was more interested in.&lt;&#x2F;p&gt;
&lt;p&gt;Since the tapes were mostly cheap games, I only tried playing with the interesting looking ones, and after a short while I put them all in a box and forgot about them.&lt;&#x2F;p&gt;
&lt;p&gt;Recently I opened up this box again and I found something rather interesting on one of the tapes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-a-demo&quot;&gt;What is a Demo?&lt;&#x2F;h2&gt;
&lt;p&gt;Before I get into what I found, for the benefit of those not familiar with the Demoscene, allow me to explain what a demo is.&lt;&#x2F;p&gt;
&lt;p&gt;A demo is a piece of software that generates an animation or visual effect in real-time. Often accompanied by music or sound effects. A demo is not simply a video file.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s a whole digital art subculture around these things, it&#x27;s called &lt;em&gt;the Demoscene&lt;&#x2F;em&gt;. Here&#x27;s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Demoscene&quot;&gt;a link to a better explanation&lt;&#x2F;a&gt; than I can provide.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m not sure if Rachel knew they were writing a demo, but they were.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-demo&quot;&gt;The Demo&lt;&#x2F;h2&gt;
&lt;p&gt;In my box of old tapes there was a completely uninteresting looking tape which was clearly bought as a blank C15 tape and had no markings on the outer packaging. I think I probably dismissed this the first time around because it looked like a blank tape.&lt;&#x2F;p&gt;
&lt;p&gt;Upon closer inspection I saw there was one thing on the index card, but it had since been thoroughly erased, but there was also a name written in pencil on the casette.&lt;&#x2F;p&gt;
&lt;p&gt;&quot;Rachel&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, I took the obvious next step and tried loading it up to see what was on there. After about one minute of loading, the sound stopped and it appeared that the spectrum had successfully loaded something.&lt;&#x2F;p&gt;
&lt;p&gt;Running the &quot;LIST&quot; command shows a BASIC listing that had just been loaded from the tape. Looks exciting, let&#x27;s run it and see what it does!&lt;&#x2F;p&gt;
&lt;p&gt;I will walk through the demo here as a sequence of images, but if you want the full experience then you&#x27;ll want to see my recording. The raw recording is avialable to watch &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;tjLsHF5Jboo&quot;&gt;on YouTube&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot2.png&quot; alt=&quot;screenshot 2&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot3.png&quot; alt=&quot;screenshot 3&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Screenshots 2 and 3 show the first part of the program, first the words &quot;THIS IS A PROGRAM ON MUSIC&quot; are shown on the screen, then a musical scale is played slowly, with a new coloured circle appearing on the screen after each note. Each circle is smaller than the previous, they are all centred on the screen. The circle and note section repeats, playing twice in total.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot4.png&quot; alt=&quot;screenshot 4&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot5.png&quot; alt=&quot;screenshot 5&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot6.png&quot; alt=&quot;screenshot 6&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Screenshots 4, 5, and 6 show the next musical section. First &quot;AND NOW&quot; flashes in large text on the screen, then it is replaced with much smaller text: &quot;CLOSE ENCOUNTERS OF THE THIRD KIND&quot;. Finally, a drawing of green alien appears on the screen, while the note sequence from close encounters of the third kind plays. Once again, this section plays twice.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot7.png&quot; alt=&quot;screenshot 7&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot8.png&quot; alt=&quot;screenshot 8&quot; &#x2F;&gt;
&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot9.png&quot; alt=&quot;screenshot 9&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The final musical section is shown in screenshots 7, 8, and 9. Introduced with a repeating rainbow pattern that says &quot;NEXT: CHARIOTS OF FIRE&quot;. The animation for this one is random coloured lines being drawn across the screen in random places. The tune from chariots of fire plays.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;screenshot10.png&quot; alt=&quot;screenshot 10&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Finally, in screenshot 10, we see that the demo has ended. the words &quot;THE END&quot; are printed in large letters, in blue and red. Below is the text &quot;THIS PROGRAM WAS WRITTEN BY THE &#x27;ONE &amp;amp; ONLY&#x27; RACHEL BRADLEY !&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Wow! That was really cool. I wonder who Rachel Bradley is. I bet Rachel was a kid in the 80s and had a ZX spectrum in their bedroom. This looks like the result of a kid just having some fun writing some code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;but-that-s-not-all-here-s-a-quiz&quot;&gt;But that&#x27;s not all, here&#x27;s a quiz!&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s not the only program of theirs on this tape either. On the other side of the tape is another program, also by Rachel. This time it&#x27;s an interactive quiz program.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s not many questions, and at the end it just stops, no score totalling or summary of your answers. The questions are the same every time. It&#x27;s a very simple program, but with impressive attention to detail. I quite like the layout of the question screen.&lt;&#x2F;p&gt;
&lt;p&gt;I would tell you more about it, but apparently I neglected to capture that side of the tape, and I don&#x27;t have a convenient way to digitise it right now.&lt;&#x2F;p&gt;
&lt;p&gt;Just those two programs on this tape, and it&#x27;s the only one in the box that has what appears to be home-made programs.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;thanks-rachel&quot;&gt;Thanks Rachel!&lt;&#x2F;h2&gt;
&lt;p&gt;I have no idea who Rachel Bradley is, but if you&#x27;re out there somewhere reading this, then thank you for your demo! I really enjoyed it. It was truly a treasure among this stack of old tapes.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s possible that I was the first person to have watched that demo for around 35 to 40 years. Who knows?&lt;&#x2F;p&gt;
&lt;p&gt;If you want to take a closer look at the demo, you can download &lt;a href=&quot;&#x2F;posts&#x2F;rachels_skill_program&#x2F;rachels_skill_program.tap&quot;&gt;this tap file&lt;&#x2F;a&gt; that I captured from the orignal tape.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Playing AmongUs with a sat solver</title>
          <pubDate>Sun, 29 Nov 2020 00:00:00 +0000</pubDate>
          <author>Lex (they&#x2F;them)</author>
          <link>http://computingthedetails.org/blog/amongus-with-sat-solver/</link>
          <guid>http://computingthedetails.org/blog/amongus-with-sat-solver/</guid>
          <description xml:base="http://computingthedetails.org/blog/amongus-with-sat-solver/">&lt;p&gt;&lt;em&gt;Looking for the code? Get the code &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;solver_for_among_us&quot;&gt;on GitHub&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A satisfyability solver (sat solver) is a tool that finds solutions to a boolean formula. These are normally used for checking mathematical proofs, or for verification of computer programs. I&#x27;ve used them for this purpose, but sometimes it&#x27;s fun to do unusual things with them. I used one to help me play the popular online video game of social deduction: &lt;em&gt;Among Us&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s actually quite hard to be good at doing perfect deductions while playing Among Us. There&#x27;s lots to remember and although the logic you need for deducing the hidden information is simple, it&#x27;s hard to apply it properly in your mind within the time.&lt;&#x2F;p&gt;
&lt;p&gt;Given how hard this game is, I thought it might be fun to create a tool that helps you play by remembering all the things you&#x27;ve learned so far, and making the deductions for you.&lt;&#x2F;p&gt;
&lt;p&gt;So I wrote a program for doing exactly this. It knows all the constraints that exist on the game state, and it knows the facts you tell it, and then it recommends people to eject after meetings. Assuming you give it only things you know for sure then it will perfectly deduce who the impostors are.&lt;&#x2F;p&gt;
&lt;p&gt;The constraints that it knows at the start are really simple things like: people who were killed cannot be impostors, there&#x27;s always more crewmates than impostors after an ejection that doesn&#x27;t end the game, and some other simple things like that.&lt;&#x2F;p&gt;
&lt;p&gt;You can tell it that someone is a crewmate, maybe you saw them do a visual task, you know they&#x27;re a crewmate. If you see someone murder or vent, then that&#x27;s an easy one, you know they&#x27;re an impostor. You also tell it who has been murdered, and who was ejected, of course. But the most useful thing is to tell it when a group of people is known to contain an impostor.&lt;&#x2F;p&gt;
&lt;p&gt;So if you never let green or red out of your sight for the whole round, but somebody was killed, then you know there&#x27;s at least one impostor amongst all the players that are not you, green, or red.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-ui&quot;&gt;The UI&lt;&#x2F;h2&gt;
&lt;p&gt;The UI for this thing is a webpage. You run the server locally on your PC, and open up your browser to use the solver. It&#x27;s a really simple UI. At first you simply select which players are present, and how many impostors there are.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;amongus_with_sat_solver&#x2F;ui_configure_game.webp&quot; alt=&quot;A user selects players from a list of colours. They select red, blue, pink, orange, black, and purple. They start a game with two impostors.&quot; title=&quot;The initial game setup interface running inside firefox.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once a game is created, you can input information by clicking the buttons on each player, or by adding them to the selection with the tick&#x2F;cross icon, and then marking the selected groups as including an impostor.&lt;&#x2F;p&gt;
&lt;p&gt;Each time more information is learned, the sat solver finds all the possible models (assignments of the variables in the boolean expression) that match all of the given knowledge. The UI shows the number of models in which each player is an impostor. The idea is that if you are in more models, then you&#x27;re more likely to be an impostor. It also recommends an action to take at the meeting.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;posts&#x2F;amongus_with_sat_solver&#x2F;ui_play_game.webp&quot; alt=&quot;A user interacts with the UI to inform the solver that the red player is a crewmate, the blue player was murdered, there is at least one impostor among black and purple, there is at least one impostor amont pink and black, black was ejected, purple is a crewmate. As they do this, the number of models shown for each player decreases from the initial 5 models each. Below the controls for information input are two lists. One of players to suspect, and one for innocent players.&quot; title=&quot;The main game view, showing how to use the interface during gameplay.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;behind-the-scenes&quot;&gt;Behind the scenes&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s a piece of python code, which serves the UI over http. It uses Z3, which is a sat-solver, to calculate all the possible explanations for the facts you provide. That means I didn&#x27;t tell it any of the logic for actually making deductions, I just told it the constraints and facts, and the logic doesn&#x27;t matter because of the way a sat-solver finds solutions to equations. It also means it&#x27;s really easy to add extra information, if ever an update to the game adds ways to learn things, or adds new game state.&lt;&#x2F;p&gt;
&lt;p&gt;Z3 has some really nice python bindings that allow you to have a python object that refers to a Z3 variable. Perhaps you&#x27;re interested to see some code? I won&#x27;t do an in-depth analysis here, but the code is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;solver_for_among_us&quot;&gt;on GitHub&lt;&#x2F;a&gt;  and has a lot of documentation comments. If you want a deep understanding of it, have a read of the code.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing that it does when a game starts is set up some basic facts about the game...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#000000, #000000);background-color: light-dark(#FFFFFF, #FFFFFF);&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#C18401, #A6E22E);&quot;&gt;GameSolver&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt; n_players&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt; n_impostors&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; Construct a GameSolver with a known number of players and number of impostors &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; n_players&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Some variables that model the state of each player&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;alive&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;murdered&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ejected&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            [&lt;&#x2F;span&gt;&lt;span&gt;z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; %&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span&gt;varname&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; player_id&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; player_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span&gt; varname&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;                &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;player-is-alive&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;player-was-murdered&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;player-is-impostor&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;player-was-ejected&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; First bit of information known is the number of impostors&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;IntVal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;n_impostors&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_alive_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;alive_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;knowns&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; for known facts that will never change until the game ends&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; State for building facts about players during the game, when what is known&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; might change (I know blue is not murderd and green is not ejected, but then&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; green murders blue, we figure it out and eject green, now blue&amp;#39;s murdered&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; state changed and green&amp;#39;s ejected state changed)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;murdered_pids&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ejected_pids&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The simple constraints are ones such as &quot;player X is a crewmate&quot;. These become basic assertions in Z3, which are tracked in the &lt;code&gt;knowns&lt;&#x2F;code&gt; variable on a &lt;code&gt;GameSolver&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; learn_certain_not_impostor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt; colour&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; learn that the player with the specified colour is certainly not an impostor &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        p&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;pid&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;colour&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;knowns&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Not&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;p&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I find the code for specifying the basic constraints of the game to be quite nicely readable, thanks to the Z3 python bindings...&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; basic_constraints&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; Returns a list of constraints representing basic facts about the possible game states &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Number of impostors is number of impostors&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; sumbools&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; A number of impostors can be alive&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_alive_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; sumbools&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;And&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;alive&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; People that were murdered cannot be impostors&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Implies&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;murdered&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Not&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Alive people have not been murdered and have not been ejected&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;And&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span&gt;z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Not&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;murdered&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Not&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ejected&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;alive&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; There is at least one not-dead impostor&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_alive_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;IntVal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt;            #&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A0A1A7, #88846F);font-style: italic;&quot;&gt; Twice the number of alive impostors is less than the number of alive crewmates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ,&lt;&#x2F;span&gt;&lt;span&gt;sumbools&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;alive&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_alive_impostors&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;IntVal&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #AE81FF);&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Normally Z3 will only find one solution to your system of equations. To make Z3 provide all of the possible models, you have to iteratively remove each model that it finds, until it cannot find any more. I do this by adding a constraint that at least one player that is an impostor in this model must not be an impostor. This makes that particular model no longer valid, and I can search for a new one.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#383A42, #F8F8F2); background-color: light-dark(#FAFAFA, #272822);&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #66D9EF);&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#4078F2, #A6E22E);&quot;&gt; not_this_model&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#986801, #FD971F);&quot;&gt; model&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt; Returns a constraint that prevents the given model from being valid &lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#50A14F, #E6DB74);&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; z3&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt;            *&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;pid&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#383A42, #F92672);&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; model&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;impostor&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span&gt;pid&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span&gt; pid&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#A626A4, #F92672);&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#0184BC, #66D9EF);&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: light-dark(#E45649, #FD971F);&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span&gt;n_players&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, this constraint must then be removed before reevaluating the solutions.&lt;&#x2F;p&gt;
&lt;p&gt;There is a lot more supporting code, and I&#x27;ve not included all of the Z3 logic here, so if you are interested, certainly do have a look at the code &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lexbailey&#x2F;solver_for_among_us&quot;&gt;on GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;can-i-use-this-to-cheat&quot;&gt;Can I use this to cheat?&lt;&#x2F;h2&gt;
&lt;p&gt;No, not really, it&#x27;s not actually that helpful during the game. I did try and found that it was actually harder than normal play. Making this tool was mostly just an academic exercise.&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
