4: BASIC Stamp Architecture – Math and Operators
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 63
work on smaller byte or nibble values. It just means that the computation
is done in a 16-bit workspace. If the value is smaller than 16 bits, the
BASIC Stamp pads it with leading 0s to make a 16-bit value. If the 16-bit
result of a calculation is to be packed into a smaller variable, the higher-
order bits are discarded (truncated).
Keep this in mind, especially when you are working with two’s
complement negative numbers, or moving values from a larger variable to
a smaller one. For example, look at what happens when you move a two’s
complement negative number into a byte (rather than a word):
Value VAR BYTE
Value = - 99
DEBUG SDEC ? Value ' Show signed decimal result (157).
How did -99 become 157? Let’s look at the bits: 99 is %01100011 binary.
When the BASIC Stamp negates 99, it converts the number to 16 bits
%0000000001100011, and then takes the two’s complement,
%1111111110011101. Since we’ve asked for the result to be placed in an 8-
bit (byte) variable, the upper eight bits are truncated and the lower eight
bits stored in the byte: %10011101.
Now for the second half of the story. DEBUG’s SDEC modifier (on the BS2,
BS2e, BS2sx and BS2p) expects a 16-bit, two’s complement value, but
we've only given it a byte to work with. As usual, it creates a 16-bit value
by padding the leading eight bits with 0s: %0000000010011101. And what’s
that in signed decimal? 157.
Table 4.5 lists the available Unary Operators. Note: the BS1 only supports
negative (-).
Operator Description Supported By:
ABS Returns absolute value All except BS1
COS
Returns cosine in two's compliment
binary radians
All except BS1
DCD 2
n
-power decoder All except BS1
~ Inverse All except BS1
- Negative All
NCD Priority encoder of a 16-bit value All except BS1
SIN
Returns sine in two's compliment
binary radians
All except BS1
SQR Returns square root of value All except BS1
2
2
2
UNARY OPERATORS.
Table 4.5: Unary Operators. Note:
the BS1 only supports the negative
(-) unary operator.
2
2
2