5.1.3
Expressions and
operators
An expression is a SymbolNote 1, constant (Numeric constantsNote 2, Character constants) , an operator combined with one of the above, or a combination of operators.
Note 1. | Only a name, a label, or an external reference name can be used for a symbol specified as an element of an expression. For the SIZEOF and STARTOF operators, a section name can be specified. |
Note 2. | When a device file is read, SFR symbols and extended SFR symbols can be handled in an expression in the same way as constants. |
Elements of an expression other than the operators are called terms, and are referred to as the 1st term, 2nd term, and so forth from left to right, in the order that they occur in the expression. The operators that can be used for a term are limited depending on the relocation attribute of the term.
The assembler supports the operators shown in "Table 5.4 Operator Types". Operators have priority levels, which determine when they are applied in the calculation. The priority order is shown in "Table 5.5 Operator Precedence Levels".
The order of calculation can be changed by enclosing terms and operators in parentheses "( )".
|
|
Arithmetic operators
|
+, -, *, /, %, +sign, -sign
|
Bit logic operators
|
~, &, |, ^
|
Relational operators
|
==, !=, >, >=, <, <=
|
Logical operators
|
&&, ||
|
Shift operators
|
>>, <<
|
Byte separation operators
|
HIGH, LOW
|
2-byte separation operators
|
HIGHW, LOWW, MIRHW, MIRLW, SMRLW
|
Special operators
|
DATAPOS, BITPOS
|
Section operators
|
STARTOF, SIZEOF
|
Other operator
|
( )
|
The above operators can also be divided into unary operators, special unary operators and binary operators.
Unary operators
|
+sign, -sign, ~, HIGH, LOW, HIGHW, LOWW, MIRHW, MIRLW, SMRLW, DATAPOS, BITPOS, STARTOF, SIZEOF
|
Binary operators
|
+, -, *, /, %, &, |, ^, ==, =, >, >=, <, <=, &&, ||, >>, <<
|
Table 5.5 | Operator Precedence Levels |
|
|
|
|
|
+sign, -sign, ~, HIGH, LOW, HIGHW, LOWW, MIRHW, MIRLW, SMRLW, DATAPOS, BITPOS, STARTOF, SIZEOF
|
|
*, /, %, >>, <<
|
|
+, -
|
|
&, |, ^
|
|
==, !=, >, >=, <, <=
|
|
&&, ||
|
Expressions are operated according to the following rules.
- | The order of operation is determined by the priority level of the operators.
When two operators have the same priority level, operation proceeds from left to right, except in the case of unary operators, where it proceeds from right to left. |
- | Sub-expressions in parentheses "( )" are operated before sub-expressions outside parentheses. |
- | Expressions are operated using 32-bit values.
When the intermediate value of an expression or a constant in an expression exceeds 32 bits during evaluation or when the resultant value of the constant or the expression after evaluation exceeds 32 bits, only the lower-order 32 bits are valid. No error will be output in this case.
Only for an expression specified as an operand for the .DB8 directive, each term is handled in 64 bits. |
- | Each term of an expression is handled as an unsigned integer, but in the following cases it is handled as a signed integer. |
Multiplication, division, division/multiplication, second term of logical shift
- | If the divisor is 0, an error occurs. |
- | Negative values are represented as two's complement. |
- | Relocatable terms are evaluated as 0 at the time when the source is assembled (the evaluation value is determined at link time). |
Table 5.6 | Evaluation examples |
|
|
5 + 8 - 6 * 2 / 4
|
10
|
5 + (8 - 6) * 2 / 4
|
6
|
(5 + 8 - 6)* 2 / 4
|
3
|
2 * (0x0F - (0x0B & (0x0A | 0x0F)))
|
8
|
2 * 0x0F - 0x0B & 0x0A | 0x0F
|
0x0F
|
HIGH(-1)
|
0xFF
|
HIGH(0x0FFFF)
|
0xFF
|
2 + 4 * 5
|
22
|
(2 + 3) * 4
|
20
|
10/4
|
2
|
0 - 1
|
0xFFFFFFFF
|
-1 > 1
|
1 (True)
|
EXTNote + 1
|
0
|
Note | EXT: External reference names |