Processor Register - Application Variable association
I have a very simple problem. I wanna write some assembler code (for SPARC) that directly operates on a single register, called %o1. All I wanna do is to initialise this register with zero, and then increment it with some immediate values. The problem is, how can I output the result in %o1 from a C-application. The skeleton I have looks like that:
void main()
{
int a; 开发者_StackOverflow中文版
asm volatile (
".text\n\t"
"mov 0, %o1 \n\t"
"add %o1, 1, %o1 \n\t"
"add %o1, 2, %o1 \n\t"
"add %o1, 3, %o1 \n\t"
);
// assign content of register %o1 somehow to variable a (a = %o1);
printf("%i\n", a);
}
So the question is how do I assign the value of %o1 (which should be 6 by the end of the calculation) to variable a so that it can be printed on the console.
It's compiler dependent. For gcc: http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
void main()
{
int a;
asm(
"mov 0, %0 \n\t"
"add %0, 1, %0 \n\t"
"add %0, 2, %0 \n\t"
"add %0, 3, %0 \n\t"
: "=r"(a)
);
printf("%i\n", a);
}
Update: looks like gcc do not allow selecting specific registers on SPARC. There is another extension for this: http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html
register int a asm ("o1");
This is much depending on your compiler (which you didn't tell us), look up its documentation. For gcc the syntax would be something like
int a __asm("%o1")__ = 78;
精彩评论