itte 在臂组件 [英] itte in arm assembly

查看:15
本文介绍了itte 在臂组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这行代码在arm组装中做了什么:

What does the following line do in arm assembly:

000031e6        2916    cmp r1, #22
000031e8        bf1a    itte    ne

我得到了第一行(比较 r1 和 22)但是第二行呢(我以前从未见过 itte 命令,谷歌搜索什么也没返回)

I get the first line (comparing r1 to 22) but what about the second line (I've never seen the itte command before and googling returned nothing)

推荐答案

是ARM的IF-THEN-ELSE指令,在Thumb-2指令集中引入.(根据您上面的具体示例,如果您在 ITTE 指令之后显示了接下来的 3 条指令,将会很有帮助,当您阅读完此答案后,您就会明白为什么.)

It is the ARM's IF-THEN-ELSE instruction, which was introduced in the Thumb-2 instruction set. (Based on your specific example above, it would have been helpful if you had shown the next 3 instructions that followed the ITTE instruction, you'll understand why when you're done reading this answer.)

该指令用于处理小序列的条件代码,最多 4 条指令.将其视为实现 ARM 条件执行的另一种方式(例如 BNE - 只有在未设置零标志时才会执行分支指令).

This instruction is used for handling small sequences of conditional code, up to 4 instructions. Think of it as a different way of implementing the ARM's conditional execution (e.g. BNE - the branch instruction is only executed if the zero flag is not set).

它的好处是它避免了使用分支的惩罚(大概你已经了解了管道等)

The benefit of it is that it avoids the penalty of taking a branch (presumably you've learned about pipelines etc.)

这个指令有点复杂,但是一旦你把它转过来,它就会非常优雅.

The instruction is a bit involved but once you wrap your head around it, it's pretty elegant.

它采用以下形式:

IT<x><y><z><cond>

其中 xyz 是可选的,并且必须是 T(对于然后") 或 E(代表else").<cond>为反映的NEEQGT等任一条件在 APSR 标志中.

where x, y, and z are optional, and must be either T (for "then") or E (for "else"). <cond> is any of the conditions such as NE or EQ or GT, etc. that are reflected in the APSR flags.

所以你总是有一个 TI 之后(指令毕竟是 IT !),然后是 0-3 ET 的.对于每个 T 和每个 E,您必须有一个与匹配顺序相同的后续指令.每个匹配的后续指令必须具有与 IT 指令匹配的条件.

So you always have one T following the I (the instruction is IT after all!), and then 0-3 E's or T's. For each T and each E, you must have a subsequent instruction in the same order that matches up. Each matching subsequent instruction must have conditions that match up with the IT instruction.

请耐心等待,我知道这很令人困惑.我将在这里举几个例子来说明.

Bear with me, I know this is confusing. I'll give a couple examples here to illustrate.

指令的最小形式是这样的:

The minimal form of the instruction would be something like:

IT LT
SUBLT.W  R2, R1

在这种情况下,如果 LT 为真(根据 APSR 标志),则会发生减法.注意 SUB 中的 LTIT 指令中的 LT 匹配.

In this case, if LT is true (per the APSR flags), the subtraction will take place. Notice the LT in the SUB matches the LT in the IT instruction.

一个完整的例子是这样的:

A full-blown example would be something like:

ITETT NE
ADDNE R0, R0, R1
ADDEQ R0, R0, R3
ADDNE R2, R4, #1
MOVNE R5, R3

所以我们有 THEN ELSE THEN THEN (TETT),带有 NE 条件.请注意,在随后的 4 条条件指令中(4 条指令,TETT 各 1 条),THEN"指令具有 NE 条件,ELSE"指令(IT 指令之后的第二条指令——记住 E 是 4 个 E 和 T 中的第二个)具有相反的条件.它不能是其他任何东西,即如果它是 LT 而不是 EQ 之类的东西,那将是一个错误.EQNE 的反义词.

So we have THEN ELSE THEN THEN (TETT), with NE condition. Notice in the 4 conditional instructions that follow (4 instructions, 1 each for TETT), the "THEN" instructions have the NE condition, and the "ELSE" instruction (the 2nd instruction after the IT instruction - remember the E was the 2nd of 4 E's and T's) has the opposite condition. It cannot be anything else, i.e. it would be an error if it was something like LT instead of EQ. EQ is the opposite of NE.

因此,如果 NE 为真,则将执行指令 1、3 和 4.否则 (EQ),只会执行指令 2 (ADDEQ).

So if NE is true, then instructions 1, 3 and 4 would be executed. Otherwise (EQ), only instruction 2 (ADDEQ) would be executed.

我给出了 1 和 4 指令的示例,但您也可以有 2 个 (IT{T,E}) 和 3 个指令 (IT{T,E}{T,E}) 形式.

I've given examples of 1 and 4 instructions, but you can also have 2 (IT{T,E})and 3 instruction (IT{T,E}{T,E}) forms as well.

最后,为了强调重点,我将举例说明如何使用该指令实现以下​​ C 代码:

Finally, to bring home the point, I'll give an example of how the following C code can be implemented using this instruction:

if (R4 == R5)
{
  R7 = R8 + R9;
  R7 /= 2;
}
else
{
  R7 = R10 + R11;
  R7 *= 2;
}

转换为

CMP R4, R5
ITTEE EQ
ADDEQ R7, R8, R9    ; if R4 = R5, R7 = R8 + R9
ASREQ R7, R7, #1    ; if R4 = R5, R7 /= 2
ADDNE R7, R10, R11  ; if R4 != R5, R7 = R10 + R11
LSLNE R7, R7, #1    ; if R4 != R5, R7 *=2

这应该足够你咀嚼一段时间了.

That should give you enough to chew on for a while.

这篇关于itte 在臂组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