简体   繁体   English

如何为 Risc-V(汇编语言)编写 NOT 操作?

[英]How do I write NOT Operation for the Risc-V (Assembly Language)?

How do I write NOT Operation for the Risc-V (Assembly Language)?如何为 Risc-V(汇编语言)编写 NOT 操作? If there's no NOT instruction, how do you achieve the same thing?如果没有 NOT 指令,你如何实现同样的目标?

Like MIPS and some other architectures, RISC V does not provide dedicated instructions for many things, including two-operand unary operations, as these operations can be had using their three-operand format, usually with x0 as the third operand, but sometimes constant 1 or -1 as the third operand.与 MIPS 和其他一些架构一样,RISC V 没有为许多事情提供专用指令,包括二元一元运算,因为这些运算可以使用它们的三元格式进行,通常使用x0作为第三个操作数,但有时是常数1-1作为第三个操作数。

For convenience, the assembler will accept what are called pseudo instructions for these unary operations (and others).为方便起见,汇编器将接受用于这些一元操作(和其他操作)的所谓伪指令。 Here's a list of the common RISC V pseudo instructions and their replacements.这是常见的 RISC V 伪指令及其替换的列表。

To do more complex or unlisted things, use math & logic, and as many instructions as needed.要执行更复杂或未列出的事情,请使用数学和逻辑以及尽可能多的指令。

li rd, immediate     | Myriad sequences               | Load immediate
mv rd, rs            | addi rd, rs, 0                 | Copy register
not rd, rs           | xori rd, rs, -1                | One’s complement
neg rd, rs           | sub rd, x0, rs                 | Two’s complement
negw rd, rs          | subw rd, x0, rs                | Two’s complement word
sext.w rd, rs        | addiw rd, rs, 0                | Sign extend word
seqz rd, rs          | sltiu rd, rs, 1                | Set if = zero
snez rd, rs          | sltu rd, x0, rs                | Set if ̸= zero
sltz rd, rs          | slt rd, rs, x0                 | Set if < zero
sgtz rd, rs          | slt rd, x0, rs                 | Set if > zero
beqz rs, offset      | beq rs, x0, offset             | Branch if = zero
bnez rs, offset      | bne rs, x0, offset             | Branch if ̸= zero
blez rs, offset      | bge x0, rs, offset             | Branch if ≤ zero
bgez rs, offset      | bge rs, x0, offset             | Branch if ≥ zero
bltz rs, offset      | blt rs, x0, offset             | Branch if < zero
bgtz rs, offset      | blt x0, rs, offset             | Branch if > zero
bgt rs, rt, offset   | blt rt, rs, offset             | Branch if >
ble rs, rt, offset   | bge rt, rs, offset             | Branch if ≤
bgtu rs, rt, offset  | bltu rt, rs, offset            | Branch if >, unsigned
bleu rs, rt, offset  | bgeu rt, rs, offset            | Branch if ≤, unsigned
j offset             | jal x0, offset                 | Jump
jal offset           | jal x1, offset                 | Jump and link
jr rs                | jalr x0, 0(rs)                 | Jump register
jalr rs              | jalr x1, 0(rs)                 | Jump and link register
ret                  | jalr x0, 0(x1)                 | Return from subroutine
call aa              | auipc x1, aa[31 : 12] + aa[11] | Call far-away subroutine
                     | jalr x1, aa[11:0](x1)          | (two instructions)
tail aa              | auipc x6, aa[31 : 12] + aa[11] | Tail call far-away subroutine
                     | jalr x0, aa[11:0](x6)          | (also two instructions)

As an aside, there's an educational processor called the LC-3.顺便说一句,有一个名为 LC-3 的教育处理器。 It has only three arithmetic/logical operations: ADD , AND , NOT .它只有三个算术/逻辑运算: ADDANDNOT Yet students are expected to write code that does multiplication, division, modulus, XOR , OR , etc..;!然而,学生需要编写执行乘法、除法、模数、 XOROR等运算的代码;! Multiplication & division/modulus are done with a loop;乘法和除法/模数是通过循环完成的; XOR and OR are done using logic sequences — we know all the boolean operation can be had using only NAND gates, so having (only) AND & NOT is primitive but sufficient. XOROR是使用逻辑序列完成的——我们知道所有 boolean 操作只能使用 NAND 门进行,因此(仅) AND & NOT是原始但足够的。

My favorite sequence for XOR on that processor comes from this formula:我最喜欢该处理器上的XOR序列来自以下公式:

(A AND NOT B) + (NOT A AND B)

Where here the + is literally ADD , which works as a substitute for OR because the two operands will never both be 1 at the same time, so carry from one bit position to another will not occur, and under those circumstances, ADD and OR are equivalent.这里的+字面上是ADD ,它可以代替OR因为两个操作数永远不会同时为 1,因此不会发生从一位 position 到另一位的进位,在这种情况下, ADDOR是相等的。

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

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