Instructions: Language of the

2023-01-03  本文已影响0人  刘东利2020

2.7 Instructions for Making Decisions

这一节讲的就是各种判断执行语句长什么样子。

首先是if 语句:

RISC-V assembly language includes two decision-making instructions, similar to an if statement with a go to. The first instruction is

beq rs1, rs2, L1

This instruction means go to the statement labeled L1 if the value in register rs1 equals the value in register rs2.

就是当rs1 = rs2时候,执行L1语句。对应地:

The mnemonic beq stands for branch if equal. The second instruction is

bne rs1, rs2, L1

It means go to the statement labeled L1 if the value in register rs1 does not equal the value in register rs2.

这两个称之为:

conditional branch An instruction that tests a value and that allows for a subsequent transfer of control to a new address in the program based on the outcome of the test.

示例:

If the five variables f through j correspond to the five registers x19 through x23, what is the compiled RISC-V code for this C if statement?

if (i == j) f = g + h; else f = g − h;

如何实现?框图是这样子的:

对应的指令如下:

bne x22, x23, Else // go to Else if i ≠ j

add x19, x20, x21 // f = g + h (skipped if i ≠ j)

beq x0, x0, Exit // if 0 == 0, go to Exit

Else:sub x19, x20, x21 // f = g − h (skipped if i = j)

Exit:

注意这里的第三句称之为unconditional branch —— 就是真值总是成立,这样才能到Exit语句。

上一篇 下一篇

猜你喜欢

热点阅读