itte in arm assembly
Asked Answered
R

4

19

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)

Riviera answered 12/8, 2011 at 15:17 Comment(0)
W
55

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.

Wildawildcat answered 13/8, 2011 at 13:56 Comment(2)
Nice overview. Some background for benefit of readers who might not be all that familiar with ARM: 32-bit ARM opcodes each had condition bits to control whether the opcode should execute or not. When the original Thumb variant was developed, there wasn't room in the 16 bit opcodes to dedicate to conditional execution bits, so only branch opcodes had conditional execution (like many other CPUs). Thumb2 added these 'conditional opcodes' to permit short sequences of conditional execution (ie., to prevent short conditional code sequences from killing the pipeline).Lisabeth
Great explanation! But what if I want more instructions in the conditional block (not limited to 4)? The only way is using branch?Coastal
S
4

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.

Selfhypnosis answered 20/12, 2015 at 14:54 Comment(0)
C
3

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

Changeup answered 12/8, 2011 at 15:24 Comment(0)
G
1

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

Gabby answered 12/8, 2011 at 15:29 Comment(2)
Ok, so what're the instructions actually saying?Riviera
OT: I you know some japanese, that line is hilariously fitting :)Insist

© 2022 - 2024 — McMap. All rights reserved.