← All lessons

Build Small Programs · Lesson 30 of 30 · about 20 minutes

Your Own Mini Operating System

Dispatch queued jobs to reusable services and record their results.

01 / Explain

Understand the idea

An operating system coordinates resources and provides services to programs. Our final project is a deliberately tiny dispatcher, not a complete modern OS: it consumes a queue of jobs, selects a service, calls it, and records results.

Each job has an operation code and a value. Code 0 doubles, code 1 squares, and code 2 adds one. The output keeps the full integer result, while the memory result log stores a byte. Use the systems lab to investigate scheduling, separate core state, and event handling around the same ideas.

02 / Try

Watch it happen

The two service results are 8 and 9. The challenge adds a queue, dispatch branches, and a third service.

LOAD 4
CALL double
STORE 64
PRINT
LOAD 3
CALL square
STORE 65
PRINT
HALT
double: MUL 2
RETURN
square: MUL R0
RETURN

Use Step to follow one instruction at a time. You can change the example and replay it.

03 / Challenge

Make it work

Read N (0–3), then N pairs (operation, value). Operation 0 doubles, 1 squares, and 2 adds one. Dispatch through CALL subroutines. Print each full result and store its byte at addresses 64, 65, … in job order.

The checker runs your current editor program in a fresh machine for each of 4 test cases. It supplies inputs and seeded memory itself; the lab’s current output and memory do not decide your result.

INPUT
COPY R0 R3
LOAD 64 R2
LOOP R3
INPUT
COPY R0 R4
INPUT
COPY R0 R1
# Choose and call a service, then store and print its result.
RETURN
HALT
double: MUL 2
RETURN
square: MUL R0
RETURN
increment: ADD 1
RETURN
Need a hint?

Save the job value before loading the operation code for CMP. Restore the value immediately before calling the chosen service.

Reveal a worked solution

Read the program, predict each instruction’s effect, then step through it in the lab.

INPUT
COPY R0 R3
LOAD 64 R2
LOOP R3
INPUT
COPY R0 R4
INPUT
COPY R0 R1
LOAD R4
CMP 0
JUMP_IF_ZERO doubleJob
CMP 1
JUMP_IF_ZERO squareJob
LOAD R1
CALL increment
JUMP finish
doubleJob: LOAD R1
CALL double
JUMP finish
squareJob: LOAD R1
CALL square
finish: STORE R2
PRINT
LOAD R2
ADD 1
COPY R0 R2
RETURN
HALT
double: MUL 2
RETURN
square: MUL R0
RETURN
increment: ADD 1
RETURN

Emoji CPU lab

Ready · 0 cycles

Emoji program

Type LOAD, ADD, or another opcode then Space to insert emoji. Ctrl/⌘ + Enter runs or pauses; Escape pauses; Ctrl/⌘ + ] indents. Tab moves focus. Labels use a colon. Jumps use zero-based instruction addresses.

Instruction map and breakpoints (0)

Breakpoints stop before an instruction. Run resumes past the stopped breakpoint once; Step executes it directly. Editing source clears old breakpoints and machine state.

    CPU registers

    R0
    0
    R1
    0
    R2
    0
    R3
    0
    R4
    0
    R5
    0
    R6
    0
    R7
    0
    Program counter (PC)
    0
    Stack pointer (SP)
    255
    Flags
    Zero: off · Negative: off · Overflow: off
    Delay request
    0 ms
    Stacks and loop frames

    SP = 255 − data depth − call depth. The stack is separate from memory.

    Data stack (bottom → top)

    Empty

    Call return addresses (bottom → top)

    Empty

    Loop frames

    Empty

    Output and input

    Run a PRINT instruction to see output.

    Queued input: Empty

    Memory · 256 bytes · 0 nonzero

    Each cell shows address:value. R = read this step; W = written this step. Select a cell to inspect or initialize it before execution. Use arrow keys to move, Home/End for the row, and Ctrl/⌘ + Home/End for the whole memory.

    Reset before initializing memory. Stored values wrap to 0–255.
    Execution trace · 0 entries

    Recent entries below. Inspect any zero-based index to see complete detached before/after state.

    Check your challenge

    You can run this check any time. Every case must pass to record completion.

    Progress uses localStorage only. It stays on this browser and is never sent to a server.