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
1
2
e
2
sx
2
p
2
U
NARY
O
PERATORS
.
Table 4.5: Unary Operators. Note:
the BS1 only supports the negative
(-) unary operator.
2
e
2
sx
2
p
2
Содержание BASIC Stamp 1
Страница 1: ...BASIC Stamp Programming Manual Version 2 0c...
Страница 30: ...Introduction to the BASIC Stamps Page 28 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 90: ...BUTTON BASIC Stamp Command Reference Page 88 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 118: ...END BASIC Stamp Command Reference Page 116 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 128: ...FREQOUT BASIC Stamp Command Reference Page 126 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 196: ...NAP BASIC Stamp Command Reference Page 194 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 206: ...OWIN BASIC Stamp Command Reference Page 204 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 214: ...OWOUT BASIC Stamp Command Reference Page 212 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 216: ...PAUSE BASIC Stamp Command Reference Page 214 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 226: ...POLLMODE BASIC Stamp Command Reference Page 224 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 232: ...POLLOUT BASIC Stamp Command Reference Page 230 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 236: ...POLLRUN BASIC Stamp Command Reference Page 234 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 240: ...POLLWAIT BASIC Stamp Command Reference Page 238 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 262: ...RCTIME BASIC Stamp Command Reference Page 260 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 274: ...RUN BASIC Stamp Command Reference Page 272 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 310: ...SEROUT BASIC Stamp Command Reference Page 308 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 324: ...STOP BASIC Stamp Command Reference Page 322 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Страница 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...