Skip to main content

Write x86 operating systems in Python — one instruction at a time

Project description

osmk

Write x86 operating systems in Python — one instruction at a time.

Every function call maps to an x86 assembly instruction. build() assembles it into a bootable 512-byte disk image.

Install

pip install osmk

You also need NASM (the assembler) and optionally QEMU (to run your OS):

# Ubuntu/Debian
sudo apt install nasm qemu-system-x86

# macOS
brew install nasm qemu

Quick Start

import osmk
from osmk import ax, bx

osmk.start("hello.img")    # begin a new OS

osmk.ax(5)                 # mov ax, 5
osmk.bx(5)                 # mov bx, 5
osmk.add(ax, bx)           # add ax, bx  →  ax = 0x000A
osmk.writehex()             # print AX as hex → "000A"

osmk.build()                # assemble → hello.img (512 bytes)

Run it:

qemu-system-i386 -drive format=raw,file=hello.img

API Reference

Lifecycle

Function Description
start(filename, bits=16, org=0x7C00) Begin a new OS program
build() Assemble and create the bootable image
dump_asm() Return the generated NASM source (for debugging)
run(memory=128) Build and launch in QEMU

Register Setters

Every register is callable. osmk.ax(5)mov ax, 5.

osmk.ax(5)          # mov ax, 5
osmk.bx(ax)         # mov bx, ax
osmk.al(0x41)       # mov al, 0x41
osmk.eax(0x1000)    # mov eax, 0x1000

Available: ax bx cx dx si di sp bp · al ah bl bh cl ch dl dh · eax ebx ecx edx esi edi esp ebp

Instructions

# Arithmetic
osmk.add(ax, bx)       osmk.sub(ax, 1)
osmk.mul(bx)            osmk.div(cx)
osmk.inc(ax)            osmk.dec(cx)
osmk.neg(ax)

# Bitwise
osmk.and_(ax, 0xFF)    osmk.or_(ax, bx)
osmk.xor(ax, ax)       osmk.not_(ax)
osmk.shl(ax, 4)        osmk.shr(ax, 1)

# Compare & Jump
osmk.cmp(ax, 0)
osmk.je("done")        osmk.jne("loop")
osmk.jl("less")        osmk.jg("greater")
osmk.jz("zero")        osmk.jnz("nonzero")
osmk.jmp("start")      osmk.jc("carry")

# Stack
osmk.push(ax)           osmk.pop(bx)

# Subroutines
osmk.call("myfunc")     osmk.ret()

# Interrupts
osmk.interrupt(0x10)    # int 0x10
osmk.cli()              osmk.sti()
osmk.hlt()

# Labels
osmk.label("loop")

Output Helpers

osmk.writehex()         # print AX as 4-digit hex (e.g. "000A")
osmk.writechar("A")     # print character 'A'
osmk.writechar()        # print whatever's in AL
osmk.prints("Hello!")   # print a string
osmk.newline()          # print CR+LF
osmk.readchar()         # wait for keypress → AL
osmk.halt()             # halt CPU (cli + hlt loop)

Custom / Escape Hatch

When osmk doesn't cover what you need:

# Inject raw NASM assembly
osmk.asm("mov ah, 0x0E")
osmk.asm("""
    mov ah, 0x00
    mov al, 0x03
    int 0x10
""")

# Inject raw bytes
osmk.raw(b"\x90\x90\x90")   # 3x NOP

# Define data
osmk.data("my_var", "dw 0x1234")

# Define a subroutine
with osmk.function("beep") as fn:
    fn.asm("mov ah, 0x0E")
    fn.asm("mov al, 0x07")  # BEL character
    fn.asm("int 0x10")

osmk.call("beep")

# Add comments
osmk.comment("this sets up video mode")

Explicit MOV

osmk.mov(ax, 0x0E)     # same as osmk.ax(0x0E)
osmk.mov(bx, ax)       # same as osmk.bx(ax)

Examples

Hello World

import osmk

osmk.start("hello.img")
osmk.prints("Hello, World!")
osmk.newline()
osmk.prints("My first OS!")
osmk.halt()
osmk.build()

Keyboard Echo

import osmk
from osmk import cx

osmk.start("echo.img")
osmk.prints("Type anything:")
osmk.newline()

osmk.label("loop")
osmk.readchar()        # key → AL
osmk.writechar()       # echo it
osmk.jmp("loop")

osmk.build()

Counter

import osmk
from osmk import ax, cx

osmk.start("counter.img")

osmk.ax(0)
osmk.cx(16)

osmk.label("count")
osmk.writehex()
osmk.prints(" ")
osmk.inc(ax)
osmk.dec(cx)
osmk.jnz("count")

osmk.halt()
osmk.build()

How It Works

  1. Each osmk.* call appends an x86 assembly instruction to an internal list
  2. build() wraps them in a bootloader template (16-bit real mode, MBR)
  3. NASM assembles it into a flat binary
  4. The binary is exactly 512 bytes with the 0xAA55 boot signature

The generated code runs in 16-bit real mode using BIOS interrupts for I/O. Use dump_asm() to see exactly what assembly your Python generates.

Resources

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osmk-0.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osmk-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file osmk-0.1.0.tar.gz.

File metadata

  • Download URL: osmk-0.1.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for osmk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e80a5cf62b50571f3a3bece017a7a6fd8ad8ff45da4d1eac8d56a452927f9cc6
MD5 4be8c83d85399119ab2c061cbed84fcc
BLAKE2b-256 fb6c13f45e6de1c99b2b3f755589da9b9980a632dc5baa9994672282e405c4f0

See more details on using hashes here.

File details

Details for the file osmk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: osmk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for osmk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58a7f1a6874651523d52f198ceb27eeac89fba7f90d398c958a5aaa5a6b30fc5
MD5 9c8565669bb41164de27dc31b5892f7f
BLAKE2b-256 2a42317119c39d7d1b983a8a6c2de0202c858f9774977edab1dbf2229c93ae3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page