为 RISC-V 设置 mstatus 寄存器

Setting the mstatus register for RISC-V

我正在尝试用另一个寄存器 t1 加载 mstatus。

 lw t1, mstatus              # load mstatys register into t1
 xori t1, t1, 0x8            # xor mstatus to set 3rd bit and leave everything else as is
 lw mstatus, t1              # set mstatus 

最初的 lw t1, mstatus 工作正常。但是,当尝试 lw mstatus, t1 时,汇编器给出

Error: illegal operands 'lw mstatus, t1'

我不知道是什么导致了这个错误,mstatus 寄存器是一个 read/write 寄存器。它应该有效。

mstatus 不是内存部分。那么就不能在通用寄存器(x1-x31)下用lw/sw指令loaded/stored.

mstatus 是通过控制和状态寄存器指令访问的 CSR(配置状态寄存器)的一部分(参见 riscv-spec 的第 2.8 章)。

然后要加载 mstatus 你应该使用 csrrs/c 指令并根据你想做什么来编写 csrrw 指令你也可以只 clear/set 寄存器的单个位。

将 t1 写入 mstatus 并且不关心 mstatus 旧值 (x0):

csrrw t1, mstatus, x0

读取 t1 中的 mstatus 并且不要触摸 mstatus 值:

csrrs x0, mstatus, t1

csrrc x0, mstatus, t1

除了@FabienM 的回答之外,我还要添加对处理 CSR 的伪指令的引用。例如。 csrr rd, csrcsrrs rd, csr, x0 的缩写,只是读取给定的 CSR。这些可以在 The RISC-V Instruction Set Manual Volume I: Unprivileged ISA.

的第 9.1 章“CSR 说明”中找到