繁体   English   中英

“||”怎么写 Risc-V 中的条件?

[英]How to write "||" condition in Risc-V?

例如,在 C

if (a == 0 || a == b) return 0;

我不知道如何在 Risc-v 中编写多个条件。 大家能帮我解一下吗?

它比 RISC V 更广泛——所有汇编语言都使用 if-goto 作为唯一条件。

If-goto 用于if -then- else语句、 forwhiledorepeat循环,以及 for &&|| 评估。

||的操作数可以分开单独工作。

if ( a == 0 || a == b ) return 0;

可以翻译成 if-goto 形式如下:

    if ( a == 0 ) goto L1;      // fall thru only if a!=0
    if ( a != b ) goto L2;      // fall thru only if a==b, branch if a!=b
L1:
    return 0;                   // reaches here if either a==0 or else a==b
L2:                             // comes here if either a!=0 or else a!=b

对于结合,

if ( a == 0 && b == 0 ) return 0;

可以翻译成 if-goto 形式如下:

    if ( a != 0 ) goto L1;     // fall thru only if a==0, branch if a!=0
    if ( b != 0 ) goto L1;     // fall thru only if b==0, branch if b!=0
    return 0;                  // reaches here when both a==0 and b==0
L1:                            // comes here if either a!=0 or else b!=0

(当然,还有其他可能的转换。)


这些到 if-goto 的翻译可以直接用各种汇编语言编写,使用 compare 和 branch。

在 RISC V 中,上述 if-goto 转换可以使用分支相等 ( beq ) 或分支不相等 ( bne ) 指令,其中包含两个操作数和一个标签。

对于使用条件代码的指令集,我们编写两条指令,比较两个操作数,然后是条件分支,以根据情况标记相等或不相等。

澄清一下:RISC-V 已经有几个可用的 C 编译器(示例包括 GCC、LLVM 和 IAR),因此您可以使用现有的 C 代码并为 RISC-V 编译它,就像为其他目标(如 ARM)一样。

如果您的问题真的更多是关于如何在 RISC-V 程序集中编写类似的东西,那么这样的东西可能会起作用(假设 a 在 reg a0 ,b 在a1 ):

  beqz a0, is_true;    # Jump to the is_true label if a == 0
  beq a0, a1, is_true; # Jump to the is_true label if a == b
  j is_false;          # Jump to the is_false label to continue the function
is_true:
  mv a0, x0;           # Load 0 into the return value register (x0 is hard-wired 0)
  ret;
is_false:
  # Continue the function...

暂无
暂无

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

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