3

I want to define a assembly to read register from arm64. Code is like the following:

asm volatile(
        "str <reg> [%0]\n"
        :
        : "r"(value)
        : "memory"
    );

how to use a macro to define such code. <reg> is in a string literal and the behaviour of macros make me confused when I use # and ##

I tried to define even in the following way, it still causes an error.

#define STR_x0 "str x0 [%0]\n"
#define STR_x1 "str x1 [%0]\n"
...
#define STR_REG(reg) STR_##reg

#define ASM_READ_REG(reg) \
    asm volatile( \
        STR_REG(reg) \
        : \
        : "r"(value) \
        : "memory" \
    )
3
  • 2
    The operand for value needs to be a pointer not a value. Confusing name. Also, you're asking the compiler for a pointer in a register, so it's going to have to generate code that sets a register. It's likely to pick x0 or x1, overwriting whatever you had there. You could maybe put reg in the clobber list as well, or use { register unsigned long regval asm("x0"); asm volatile("" : "=r"(regval)); value = regval;} to capture a register value as an output operand for an asm statement. Commented Apr 8 at 3:03
  • It is also usually kind of pointless to know the value of a certain (general purpose) register when you are inside compiled code, since the compiler may have chosen to store anything in that register. Some strongly related questions: stackoverflow.com/questions/67860150/… stackoverflow.com/questions/67864098/… stackoverflow.com/questions/2114163/… Commented Apr 8 at 11:02
  • hi it still causes an error. what error? Commented Apr 12 at 10:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.