<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>A20 Gate - Blog</title>
    <subtitle>News and stories from A20 Gate</subtitle>
    <link rel="self" type="application/atom+xml" href="https://www.a20gate.net/blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://www.a20gate.net/blog/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-11-15T00:00:00+00:00</updated>
    <id>https://www.a20gate.net/blog/atom.xml</id>
    <entry xml:lang="en">
        <title>Retro Rabbit Holes Ep. #1 - Rogue&#39;s Abandoned Hall of Fame</title>
        <published>2026-11-07T00:00:00+00:00</published>
        <updated>2026-11-15T00:00:00+00:00</updated>
        
        <author>
          <name>ChrisR3tro</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/"/>
        <id>https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/</id>
        
        <content type="html" xml:base="https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/">&lt;p&gt;This story started with a missing high score and ended with a 1981 file &quot;API&quot;, a filesystem from 1996, and the slightly absurd conclusion that in certain cases &lt;a rel=&quot;external&quot; href=&quot;https://www.freedos.org/&quot;&gt;FreeDOS&lt;/a&gt; runs an old &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/MS-DOS&quot;&gt;MS-DOS&lt;/a&gt; game better than MS-DOS does. Who would have thought?&lt;/p&gt;
&lt;h2 id=&quot;meet-rogue&quot;&gt;Meet Rogue&lt;/h2&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/dungeon.png&quot; alt=&quot;Rogue: Game screenshot&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Rogue: Explored Level 1&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Rogue_(video_game)&quot;&gt;Rogue&lt;/a&gt; is the grandfather of an entire &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Roguelike&quot;&gt;genre&lt;/a&gt;. Michael Toy and Glenn Wichman wrote it on Unix around 1980, with Ken Arnold joining in later. You, as the player, go down into the Dungeons of Doom, fight letters of the alphabet that are actually monsters, and try to bring back the Amulet of Yendor. The levels are procedurally generated and death is permanent (spoiler: you will die a lot). Every &quot;roguelike&quot; since is named after this game.&lt;/p&gt;
&lt;p&gt;The IBM PC version came from A.I. Design, a small company founded by Michael Toy and Jon Lane to port Rogue to the PC — little did they know back then that decades later &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Claude_(AI)&quot;&gt;A.I.&lt;/a&gt; would be used to reverse engineer their product in hopes of fixing things. The irony! At first they packaged and sold it themselves until around 1984 &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Epyx&quot;&gt;Epyx&lt;/a&gt; took over marketing and commissioned ports to the Macintosh, Amiga and Atari ST. The PC version swapped the Unix letters for the PC&#39;s extended character set: smiley-face hero ☺, box-drawing walls, little suns for gold. The title screen reads &quot;Adapted for the IBM PC by: Jon Lane&quot;, and every gravestone it draws is dated 1983. (That&#39;s not a clock bug, by the way, at least according to Claude: the original Unix Rogue engraves the current year, but the PC port simply has the year &quot;1983&quot; baked into its tombstone, right below &quot;REST IN PEACE&quot;.)&lt;/p&gt;
&lt;p&gt;Two features are broken on later versions of MS-DOS (all tests were done with Rogue version 1.1):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Saving:&lt;/strong&gt; Press S and the game writes your current game to &lt;code&gt;rogue.sav&lt;/code&gt; so you can continue later.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Hall of Fame:&lt;/strong&gt; If your character dies, the game shows a gravestone, &quot;REST IN PEACE ... killed by a &amp;lt;monster of the day&amp;gt;&quot;, and then &quot;[Press Enter to see rankings]&quot;. The scores live in a so-called &quot;scorefile&quot;, &lt;code&gt;ROGUE.SCR&lt;/code&gt;, a small file next to the game&#39;s executable.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-bug&quot;&gt;The &quot;Bug&quot;&lt;/h2&gt;
&lt;p&gt;...or can it even be considered one in the true sense? Why I even found this error is an entirely different story: I wanted to let an LLM play Rogue, but that&#39;s a topic for an upcoming article (small teaser!). First, Rogue had a real problem to be dealt with, or at least investigated, because things like these tend to keep me up at night. The error made itself known after every death: instead of the rankings, it showed:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;No scorefile: Create Retry Abort&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Retry: Same message, the score sadly was never written. :-(&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/scorefile-bug.png&quot; alt=&quot;Rogue&amp;#39;s gravestone followed by multiple rounds of &amp;quot;No scorefile: Create Retry Abort&amp;quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;As if being killed by a hobgoblin is not enough, you are denied a place in the Hall of Fame. (R)etry or (C)reate all you want, it&#39;s no use. Mission Abort!&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;down-the-rabbit-hole&quot;&gt;Down the Rabbit Hole&lt;/h2&gt;
&lt;p&gt;I handed the quest to Claude (my trusty AI clanker at the moment), with a clear mission: dig deep into the machine code of &lt;code&gt;ROGUE.EXE&lt;/code&gt;. No editing or patching, just figure out why.&lt;/p&gt;
&lt;p&gt;Claude started where any sane reverse engineer would: with the strings in the game&#39;s executable. Right next to each other were &lt;code&gt;[Press Enter to see rankings]&lt;/code&gt;, &lt;code&gt;rogue.scr&lt;/code&gt; and the error message &lt;code&gt;No scorefile: %iCreate %iRetry %iAbort&lt;/code&gt;. (Plus a little bonus string, &lt;code&gt;(Mr. Mctesq was here)&lt;/code&gt;, possibly a cracker&#39;s signature? In my legally acquired copy, how could this be? Shout out to Mr. Mctesq!) Anyway, from there, the disassembler found the score routine: open &lt;code&gt;rogue.scr&lt;/code&gt; for reading and writing and if that fails, show the Create/Retry/Abort prompt.&lt;/p&gt;
&lt;p&gt;The interesting part was in the game&#39;s built-in C library. To open a file, it uses the original DOS 1.x way: it fills in a small 37-byte structure in memory, a &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/File_Control_Block&quot;&gt;&lt;code&gt;File Control Block (FCB)&lt;/code&gt;&lt;/a&gt;, and calls DOS function &lt;code&gt;0Fh&lt;/code&gt;, &quot;open file using FCB&quot;. Create, read, write, close and delete all go through FCBs too.&lt;/p&gt;
&lt;p&gt;That was a big clue. And as it turns out, it wasn&#39;t just the scores: savegames were broken too. If you press Shift+S to save the game, Rogue answers with &quot;Could not creat rogue.sav&quot;. That&#39;s not a typo, btw: &lt;code&gt;creat&lt;/code&gt; is the old Unix name of the &quot;create file&quot; function, and the game&#39;s message uses it. Apparently here lies some ancient Unix lore, which Claude just unearthed from the machine code. How nice! For debugging we now had a mechanism to reproduce the bug much easier than letting your character get killed over and over. (yeah, I know, as if that&#39;s not the default mode when playing this game...)&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;p&gt;&lt;img src=&quot;https://www.a20gate.net/blog/retro-rabbit-holes-ep1-rogues-abandoned-hall-of-fame/savegame-bug.png&quot; alt=&quot;Rogue&amp;#39;s first level with the message &amp;quot;Could not creat rogue.sav&amp;quot; at the top&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Ten seconds into a new game, and saving already fails.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The next step was to cut the game out of the picture. Claude chose good old &lt;code&gt;DEBUG.EXE&lt;/code&gt; and typed in four assembly instructions that make exactly the call Rogue makes: FCB open on &lt;code&gt;ROGUE.SCR&lt;/code&gt;. DOS answered &lt;code&gt;AL=FF&lt;/code&gt; (translation: epic fail).&lt;/p&gt;
&lt;p&gt;So DOS itself refused to open files on drive &lt;code&gt;C:&lt;/code&gt; this way. One &lt;code&gt;CHKDSK&lt;/code&gt; later the culprit was clear: &lt;code&gt;C:&lt;/code&gt; has 470,560 clusters of 4 KB each. FAT16 can&#39;t have more than 65,524 clusters. &lt;code&gt;C:&lt;/code&gt; must be FAT32!&lt;/p&gt;
&lt;p&gt;To make sure, I also confirmed the bug on real hardware with MS-DOS 7.1 and two partitions: Rogue saves fine on FAT16 and fails on FAT32. Same DOS, same game, different filesystem.&lt;/p&gt;
&lt;h2 id=&quot;a-quick-history-of-fcbs&quot;&gt;A quick history of FCBs&lt;/h2&gt;
&lt;p&gt;An FCB, or &lt;code&gt;File Control Block&lt;/code&gt;, is how programs talked to files in the very first MS-DOS, version 1.0 from 1981. It was copied almost directly from CP/M, the 8-bit operating system that DOS imitated, so that CP/M software could be ported quickly.&lt;/p&gt;
&lt;p&gt;The concept: the program reserves a small block of memory, writes the drive letter and an &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/8.3_filename&quot;&gt;8.3 filename&lt;/a&gt; into it (as &lt;strong&gt;ROGUE   SCR&lt;/strong&gt;, padded with spaces, no dot), and passes it to DOS. DOS uses the rest of the block as scratch space to keep track of the open file. DOS 1 had no folders at all, so there was nothing more to say.&lt;/p&gt;
&lt;p&gt;Then came MS-DOS 2.0 in 1983, built for the IBM PC XT and its hard disk. It brought sub-directories and, with them, a new Unix-style API: file handles. You pass a full path like &lt;code&gt;C:\GAMES\ROGUE\ROGUE.SCR&lt;/code&gt;, DOS gives you back a small number, and you use that number to read, write and close while DOS does the bookkeeping. FCBs couldn&#39;t handle paths, so from then on they were the legacy option. Microsoft kept them for compatibility and encouraged everyone to use handles. By the late 1980s practically every program did.&lt;/p&gt;
&lt;p&gt;Except, of course, the ones written for DOS 1. Rogue&#39;s built-in library was meant to run on the original IBM PC with DOS 1.x, so it used FCBs, which I&#39;d say was a perfectly reasonable decision in 1983.&lt;/p&gt;
&lt;h2 id=&quot;why-fat16-yay-fat32-nay&quot;&gt;Why FAT16 yay, FAT32 nay?&lt;/h2&gt;
&lt;p&gt;FAT32 arrived in 1996 with Windows 95 OSR2 (MS-DOS 7.1) and became mainstream with Windows 98. On FAT32 drives, MS-DOS simply refuses FCB file calls. The classic DOS reference, Ralf Brown&#39;s Interrupt List, also notes it: on FAT32, FCB file I/O &lt;a rel=&quot;external&quot; href=&quot;https://osfree.org/doku/doku.php?id=en:docs:dos:api:int21:16&quot;&gt;isn&#39;t supported&lt;/a&gt;. On FAT12 and FAT16 drives, the very same DOS still handles FCBs exactly as it always did. Nothing was removed, FAT32 was simply left out.&lt;/p&gt;
&lt;p&gt;To my knowledge, Microsoft never explained the decision, so here are some of Claude&#39;s speculations (take them with a grain of salt):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The technical side:&lt;/strong&gt; the FCB is a fixed 37-byte structure, and DOS uses a few reserved bytes in it for its own notes about the open file, including where the file starts on disk. Those notes are 16-bit numbers. There&#39;s also a nasty trick: DOS only keeps a small pool of slots for FCB files (&lt;code&gt;FCBS=&lt;/code&gt; in &lt;code&gt;CONFIG.SYS&lt;/code&gt;), and when the pool is full it quietly closes the oldest one. When the program uses that FCB again, DOS reopens the file from the numbers stored in the FCB. FAT32 cluster numbers need 28 bits. They don&#39;t fit. Making it work would have meant a new FCB layout (breaking every program that knows the old one) or a hidden table inside DOS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The economic side:&lt;/strong&gt; by 1996, FCBs had been obsolete for thirteen years. FAT32 shipped preinstalled on new PCs, for Windows 95 and its programs, which never touched FCBs. FAT32 broke a few other low-level DOS features anyway, so disk utilities needed updates regardless. And FAT32 was optional: anyone with old software could keep a FAT16 partition.&lt;/p&gt;
&lt;p&gt;So it wasn&#39;t an oversight: rewriting an ancient data structure for a handful of DOS 1-era programs just wasn&#39;t worth it, maybe.&lt;/p&gt;
&lt;h2 id=&quot;freedos-does-it-better&quot;&gt;FreeDOS does it better&lt;/h2&gt;
&lt;p&gt;Obviously, I had to know how FreeDOS deals with this, so I tried it. Rogue saved without a hitch on FreeDOS 1.4, on a FAT32 partition - lol.&lt;/p&gt;
&lt;p&gt;FreeDOS is open source, so Claude could check (or hallucinate ☺) why instead of guessing. In the kernel (&lt;code&gt;kernel/fcbfns.c&lt;/code&gt;), the FCB open function hands the filename to &lt;code&gt;DosOpenSft()&lt;/code&gt;, the very same internal routine the modern handle-based open uses. The only thing it writes into the FCB is a small index into DOS&#39;s table of open files. Reads and writes go through the normal file code, too, which knows all about FAT32. There&#39;s no FAT32 check anywhere in the FCB code.&lt;/p&gt;
&lt;p&gt;The difference: MS-DOS stores where the file sits on disk inside the FCB, so it can close and reopen the file behind the program&#39;s back and FAT32 numbers don&#39;t fit in there. In FreeDOS, an FCB is more like a thin wrapper around a normal open file: DOS keeps track of the file in its own table, and the FCB only holds a pointer into that table.&lt;/p&gt;
&lt;h2 id=&quot;is-this-only-rogue-s-problem&quot;&gt;Is this only Rogue&#39;s problem?&lt;/h2&gt;
&lt;p&gt;Nope, any DOS program that uses FCBs for its own files will run into this on a FAT32 partition under MS-DOS 7.1. That mostly means:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;programs from the DOS 1 era, roughly 1981 to 1984, when FCBs were the only way to access files&lt;/li&gt;
&lt;li&gt;early ports from CP/M, which used FCBs natively&lt;/li&gt;
&lt;li&gt;programs built with early compilers whose runtime libraries still used FCBs, as Rogue&#39;s does&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Which DOS versions are affected? MS-DOS 7.0 from the first Windows 95 can&#39;t be, because it doesn&#39;t know FAT32 at all. MS-DOS 7.1 (Windows 95 OSR2 and Windows 98) is, and MS-DOS 8.0 from Windows Me very likely is, too.&lt;/p&gt;
&lt;p&gt;The symptom is a bit sneaky: a game starts and plays fine, because DOS loads programs with modern calls. It only breaks when the program is trying to write to files, e.g. for keeping a list of high scores. If an early-80s game on a Windows 98 machine &quot;can&#39;t save&quot;, FAT32 is the prime suspect. Self-booting games, which skip DOS entirely, and pretty much anything from the late 1980s onward aren&#39;t affected.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do&quot;&gt;What to do?&lt;/h2&gt;
&lt;p&gt;Well, two obvious options: a) use FreeDOS or b) keep a FAT16 partition on MS-DOS. Either way, it&#39;s nice to know the cause. Thanks, Claude!&lt;/p&gt;
&lt;p class=&quot;notice&quot;&gt;Update on 2026-11-15: At least for Rogue, it turns out later versions published by Epyx (1.4x+) don&#39;t use FCBs anymore, but were updated to make use of regular file handle calls, so they work on MS-DOS 7.1 with FAT32 again!&lt;/p&gt;
&lt;p&gt;- Chris&lt;/p&gt;
&lt;p class=&quot;midgrey&quot;&gt;Note: AI was used to help analyze ROGUE.EXE and phrase this article.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Welcome to www.a20gate.net!</title>
        <published>2026-10-21T00:00:00+00:00</published>
        <updated>2026-10-21T00:00:00+00:00</updated>
        
        <author>
          <name>ChrisR3tro</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://www.a20gate.net/blog/welcome-to-www-a20gate-net/"/>
        <id>https://www.a20gate.net/blog/welcome-to-www-a20gate-net/</id>
        
        <content type="html" xml:base="https://www.a20gate.net/blog/welcome-to-www-a20gate-net/">&lt;p&gt;Welcome to A20 Gate, my new homepage! For now, this is where I publish
