0

I am trying to use the value of a variable I've declared in C as the argument of a mov instruction in assembly. I've tried using a conversion character like this (where ptr is the variable):

asm("mov x9, %p\n\t" : : "r"(ptr));

But it doesn't compile and outputs an error of:

error: invalid % escape in inline assembly string
            asm("mov x9, %p\n\t" : : "r"(ptr));
                ~~~~~~~~~~~^~~~~

I've also tried it without the \n\t and it gives the same error. It's not really specific as to what's wrong with it so I'm not sure how to deal with this. How can I get it to compile or achieve the same outcome in a different way?

3h6_1
  • 13
  • 1
  • 2
  • 5
  • Why are you using `%p`? – Eric Postpischil May 28 '21 at 23:35
  • 1
    If you do not give names to the items passed to the assembly, they are automatically numbered: `%0`, `%1`, `%2`… However, it is clearer to give them names: `asm("mov x9, %[myname]" : : [myname] "r" (ptr));`. – Eric Postpischil May 28 '21 at 23:36
  • @EricPostpischil I'm using %p because the variable is a pointer and I want it to appear as an address. – 3h6_1 May 28 '21 at 23:37
  • 3
    It is not a `printf` statement. Forget all the `printf` conversion specifiers when using `asm`. – Eric Postpischil May 28 '21 at 23:38
  • Also, what is your intent with the `mov` instruction? To copy the pointer into `x9`? Or to load what is in the memory pointed to by `ptr` into `x9`? (Or the other way around; I do not know which assembler you are using, and the operand order varies.) If you want to refer to the memory pointed to by pointer, you may want a `"m"` constraint rather than `"r"` and to using `(*ptr)` for the value rather than `(ptr)`. – Eric Postpischil May 28 '21 at 23:44
  • 1
    Note you also have to declare `x9` as a clobber in order for this to be safe. – Nate Eldredge May 28 '21 at 23:44
  • @EricPostpischil I intend to copy the pointer into x9. – 3h6_1 May 28 '21 at 23:47
  • Then you need to tell the compiler that your asm statement destroys the old value of x9, by declaring a clobber on `"x9"`. https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html. If not, it will be unsafe once you fix the `%p` instead of `%0` bug and get it to compile. – Peter Cordes May 29 '21 at 00:15

0 Answers0