I want to initialize memory block of riscv32 x'10000000 to x'10002000 with 0 to 2048 and variable "key" will take the value from address x'10000400 as the pass word. Because of optimization of assembler, use a pointer can't work, so I try to write with inline assembly. And while charge in card, no response when I tap the enter button. I suppose the program block in somewhere. The wrong part of code is shown as below.
int key=0;
asm volatile(
"li t0, 0\n\t"
"li t1, 0x10000000\n\t"
"loop: \n\t"
"sw t0, 0(t1)\n\t"
"addi t0, t0, 1\n\t"
"addi t1, t1, 4\n\t"
"li t2, 0x800\n\t"
"blt t0, t2, loop\n\t"
);
asm volatile(
"li t0, 0x10000400\n\t"
"lw %[key], 0(t0)\n\t"
: [key] "=r" (key)
);
I have tried to correct my code. The new one is as below. I use a pointer to initialize the memory, but when I load the program in my card, it can't execute ether.
unsigned int i;
unsigned int *mem_ptr=(unsigned int *)0x10000000;
for (i = 0; i < 0x800; i++) {
asm volatile(
"sw %[i], 0(%[mem_ptr])\n\t"
: [mem_ptr] "+r" (mem_ptr)
: [i] "r" (i)
:);
if (mem_ptr==0x10000400) {
key=i;
}
mem_ptr=mem_ptr+4;
}
volatile uint32_t *. You don't need inline asm to do stuff to memory at an absolute address. But if you were going to, anasmstatement that uses registers without telling the compiler about it (clobber declarations) can't work safely either. gcc.gnu.org/wiki/ConvertBasicAsmToExtended and other stuff in stackoverflow.com/tags/inline-assembly/info Also, your minimal reproducible example left out any details of what problem you had when running it.