views:

245

answers:

2

I need to pass assembly code as arguments in Visual Studio, therefore have a function e.g.: myasm(char *x) that will accept arguments like "mov eax,eax\n add eax,eax"

Unfortunately I can't use GCC compiler for what I'm doing. Is there a way to do this in VS?

(Note: the assembly code I'm passing is dynamic so I can't predict what it is going to be.)

A: 

Do you really need to use assembler? You might be better off with nanojit.

Otherwise, you can certainly do this - but it won't be pretty. You can call the VisualStudio assembler, ml.exe, from your code. You would take the instructions, write them to a file with an appropriate header/trailer defining a unique proc name and assemble them into a dll, then call LoadLibrary() and GetProcAddress() to get the address of the code to call, which you can then call like any other function pointer. If you want to get the byte values of particular instructions, use offsets from your header/trailer to figure that out.

See here for how to configure a project with asm files in it. Everything in it you can do dynamically by calling ml yourself.

plinth
A: 

I think things are a little confused. The inline assembler is part of the compiler. It sounds to me like you want to be assembling that code at runtime, when the compiler is no longer available. Have you looked like tools like LLVM instead, which provide a runtime framework for portable code generation? Alternatively, you could ship and invoke an assembler (not the MSVC one, which doesn't allow redistribution like that, but perhaps GNU gas or nasm) with your product.

Andy Ross