Keil Logo

Difference between operator precedence in assembly language and C

7.28 Difference between operator precedence in assembly language and C

armasm does not follow exactly the same order of precedence when evaluating operators as a C compiler.

For example, (1 + 2 :SHR: 3) evaluates as (1 + (2 :SHR: 3)) = 1 in assembly language. The equivalent expression in C evaluates as ((1 + 2) >> 3) = 0.
ARM® recommends you use brackets to make the precedence explicit.
If your code contains an expression that would parse differently in C, and you are not using the --unsafe option, armasm gives a warning:
A1466W: Operator precedence means that expression would evaluate differently in C
In the following tables:
  • The highest precedence operators are at the top of the list.
  • The highest precedence operators are evaluated first.
  • Operators of equal precedence are evaluated from left to right.
The following table shows the order of precedence of operators in assembly language, and a comparison with the order in C.

Table 7-9 Operator precedence in ARM assembly language

assembly language precedence equivalent C operators
unary operators unary operators
* / :MOD: * / %
string manipulation n/a
:SHL: :SHR: :ROR: :ROL: << >>
+ - :AND: :OR: :EOR: + - & | ^
= > >= < <= /= <> == > >= < <= !=
:LAND: :LOR: :LEOR: && ||
The following table shows the order of precedence of operators in C.

Table 7-10 Operator precedence in C

C precedence
unary operators
* / %
+ - (as binary operators)
<< >>
< <= > >=
== !=
&
^
|
&&
||
Related concepts
Related reference
Non-ConfidentialPDF file icon PDF versionARM DUI0379H
Copyright © 2007, 2008, 2011, 2012, 2014-2016 ARM. All rights reserved. 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.