An evening with Rust for DOS
Published: 2026-10-17
Spent this evening compiling Rust for DOS with rust_dos by @o8_vm, 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
Getting it to build
Versions used:
- rustc 1.101.0-nightly (2026-10-16)
- LLVM 23.1.4
- OpenWatcom 1.9 for comparing the generated machine code
The steps in the README (cargo-binutils and llvm-tools-preview) aren't quite enough with a current nightly:
rustup component add rust-srcis needed too, because the build compilescoreandallocfrom source.- Current nightlies want
json-target-spec = trueunder[unstable]in.cargo/config.toml. Without it, the build stops with`.json` target specs require -Zjson-target-spec. rust-toolchainjust saysnightly, so the next nightly can break the build again. Pinning a date, e.g.nightly-2026-10-16, avoids that.
Some constraints to consider (a.k.a. the rabbit hole)
No .EXE, but a .COM file!
The output is a .COM file: cargo objcopy -- -O binary strips the ELF file down to raw bytes, with no header, loaded at offset 100h. That means we'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.
Target CPU
The target is i586-unknown-none-code16: 16-bit real-mode code, but built as 32-bit code with 66h/67h prefixes. So, Rust for DOS needs a 386 or newer. And because the target spec says cpu: pentium, LLVM may also use 486 and Pentium instructions like bswap or cmpxchg8b, so even a real 386 isn't guaranteed to work.
The 8086 and 286 are out of the question, though: the lowest x86 CPU that LLVM knows is the 386.
Still, making it target the 386 should be an easy fix: changing the target spec to cpu: i386 (and dropping the 64-bit atomics) should do. Just stay away from floating point on machines without a 387.
Why 32-bit registers in real mode?
LLVM can't generate real 16-bit x86 code. The code16 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.
With some help from AI, I disassembled the output (cargo objdump, built with opt-level = "z") to see where the prefixes show up:
| Code | Prefixes |
|---|---|
u8/u16 arithmetic | none |
u32/i32/usize | 66h |
Memory access via [esp]/[eax] | 67h |
call/ret, stack args, pointers | 66h, always |
So 16-bit types only help partly: loops over slices stay 32-bit, because pointers and usize are 32-bit.
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's a net gain; for values that fit in 16 bits it's pure overhead.
For comparison, OpenWatcom's 16-bit compiler produces identical code for -0, -3 and -6 (8086, 386 and Pentium Pro), without any 32-bit registers. A long 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 muls.
The AI'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.
The Rust DOS library
The DOS layer is bare-bones, but works! Two things that are noteworthy:
println!makes oneint 21hcall (function 02h) per character. DOS function 40h could write the whole buffer with one call - here lies some potential for optimization.- The
asm!block inprintcdoesn't tell the compiler that function 02h overwritesAL. Addinglateout("al") _would fix that.
Conclusion
The compiler side is solid, and rust_dos is a working demo: tiny model only, 386+ and a basic DOS "API". This should be very good for learning and small tools. For bigger DOS projects, I'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.
Great fun to see Rust running on DOS!
- Chris
Note: AI was used to help analyze Rust for DOS and in phrasing this article.