&lt;a href=&quot;/dos-tools/&quot;&gt;my collection of tools&lt;/a&gt; for MS-DOS and compatible operating systems, and from time to time (hopefully), other things about retro computing.&lt;/p&gt;
&lt;p class=&quot;centered&quot;&gt;&lt;img src=&quot;/img/construction.gif&quot; alt=&quot;Under construction&quot; width=&quot;574&quot; height=&quot;51&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-s-with-the-strange-name&quot;&gt;What&#39;s with the strange name?&lt;/h2&gt;
&lt;p&gt;A20 Gate    n.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A switch on the 21st address line (A20) of IBM PC/AT compatibles that, when closed, makes memory addresses wrap around at 1 MB, just as they did on the 8086. Famously operated through the keyboard controller.&lt;/li&gt;
&lt;li&gt;Anything kept alive long after its purpose has passed, purely so that nothing breaks.&lt;/li&gt;
&lt;li&gt;This website.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Origin: IBM, 1984. Dropped by Intel in the 2010s, outlived by its legend.&lt;br&gt;
See also: &lt;a class=&quot;helplink&quot; href=&quot;https://en.wikipedia.org/wiki/HIMEM.SYS&quot; target=&quot;_blank&quot;&gt;◄HIMEM.SYS►&lt;/a&gt; &lt;a class=&quot;helplink&quot; href=&quot;https://en.wikipedia.org/wiki/High_memory_area&quot; target=&quot;_blank&quot;&gt;◄HMA►&lt;/a&gt; &lt;a class=&quot;helplink&quot; href=&quot;https://en.wikipedia.org/wiki/A20_line&quot; target=&quot;_blank&quot;&gt;◄A20 line►&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;- Chris&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>An evening with Rust for DOS</title>
        <published>2026-10-17T00:00:00+00:00</published>
        <updated>2026-10-17T00:00:00+00:00</updated>
        
        <author>
          <name>ChrisR3tro</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://www.a20gate.net/blog/an-evening-with-rust-for-dos/"/>
        <id>https://www.a20gate.net/blog/an-evening-with-rust-for-dos/</id>
        
        <content type="html" xml:base="https://www.a20gate.net/blog/an-evening-with-rust-for-dos/">&lt;p&gt;Spent this evening compiling Rust for DOS with
