EasyManua.ls Logo

Parallax BASIC Stamp 2e - Absolute Value (ABS); Cosine (COS); Decoder (DCD); Inverse

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
BASIC Stamp Architecture ABS, COS, DCD, ~, -
Page 64 BASIC Stamp Programming Manual 2.0b www.parallaxinc.com
The Absolute Value operator (ABS) converts a signed (two’s complement)
16-bit number to its absolute value. The absolute value of a number is a
positive number representing the difference between that number and 0.
For example, the absolute value of -99 is 99. The absolute value of 99 is
also 99. ABS works on two’s complement negative numbers. Examples of
ABS at work:
Result VAR WORD
Result = -99 ' Put -99 (2's complement format) into Result.
DEBUG SDEC ? Result ' Display it on the screen as a signed #.
DEBUG SDEC ? ABS Result ' Display it on the screen as a signed #.
The Cosine operator (COS) returns the two’s complement, 16-bit cosine of
an angle specified as an 8-bit (0 to 255) angle. See the explanation of the
SIN operator, below. COS is the same in all respects, except that the cosine
function returns the x distance instead of the y distance. To demonstrate
the COS operator, use the example program from SIN, below, but
substitute COS for SIN.
The Decoder operator (DCD) is a 2
n
-power decoder of a four-bit value.
DCD accepts a value from 0 to 15, and returns a 16-bit number with the
bit, described by value, set to 1. For example:
Result VAR WORD
Result = DCD 12 ' Set bit 12.
DEBUG BIN ? Result ' Display result (%0001000000000000)
The Inverse operator (~) complements (inverts) the bits of a number. Each
bit that contains a 1 is changed to 0 and each bit containing 0 is changed to
1. This process is also known as a “bitwise NOT” and one's compliment.
For example:
Result VAR BYTE
Result = %11110001 ' Store bits in byte Result.
DEBUG BIN ? Result ' Display in binary (%11110001).
Result = ~ Result ' Complement Result.
DEBUG BIN ? Result ' Display in binary (%00001110).
The Negative operator (-) negates a 16-bit number (converts to its two’s
complement).
SYMBOL Result = W1
Result = -99 ' Put -99 (2's complement format) into Result.
Result = Result + 100 ' Add 100 to it.
DEBUG Result ' Display Result (1)
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
2
e
2
sx
2
p
2
1
2
e
2
sx
2
p
2
1
ABSOLUTE VALUE: ABS
C
OSINE: COS
D
ECODER: DCD
I
NVERSE: ~
N
EGATIVE
:
-

Table of Contents

Related product manuals