2
2
e
D
: /
IVIDE
2
1
2
e
2
2
e
is the upper byte of the multiplier (0 to 255 whole units) and the fraction is
the lower byte of the multiplier (0 to 255 units of 1/256 each). The */ (star-
slash) instruction gives you an excellent workaround for the BASIC
Stamp's integer-only math. Suppose you want to multiply a value by 1.5.
The whole number, and therefore the upper byte of the multiplier, would
be 1, and the lower byte (fractional part) would be 128, since 128/256 = 0.5.
It may be clearer to express the */ multiplier in hex—as $0180—since hex
keeps the contents of the upper and lower bytes separate. Here's an
example:
2
2
Value1
VAR
sx
p
Value1= 100
Value1= Value1*/ $0180
debug ? Value1
To calculate constants for use with the */ instruction, put the whole
number portion in the upper byte, then use the following formula for the
value of the lower byte: Hint: INT( fraction * 256). For instance, take Pi ( π ,
3.14159). The upper byte would be $03 (the whole number), and the lower
would be INT(0.14159 * 256) = 36 ($24). So the constant Pi for use with */
would be $0324. This isn't a perfect match for Pi, but the error is only
about 0.1%.
The Divide operator (/) divides variables and/or constants, returning a
16-bit result. Works exactly as you would expect with unsigned integers
2
2
from 0 to 65535. Use / only with positive values; signed values do not
p
sx
provide correct results. Here's an example of unsigned division:
1
SYMBOL
Value1 = W0
SYMBOL
Value2 = W1
Value1= 1000
Value2= 5
Value1= Value1 / Value2
DEBUG Value1
-- OR --
2
2
Value1
VAR
p
sx
Value2
VAR
Value1= 1000
Value2= 5
Value1= Value1 / Value2
DEBUG DEC ? Value1
4: BASIC Stamp Architecture – /
WORD
' Multiply by 1.5 [1 + (128/256)]
' Show result (150).
WORD
WORD
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 69
' Divide the numbers.
' Show the result (200).
' Divide the numbers.
' Show the result (200).
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?