I have two macros, one is written in assembly and the other in C. The second macro uses the first macro. However, I also want to write the second macro in assembly with volatile, so I can control its placement in the code. And please note that tid is a runtime value, not a constant like n.
What is a good way to write that in assembly? Also, is it possible to control placement of a C code like assembly with volatile?
#define SAVE_SP(n) __asm__ __volatile__ ("movq %rsp, msp"#n";" \
"movq ts"#n", %rsp;" \
)
#define SAVE_STACK_POINTER( tid ) \
switch( tid ) \
{ \
case 0: \
SAVE_SP( 0 ); \
break; \
case 1: \
SAVE_SP( 1 ); \
break; \
case 2: \
SAVE_SP( 2 ); \
break; \
case 3: \
SAVE_SP( 3 ); \
break; \
}
You can ask gcc its idea of how to write your code in assembly:
gcc -S foo.corgcc -Wa,-alh=foo.s -c foo.c. You may want to improve on the results, of course. You will need to do a little extra: use%0for the parameter that you pass for the assembly chunk, and declare the registers that you’ve clobbered. Look up Assembler Instructions with C Expression Operands in the GCC manual if you aren’t familiar. Here’s how this might look like (warning, typed directly into the browser, and don’t really know x86 assembly syntax).A fancier method would involve figuring out how many bytes each
movq–movq–jmpblock takes (note: I haven’t checked, I use 8) and making a computed jump into it; something like