如何编写Risc-V(汇编语言)的NOT操作? [英] How do I write NOT Operation for the Risc-V (Assembly Language)?

查看:179
本文介绍了如何编写Risc-V(汇编语言)的NOT操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Risc-V(汇编语言)编写非"运算?如果没有NOT指令,您如何实现同一目标?

How do I write NOT Operation for the Risc-V (Assembly Language)? If there's no NOT instruction, how do you achieve the same thing?

推荐答案

类似于MIPS和其他一些体系结构,RISC V并未针对许多事物提供专用指令,包括二操作数一元操作,因为这些操作可以通过使用它们的操作来实现.三操作数格式,通常将 x0 作为第三个操作数,但有时将常量 1 -1 作为第三个操作数.

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.

为方便起见,汇编程序将接受这些一元操作(和其他操作)的伪指令.这是常见的RISC V伪指令及其替代清单.

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.

要执行更复杂或不列出的内容,请使用数学&逻辑,并根据需要提供许多指令.

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)


顺便说一句,有一个称为LC-3的教育处理器.它只有三个算术/逻辑运算: ADD AND NOT .然而,希望学生编写能完成乘法,除法,模, XOR OR 等的代码.乘法与除法/模数是通过循环完成的; XOR OR 是使用逻辑序列完成的-我们知道所有布尔运算都可以仅使用NAND门来完成,因此(仅) AND & NOT 很原始但是足够.


As an aside, there's an educational processor called the LC-3.  It has only three arithmetic/logical operations: ADD, AND, NOT.  Yet students are expected to write code that does multiplication, division, modulus, XOR, OR, etc..!!  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.

在该处理器上我最喜欢的 XOR 序列来自以下公式:

My favorite sequence for XOR on that processor comes from this formula:

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

这里的 + 实际上是 ADD ,它可以替代 OR ,因为这两个操作数永远不会为1.同时,因此不会发生从一位到另一位的携带,在这种情况下, ADD OR 是等效的.

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.

这篇关于如何编写Risc-V(汇编语言)的NOT操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