The if then instructions allow up to four succeeding instructions (called an IT block) to be conditionally executed. They are in following formats:
IT[x] [cond]
IT[x][y] [cond]
IT[x][y][z> [cond]
where:
[x] specifies the execution condition for the second instruction
[y] specifies the execution condition for the third instruction
[z] specifies the execution condition for the fourth instruction
[cond] Specifies the base condition of the instruction block; the first instruction following IT executes if
Each of [x], [y] and [z] can be either T (then) or E (else), which refers to the base condition
Here is an example of IT use:
if (R0 equal R1) then
{
R3 = R4 + R5
R3 = R3 / 2
}
else
{
R3 = R6 + R7
R3 = R3 / 2
}
This can be written as:
CMP R0, R1 ; Compare R0 and R1
ITTEE EQ ; If R0 equal R1, Then-Then-Else-Else
ADDEQ R3, R4, R5 ; Add if equal
ASREQ R3, R3, #1 ; Arithmetic shift right if equal
ADDNE R3, R3, #1 ; Add if not equal
ASRNE R3, R3, #1 ; Arithmetic shift right if not equal
Some IT instruction:
ITT
ITE
ITTT
ITEE
ITTE
ITTTT
ITEEE
ITTET
Reference: The Definitive Guide to the ARM Cortex-M3 by Joseph Yiu
Thursday, April 2, 2009
Arm Cortex M3 If-then (IT) instruction
Special Control Register that controls the ARM Cortex M3 Operation Mode
ARM Cortex M3, when running in thread mode, has two privilege modes: "privileged" and "user". When thread mode is in "user" mode, it can not access to system control space and some instructions that access to special registers such as MSR are restricted.
Special control register is used to define the privilege level and the stack pointer selection. This register has 2 bits, bit zero and bit one.
CONTROL[0] : Stack status
1 = Alternate stack is used
0 = Default stack (Main stack pointer) is used
CONTROL[1] : Privilege status
1 = User state in thread mode
0 = Privileged in thread mode
Control bit 1 is writable only when the core is in thread mode and privileged.
Control bit 0 is writable only in a privileged state. Once it enters the user state, the only way to switch back to privileged is to trigger an interrupt and change this bit in priveleged hander mode.
| Privileged | User | |
| When running an exception | Handler Mode | |
| When running main program | Thread Mode | Thread Mode |
Reference: The Definitive Guide to the ARM Cortex-M3 by Joseph Yiu pg 36