ARM VERSION 1.2 Computer Hardware User Manual


 
Writing ARM and Thumb Assembly Language
2-22 Copyright © 2000, 2001 ARM Limited. All rights reserved. ARM DUI 0068B
2.5.3 Using conditional execution in ARM state
You can use conditional execution of ARM instructions to reduce the number of branch
instructions in your code. This improves code density.
Branch instructions are also expensive in processor cycles. On ARM processors without
branch prediction hardware, it typically takes three processor cycles to refill the
processor pipeline each time a branch is taken.
Some ARM processors, for example ARM10
and StrongARM
®
, have branch
prediction hardware. In systems using these processors, the pipeline only needs to be
flushed and refilled when there is a misprediction.
2.5.4 Example of the use of conditional execution
This example uses two implementations of Euclids Greatest Common Divisor (gcd)
algorithm. It demonstrates how you can use conditional execution to improve code
density and execution speed. The detailed analysis of execution speed only applies to
an ARM7
processor. The code density calculations apply to all ARM processors.
In C the algorithm can be expressed as:
int gcd(int a, int b)
{
while (a != b) do
{
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
You can implement the gcd function with conditional execution of branches only, in the
following way:
gcd CMP r0, r1
BEQ end
BLT less
SUB r0, r0, r1
B gcd
less
SUB r1, r1, r0
B gcd
end