0

I have a case that I want to automate my inline assembly inside a function. regx has 32 bits, and read has 8 bits.

uint32_t *r(uint32_t regx){
  uint32_t *read;

  for(int i = 0; i<4; i ++){
  __asm__ volatile("csrr %0, regx" : "=r"(read));
  }
  return read;
}

regx is the argument that I want to change with different values (regx= 0x3A) for example. I appreciate any suggestions.

Fangting
  • 31
  • 3
  • 1
    Are you asking how you would use the value of the `regx` parameter in the inline assembly? You have already passed information about the `read` variable into the inline assembly; what problem are you having passing the `regx` parameter? Did you write `__asm__ volatile("csrr %0, regx" : "=r"(read));` yourself or just copy it from somewhere? In other words, do you know some syntax for inline assembly and just need help with this additional bit, or do you not know the inline assembly syntax at all? Does the `csrr` instruction take a register operand for the second operand? – Eric Postpischil Jun 16 '22 at 10:14
  • What do you mean you want to “automate“ this? – Eric Postpischil Jun 16 '22 at 10:15
  • According to you, then I'm have a misunderstanding? I thought ```"=r"(read)```, is the output from ```regx```, and I have different register addresses for regx. So basically, I want to pass different address values (```regx```) to the inline assembly. – Fangting Jun 16 '22 at 10:47
  • https://five-embeddev.com/riscv-isa-manual/latest/csr.html says `csrr` instructions need the CSR number as an immediate, so you need a `"I"(regx)` input constraint. (https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html). And the function may need optimization to be enabled for constant-propagation to work. – Peter Cordes Jun 16 '22 at 11:00
  • I found the answer here, https://stackoverflow.com/questions/58947716/how-to-interact-with-risc-v-csrs-by-using-gcc-c-code Thank you Peter. – Fangting Jun 16 '22 at 11:17

0 Answers0