Howdy,
I wonder if there is any lib or function to convert a string of assembly code to machine code, and vice versa?
Thanks.
Howdy,
I wonder if there is any lib or function to convert a string of assembly code to machine code, and vice versa?
Thanks.
Yep... it's called a compiler.
Many C compilers also include an "inline" assembler that allows you to intersperse ASM code in your C programs.
I know both VC++ and Pelles C have this feature... not too sure about MinGw or GCC...
I think I didnt make the question clear. I'm looking for a function/lib which capable taking assembly code as input, translate and output machine code. Something like:
Thanks.Code:char asm[] = {"mov r0,r1"}; unsigned int output = 0; output = asm2mach(asm); ...
Not that I'm personally aware of - but perhaps writing such a function yourself would be a great exercise.
@OP Now one is blowing you off here but what you are asking to do is rather complex; aka it takes more than just some simple manipulations. There are many open source compilers out there such as gcc and fasm that could get you started in the right direction but this isn't a task for the faint of heart.
Speaking of assembly however, I know with gcc you can use the -S to create the assembly language for your C program. I use to know how to do it with the older versions for VC++ compiler but 2010 has seemed to hide it on me. Does anyone now where they moved that setting to? I did try searching but all I was able to turn up was info for the older versions.
I don't know what platform you're on, but I'm running Fedora. I found a package yasm-devel which looks like it might have the features you're after. Never used it though, so I can't tell you a thing about it, and if you're not on Linux, I don't know what to tell you.
Thanks. I'll look that up. btw I'm using Ubuntu.
You could look at hacking up GNU binutils. The two programs you'll want to look at are as (the assembler) and objdump (which can disassemble object code).
In GNU as, you'll want to isolate the stuff necessary to call md_assemble() for the architecture you're interested in.
Objdump is mostly just a wrapper around libbfd - which for your purposes is mainly going to use stuff from libopcodes, I think.
It's well worth playing with. A rather ham-fisted method I used many years ago - self-writing code?
@OP:
For an assembler, fasm can quite easily be integrated as a library. And there's also my personal favourite AsmJit, but that one is unfortunately C++ only.
For a disassembler, there is for example BeaEngine which is C99.
/FA, /Fa (Listing File)
Thanks Mike.
If x86 is the target, you could use xed2 ("x86 encoder/decoder"). It can be found as part of Intel's Pin package:
Pin - A Dynamic Binary Instrumentation Tool
After extracting the archive, look in extras/xed2-ia32.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}