Reading a GPR with inline assembler

187 views Asked by At

I am using the Diab C compiler (v5.9.4) for the PowerPC and I need to copy the content of a General purpose register into a function's local variable.

Are there any examples of how can I read a GPR and move its value in a variable in C?

1

There are 1 answers

2
Qiu Chaofan On

The compiler you mentioned is likely a variant of Clang or GCC. You can write inline assembly as below:

// Read from GPR, change the '0' to the GPR you'd want
unsigned long gprv;
asm volatile("mr %0, 0" : "=r"(gprv));

// Writing to GPR
// Thanks Peter Cordes for pointing out clobber list
asm volatile("mr 0, %0" : : "r"(gprv) : "0");