4.7 Expressions and Operators
Expressions can consist of unary operators (one operand, e.g.,
not
) or binary operators (two operands, e.g., +
). The
operators allowable in expressions are listed in the table below.
Operators within expressions are evaluated left to right, thus the expression 5 + 1
* 2
will yield the value 12. Use parenthesis to change the order of operator
execution, for example 5 + (1 * 2)
will yield the value 7.
With the exception of the equality (e.g. =
) and
relational operators (e.g. >=
), all the operators listed can be freely
combined in both constant and relocatable expressions. Relocatable expressions will be
evaluated by the linker at link time, after psects have been positioned and symbol values
have been determined.
Operator | Purpose | Example |
---|---|---|
* |
multiplication | movlw 4*33,w |
+ |
addition | bra $+1 |
- |
subtraction | DB 5-2 |
/ |
division | movlw 100/4 |
= or
eq |
equality | IF inp eq 66 |
> or
|
signed greater than | IF inp > 40 |
>= or
ge |
signed greater than or equal to | IF inp ge 66 |
< or
lt |
signed less than | IF inp < 40 |
<= or
le |
signed less than or equal to | IF inp le 66 |
<> or
ne |
signed not equal to | IF inp <> 40 |
low |
low byte of operand | movlw low(inp) |
high |
high byte of operand | movlw high(1008h) |
highword |
high 16 bits of operand | DW highword(inp) |
mod |
modulus | movlw 77mod4 |
& or
and |
bitwise AND | clrf inp&0ffh |
^ |
bitwise XOR (exclusive or) | movf inp^80,w |
| |
bitwise OR | movf inp|1,w |
not |
bitwise complement | movlw not 055h,w |
<< or
shl |
shift left | DB inp>>8 |
>> or
shr |
shift right | movlw inp shr 2,w |
rol |
rotate left | DB inp rol 1 |
ror |
rotate right | DB inp ror 1 |
float24 |
24-bit version of real operand | DW float24(3.3) |
nul |
tests if macro argument is null |