Thursday, April 2, 2009

Arm Cortex M3 If-then (IT) instruction

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 is true

Each of [x], [y] and [z] can be either T (then) or E (else), which refers to the base condition , whereas uses traditional syntax such as EQ, NE, GT, or the like.

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

1 comments:

josimpso said...

Hi, I am the publisher of Joseph Yiu's book 'The Definitive Guide to the ARM Cortex-M3'. Would you be interested in provideing a short review of the new edition plans for this book, in return for a fee? I would need your comments (in the form of a questionnaire) by end of day 14 June, so not much time!

Thanks
Jonathan Simpson
j.simpson@elsevier.com
Publisher
Newnes Press / Elsevier
www.elsevierdirect.com