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" \
)
valueneeds 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 pickx0orx1, overwriting whatever you had there. You could maybe putregin 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.it still causes an error.what error?