26 CSRRS 与 CSRRW 与 CSRRCriscv指令 Tips

本文详细介绍了RISC-V指令集中的CSRRS(原子读取并设置CSR位)和CSRRC(原子读取并清除CSR位)指令,以及它们与CSRRW指令的区别。这些指令涉及对控制与状态寄存器(CSR)的操作,其中当rs1为x0时,不会修改CSR的值。此外,无论rs1和rd字段如何,CSRRS和CSRRC都会读取指定的CSR并产生任何读取副作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSRRS 与 CSRRW 与 CSRRCriscv指令 详解

参考riscv指令集手册第76页

The CSRRS (Atomic Read and Set Bits in CSR) instruction reads the value of the CSR, zero- extends the value to XLEN bits, and writes it to integer register rd. The initial value in integer register rs1 is treated as a bit mask that specifies bit positions to be set in the CSR. Any bit that is high in rs1 will cause the corresponding bit to be set in the CSR, if that CSR bit is writable.
Other bits in the CSR are not explicitly written.
The CSRRC (Atomic Read and Clear Bits in CSR) instruction reads the value of the CSR, zero- extends the value to XLEN bits, and writes it to integer register rd. The initial value in integer register rs1 is treated as a bit mask that specifies bit positions to be cleared in the CSR. Any bit that is high in rs1 will cause the corresponding bit to be cleared in the CSR, if that CSR bit is writable. Other bits in the CSR are not explicitly written.
For both CSRRS and CSRRC, if rs1=x0, then the instruction will not write to the CSR at all, and so shall not cause any of the side effects that might otherwise occur on a CSR write, nor raise illegal instruction exceptions on accesses to read-only CSRs. Both CSRRS and CSRRC always read the addressed CSR and cause any read side effects regardless of rs1 and rd fields. Note that if rs1 specifies a register holding a zero value other than x0, the instruction will still attempt to write the unmodified value back to the CSR and will cause any attendant side effects. A CSRRW with rs1=x0 will attempt to write zero to the destination CSR.
在这里插入图片描述

指令可以分为三种
1:带立即数的 CSRRWI 与 不带立即数的 CSRRW
2:带立即数的 CSRRS 与 不带立即数的 CSRRSI
3:带立即数的 CSRRC 与 不带立即数的 CSRRC

需要注意上述加粗字体,当rs1为 x0,就是当为gpr寄存器为第一个寄存器时,且指令为 CSRRS 或 CSRRC,此时csr中的值不发生改变。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值