Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
ARM architecture family
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Conditional execution==== Almost every ARM instruction has a conditional execution feature called [[predication (computer architecture)|predication]], which is implemented with a 4-bit condition code selector (the predicate). To allow for unconditional execution, one of the four-bit codes causes the instruction to be always executed. Most other CPU architectures only have condition codes on branch instructions.<ref>{{cite web |title=Condition Codes 1: Condition flags and codes |url=https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/condition-codes-1-condition-flags-and-codes |website=ARM Community |date=11 September 2013 |access-date=26 September 2019}}</ref> Though the predicate takes up four of the 32 bits in an instruction code, and thus cuts down significantly on the encoding bits available for displacements in memory access instructions, it avoids branch instructions when generating code for small [[conditional (programming)|<code>if</code> statements]]. Apart from eliminating the branch instructions themselves, this preserves the fetch/decode/execute pipeline at the cost of only one cycle per skipped instruction. An algorithm that provides a good example of conditional execution is the subtraction-based [[Euclidean algorithm]] for computing the [[greatest common divisor]]. In the [[C (programming language)|C programming language]], the algorithm can be written as: <syntaxhighlight lang="c"> int gcd(int a, int b) { while (a != b) // We enter the loop when a < b or a > b, but not when a == b if (a > b) // When a > b we do this a -= b; else // When a < b we do that (no "if (a < b)" needed since a != b is checked in while condition) b -= a; return a; } </syntaxhighlight> The same algorithm can be rewritten in a way closer to target ARM [[instruction set architecture|instructions]] as: <syntaxhighlight lang="c"> loop: // Compare a and b GT = a > b; LT = a < b; NE = a != b; // Perform operations based on flag results if (GT) a -= b; // Subtract *only* if greater-than if (LT) b -= a; // Subtract *only* if less-than if (NE) goto loop; // Loop *only* if compared values were not equal return a; </syntaxhighlight> and coded in [[assembly language]] as:<!-- using nasm because "gas", although correct, does not recognize all the insns. the nasm lexer just looks for all uppercase on the other hand. --> <syntaxhighlight lang="nasm"> ; assign a to register r0, b to r1 loop: CMP r0, r1 ; set condition "NE" if (a β b), ; "GT" if (a > b), ; or "LT" if (a < b) SUBGT r0, r0, r1 ; if "GT" (Greater Than), then a = a β b SUBLT r1, r1, r0 ; if "LT" (Less Than), then b = b β a BNE loop ; if "NE" (Not Equal), then loop B lr ; return </syntaxhighlight> which avoids the branches around the <code>then</code> and <code>else</code> clauses. If <code>r0</code> and <code>r1</code> are equal then neither of the <code>SUB</code> instructions will be executed, eliminating the need for a conditional branch to implement the <code>while</code> check at the top of the loop, for example had <code>SUBLE</code> (less than or equal) been used. One of the ways that Thumb code provides a more dense encoding is to remove the four-bit selector from non-branch instructions.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)