3.6.9. Operator precedence

The assembler includes an extensive set of operators for use in expressions. Many of the operators resemble their counterparts in high-level languages such as C (see Unary operators and Binary operators).

There is a strict order of precedence in their evaluation:

  1. Expressions in parentheses are evaluated first.

  2. Operators are applied in precedence order.

  3. Adjacent unary operators are evaluated from right to left.

  4. Binary operators of equal precedence are evaluated from left to right.

Note

The order of precedence is not exactly the same as in C.

For example, (1 + 2 :SHR; 3) evaluates as (1 + (2 :SHR: 3)) = 1 in armasm. The equivalent expression in C evaluates as ((1 + 2) >> 3) = 0.

You are recommended to use brackets to make the precedence explicit.

Table 3.2 shows the order of precedence of operators in armasm, and a comparison with the order in C.

If your code contains an expression which would parse differently in C, armasm normally gives a warning:

A1466W: Operator precedence means that expression would evaluate differently in C

The warning is not given if you use the -unsafe command line option.

Table 3.2. Operator precedence in armasm

armasm precedenceequivalent C operators
unary operatorsunary operators
* / :MOD:* / %
string manipulationn/a
:SHL: :SHR: :ROR: :ROL:<< >>
+ - :AND: :OR: :EOR:+ - & |
= > >= < <= /= <>== > >= < <= !=
:LAND: :LOR: :LEOR:&& ||

Table 3.3. Operator precedence in C

C precedence
unary operators
* / %
+ - (as binary operators)
<< >>
< <= > >=
== !=
&
^
|
&&
||

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.

Copyright © 2000, 2001 ARM Limited. All rights reserved.ARM DUI 0068B
Non-Confidential