informedcitizenry / 6502.Net

A .Net-based Cross-Assembler for Several 8-Bit Microprocessors
MIT License
58 stars 17 forks source link

Something I really miss: A simple way to assemble from string to byte[] #13

Closed SuperJMN closed 1 year ago

SuperJMN commented 1 year ago

Hi :) Really interesting project!

I've taken a look to the code and I found almost impossible to use your library to assemble code without calling the .exe.

My goal is to invoke your code from another C# project, so I can avoid writing files to disk. I'd like to get the assembled result as byte[];

I'd expected something like:

var assembler = new Z80Assembler();

var source = @"CALL main
HALT
main:
LD hl, 30h
LD (hl), 1
RET";

byte[] output = assembler.Assemble(source);

Maybe I'm missing something, but this would be a really good API in my humble opinion :)

I hope you can help me getting something like this out of the code. Thanks in advance!

informedcitizenry commented 1 year ago

Hi there, if you are customizing the source for your own purposes, you could change the signature of the Assemble method and have it return a byte[] array. The assembled output can be queried from the CodeOutput's GetCompilation method. Something like this:

public byte[] Assemble()
{
    byte[] bytes = { };
    // ... do a bunch of things and if successful return compiled bytes
    return _services.Output.GetCompilation().ToArray();
}
SuperJMN commented 1 year ago

Thanks for the info. The problem is that AssemblyController still deals with commandline arguments that require paths to files in the filesystem.

Thanks a lot!

informedcitizenry commented 1 year ago

Ahh I see. I made this as a command line tool. Most options that set behavior are parsed from the options in the command line. Feel free to customize for your purposes of course. Be aware also that the assembler recognizes certain directives in source that can do the same, for instance:

   .cpu "z80"
   .format "zx"

See the directives section in the wiki for more info