0

I need to read a register (r9) into a variable.

I have this

 pxTopOfStack[9-4] = 0x20000000;  // Set the task's initial R9 value

0x20000000 is stored in R9.

how could I inline this in arm assembly? I can set r9 in assembly as follows:

__asm volatile ("LDR r9, = 0x20000000");

but how would I set a plain C variable in inline assembly?

pseudo code

__asm volatile ("MOV pxTopOfStack[9-4], R9"); // just trying to illustrate what I am looking for
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
bas
  • 13,550
  • 20
  • 69
  • 146
  • This depends on the compiler. In GNU C you may use: `__asm volatile("mov %0, r9":"=r" (pxTopOfStack[9-4]));` or `__asm volatile("str r9, %0":"=m" (pxTopOfStack[9-4]))`. However, keep in mind that the C compiler may modify `r9` before the `__asm` line... – Martin Rosenau Apr 29 '22 at 20:27
  • Hi @MartinRosenau, thx, found something on arm website that does the trick. Seems to be quite similar to what you suggested! – bas Apr 29 '22 at 20:37
  • 1
    Near duplicate of [Why can't I get the value of asm registers in C?](https://stackoverflow.com/q/67864098) plus [ARM inline asm: exit system call with value read from memory](https://stackoverflow.com/a/37363860) (showing how to make an asm `"r"` or `"=r"` constraint pick a specific ARM register.) – Peter Cordes Apr 30 '22 at 08:48

1 Answers1

0

Copy pasted an example from developer.arm and modified it. Seems to work!

__asm ("MOV %[result], R9"
    : [result] "=r" (pxTopOfStack[9-4])
  );
bas
  • 13,550
  • 20
  • 69
  • 146
  • 1
    If you want it in an array, you might as well `str` with an `"=m"(lvalue)` memory destination operation. If the surrounding C doesn't always want to look at it. – Peter Cordes Apr 30 '22 at 08:39
  • 1
    One day i will get good at assembly – bas Apr 30 '22 at 08:55