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:
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.
Edit: Fixed wrong opcode for BIT_ABS (was 2D instead of 2C)
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();
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();
}
}
{
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)