itte 臂组件

发布于 2024-11-29 10:25:14 字数 177 浏览 1 评论 0原文

下面的行在arm汇编中做了什么:

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

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

What does the following line do in arm assembly:

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒夜笙歌凉 2024-12-06 10:25:14

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

)指令用于处理小序列的条件代码,最多 4 条指令。将其视为实现 ARM 条件执行的不同方式(例如 BNE - 仅在未设置零标志时才执行分支指令)。

它的好处是它避免了采用分支的惩罚(假设您已经了解了管道等)。

该指令有点复杂,但是一旦您了解它,它就非常优雅。

它采用以下形式:

IT

其中 xy>z 是可选的,并且必须是 T(表示“then”)或 E(表示“else”)。 是反映的任何条件,例如 NEEQGT 等在 APSR 标志中。

因此,I 后面总是有一个 T(毕竟指令是 IT!),然后是 0-3 E< /code> 或 T。对于每个 T 和每个 E,您必须有一个按相同顺序匹配的后续指令。每个匹配的后续指令必须具有与 IT 指令匹配的条件。

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

该指令的最小形式类似于:

IT LT
SUBLT.W  R2, R1

在这种情况下,如果 LT 为 true(根据 APSR 标志),则将进行减法。请注意,SUB 中的 LTIT 指令中的 LT 相匹配。

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

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相反。

因此,如果 NE 为 true,则指令 1、3 和 4 将被执行。否则 (EQ),仅执行指令 2 (ADDEQ)。

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

最后,为了让大家明白这一点,我将给出一个示例,说明如何使用此指令实现以下​​ C 代码:

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

这应该足以让您咀嚼一段时间。

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.)

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 takes the form:

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

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.

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

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

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.

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

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.

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;
}

converts to

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.

万劫不复 2024-12-06 10:25:14

简单来说,ITTE 根据上面的 cmp 指令执行以下 3 个执行:IF THEN {} THEN {} ELSE {}。

在ARMv6T2及更高版本的架构中,您可以使用IT指令进行条件执行。在 ARMv6T2 之前的体系结构中,没有 IT 指令,因此除 B 分支指令外,Thumb 指令无法有条件执行。汇编器检查 IT 指令,但在汇编为 ARM 代码。

对于您的解决方案,让我们首先了解 ARM 汇编的简单 IT 指令(在 Thumb 2 中引入)的语法,它是 ITTE 的基础。

IT{pattern} {cond}

If-then,设置最多 4 条后续指令的执行条件
最多可以是三个 T(then) 和 E(else) 字母的任意组合,IT 后面的第一条指令始终是 cond (T) 可以修改程序计数器的指令必须位于 IT 块的最后

< code>then 条件必须与条件代码匹配,任何 else 条件必须是相反条件。下表显示了条件代码及其相反条件:

在此处输入图像描述

让我们了解另一个cmp操作说明。

CMP Rn, #imm

Rn 必须是 Lo 寄存器。 imm 范围 0-255。
这些指令根据结果更新 N、Z、C 和 V 标志。

请记住:IT 允许一到四个以下 Thumb 指令(IT 块)有条件或您可以说这里 ITTE 用于处理条件代码的小序列,最多 4 条指令。

简单示例

Ex 1:

cmp r1, #22      Compare r1 value with 22 
IT  EQ           Read this as If EQual Then ADD R1,R1,#1
ADD R1,R1,#1     <- This will only be executed if above r1 value equal to 22(means when z condition flag is equal to 1)

Ex 2:

cmp r1, #22       Compare r1 value with 22
ITE EQ            Read this as If EQual Then ADD R1,R1,#1 Else ADD R0,R0,#1
ADD R1,R1,#1     <- This will only be executed if the Z condition flag is 1
ADD R0,R0,#1     <- This will only be executed if the Z condition flag is 0

ITTE 做什么?这是你的问题

   CMP   R1, #22       Compare r1 value with 22
   ITTE  NE            Read this as IF NotEqual Then ADD R1,R1,#1 Then ADD R0,R0,#1 Else ADD R2,R2,#1
   ADD R1,R1,#1     <- This will only be executed if the Z condition flag is 0
   ADD R0,R0,#1     <- This will only be executed if the Z condition flag is 0
   ADD R2,R2,#1     <- This will only be executed if the Z condition flag is 1

这里ITTE对前两条指令施加NE条件,对下一条指令施加EQ条件。

注意: IT 块中存在的任何分支都必须是该块中的最后一条指令。参考自 此处
以下示例将具有未定义的行为,因为分支指令在分支指令的中间使用。

ite     eq  
blxeq   some_label  @ UNPREDICTABLE during an IT block.  
movne   r0, #0  

实现上述内容的正确方法是将 mov 放在 blx 之前,如下所示:

ite     ne  
movne   r0, #0  
blxeq   some_label  @ Ok at the end of an IT block.

有关更多信息 THUMB-2指令集参考手册第4-92页

IT{x{y{z}}}<q> <Firstcondition>

第二条指令的条件IT 块

