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