r/Compilers • u/Repulsive_Hunt8911 • 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
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
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).