Thread: inline asm with machine code, non-mnemonic input?

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

    inline asm with machine code, non-mnemonic input?

    Hi,

    I'm trying to find a way to execute bin/hex machine code in C. I understand inline assembly allow us to use mnemonic instructions "MOV, SUB, ADD, ..." but is it possible to implement binary or hex form of machine code in C?

    Many Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So what you want is flat assembled machine code, not an executable object file? Then just insert that as a string and call it from your c source? What's the purpose of this exactly, it seems to be a much harder to manage and more obfuscated way of doing things. You can call an assembly routine from C as if it where a function as far as I know, but I don't know enough details about it.

    Here's a link describing that though: Interfacing Assembly Language Routines with C

    Someone else around here probably knows more about this.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by wenxinleong View Post
    Hi,

    I'm trying to find a way to execute bin/hex machine code in C. I understand inline assembly allow us to use mnemonic instructions "MOV, SUB, ADD, ..." but is it possible to implement binary or hex form of machine code in C?

    Many Thanks.
    I'm not sure I follow. Why wouldn't you want to use the mnemonics, since they're easier and clearer? Could you explain more about why you would want to do this and give an example of how you might want it to work? Why don't you want to use mnemonics?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Thanks for the quick prompt.
    It is part of my project I'm afraid. It is inappropriate to disclose more details. But in simple words, it is to provide an environment to run an arbitrary generated instruction. The instruction is generated in binary or hex following a certain format so there is no need to refer to instruction manual time to time. Ideally:
    Code:
    int main() {
        asm ("1011000001100001" :); //instruction 10110 (MOV), data 01100001 to register 000
        return 0 ;
    }
    Well of course this will not work.
    Last edited by wenxinleong; 03-01-2011 at 01:01 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, because the binary formats are not actually assembly code, they're machine code. Assembly code is the mnemonics. You wont get the inline assembler to handle inline machine code. This sounds an awful lot like something that would be used for malicious purposes, so I think I'm done talking.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Quote Originally Posted by anduril462 View Post
    No, because the binary formats are not actually assembly code, they're machine code. Assembly code is the mnemonics. You wont get the inline assembler to handle inline machine code. This sounds an awful lot like something that would be used for malicious purposes, so I think I'm done talking.
    I can assure you it is not for malicious purposes, it is a research project.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wenxinleong View Post
    I can assure you it is not for malicious purposes, it is a research project.
    Well, that's all fine and good...

    Except for the small problem that no matter your good intentions, posting such code here *for you* would make it visible to those of less than admirable intent.

    For all intents and purposes, there is no way to run code from strings that *does not* involve the creation of malicious code.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Well hacking is a way of learning and yes there are people with bad intention...
    Anyhow, I guess I won't be receiving any advice on this topic here then.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well, it used to be possible to simply assign a generic function pointer to an arbitrary string and then call it. Of course you have to make sure the appropriate RET instructions were included in the string. That was decades ago. I'm not sure if today's CPU ring level needs to be altered to zero or some such. The operating system may get a trap because you are attempting to treat data space as code space.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Well, it used to be possible to simply assign a generic function pointer to an arbitrary string and then call it. Of course you have to make sure the appropriate RET instructions were included in the string. That was decades ago. I'm not sure if today's CPU ring level needs to be altered to zero or some such. The operating system may get a trap because you are attempting to treat data space as code space.
    Yep... they've done a LOT to make this undoable... For example in Win 7...

    Control Panel -> System -> Advanced system settings -> Advanced -> Performance -> Data Execution Prevention.

    Note that it does not let you turn it off, only to select it's scope...

    It's always on precisely because there is no non-malicious reason for executing code from data.
    Last edited by CommonTater; 03-01-2011 at 02:18 PM.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Thanks, CommonTater. I had a feeling that more of the fun has been taken away.
    It used to be possible to write a machine emulator that way - dynamically plunking down opcode and executing it, displaying all registers. Great way to learn about things.
    Last edited by nonoob; 03-01-2011 at 03:31 PM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wenxinleong View Post
    Well hacking is a way of learning and yes there are people with bad intention...
    Anyhow, I guess I won't be receiving any advice on this topic here then.
    It's actually board policy.

    However; you are still welcome and encouraged to ask about other things...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck making part of my code work for testing input
    By KevinP in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 08:52 AM
  2. Help using inline assembly for keyboard input
    By sleventeen in forum C Programming
    Replies: 7
    Last Post: 05-10-2009, 01:31 AM
  3. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Inline asm - I love it!
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-08-2002, 02:19 PM