Instructions: Language of the
2.6 Logical Operations
除了数字操作,还有逻辑操作,如下:
其中,第一类是shift操作。示例1,左移相当于乘法:
这背后就是:144 = 9 * 2^4。对应的RISC-V指令:
意义是:
The encoding of slli is 19 in the opcode field, rd contains 11, funct3 contains 1, rs1 contains 19, immediate contains 4, and funct6 contains 0.
相应地,shift right就相当于是除法了。
第三种移位操作,称之为shift right arithmetic:
RISC-V provides a third type of shift, shift right arithmetic (srai). This variant is similar to srli, except rather than filling the vacated bits on the left with zeros, it fills them with copies of the old sign bit.
shift类别操作还可以从register中取数字:
It also provides variants of all three shifts that take the shift amount from a register, rather than from an immediate: sll, srl, and sra.
最后四个,是通常认为的逻辑算数,对应的操作:
AND A logical bit-by�bit operation with two operands that calculates a 1 only if there is a 1 in both operands.
OR A logical bit-by�bit operation with two operands that calculates a 1 if there is a 1 in either operand.
NOT A logical bit-by�bit operation with one operand that inverts the bits; that is, it replaces every 1 with a 0, and every 0 with a 1.
XOR A logical bit-by�bit operation with two operands that calculates the exclusive OR of the two operands. That is, it calculates a 1 only if the values are different in the two operands.
以上四个都是针对两个变量进行操作,如:
而考虑到常量操作:
Constants are useful in logical operations as well as in arithmetic operations, so RISC-V also provides the instructions and immediate (andi), or immediate (ori), and exclusive or immediate (xori) .