0

Question:

Hello, I have recently experienced trouble in the subject of C++'s inline Assembly implementation, using Microsoft Visual Studio 2017. I have been able to run inline Assembly, however I would like to allow the user to type in Assembly code into the console window, and then use C++'s inline Assembler to run the user's Assembly code. I could not figure out how to do this, after much searching, nothing was found. So I am here to ask if anyone knows of a way to accomplish this task.

Question Overview:

How would I go about using Microsoft Visual Studio 2017's C++ inline Assembler to run user inputted x86 Assembly code?

Additional Information:

Microsoft Visual Studio 2017, Windows 10 home, x64 system, x86 application, debug mode.

Template Code:

#include <Windows.h>
#include <iostream>
#include <string>

int main()
{
    std::string _string;
    std::getline(std::cin, _string);
    __asm
    {
        //  How would I run the user inputted Assembly code in the "_string" string?
    }
    std::getchar();
    return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Tim Hardly
  • 129
  • 4
  • 1
    i doubt thats possible, since the string is not known at compile-time. – Domso Jul 12 '18 at 17:54
  • _"How would I run the user inputted Assembly code"_ That assembly code appearing there is injected at compile time, there's no such thing like a _runtime assembler_ binding. THX TL! – πάντα ῥεῖ Jul 12 '18 at 17:56
  • Allowing user-input to run as code would be an incredibly large security hole. – Some programmer dude Jul 12 '18 at 17:56
  • Distinguish compile-time and run-time constructs. Plus, executing user-inputted code (assembly or not) is a serious security problem. –  Jul 12 '18 at 17:56
  • You are aware that you would need to *assemble* the text input into object code before even trying to feed it to the CPU, right? Or are you planning on interpreting it? – Nikolai Fetissov Jul 12 '18 at 17:57
  • Hmm... I'll see what people have to say then and hopefully it is possible, there could be a "hacky" way to do it. – Tim Hardly Jul 12 '18 at 17:57
  • its not impossible, but it would require some sort of runtime compiler, which compiles the user-input... – Domso Jul 12 '18 at 17:58
  • Why do you want to execute user-inputted assembly code? What is the underlying goal? –  Jul 12 '18 at 18:01
  • @NickyC I was just trying to create random ideas that came to my head of things I could make, something along the lines of something like creating an Assembly shell like they have shells for languages like Python came to my head. – Tim Hardly Jul 12 '18 at 18:03
  • Can you give us an example of the string you will be entering that you want run? It is true that if the string is assembly code it would have to be assembled into machine code first and then placed in memory that is executable before you could transfer control to it. If the string is ASCII executable code then the assembling to machine code stage can be skipped. – Michael Petch Jul 12 '18 at 18:05
  • @TimHardly _"... like they have shells for languages ..."_ It's done in a different way, and always needs to have the interpreter core installed at the target machine (like .NET, Java, Python). – πάντα ῥεῖ Jul 12 '18 at 18:06
  • @MichaelPetch As a string of Assembly code, like the user could type into the console `mov ebx, 7` and the program would run that Assembly. – Tim Hardly Jul 12 '18 at 18:13
  • @TimHardly : No, entering machine code that happens to consist of only printable ASCII characters that can be executed directly. – Michael Petch Jul 12 '18 at 18:16
  • Why can't I just store the __asm block inside a function that takes a string of assembly code as a paramater, to avoid the whole runtime compilation thing? – Tim Hardly Jul 12 '18 at 18:17
  • Because Microsoft's inline assembly is compile time only and doesn't work at runtime. You can't pass a character string as a parameter to inline assembly and have it executed. – Michael Petch Jul 12 '18 at 18:20
  • @TimHardly The closest I can think of is a pre-processor macro definition, but that's still at compile time. Any other ways need kind of an interpreter, which _"emulates"_ or controls a CPU core directly. That's kinda what debuggers are doing. Instrumenting the CPU to run in step mode, and display it's state (regarding registers and state-flags) – πάντα ῥεῖ Jul 12 '18 at 18:32
  • Some people have talked about "interpreting" it.This may seem like a stupid question, but can't I just store the opcodes somewhere then use switches or if statements to interpret the assembly opcodes and then have the operands also stored, because I know you can use C/C++ variables in a __asm{} block. – Tim Hardly Jul 12 '18 at 18:34
  • @TimHardly When implementing such thing some `__asm` blocks might be useful or course, e.g. to drag all current CPU register values to your debugging shell. – πάντα ῥεῖ Jul 12 '18 at 18:35
  • @TimHardly _" but can't I just store the opcodes somewhere then use ..."_ Of course you can do that, in conjunction to control the CPU interactively (execute assembly code under controlled conditions, set breakpoints/step mode execution, get CPU state at each execution step, etc ). It might quickly get more complicated as you may think of it now. Biggest obstacle: Have an assembler that emits the _bytecode_ in 1st place. You don't want to write such thing yourself at all (and there are plenty publicly available already). – πάντα ῥεῖ Jul 12 '18 at 18:40
  • Okay thanks, so I think I got the idea that I need to write something like an interpreter for the user input instead of running a string in an __asm{} block. – Tim Hardly Jul 12 '18 at 18:41
  • @TimHardly De nada. Pining me directly isn't that hard you might think it is ;-). – πάντα ῥεῖ Jul 12 '18 at 18:43
  • @πάνταῥεῖ Okay, but why do I need to see the CPU state if all I'm doing is interpreting the user input then running it based on the interpretation, and how would I get the CPU state? – Tim Hardly Jul 12 '18 at 18:47
  • 1
    @TimHardly _"... that I need to write something like an interpreter ..."_ Don't do that yourself please, just integrate an [existing one](https://en.wikipedia.org/wiki/Comparison_of_assemblers) into your program. – πάντα ῥεῖ Jul 12 '18 at 18:47

0 Answers0