Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itte in arm assembly

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)

like image 699
Johnathon Avatar asked Aug 12 '11 15:08

Johnathon


People also ask

How do you write if else in ARM assembly?

The else if part in the C source is redundant). Also, as an ARM-specific optimization, this looks like a use-case for predicated instructions. Like cmp r1, r2 ; subgt r1,r1, r2 ; sublt r2,r2, r1 ; bne loop . (If "eq" is true, neither of the predicated sub instructions will execute and we fall right out of the loop.

What is LSR in ARM assembly?

LSR provides the unsigned value of a register divided by a variable power of two. Both instructions insert zeros into the vacated bit positions. ROR provides the value of the contents of a register rotated by a value. The bits that are rotated off the right end are inserted into the vacated bit positions on the left.

What is TST in ARM assembly?

The TST instruction performs a bitwise AND operation on the value in Rn and the value of Operand2 . This is the same as a ANDS instruction, except that the result is discarded.


2 Answers

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.

like image 96
Dan Avatar answered Oct 03 '22 01:10

Dan


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

like image 32
Paul R Avatar answered Oct 03 '22 01:10

Paul R