RISC-V: operation with a0 changes other register values

1

I'm currently doing a small project on risc-v. The problem is that, in a function, when I do something like this,

foo:
    ... 
    lw a3, 4(sp) 
    srli a2, a2, 16 
    srli a4, a4, 16
    add a0, a2, a4 
    ret

after executing add a0, a2, a4 the value of a2 and a4 also change. for e.g. if I comment out the add a0, a2, a4 and execute it, the result: a0 = 0x33333333, a2 = a4 = 0x00000000. If I add the "add" line, the result becomes : a0 = ax000063e3, a3=0x123392c8, a4=0x00000d6e, a2 = 0x00005675.

Even it is the last statement of the function, it also changes the value of the registers that are above it. (like a3)

Am I missing something about risc-v? is it not sequentially executed?

Also in the function to make the result become 0,

...
beq a1, zero, exit_loop
...

exit_loop:
add a0, zero, zero
j finish

...
finish:
addi sp, sp, 28
ret 

But this returns 0x00000001.If I'm understanding the fundamental parts of risc-v, let me know. Thanks.

assembly
riscv
riscv32
asked on Stack Overflow Nov 4, 2019 by TAYTAY1313

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0