0

I'm trying to execute inline assembly, giving a variable as input

void main(void)
{
    char a[20] = "mov edx, 88";

    asm("%[a]" : : [a]"r"(a));
}

But :

gcc a.c -masm=intel
Error: no such instruction: `eax'

How can I make this work ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Aramya
  • 41
  • 4
  • 8
    This won't work regardless of how you do it. There is no assembler present at runtime, you can't have assembly code in a variable and have it run in your program. That's not possible. – fuz Nov 17 '17 at 17:57
  • You can write source file to disk, and launch compiler (i.e. your app will work sort of like `make`) ... mind you, such architecture screams about problems, so it's good only for experimental purposes, I wouldn't suggest to use anything like that for some production system, which has different main purpose than being a compiler. – Ped7g Nov 17 '17 at 17:59
  • You can invoke inline assembler instructions at compile time (which appears to be what you are somewhat trying to do?). But at run time, you would need to change that into byte codes to be run - and hope you have an OS that allows you to run executable code from a data buffer. – Michael Dorgan Nov 17 '17 at 18:02
  • 1
    See https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html – Michael Dorgan Nov 17 '17 at 18:04
  • The data buffer `a[]` is in a memory page that does not have the `executable` attribute. There is no (reasonable) way to fix this. Suggest all your executable asm statements be within an `asm()` block – user3629249 Nov 17 '17 at 19:14
  • @fuz: Technically, it is possible. One can invoke the assembler and the linker and dynamically link the result into the current executable. Or even build an assembler into one’s own program. Not for beginners thought. – Eric Postpischil Nov 17 '17 at 19:27
  • @user3629249: On Unix systems, `mprotect` can be used to make a page executable. – Eric Postpischil Nov 17 '17 at 19:28
  • The dup-target I found is for MSVC inline asm, but the situation is identical for GNU C inline asm. Update: found another dup target wanting to use a `char[]` for GNU-style `asm volatile` – Peter Cordes Nov 17 '17 at 20:03
  • @EricPostpischil, Yes, `mprotect()` can be used (after finding which page has the .data section of the link). Using `mprotect()` to make the data executable is opening lots of security holes. I would recommend against that action. – user3629249 Nov 17 '17 at 21:04
  • Note that putting `char a[20] = "mov edx, 88"` in an executable page would be useless. That's asm source *text*, not machine code. See [How to get c code to execute hex machine code?](https://stackoverflow.com/q/9960721) for ways to run a buffer containing *machine* code. – Peter Cordes Jul 10 '21 at 21:18

1 Answers1

0

The instructions must be in a form of a string literal(actual written string, the name of a char array is a pointer btw). Other than that you got the general idea:)

#include <stdio.h>

int main(int argc, char ** argv){
char a[20] = "nice try:)";
char * dst;

asm("mov %[dst], %[src]\n\t"
    : [dst]"=r" (dst) : [src]"r"(a));

printf("%s\n", dst);
return 0;
}

and a useful link: https://dmalcolm.fedorapeople.org/gcc/2015-08-31/rst-experiment/how-to-use-inline-assembly-language-in-c-code.html

Efi Shtainer
  • 394
  • 3
  • 8