Maged Rawash
Maged Rawash

Reputation: 41

Defining and Using Global Variable in PowerPC Assembly file

I want to save the contents of a SPR ( Special Purpose Register ) to a global variable. I don't have much experience in Assembly, but i tried to do it as follows :

.global __The_Global_Variable

mfspr    r16, 695  #695 is the number of the SPR Register
stw      r16, __The_Global_Variable #I get Syntax error at this line

I get a Syntax error, so can anyone help in that ?

I have also the following questions :

1- How to define a global variable in Assembly file ? 2- What is the correct instruction to use to store the contents of a register in a variable ?

Upvotes: 1

Views: 589

Answers (1)

dja
dja

Reputation: 1503

You could do this with an inline asm directive. For example, here's how you could get the unprivileged DSCR on a PPC64 system:

#include <stdio.h>

int spr_val;

int main(int argc, char ** argv) {

    asm ("mfspr %0, 3"
         : "=r" (spr_val)
         : : );

    printf("DSCR is %x\n", spr_val);
    return 0;
}

This works as it should - verified by setting the DSCR using ppc64_cpu:

dja@dja-builder ~/e/foo> make foo
cc     foo.c   -o foo
dja@dja-builder ~/e/foo> sudo ppc64_cpu --dscr=0
dja@dja-builder ~/e/foo> ./foo 
DSCR is 0
dja@dja-builder ~/e/foo> sudo ppc64_cpu --dscr=6
dja@dja-builder ~/e/foo> ./foo 
DSCR is 6

Upvotes: 1

Related Questions