CNC Subprograms & Macros (M98, M99, Variables): Advanced Programming Techniques
Subprograms and macros transform CNC programming from simple part machining into parametric, reusable, and automated processes.
This guide covers M98 (call subprogram), M99 (return/loop), and variables (#100, #500, etc.) with real examples.
📌 1. What are Subprograms?
A subprogram is a reusable block of code stored in the CNC memory.
- Call with M98 Pxxxx
- End with M99
📌 2. Simple Fanuc Subprogram Example
%
O4000 (MAIN PROGRAM)
G90 G17 G21
M98 P5000 L6 (CALL SUBPROGRAM O5000, REPEAT 6 TIMES)
M30
O5000 (SUBPROGRAM)
G81 X[#500+10*#1] Y0 Z-15 R2 F120
G80
M99
%
- M98 P5000 → calls sub O5000
- L6 → repeat 6 times
- M99 → return to main
📌 3. Haas Subprogram Example
Haas uses the same M98/M99 logic:
O0100
M98 P0200 L4
M30
O0200
G83 Z-20. R2. Q5. F150
M99
📌 4. Siemens Subprogram Equivalent
CALL PGM="DRILL_SUB"
Subprograms stored as separate files.
📌 5. Heidenhain Equivalent
CALL LBL 1 REP 4
LBL 1
CYCL DEF 200 DRILLING
LBL 0
📌 6. Macro Variables — Fanuc / Haas
- Local Variables: #1–#33 (only inside program)
- Common Variables: #100–#199 (persistent until power off)
- Permanent Variables: #500–#999 (stay after power off)
Example: Parametric Drilling Pattern
#100=0
WHILE[#100LT6]DO1
G81 X[#100*20] Y0 Z-15 R2 F150
#100=#100+1
END1
G80
This drills 6 holes spaced 20 mm apart automatically.
📌 7. Conditional Logic with Macros
IF[#100GT50]THEN #3000=1 (TOOL TOO LARGE – ALARM)
- #3000=1 → custom alarm message on Fanuc
- Enables error-proof programming
📌 8. Looping with M99
O6000
#100=0
N10
G81 X[#100*10] Y0 Z-15 R2 F120
#100=#100+1
IF[#100LT5]GOTO10
M30
This drills 5 holes using a loop and M99 return.
📌 9. Best Practices for Subprograms & Macros
- Keep subprograms modular and reusable
- Comment variable functions clearly
- Test macros at reduced feed (F50%) first
- Use custom alarms (#3000) for operator guidance
- Store probing, hole patterns, and repeat cycles in subprogram libraries
📌 10. Future of Macro Programming (2025–2030)
- AI-enhanced macros — automatic feed/speed adjustment
- Smart libraries — share macros across multiple machines via cloud
- Self-learning macros — adjust based on part quality feedback
✅ Conclusion
Subprograms and macros (M98, M99, variables) give CNC programmers unmatched flexibility.
By mastering parametric programming, you reduce code size, automate repetitive tasks, and improve shop-wide efficiency.
Leave a comment