美文网首页
Instructions: Language of the

Instructions: Language of the

作者: 刘东利2020 | 来源:发表于2023-01-07 17:42 被阅读0次

    其次是Loops。先一个例子:

    RISC-V中的实现:

    Loop: slli x10, x22, 3 // Temp reg x10 = i * 8

    add x10, x10, x25 // x10 = address of save[i]

    ld x9, 0(x10) // Temp reg x9 = save[i]

    bne x9, x24, Exit // go to Exit if save[i] ≠ k

    addi x22, x22, 1 // i = i + 1

    beq x0, x0, Loop // go to Loop

    Exit:

    这是相等判断,其他条件呢?

    The branch if less than (blt) instruction compares the values in registers rs1 and rs2 and takes the branch if the value in rs1 is smaller, when they are treated as two’s complement numbers.

    Branch if greater than or equal (bge) takes the branch in the opposite case, that is, if the value in rs1 is at least the value in rs2.

    Branch if less than, unsigned (bltu) takes the branch if the value in rs1 is smaller than the value in rs2 when the values are treated as unsigned numbers.

    Finally, branch if greater than or equal, unsigned (bgeu) takes the branch in the opposite case.

    最后,是一个边界检查的简单方法(Bounds Check Shortcut):

    Treating signed numbers as if they were unsigned gives us a low-cost way of checking if 0 ≤ x < y, which matches the index out-of-bounds check for arrays. The key is that negative integers in two’s complement notation look like large numbers in unsigned notation; that is, the most significant bit is a sign bit in the former notation but a large part of the number in the latter. Thus, an unsigned comparison of x < y checks if x is negative as well as if x is less than y.

    示例:

    Use this shortcut to reduce an index-out-of-bounds check: branch to IndexOutOfBounds if x20 ≥ x11 or if x20 is negative.

    The checking code just uses unsigned greater than or equal to do both checks:

    bgeu x20, x11, IndexOutOfBounds // if x20 >= x11 or x20 < 0, goto IndexOutOfBounds

    以上是各种判断语句的用法。

    除了采用一系列的条件结合各种判断语句,还有一种方式,就是采用分支地址表:

    branch address table Also called branch table. A table of addresses of alternative instruction sequences.

    对应的RISC-V实现是:

    To support such situations, computers like RISC-V include an indirect jump instruction, which performs an unconditional branch to the address specified in a register. In RISC-V, the jump-and-link register instruction (jalr) serves this purpose. We’ll see an even more popular use of this versatile instruction in the next section.

    相关文章

      网友评论

          本文标题:Instructions: Language of the

          本文链接:https://www.haomeiwen.com/subject/awhycdtx.html