Like Tree1Likes
  • 1 Post By Fordy

Convert assembly>machine code, machine code>assembly

This is a discussion on Convert assembly>machine code, machine code>assembly within the C Programming forums, part of the General Programming Boards category; Howdy, I wonder if there is any lib or function to convert a string of assembly code to machine code, ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Convert assembly>machine code, machine code>assembly

    Howdy,

    I wonder if there is any lib or function to convert a string of assembly code to machine code, and vice versa?

    Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    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:

    Code:
    char asm[] = {"mov r0,r1"};
    unsigned int output = 0;
    
    output = asm2mach(asm);
    ...
    Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And that's called an assembler.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    Not that I'm personally aware of - but perhaps writing such a function yourself would be a great exercise.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    @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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    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.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Thanks. I'll look that up. btw I'm using Ubuntu.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    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.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Quote Originally Posted by Matticus View Post
    Not that I'm personally aware of - but perhaps writing such a function yourself would be a great exercise.
    It's well worth playing with. A rather ham-fisted method I used many years ago - self-writing code?

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    @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.

    Quote Originally Posted by AndrewHunter View Post
    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.
    /FA, /Fa (Listing File)

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Thanks Mike.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    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);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2008, 10:00 PM
  2. Replies: 4
    Last Post: 01-18-2008, 06:05 PM
  3. machine learning code in c/c++
    By nchauhan in forum Game Programming
    Replies: 0
    Last Post: 11-14-2003, 09:26 AM
  4. The relationship between C++ and assembly and machine code
    By TotalBeginner in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2002, 02:46 PM
  5. Machine code, asm, poop
    By tim545666 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-02-2002, 12:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21