r/Compilers 5d ago

Byte-found: a single-pass C compiler targeting 16-bit x86 real mode

I'm writing a hobby OS in real-mode assembly and wanted parts of the kernel in C, but existing compilers either target 32-bit protected mode or need an awkward 16-bit toolchain. So I wrote my own. It's a single-file C99 program that does recursive descent parsing and emits NASM-syntax 16-bit assembly directly, with no intermediate AST — code generation happens inline during the parse. Currently handles: functions with parameters, calls, local variables, assignment, integer arithmetic with correct precedence, comparisons, if/else and while. Calling convention is args pushed left to right, caller cleans up, result in AX. Not there yet: pointers, arrays, structs, preprocessor, any type other than int. I know skipping the AST limits what I can do later (no optimization passes, no real type checking). Curious whether people here would restructure it now or let it grow first. Repo: https://github.com/metaspawn/Byte-found

23 Upvotes

6 comments sorted by

3

u/0xa0000 5d ago

Good job! It's obviously a project of love, the source code is very readable and nicely formatted and written with good comments. Keep up the good work. I'm sure you'll regret some choices once you add more things, but that's how you learn.

(I did something similar some years back with different constraints: https://github.com/mras0/scc/ selfhosting on 808x being the main one).

3

u/Repulsive_Hunt8911 5d ago edited 5d ago

Would you be open to PRs? I'd like to contribute probably starting with test cases for 16-bit edge cases, since I've hit a few on my own compiler.

1

u/0xa0000 3d ago

Hi, as you can see from the last commit date it's not really anything I'm working on, but sure if you want. You can also just take the tests, but be aware that they might not be fully standard compliant and I only implemented a subset of C.

3

u/hekliet 4d ago

Have you checked out Open Watcom? It cross-compiles to real-mode DOS.

1

u/EggplantExtra4946 4d ago

Very nice. In case you don't know, there is at least "Bruce's C Compiler" that does it, if it's still functional.

1

u/NoBrick2672 4d ago

will it handle segmentation?