PicBasic supports a number of mathematical and logical functions which make calculations easy in programs. The operations are performed on integer numbers only with 16-bit precision and there is no floating-point number format. Also, all math operations are performed strictly from left to right. The operators supported are
addition
subtraction
* multiplication
** most significant bit (MSB) of multiplication
/ division
// remainder in a division MIN limit
to minimum value MAX limit to maximum value
& bitwise AND
| bitwise OR
^ bitwise XOR
&/ bitwise AND NOT
|/ bitwise OR NOT
^/ bitwise XOR NOT
Multiplication is done on 16 16 bit numbers, resulting in a 32-bit result.
The “*” operator returns the
lower
16-bits of the 32-bit result.
Similarly, the “**”
operator returns the
upper 16-bits of the result. For example,
W2 W1 * W0 ‘ Multiply W1
with W0. The lower 16-bits of the result
‘ are
placed in W2
or,
W2 W1 ** W0 ‘ Multiply W1
with W0. The upper 16-bits of the result
‘ are
placed in W2
W2 W1 * 100 ‘ Multiply W1
with 100. Place the lower 16-bits of the
‘ result in W2. Note
that this is the multiplication
‘ found in most programming languages
Similarly
with division,
or,
W2
W1 /
W0 ‘ Divide W1 by W0. The result is placed in W2
W2 W1 //
W0 ‘ Divide W1 by W0. The remainder is placed in W2
MIN
is used to limit the result to the minimum value
defined. For example, B1 B0 MIN 100
Sets B1 to the smaller of B0 and
100, i.e. B1 cannot be greater than
100.
Similarly,
MAX is used to limit the result to the maximum value defined. For example,
B1 B0 MAX 100
sets B1 to the larger of B0 and 100, i.e. B1 will be between 100 and 255.
Bitwise logical operations operate on the entire byte and these operations can be used to extract bits from bytes or to set and clear bits of a byte. For example, to extract
the least significant bit of
B0 we can write
B0 B0 & 000001
Similarly,
to set bit 2 of B1 to be 1 we can
write
B1 B1 | 000100
To
store the upper four bits of B2 in B1 we
can write
B1 B2 & %11110000
Comments
Post a Comment