This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Mini Assembler for C++ and C# projects

Mini Assembler for C++ and C# projects
by on (#243179)
I just made a mini 6502 assembler for C++ and C# projects. Most useful for Romhacking tools, but if there is any other need to emit 6502 asm within C# or C++ code, it might be useful.

C++ Version: Assembler.h, Assembler.cpp
C# Version: Assembler.cs

Example usage of C++ version, a simple memory clear loop:

Code:
   using namespace assembler;
   byte code[0x8000];
   SetMemory(code, 0x8000);
   SetCurrentAddress(0x8000);
   SEI();
   LDA_IMM(0);
   STA_ABS(0x2000);    //disable NMI
   LDX_IMM(0xFF);
   TSX();              //reset stack
   TAX();
   Label("clrloop");
   STA_ZPG_X(0x00);
   STA_ABS_X(0x100);
   STA_ABS_X(0x200);
   STA_ABS_X(0x300);
   STA_ABS_X(0x400);
   STA_ABS_X(0x500);
   STA_ABS_X(0x600);
   STA_ABS_X(0x700);
   INX();
   BNE("clrloop");
   ApplyFixups();


Then the corresponding code for the C# version. You inherit the Assembler class if you don't want to put "assembler." before each method call.

Code:
    class MyCode : Assembler
    {
        public void Build(byte[] memory)
        {
            SetMemory(memory, 0x8000);
            SetCurrentAddress(0x8000);
            SEI();
            LDA_IMM(0);
            STA_ABS(0x2000);    //disable NMI
            LDX_IMM(0xFF);
            TSX();              //reset stack
            TAX();
            Label("clrloop");
            STA_ZPG_X(0x00);
            STA_ABS_X(0x100);
            STA_ABS_X(0x200);
            STA_ABS_X(0x300);
            STA_ABS_X(0x400);
            STA_ABS_X(0x500);
            STA_ABS_X(0x600);
            STA_ABS_X(0x700);
            INX();
            BNE("clrloop");
            ApplyFixups();
        }
    }


Edit: Fixed wrong opcode for BIT_ABS (was 2D instead of 2C)
Re: Mini Assembler for C++ and C# projects
by on (#243194)
Interesting. I considered doing something like this a while ago, so that I could quickly come up with an assembler without having to go through the trouble of parsing, tokenizing, etc., but ultimately decided that it would compromise the readability of larger programs too much so I gave up. Still cool for tiny projects, though!