问题描述
我以前使用过8086的汇编语言,所以8086中的旋转操作只是一个命令.但是我找不到使用Risc-V汇编语言进行旋转操作的特定关键字.
I have worked with assembly language of 8086 previously, rotation operation in 8086 was just a command. But I can't find a specific keyword for rotation operation in Risc-V assembly language.
推荐答案
RISC-V基本指令集不包括旋转指令.
The RISC-V base instruction set doesn't include rotate instructions.
因此,您必须使用多个基本指令来实现轮换(也称为循环移位):
Thus, you have to implement rotation (a.k.a. cyclic shift) with multiple basic instructions e.g.:
.text
.balign 4
# unsigned long rotl(unsigned long x, unsigned long amnt);
.global rotl
rotl:
sll a2, a0, a1
sub a4, zero, a1
srl a3, a0, a4
or a0, a2, a3
ret
# unsigned long rotr(unsigned long x, unsigned long amnt);
.global rotr
rotr:
srl a2, a0, a1
sub a4, zero, a1
sll a3, a0, a4
or a0, a2, a3
ret
请注意,sub a4, zero, a1
以零环绕,左移逻辑(sll
)仅使用a4
的低六位(RV64G)或五位(RV32G)作为移位数量.
Note that sub a4, zero, a1
wraps around at zero and shift-left-logical (sll
) only uses the lower six (RV64G) or five bits (RV32G) of a4
as shift amount.
按立即数操作数移位时,GNU as
不会不隐式截断移位量,因此必须显式屏蔽它,例如:
When shifting by an immediate operand, GNU as
does not implicitly truncate the shift-amount, thus one has to explicitly mask it, e.g.:
# unsigned long rotl3(unsigned long x);
.global rotl3
rotl3:
slli a2, a0, 3
srli a3, a0, (-3 & 63) # & 31 for RV32G
or a0, a2, a3
ret
# unsigned long rotr3(unsigned long x);
.global rotr3
rotr3:
srli a2, a0, 3
slli a3, a0, (-3 & 63) # & 31 for RV32G
or a0, a2, a3
ret
.global rotl
RISC-V Bitmanip扩展名"B" 规范草案的确包含一些额外的变更和随机播放指令,包括左右旋转:
The RISC-V Bitmanip Extension "B" draft specification does include several additional shift and shuffle instruction including left and right rotate:
# RV32, RV64:
ror rd, rs1, rs2
rol rd, rs1, rs2
rori rd, rs1, imm
# RV64 only:
rorw rd, rs1, rs2
rolw rd, rs1, rs2
roriw rd, rs1, imm
( RISV-V Bitmanip扩展V0.92 ,第2.2.1节,第14页)
(RISV-V Bitmanip Extension V0.92, Section 2.2.1, page 14)
当然,从2020年开始,由于草案的状态,bitmanip指令及其编码可能会更改,并且对软件工具链,模拟器和硬件中的"B"扩展的支持也不广泛.
Of course, as of 2020, because of the draft status, bitmanip instructions and their encodings may change and support for the "B" extension in software toolchains, simulator and hardware isn't widely available.
这篇关于如何编写Risc-V(汇编语言)的旋转操作?是否像8086中那样有任何命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!