M98 and M99 are two of the most powerful and misunderstood commands in all CNC programming. They control subprogram calls, looping logic, automated machining sequences, pattern repetition, multi-fixture production, and parametric manufacturing. As 2025–2026 automation requirements continue to push CNC machines toward lights-out production, mastering M98/M99 has become a core skill for high-income CNC programmers.
This guide is a complete deep-dive into how modern Fanuc, Haas, Siemens-compatible systems execute subprograms, how stack processing works, how to avoid infinite loops, and how to design industrial-grade reusable G-code routines.
1. What M98 Actually Does
M98 calls another program or repeats a programmed block multiple times.
Format:
- M98 Pxxxx → Calls program number Oxxxx
- M98 Pxxxx L# → Calls program repeatedly (L times)
Example:
M98 P2000
Calls subprogram O2000 once.
Example (loop 5 times):
M98 P2000 L5
This is widely used for:
- Hole patterns
- Repeated toolpaths
- Multi-part machining on a fixture plate
- Automation sequences
- Pattern copying without CAM
2. What M99 Actually Does
M99 returns from a subprogram to its calling program.
Location matters:
- Inside a subprogram → return
- Inside a main program (Oxxxx) → loop back to top (infinite loop unless conditional control is used)
Example inside O2000:
M99
Returns to the main program line after M98.
3. Professional Example — Drilling 10 Holes Using a Subprogram
Main program:
O1000
T4 M06
G90 G54
M98 P2000 L10
M30
Subprogram O2000:
O2000
G81 X#100 Y#101 Z-20. R2. F180
100 = #100 + 20
M99
This creates an incremental drilling pattern that updates itself using macro variables.
4. Professional Example — Multi-Fixture Automated Production
Fixtured parts:
- Part 1 → G54
- Part 2 → G55
- Part 3 → G56
O1000
M98 P3000 (Run OP10 sequence for G54)
M98 P3000 (Run OP10 sequence for G55)
M98 P3000 (Run OP10 sequence for G56)
M30
Subprogram O3000:
O3000
G54 (Or call #5221/#5222 dynamically)
G90 G00 X0 Y0
G71 … (Rough)
G70 … (Finish)
M99
This ensures 100% consistent machining on every fixture position.
5. Advanced Example — M98 With Parametric Control (AI-Driven CNC Logic)
Create dynamic loops based on variable conditions:
500 = 0
WHILE[#500 LT 6] DO1
M98 P4000
#500 = #500 + 1
END1
This is fully compatible with:
- Adaptive machining
- Automated probing workflows
- Part-presence detection
- Lights-out manufacturing
6. Example — Safe Infinite Loop (Industry 2025 Style)
For automated pallet systems:
O9000
(Waiting loop)
IF[#100 EQ 1] GOTO100
M99
N100 (Continue when pallet is loaded)
Machine remains safely idle until sensor variable updates.
7. Avoiding the #1 Danger: Uncontrolled Infinite Loops
Bad code:
M99 (in main body with no logic)
→ machine loops forever
→ cannot stop unless reset
→ extremely dangerous in automation cells
Correct safe method:
IF[#5021 GT 0] M30
M99
Logic prevents unexpected behavior.
8. Real Industrial Example — Subprogram for Chamfering All Edges
O1000
M98 P7777 L12
M30
Subprogram:
O7777
G01 X#100 Y#101 Z-1. F250
G01 X[#100+2] Y[#101+2]
M99
Simple, reusable, extremely powerful.
9. M98 for Machine-Independent Modular Programming
Large OEM manufacturers use M98/M99 to create:
- Standard OP10, OP20, OP30 routines
- Family-of-parts machining templates
- “Plug-and-play” CAM-free solutions
- Highly maintained enterprise G-code libraries
Instead of duplicating code across 40+ machines, only one subprogram is updated.
10. Subprogram Stack Depth Visualization (Important!)
Fanuc systems maintain a stack:
Main program
→ M98 → Sub1
→ M98 → Sub2
→ M98 → Sub3
M99 returns to Sub2
M99 returns to Sub1
M99 returns to Main
Maximum nesting is typically 4–10 levels depending on control.
11. Summary
M98 and M99 enable:
- Scalable CNC automation
- Intelligent multi-fixture workflows
- Reduced CAM dependency
- Industry-ready reusable G-code
- Fast production cycle deployment
- Clean, modular manufacturing software
Mastering subprogram architecture is strategically one of the most profitable CNC programming skills in 2025–2026.
Leave a comment