A C Compiler that fits in the 512 byte boot sector of an x86 machine
A C Compiler that fits in the 512 byte boot sector of an x86 machine
SectorC is a C compiler written in x86-16 assembly that fits within the 512 byte boot sector of an x86 machine. It supports a subset of C that is large enough to write real and interesting programs. It is quite likely the smallest C compiler ever written.
In a base64 encoding, it looks like this:
…A fairly large subset is supported: global variables, functions, if statements, while statements, lots of operators, pointer dereference, inline machine-code, comments, etc. All of these features make it quite capable.
For example, the following program animates a moving sine-wave:
…A few examples are provided that leverage the unique hardware aspects of the x86-16 IBM PC:
examples/hello.c: Print a text greeting on the screen writing to memory at 0xB8000examples/sinwave.c: Draw a moving sine wave animation with VGA Mode 0x13 using an appropriately bad approximation of sin(x)examples/twinkle.c: Play “Twinkle Twinkle Little Star” through the PC Speaker (Warning: LOUD)The following grammar is accepted and compiled by sectorc:
…In addition, both // comment and /* multi-line comment */ styles are supported.
(NOTE: This grammar is 704 bytes in ascii, 38% larger than its implementation!)
See blog post: SectorC: A C Compiler in 512 bytes
In 2020, cesarblum wrote a Forth that fits in a bootsector: (sectorforth)
In 2021, jart et. al. wrote a Lisp that fits in the bootsector: (sectorlisp)
Naturally, C always needs to come and crash (literally) every low-level systems party regardless of whether it was even invited.
Dependencies:
nasm for assembling (I used v2.16.01)qemu-system-i386 for emulating x86-16 (I used v8.0.0)Build: ./build.sh
Run: ./run.sh your_source.c
NOTE: Tested only on a MacBook M1
Probably Nothing.
Or at least that's what I thought when starting out. But, I didn't think I'd get such a feature set. Now, I'd say that it might be useful for someone that wants to explore x86-16 bios functions and machine model w/o having to learn lots of x86 assembly first. But, then again, you should just use a proper C compiler and write a tiny bootloader to execute it.
No open issues yet, or sync has not completed.