本文介绍了“And"的条件执行在 ARM 语法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试掌握 ARM 编程中的条件执行.

I'm trying to get a grip on conditional execution within ARM programming.

所以我有点理解这样的情况:

So I kind of understand a situation like this:

if ( (R0 != 5) || (R2 != R3) ) ; != means not equal,  || mean OR
{
R4-- ; // R4 = R4 - 1
}

ARM 版本将是:

CMP   R0, 5
CMPEQ R2, R3
SUBNE R4, R4, 1

我很好奇 ARM 怎么知道这是一个或"(||).所以它做了两次比较.但是如果两个比较都不正确会发生什么.那是NE做的吗?如果它们不同会发生什么(如下例所示).

I was curious how ARM knows that that is an "OR" (||). So it does two compares. But what happens if both compares aren't correct. Is that what NE does? What happens if they were different (like example below).

所以让我们说代码是这样的:

So lets say the code is this:

if ( (R0 > 5) && (R2 != R3) ) ; != means not equal,  && mean AND
{
R4-- ; // R4 = R4 - 1
}

您将如何使用 ARM 中的条件指令编写此代码?

How would you write this with conditional instructions in ARM?

谢谢!

推荐答案

这是一些简单的逻辑,对于OR"的情况似乎有悖直觉.在我看来,'AND' 更简单.关键是只有当第一个条件为AND"时才应评估第二个条件.对于OR"情况,情况正好相反.你短路了逻辑,不用费心去执行第二个条件.

This is some simple logic that seems counter intuitive for the 'OR' case. The 'AND' is simpler in my opinion. The key is that the 2nd condition should only be evaluated if the first passed for 'AND'. For the 'OR' case it is the opposite. You short circuit the logic and don't bother to execute the 2nd condition.

这里是AND"情况的步骤,

Here are the steps for the 'AND' case,

  1. 测试cc1
  2. 如果 cc1 测试 cc2
  3. 如果 cc2 则执行

如果 'cc1 == cc2' 并且第一个为假,则第 2 步不会执行,因此第 3 步也不执行.

If 'cc1 == cc2' and the first is false then step 2 doesn't execute and hence not 3 as well.

举个例子,

if((R0 > 5) && (R2 != R3)) R4--;

它会翻译成,

TST   R2, R3     ; step 1 as (R2 != R3)
MSREQ CPSR_f,#3<<30 ; set N and Z flags  (also APSR_nz)
CMPNE R0, #5     ; step 2 as (R0 > 5)
SUBPL R4, #1     ; step 3

必须注意确保步骤 1 中的第一个测试不会影响步骤 3 的执行.可以针对AND"条件重新排序测试,因为两者都必须进行测试.

Care must be taken to ensure that the first test in step 1 does not influence the execution of step 3. Tests can be re-ordered for the 'AND' condition as both must be tested.

见:
Dave 空间条件执行
HeyRick 状态登记

这篇关于“And"的条件执行在 ARM 语法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:13