IT 块中第三条指令的条件

IT 块中第四条指令的条件

指定指令上的可选汇编限定符

此处定义的两个限定符:

       .N  Meaning Narrow. Assembler has to choose 16-bit encoding for the instruction if it is not possible then error.

       .W  Meaning Wide. Assembler has to select 32-bit encoding for the instruction if is not possible then error.

IT 块中第一条指令的条件,即 EQ、NE、CC、CS。

In simple words ITTE executes following 3 execution as IF THEN {} THEN {} ELSE {} based on above cmp instruction.

In ARMv6T2 and later architectures, you can use the IT instruction for conditional execution. In architectures before ARMv6T2, there is no IT instruction and therefore Thumb instructions cannot be executed conditionally except for the B branch instruction.The assembler checks the IT instructions, but omits them on assembly to ARM code.

For your solution lets first understand syntax of simple IT instruction (introduced in Thumb 2)of ARM assembly ,which is base of ITTE.

IT{pattern} {cond}

If-then, sets the execution conditions for up to 4 following instructions
can be any combination of up to three T(then) and E(else) letters,the first instruction following IT is always cond (T) Instructions that can modify the program counter must be last in an IT block

The then conditions must match the condition code, and any else conditions must be the opposite condition.The table below shows the condition codes and their opposites:

enter image description here

Let's understand another cmp instruction.

CMP Rn, #imm

Rn must be a Lo register. imm range 0-255.
These instructions update the N, Z, C and V flags according to the result.

Remember : IT allows one to four following Thumb instructions (the IT block) to be conditional or you can say here ITTE is used for handling small sequences of conditional code, up to 4 instructions.

Simple examples

Ex 1:

cmp r1, #22      Compare r1 value with 22 
IT  EQ           Read this as If EQual Then ADD R1,R1,#1
ADD R1,R1,#1     <- This will only be executed if above r1 value equal to 22(means when z condition flag is equal to 1)

Ex 2:

cmp r1, #22       Compare r1 value with 22
ITE EQ            Read this as If EQual Then ADD R1,R1,#1 Else ADD R0,R0,#1
ADD R1,R1,#1     <- This will only be executed if the Z condition flag is 1
ADD R0,R0,#1     <- This will only be executed if the Z condition flag is 0

What ITTE do ? is your question here

   CMP   R1, #22       Compare r1 value with 22
   ITTE  NE            Read this as IF NotEqual Then ADD R1,R1,#1 Then ADD R0,R0,#1 Else ADD R2,R2,#1
   ADD R1,R1,#1     <- This will only be executed if the Z condition flag is 0
   ADD R0,R0,#1     <- This will only be executed if the Z condition flag is 0
   ADD R2,R2,#1     <- This will only be executed if the Z condition flag is 1

Here ITTE imposes the NE condition on first two following instruction and the EQ condition on the next.

NOTE: Any branches that exist in an IT block must be the last instruction in the block.Taken reference from here
Following example will be having undefined behaviour because branch instruction is used in middle of branch instruction.

ite     eq  
blxeq   some_label  @ UNPREDICTABLE during an IT block.  
movne   r0, #0  

Correct way to implement the above would be to put the mov before the blx, as follows:

ite     ne  
movne   r0, #0  
blxeq   some_label  @ Ok at the end of an IT block.

For more info THUMB-2 Instruction set reference manual Page 4-92

IT{x{y{z}}}<q> <Firstcondition>

<x> condition for second instruction in IT block

<y> condition for third instruction in IT block

<z> condition for fourth instruction in IT block

<q> specifies optional assembler qualifiers on the instruction

Two qualifier defined here :

       .N  Meaning Narrow. Assembler has to choose 16-bit encoding for the instruction if it is not possible then error.

       .W  Meaning Wide. Assembler has to select 32-bit encoding for the instruction if is not possible then error.

<Firstcondition> Condition for first instruction in IT block i.e EQ, NE,CC,CS.

樱娆 2024-12-06 10:25:14

它似乎是 IT (if-then) 指令系列的一部分:http://infocenter.arm.com/help/topic/com.arm.doc.qrc0006e/QRC0006_UAL16.pdf(第二页)。基本指令是 IT,然后用 T 表示“then”,E 表示“else”,得到 ITTE 和条件代码 NE ==“不等于”。

It appears to the part of the IT (if-then) instruction family: http://infocenter.arm.com/help/topic/com.arm.doc.qrc0006e/QRC0006_UAL16.pdf (second page). The basic instruction is IT and then you have T for "then" and E for "else" to give ITTE and a condition code of NE == "not equal".

凑诗 2024-12-06 10:25:14

它是 If-Then 指令系列的一部分(这是对 Thumb-2 代码使用条件执行的唯一方法)

查看此链接:
http://infocenter.arm。 com/help/index.jsp?topic=/com.arm.doc.dui0204j/Cjabicci.html

It's part of the If-Then family of instructions (which is the only way to use conditional execution for Thumb-2 code)

Check out this link:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Cjabicci.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文