VM Execution & Step Limits

Understanding how the Romasm VM executes programs and why step limits exist

What is a "Step"?

A step is the execution of a single Romasm instruction by the Virtual Machine.

Example:

LOAD R0, 10 ; Step 1: Load instruction ADD R0, R1 ; Step 2: Add instruction PRINT R0 ; Step 3: Print instruction

This program takes 3 steps to execute.

Each instruction type takes exactly 1 step, regardless of complexity:

  • LOAD R0, 10 = 1 step
  • ADD R0, R1 = 1 step
  • CALL sin = 1 step (but the sin function itself takes many steps)
  • JMP loop = 1 step

Why Do We Have Step Limits?

Safety First!

Step limits prevent infinite loops from freezing your browser.

Without limits, a buggy program like this would run forever:

loop: PRINT R0 JMP loop ; Infinite loop - never stops!

This would freeze your browser tab, requiring you to force-close it.

Default Limits:

  • Regular scripts: 10,000 steps
  • Plotting scripts: 50,000 steps
  • Plotting with trig functions: 200,000 steps

How Many Steps Do Programs Take?

Simple Programs:

  • Basic arithmetic: 5-10 steps
  • Factorial of 5: ~50 steps
  • Fibonacci sequence (10 numbers): ~100 steps

Complex Programs:

  • Sine function call: ~100-200 steps (includes reduction loops, Taylor series)
  • Plotting sine wave (41 points): ~8,000-16,000 steps
  • Large loops with function calls: Can easily exceed 10,000 steps

Why Plotting Takes So Many Steps:

When plotting a sine wave:

  1. Loop runs 41 iterations (one per point)
  2. Each iteration calls CALL sin
  3. Each sin call does:
    • Angle reduction (multiple loops)
    • Taylor series calculation (many operations)
    • Scaling and conversion
  4. Each iteration also does PRINT (output coordinates)

Total: 41 × ~200 steps = ~8,200 steps minimum

With overhead, it can easily reach 15,000+ steps!

Chunked Execution

For plotting scripts, Romasm uses chunked execution:

  • Executes in chunks of 5,000-10,000 steps
  • Yields to the browser between chunks (prevents freezing)
  • Allows UI updates during long-running scripts
  • Total limit: 100,000-200,000 steps for plotting

How It Works:

  1. Execute 5,000 steps
  2. Yield to browser (allows UI updates)
  3. Continue with next 5,000 steps
  4. Repeat until program completes or limit reached

What Happens When the Limit is Reached?

If a program exceeds the step limit:

  • Execution stops immediately
  • A warning message is displayed
  • Any output generated so far is shown
  • The browser remains responsive (doesn't freeze)

Example Warning:

Warning: Maximum steps exceeded Final Registers: RII: 1000 RIV: 500

This means the program was stopped at the limit, but you can see what it computed so far.

Optimizing Your Programs

To reduce step counts:

  • Use larger step sizes in loops (fewer iterations)
  • Avoid nested loops when possible
  • Simplify calculations (use approximations for plotting)
  • Break large programs into smaller functions

Example: Optimized Sine Wave Plotting

Before: Step size 20 (0.2 units) = 101 iterations = ~20,000 steps

After: Step size 50 (0.5 units) = 41 iterations = ~8,000 steps

Still smooth enough for visualization, but much faster!

Related Documentation