简体   繁体   English

我如何为 Risc-V(汇编语言)编写旋转操作,我们是否有像 8086 中那样的命令?

[英]How do I write rotation Operation for the Risc-V(Assembly Language) Do we have any command for it like we have have in 8086?

I have worked with assembly language of 8086 previously, rotation operation in 8086 was just a command.我以前用过8086的汇编语言,8086中的旋转操作只是一个命令。 But I can't find a specific keyword for rotation operation in Risc-V assembly language.但是我在 Risc-V 汇编语言中找不到用于旋转操作的特定关键字。

It looks like extension "B" should define such an instruction eventually.看起来扩展“B”最终应该定义这样的指令。

Until then you have to compose it using left and right shifts.在此之前,您必须使用左移和右移来撰写它。

Here's an equivalent of the MIPS32R2 rotrv instruction (rotate right by variable count):这是 MIPS32R2 rotrv 指令的等效指令(按变量计数向右旋转):

rotrv:
    subu    neg_count, zero, count
    srlv    tmp1, src, count
    sllv    tmp2, src, neg_count
    or      dst, tmp1, tmp2

You can do the same on riscv.你可以在 riscv 上做同样的事情。

The RISC-V base instruction set doesn't include rotate instructions. RISC-V 基本指令集不包括旋转指令。

Thus, you have to implement rotation (aka cyclic shift) with multiple basic instructions eg:因此,您必须使用多个基本指令实现旋转(又名循环移位),例如:

    .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

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.请注意, sub a4, zero, a1在零处回绕,并且左移逻辑( sll ) 仅使用a4的低六位 (RV64G) 或五位 (RV32G) 作为移位量。

When shifting by an immediate operand, GNU as does not implicitly truncate the shift-amount, thus one has to explicitly mask it, eg:当通过立即操作数移位,GNU as不会隐式截断的移位量,从而一个具有明确掩盖它,例如:

# 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

The RISC-V Bitmanip Extension "B" draft specification does include several additional shift and shuffle instruction including left and right rotate: RISC-V Bitmanip 扩展“B”草案规范确实包括几个额外的移位和随机指令,包括左右旋转:

 # 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 Extension V0.92 , Section 2.2.1, page 14) RISV-V Bitmanip 扩展 V0.92 ,第 2.2.1 节,第 14 页)

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.当然,截至 2020 年,由于草案状态,bitmanip 指令及其编码可能会发生变化,并且对软件工具链、模拟器和硬件中的“B”扩展的支持并未广泛可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM