6502pasm

6502pasm (WIP)

A portable 6502 assembler written in C++

The Goal

I wanted to make a 6502 IDE for the M5Stack Cardputer (an ESP32 based computer), but I couldn’t find an assembler that didn’t rely on the underlying operating system (which the Cardputer doesn’t have) so I’m making my own! I’m hoping to have a 6502 assembler contained in a single file (well, two files including the .h) while using minimal standard libraries (hopefully only stdio for sprintf, stdlib for malloc, and regex)

Todo

Usage

To test:

Docs (also WIP)

Assembler() class

This is the class that contains the assembler. It has:

Example

#include "6502pasm.h"

int main() {
  Assembler assembler = Assembler();

  // Loads some simple stuff into input_lines
  assembler.input_lines.push_back(std::string(".org $0800"));
  assembler.input_lines.push_back(std::string(".macro addr $05"));
  assembler.input_lines.push_back(std::string("main:"));
  assembler.input_lines.push_back(std::string(" lda #$2e ; comment"));
  assembler.input_lines.push_back(std::string(" LDX $04 ; capital mnemonic"));
  assembler.input_lines.push_back(std::string(" ldy %addr ; macro"));
  assembler.input_lines.push_back(std::string(" jmp main ; uses a label"));

  // Assemble it
  assembler.assemble();

  // Output it
  printf("Output hex (%d bytes):\n",assembler.output_bytes.size());
  for (int i=0;i<assembler.output_bytes.size();i++) {
    if (i%8==0)
      printf("  %02x: ",i);
    printf("%02x ",assembler.output_bytes[i]);
    if (i%8==7)
      printf("\n");
  }
  printf("\n");
  printf("Labels (%d labels):\n",assembler.labels.size());
  for (Label l : assembler.labels) {
    printf("  %s @ 0x%04x\n",l.name.c_str(),l.location);
  }
  printf("Macros (%d macros):\n",assembler.macros.size());
  for (Macro m : assembler.macros) {
    printf("  %s = '%s'\n",m.name.c_str(),m.text.c_str());
  }

  return 0;
}

You can also look at test/main.cpp