是否可以在不使用`jump`和`goto`的情况下进行装配决策? [英] Is it possible to make decisions in assembly without using `jump` and `goto` at all?
问题描述
在此问题中,显示了一些答案如何在不使用"if"语句的情况下进行决策,但是我怀疑这是可能的,因为"if"不是生成jump
指令的唯一语句.
In this question, some answers showed how it is possible to make decisions without using "if" statements, however I suspect this is possible because "if" is not the only statement that generates jump
instructions.
给定一种固定的编译语言(例如C ++),生成的程序集可以在不使用jump
和goto
指令的情况下做出某种决定吗?
Given a fixed compiled language (in example C++), can the generated assembly do some kind of decisions without using jump
and goto
instructions?
请给出一个简单的if/else语句的替代示例,该示例在肯定回答的情况下不使用此类指令.
Please give an example alternative to a simple if/else statement that does not use such instructions in case of affirmative answer.
推荐答案
ARM体系结构具有有趣的条件执行特征.在完全ARM模式下运行,几乎每条指令都可以附加一个条件.这些条件与B
ranch指令所使用的条件相同.诸如add r0, r0, #15
之类的指令将始终执行,但是诸如addeq r0, r0, #15
之类的指令仅在置零标志时才会执行.使用分支的等效项是:
The ARM architecture has an interesting conditional execution trait. Running in full ARM mode, nearly every instruction can have a condition attached to it. These are the same conditions that are used on the B
ranch instructions. An instruction such as add r0, r0, #15
will always execute, but an instruction such as addeq r0, r0, #15
will only execute if the zero flag is set. An equivalent using branches would be:
beq afteradd ; skip add if equal
add r0, r0, #15 ; add 15 to R0
afteradd:
在使用Thumb-2的内核上运行时,条件执行受到更多限制,但仍然可以通过使用IT
指令来执行.该指令将在不使用分支的情况下创建"if-then"构造. IT
构造实际上是ARM统一汇编语言的一部分,无论您是为ARM还是Thumb-2编写,都应使用该构造.这是上面的条件添加的UAL版本.
When running on a core that uses Thumb-2, the conditional execution is more limited, but still possible by using the IT
instruction. This instruction will create an "if-then" construct without using branches. The IT
construct is actually part of ARM's Unified Assembly Language, and should be used whether you're writing for ARM or Thumb-2. Here's the UAL version of the conditional add from above.
it eq ; if equal
addeq r0, r0, #15 ; then add 15 to r0
IT
构造可以包含多个指令.在指令中使用一系列的T
和E
,您可以添加更多条件指令.在下面的示例中,如果设置了零标志,我们将在R0
中加15;否则,我们将从R0
中减去15. ITE
的字面意思是if-then-else.接下来的第一条指令应具有与您的ITE
条件相匹配的条件,然后第二条指令将成为"else",并且应具有与ITE
条件相反的条件.
The IT
construct can include more than just a single instruction. Using a series of T
and E
in the instruction, you can add more conditional instructions. In the below example, we will add 15 to R0
if the zero flag is set, otherwise we will subtract 15 from R0
. The ITE
means, fairly literally, if-then-else. The first following instruction should have a condition that matches your ITE
condition, and then the second instruction will become the "else" and should have a condition that is the opposite of the ITE
condition.
ite eq ; if equal
addeq r0, r0, #15 ; then add 15 to r0
subne r0, r0, #15 ; else subtract 15 from r0
我想这种方法确实会使用if,这可能与您的要求背道而驰.在执行方面,尽管如此,它还是在不使用跳转的情况下实现了if.
I guess this kind of does use if, which may go against what you were asking. In terms of the execution, this implements the if without using a jump, though.
这篇关于是否可以在不使用`jump`和`goto`的情况下进行装配决策?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!