Skip to content

jbrahy/OSX-GW-BASIC

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OSX-GW-BASIC

A modern C implementation of Microsoft's GW-BASIC interpreter (1983), converted from the original x86 assembly language source code. This project serves as both a functional BASIC interpreter and an educational resource for understanding assembly-to-C conversion principles.

🎯 Purpose

This project demonstrates how to systematically convert legacy x86 assembly code to portable, maintainable C code while preserving original functionality. It's designed for:

  • Students learning interpreter implementation
  • Developers working with legacy codebases
  • Educators teaching assembly-to-C conversion
  • Retro computing enthusiasts exploring 1980s programming languages

πŸ—οΈ What's Inside

Core Implementation (gwbasic-c/)

A complete BASIC interpreter written in C with comprehensive educational documentation:

  • Tokenizer - Lexical analysis of BASIC source code
  • Parser - Syntactic analysis and program structure
  • Evaluator - Expression evaluation with operator precedence
  • Interpreter - Main execution engine with control flow
  • Statements - BASIC statement implementations (PRINT, FOR, IF, etc.)
  • Utilities - Type system and memory management

Original Source (/)

The complete original Microsoft GW-BASIC assembly source code (1983) for reference and comparison.

Sample Programs (gwbasic-c/gw-basic-programs/)

Educational BASIC programs demonstrating interpreter features:

  • fibonacci.bas - Fibonacci sequence generation
  • math.bas - Arithmetic operations
  • loops.bas - FOR/NEXT loop constructs
  • And more...

πŸš€ Quick Start

Prerequisites

  • macOS with Xcode command line tools
  • GCC or Clang compiler
  • Make

Building

cd gwbasic-c
make

Running Programs

# Run a sample program
./gwbasic gw-basic-programs/fibonacci.bas

# Interactive mode
./gwbasic
Ok
10 PRINT "Hello, World!"
20 END
RUN

Testing

# Test all sample programs
for file in gw-basic-programs/*.bas; do
    echo "Testing $file"
    ./gwbasic "$file"
done

πŸ“š Educational Features

Assembly-to-C Conversion Guide

This implementation demonstrates key principles for converting x86 assembly to C:

  1. Data Structure Evolution

    • ASM: Raw memory segments, manual offset calculations
    • C: Structured types with automatic memory layout
  2. Control Flow Translation

    • ASM: JMP/CALL instructions with manual stack management
    • C: Structured functions with automatic stack handling
  3. Memory Management Modernization

    • ASM: Manual allocation with no safety checks
    • C: Structured malloc/free with ownership rules

Original ASM Mapping

Every C module maps directly to original assembly files:

Original ASM C Implementation Purpose
GWMAIN.asm main.c, interpreter.c Main execution loop
GWEVAL.asm evaluator.c Expression evaluation
GWSTS.asm statements.c Statement execution
GWDATA.asm utilities.c Data type handling
BINTRP.h gwbasic.h Core data structures

Memory Safety Patterns

The implementation showcases critical C memory management patterns:

  • Deep vs. Shallow Copying - Preventing double-free errors
  • Resource Ownership - Clear memory lifecycle management
  • Defensive Programming - NULL checks and bounds validation
  • Structured Cleanup - Systematic resource deallocation

πŸ”§ Language Features

Implemented BASIC Statements

  • PRINT - Output with formatting control
  • LET - Variable assignment
  • FOR/NEXT - Loop constructs with STEP support
  • IF/THEN - Conditional execution
  • GOTO/GOSUB/RETURN - Control flow statements
  • END - Program termination

Data Types

  • INTEGER - 32-bit signed integers
  • SINGLE - 32-bit floating point
  • DOUBLE - 64-bit floating point
  • STRING - Variable-length character strings

Interactive Commands

  • RUN - Execute the current program
  • LIST - Display program listing
  • NEW - Clear current program
  • HELP - Show available commands

🏫 Learning Path

For Beginners

  1. Start with gwbasic-c/TEACHING_GUIDE.md
  2. Examine the sample programs in gw-basic-programs/
  3. Read the comprehensive header documentation in gwbasic.h

For Advanced Users

  1. Compare C implementations with original ASM files
  2. Study memory management patterns in utilities.c
  3. Analyze interpreter architecture in interpreter.c

Practical Exercises

  1. Trace Execution - Follow a simple program through tokenization β†’ parsing β†’ execution
  2. Memory Analysis - Study the FOR loop stack management
  3. Add Features - Implement new BASIC statements or data types

πŸ› οΈ Technical Highlights

Memory Management Fixes

This implementation solves several critical memory safety issues common in ASM-to-C conversion:

  • Double-Free Prevention - Using copy_value() for safe value sharing
  • Ownership Clarity - Clear rules about who owns and frees memory
  • Stack Safety - Proper cleanup of control flow stacks

Performance Considerations

While prioritizing educational clarity over raw performance, the implementation includes:

  • Efficient tokenization with minimal memory allocation
  • Structured variable lookup (easily upgradeable to hash tables)
  • Minimal copying in the critical execution path

πŸ“– Documentation

All code includes comprehensive documentation focusing on:

  • Original ASM correspondence - How each C function relates to assembly
  • Teaching concepts - Interpreter theory and implementation patterns
  • Memory safety - Critical patterns for preventing common C pitfalls
  • Type system design - How dynamic languages handle multiple data types

🀝 Contributing

This is an educational project focused on demonstrating assembly-to-C conversion principles. Contributions that enhance the educational value are welcome:

  • Improved documentation and teaching examples
  • Additional sample BASIC programs
  • Enhanced error messages and debugging features
  • Performance optimizations with educational explanations

πŸ“œ License

This project contains:

  • Original GW-BASIC source: Microsoft's original assembly code (see LICENSE)
  • C implementation: Educational conversion with comprehensive documentation

πŸ™ Acknowledgments

  • Microsoft for releasing the original GW-BASIC source code
  • Bill Gates, Paul Allen, Rich Murphey, Dan Illowsky, and others who worked on the original implementations
  • The retrocomputing community for preserving computing history

πŸ”— Resources


"The best way to understand how computers work is to build one yourself." - This project lets you build an interpreter and understand the evolution from assembly to modern C programming.

About

Porting the original source code of Microsoft GW-BASIC from 1983 to run on OSX

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C 95.2%
  • BASIC 4.5%
  • Makefile 0.3%