An open-source custom python-to-assembly compiler pipeline built to design lightweight, bare-metal operating systems using pythonic syntax.
The project parses standard Python code into an Abstract Syntax Tree (AST), maps pythonic constructs straight into hardware operations, emits pure x86 16-bit Assembly (NASM), and packs everything into a bootable disk image (os.img).
-
Zero-Dependency OS Source: Code natively in Python without functions or boilerplates (
os.py). -
Built-in VGA Graphics Support: Seamlessly call
draw_to(x, y, matrix)natively from your code to draw pixel bitmaps using VGA Mode 13h ($320 \times 200$ , 256 colors). - Native Terminal Emulation: Run a simulated pass of your OS directly inside your shell environment before compilation.
- Low-Level Native Output: Compiles directly down to raw flat binaries combined with a customized 512-byte Master Boot Record (MBR) bootloader.
Before compiling or simulating the OS, make sure you have the following requirements installed:
- Python 3.x
- NASM (Netwide Assembler) added to your system's PATH variable.
- QEMU (Optional, recommended for emulation/testing).
βββ bootloader.asm # Initial 512-byte MBR bootloader (Real Mode 16-bit)
βββ compiler.py # Core AST Compiler & toolchain driver
βββ os.py # Your custom OS code written in Python
βββ README.md # Project documentation
π Getting Started
1. Write your OS code (os.py)
Create your source script. You can use standard prints and the compiler's native draw_to() graphical matrix handler:
Python
print("Booting Custom OS into VGA Graphics Mode...")
# An 8x8 Space Invader sprite matrix
invader = [
"00100100",
"00011000",
"01111110",
"11011011",
"11111111",
"00111100",
"01000010",
"10000001"
]
# Draws the matrix natively at X=20, Y=30 (1=White, 0=Black)
draw_to(20, 30, invader)
print("Render successful.")
2. Verify with Local Simulation
Test your logic layout inside your desktop console workspace before running the compiler pipeline:
Bash
python compiler.py --run
3. Compile to Bare-Metal Binary
Run the compiler toolchain to translate os.py to x86 Assembly (kernel.asm), compile it via NASM, and merge it sequentially behind your compiled bootloader block:
Bash
python compiler.py
This generates a final bootable image named os.img.
π» Running the Operating System
To execute your newly baked OS image in the QEMU PC emulator, run:
Bash
qemu-system-x86_64 -drive format=raw,file=os.img
Alternatively, you can write the os.img file to a physical USB drive using tools like dd or Rufus to boot it live on raw x86 hardware.
βοΈ How it Works under the Hood
AST Extraction: The compiler reads os.py using Python's native ast library to break statements down structurally without evaluating them.
Graphics Memory Mapping: When encountering draw_to, the compiler flattens the multidimensional array and pushes raw byte states directly into the hardware-mapped VGA buffer location (0xA000:0000).
Linear Assembly Stitching: The engine sets the target origin offset segment to 0x1000, processes instructions linearly, appends custom low-level TTY string loops, and logs out raw binary opcodes.
π License
This project is open-source and available under the MIT License.