armcpu-architecturemachine-instruction

What does the ARM7 IT (if then) instruction really do?


I am having trouble wrapping my head around the functionality of the IT instruction (if then). The quick reference card has this:

Operation: If-Then
Assembler: IT{pattern} {cond}
Action: Makes up to four following instructions conditional, according to pattern. pattern is a string of up to three letters. Each letter can be T (Then) or E (Else). The first instruction after IT has condition cond. The following instructions have condition cond if the corresponding letter is T, or the inverse of cond if the corresponding letter is E.

Actually, that synopsis makes a little bit of sense. The architecture manual entry didn't get me anywhere on the journey to understanding:

If-Then condition instruction.
Syntax

IT{x{y{z}}} cond

Where: x specifies the condition switch for the second instruction in the IT block.
y Specifies the condition switch for the third instruction in the IT block.
z Specifies the condition switch for the fourth instruction in the IT block.
cond Specifies the condition for the first instruction in the IT block.

The condition switch for the second, third and fourth instruction in the IT block can be either:
T Then. Applies the condition cond to the instruction.
E Else. Applies the inverse condition of cond to the instruction.

Note

It is possible to use AL (the always condition) for cond in an IT instruction. If this is done, all of the instructions in the IT block must be unconditional, and each of x, y, and z must be T or omitted but not E. Operation

The IT instruction makes up to four following instructions conditional. The conditions can be all the same, or some of them can be the logical inverse of the others. The conditional instructions following the IT instruction form the IT block.

The instructions in the IT block, including any branches, must specify the condition in the {cond} part of their syntax.

Since (most) every instruction can easily specify a conditional, what good is the IT instruction?


Solution

  • First note that most instruction can specify a condition code in ARM instruction, not in Thumb.

    With IT instruction, you can specify condition code for up to 4 instructions. For each instruction, you specify if it's part of the If (T) or Else (E).

    For example:

    ITTET EQ
    ADD r0,r0,r0
    ADD r1,r0,r0
    ADD r2,r0,r0
    ADD r3,r0,r0
    

    Will actually translate to:

    ADDEQ r0,r0,r0  (Always if for 1st one)
    ADDEQ r1,r0,r0  (T for 2nd one)
    ADDNE r2,r0,r0  (E for 3rd one)
    ADDEQ r3,r0,r0  (T for 4th one)