1

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;
        } 
9
  • 1
    Sounds like you want a volatile uint32_t *. You don't need inline asm to do stuff to memory at an absolute address. But if you were going to, an asm statement 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. Commented May 23, 2023 at 7:56
  • 1
    As a side note, please do not insert a blank line after every line. It makes the code much less readable and reduces the number of lines to be displayed at a time. Commented May 23, 2023 at 8:34
  • When I use volatile uint32_t, the compiler will automatically do the optimization and the program doesn't initialize the value in the address I want. So I try to write an asm code to force the compiler do not optimize @Peter Cordes Commented May 23, 2023 at 8:47
  • 1
    As a sidenote: You might have more people looking at the question if you gave it a better title. "issue during implementation" could be any question on this site. It also helps people find your question that have the same problem if you give it a more specific title. Commented May 23, 2023 at 8:53
  • 1
    There you go, that's a better title. The question itself is still a case of not using GNU C inline asm properly, not telling the compiler about registers you're modifying, like I mentioned in the first comment. If there's any problem other than that, IDK, because that obvious problem needs to get fixed before it's worth looking for others. Commented May 23, 2023 at 9:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.