itte in arm assembly

Johnathon picture Johnathon · Aug 12, 2011 · Viewed 8.3k times · Source

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)

Answer

Dan picture Dan · Aug 13, 2011

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.