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.
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.
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.
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:
Well of course this will not work.Code:int main() { asm ("1011000001100001" :); //instruction 10110 (MOV), data 01100001 to register 000 return 0 ; }
Last edited by wenxinleong; 03-01-2011 at 01:01 PM.
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.
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.
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.
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.
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.