手臂组装中的 itte [英] itte in arm assembly

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

问题描述

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

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

其中 xyz 是可选的,并且必须是 T(对于then") 或 E(用于其他").<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.

所以你总是有一个 T 跟在 I 之后(毕竟指令是 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.

指令的最小形式如下:

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 中的第 2 个,而 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天全站免登陆