Pasting constant into asm code block
-
15-04-2021 - |
質問
I have this C macro:
#define syscall(number) \
({ \
asm volatile ( \
".set noreorder\n" \
"nop\n" \
"syscall "#number"\n" \
);\
})
It works great when I call it with integer:
syscall(5);
However when I do something like this:
#define SYSCALL_PUTC 5
syscall(SYSCALL_PUTC);
I get this error:
Error: Instruction syscall requires absolute expression
How do I work around this? I don't want to have my code littered with magic numbers.
解決
Use stringification like this:
#define xstr(s) str(s)
#define str(s) #s
#define syscall(number) \
({ \
asm volatile ( \
".set noreorder\n" \
"nop\n" \
"syscall "xstr(number)"\n" \
);\
})
For more info on stringification, see this gcc page:
他のヒント
A bit of a hack, but not THAT much: make a copy of your original syscall macro for each syscall number, naming each for that syscall. Thus, you'd have a macro named SYSCALL_PUTC which would expand into the same as syscall(5) would have.
所属していません StackOverflow