1

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 ?

  • 1
    What is it doing? What do you expect it to do? – fuz Sep 29 '16 at 13:05
  • generally, i want to view the contents of a SPR Register using WinIDEA. and in order to do so, i have to store the contents of the SPR to a global variable, and like that i can watch the contents of the register. – Maged Rawash Sep 29 '16 at 13:18
  • That's the “what do you want to do part.” So what does the code you have there instead? Does it assemble? Are there any error messages? Gives us all information you have. – fuz Sep 29 '16 at 13:27
  • It gives a Syntax error at the third line, – Maged Rawash Sep 29 '16 at 13:43
  • This is like pulling nails. What syntax error do you get? Please post the complete error message. – fuz Sep 29 '16 at 14:20
  • I'm not trying to solve the error, because i'm not sure if this is the correct assembly to use or not. what i'm looking for is a recommendation on which assembly instructions to use to do the desired functionality. – Maged Rawash Sep 30 '16 at 08:36

1 Answers1

1

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
dja
  • 1,453
  • 14
  • 20
  • I just saw you added some extra questions about global variables in asm. This is a bit tricky at the raw assembler level: you have use the Global Offset Table (GOT) / Table Of Contents (TOC) which is stored through r2. You might be better asking a different, more specific question about them if you can't use inline asm. – dja Sep 30 '16 at 00:07