&lt;a href=&quot;https://github.com/o8vm/rust_dos&quot; target=&quot;_blank&quot;&gt;rust_dos&lt;/a&gt; by
&lt;a href=&quot;https://x.com/o8_vm&quot; target=&quot;_blank&quot;&gt;@o8_vm&lt;/a&gt;,
using nothing but plain rustc and LLVM. And it really works! I was wondering if I could consider this setup complete enough to use it for real DOS projects. What I found along the way turned out to be a little rabbit hole - just the way I like things. :D&lt;/p&gt;
&lt;h2 id=&quot;getting-it-to-build&quot;&gt;Getting it to build&lt;/h2&gt;
&lt;p&gt;Versions used:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;rustc 1.101.0-nightly (2026-10-16)&lt;/li&gt;
&lt;li&gt;LLVM 23.1.4&lt;/li&gt;
&lt;li&gt;OpenWatcom 1.9 for comparing the generated machine code&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The steps in the README (cargo-binutils and llvm-tools-preview) aren&#39;t quite enough with a current nightly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rustup component add rust-src&lt;/code&gt; is needed too, because the build compiles &lt;code&gt;core&lt;/code&gt; and &lt;code&gt;alloc&lt;/code&gt; from source.&lt;/li&gt;
&lt;li&gt;Current nightlies want &lt;code&gt;json-target-spec = true&lt;/code&gt; under &lt;code&gt;[unstable]&lt;/code&gt; in &lt;code&gt;.cargo/config.toml&lt;/code&gt;. Without it, the build stops with &lt;code&gt;`.json` target specs require -Zjson-target-spec&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rust-toolchain&lt;/code&gt; just says &lt;code&gt;nightly&lt;/code&gt;, so the next nightly can break the build again. Pinning a date, e.g. &lt;code&gt;nightly-2026-10-16&lt;/code&gt;, avoids that.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;some-constraints-to-consider-a-k-a-the-rabbit-hole&quot;&gt;Some constraints to consider (a.k.a. the rabbit hole)&lt;/h2&gt;
&lt;h3 id=&quot;no-exe-but-a-com-file&quot;&gt;No .EXE, but a .COM file!&lt;/h3&gt;
&lt;p&gt;The output is a .COM file: &lt;code&gt;cargo objcopy -- -O binary&lt;/code&gt; strips the ELF file down to raw bytes, with no header, loaded at offset 100h. That means we&#39;re using the tiny memory model: code, data and stack all have to fit into one 64 KB segment. For bigger projects, this could be a real limitation.&lt;/p&gt;
&lt;h3 id=&quot;target-cpu&quot;&gt;Target CPU&lt;/h3&gt;
&lt;p&gt;The target is &lt;code&gt;i586-unknown-none-code16&lt;/code&gt;: 16-bit real-mode code, but built as 32-bit code with &lt;a href=&quot;https://pdos.csail.mit.edu/6.828/2018/readings/i386/s17_01.htm&quot; target=&quot;_blank&quot;&gt;66h/67h prefixes&lt;/a&gt;. So, Rust for DOS needs a 386 or newer. And because the target spec says &lt;code&gt;cpu: pentium&lt;/code&gt;, LLVM may also use 486 and Pentium instructions like &lt;code&gt;bswap&lt;/code&gt; or &lt;code&gt;cmpxchg8b&lt;/code&gt;, so even a real 386 isn&#39;t guaranteed to work.&lt;/p&gt;
&lt;p&gt;The 8086 and 286 are out of the question, though: the lowest x86 CPU that LLVM knows is the 386.&lt;/p&gt;
&lt;p&gt;Still, making it target the 386 should be an easy fix: changing the target spec to &lt;code&gt;cpu: i386&lt;/code&gt; (and dropping the 64-bit atomics) should do. Just stay away from floating point on machines without a 387.&lt;/p&gt;
&lt;h2 id=&quot;why-32-bit-registers-in-real-mode&quot;&gt;Why 32-bit registers in real mode?&lt;/h2&gt;
&lt;p&gt;LLVM can&#39;t generate real 16-bit x86 code. The &lt;code&gt;code16&lt;/code&gt; target just encodes its normal 32-bit instructions for a 16-bit segment, and adds a 66h or 67h prefix wherever an instruction uses 32-bit operands or addresses.&lt;/p&gt;
&lt;p&gt;With some help from AI, I disassembled the output (&lt;code&gt;cargo objdump&lt;/code&gt;, built with &lt;code&gt;opt-level = &quot;z&quot;&lt;/code&gt;) to see where the prefixes show up:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Code&lt;/th&gt;&lt;th&gt;Prefixes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;u8&lt;/code&gt;/&lt;code&gt;u16&lt;/code&gt; arithmetic&lt;/td&gt;&lt;td&gt;none&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;u32&lt;/code&gt;/&lt;code&gt;i32&lt;/code&gt;/&lt;code&gt;usize&lt;/code&gt;&lt;/td&gt;&lt;td&gt;66h&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Memory access via &lt;code&gt;[esp]&lt;/code&gt;/&lt;code&gt;[eax]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;67h&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;call&lt;/code&gt;/&lt;code&gt;ret&lt;/code&gt;, stack args, pointers&lt;/td&gt;&lt;td&gt;66h, always&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;So 16-bit types only help partly: loops over slices stay 32-bit, because pointers and &lt;code&gt;usize&lt;/code&gt; are 32-bit.&lt;/p&gt;
&lt;p&gt;A 66h prefix costs one byte and about one cycle on a 486, a bit more on the Pentium and P6 (lost instruction pairing, decoder stalls). For real 32-bit math that&#39;s a net gain; for values that fit in 16 bits it&#39;s pure overhead.&lt;/p&gt;
&lt;p&gt;For comparison, OpenWatcom&#39;s 16-bit compiler produces identical code for &lt;code&gt;-0&lt;/code&gt;, &lt;code&gt;-3&lt;/code&gt; and &lt;code&gt;-6&lt;/code&gt; (8086, 386 and Pentium Pro), without any 32-bit registers. A &lt;code&gt;long&lt;/code&gt; lives in a register pair (DX:AX), shifts loop one bit at a time, and a multiplication calls a helper that does three 16-bit &lt;code&gt;mul&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;The AI&#39;s guess (not measured yet): Rust wins at 32-bit math like CRCs, OpenWatcom wins with plain 16-bit code, memory models beyond 64 KB and 8086 support.&lt;/p&gt;
&lt;h2 id=&quot;the-rust-dos-library&quot;&gt;The Rust DOS library&lt;/h2&gt;
&lt;p&gt;The DOS layer is bare-bones, but works! Two things that are noteworthy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;println!&lt;/code&gt; makes one &lt;code&gt;int 21h&lt;/code&gt; call (function 02h) per character. DOS function 40h could write the whole buffer with one call - here lies some potential for optimization.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;asm!&lt;/code&gt; block in &lt;code&gt;printc&lt;/code&gt; doesn&#39;t tell the compiler that function 02h overwrites &lt;code&gt;AL&lt;/code&gt;. Adding &lt;code&gt;lateout(&quot;al&quot;) _&lt;/code&gt; would fix that.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The compiler side is solid, and rust_dos is a working demo: tiny model only, 386+ and a basic DOS &quot;API&quot;.
This should be very good for learning and small tools. For bigger DOS projects, I&#39;d still go with OpenWatcom for now.
But I bet someone could use this very cool foundational work by @o8_vm and build a fully fledged toolchain out of it.&lt;/p&gt;
&lt;p&gt;Great fun to see Rust running on DOS!&lt;/p&gt;
&lt;p&gt;- Chris&lt;/p&gt;
&lt;p class=&quot;midgrey&quot;&gt;Note: AI was used to help analyze Rust for DOS and in phrasing this article.&lt;/p&gt;
</content>
        
    </entry>
</feed>
