Adding Words
Archiver
Posts: 46,084
I was passed along the following code to add 2 words, data(1) and
data(2), together:
work var word
work=0
work=work+data(1) 'Load first byte
work=work+work<<8 'Shift to High byte
work=work+data(2) 'Add in Low byte
My question: Where in the STAMP users manual is this shown on how to
shift to the High byte ("work+work<<8")
..or is there something in the public domain on programming with the
STAMP besides what Parallax offers?
Thanx
Jay
data(2), together:
work var word
work=0
work=work+data(1) 'Load first byte
work=work+work<<8 'Shift to High byte
work=work+data(2) 'Add in Low byte
My question: Where in the STAMP users manual is this shown on how to
shift to the High byte ("work+work<<8")
..or is there something in the public domain on programming with the
STAMP besides what Parallax offers?
Thanx
Jay
Comments
pcb4u@e... writes:
> I was passed along the following code to add 2 words, data(1) and
> data(2), together:
>
> work var word
> work=0
>
> work=work+data(1) 'Load first byte
> work=work+work<<8 'Shift to High byte
> work=work+data(2) 'Add in Low byte
>
> My question: Where in the STAMP users manual is this shown on how to
> shift to the High byte ("work+work<<8")
>
> ..or is there something in the public domain on programming with the
> STAMP besides what Parallax offers?
There seems to be a lot of unnecessary work here. If the point is to move to
bytes into one word-sized variable, it's pretty easy:
work = (highByteVar << 8) + lowByteVar
or, if you want to deal with the bytes individually:
work.HIGHBYTE = highByteVar
work.LOWBYTE = lowByteVar
If, indeed, you want to add the value of the two seperate bytes (perhaps they
are independently modfied by your program) to a word-sized variable, one way
is to alias your byte variables to a word variable, then it's simple addition.
Note that the Stamp considers all of it's memory an array -- in fact, the
array index is just an offset (same size as target) from the target variable.
The point is, you don't have to declare an array to use one. Take a look.
work1 VAR Word
work2 VAR Word
byteVar VAR wordVar2.HIGHBYTE ' can be called byteVar(0)
byteVar2 VAR wordVar2.LOWBYTE ' can be called byteVar(1)
work1 = work1 + work2
This technique does not use any more memory than your example and it's
certainly easier to understand. BTW, the discussion of the shift operators
is on page 244 of the Stamp manual (Version 1.9).
-- Jon Williams
-- Dallas, TX
The << operator is documented with the rest of the operators (page 239 of
the printed manual I have). You can also read about it on page 140 of my
Microcontroller Projects with Basic Stamps which organizes functions and
operators by function instead of alphabetically.
However, I'm not clear what your code is trying to do. You imply that
data(1) is a byte. since work=0 then work+data(1) is just data(1). So a
simple transliteration of your code could be:
work var word
work = work + (data(1)<<8) + data(2)
If work did not have zero in in, then your code doesn't add at all. Because
adding the high byte to the low byte would be a bad idea. In other words,
work=$80AA data(1)=$01 data(2)=$00 (you know arrays start at 0 right?). So
work+data(1)=$80AB, after the shift you have $AB00 and then adding zero
leaves that as the answer instead of $81AA.
However, there are several other ways to do this that might be more
efficient. For example:
work.highbyte=work.highbyte+data(1)
work=work+data(2) ' don't use lowbyte or carry won't propagate
You can also equate variables (like a union in C or an equivalence in
Fortran if anybody else is that old). However, with the arrays, starting at
1 I'm not sure if that is an option. Consider this (data is a reserved word,
so I'm using datas):
dword var word
datas var dword.lowbyte
work var word
datas(0)=$AA ' low part
datas(1)=$55 ' high part
debug hex dword
work = work + dword ' work = work + $55AA
However, I can't figure how to make this work with a partial array. The code
above works because every variable is an array in PBasic if you want it to
be. However, I couldn't see an easy way to "slide" the equivalence to a
separate element in an existing array.
Regards,
Al Williams
AWC
* Floating point math for the Stamp, PIC, SX, or any microcontroller:
http://www.al-williams.com/awce/pak1.htm
>
Original Message
> From: Jay Kobelin [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=96bAYz-x2bmtRG5KKiY0FKcnuBE3fvQGPWsOCLgq2B2I1xT5Tt6VO6ktzG7sHneSVtgaQNNgvHH69yk5]pcb4u@e...[/url
> Sent: Saturday, October 07, 2000 12:02 PM
> To: basicstamps@egroups.com
> Subject: [noparse][[/noparse]basicstamps] Adding Words
>
>
> I was passed along the following code to add 2 words, data(1) and
> data(2), together:
>
> work var word
> work=0
>
> work=work+data(1) 'Load first byte
> work=work+work<<8 'Shift to High byte
> work=work+data(2) 'Add in Low byte
>
> My question: Where in the STAMP users manual is this shown on how to
> shift to the High byte ("work+work<<8")
>
> ..or is there something in the public domain on programming with the
> STAMP besides what Parallax offers?
>
> Thanx
>
> Jay
>
>
>
>
>
>data(2), together:
> work var word
>work=0
>work=work+data(1) 'Load first byte
> work=work+work<<8 'Shift to High byte
> work=work+data(2) 'Add in Low byte
>My question: Where in the STAMP users manual is this shown on how to
>shift to the High byte ("work+work<<8")
>..or is there something in the public domain on programming with the
>STAMP besides what Parallax offers?
Hi Jay,
There's a bitty bug in that code, as the shift should read:
work=work+(work<<8) ' with parens
or
work=work<<8+work ' executes left to right
because the stamp executes stricly from left to right, without precedence,
unless you use parentheses.
Also, DATA is a reserved word for the stamp. Better choose a different
variable name. No need for the initial "work=0". The whole thing can be
put on one line:
work=mydata(1)<<8+mydata(0) ' combine two bytes into one word.
Array variables are numbered 0,1,...
There are some tricky ways to do it by using alias variable names:
work var word
mydata var work.byte0 ' name for the low byte of work
' now mydata(0) and mydata(1) are implicitly the two bytes of work.
' no assignment statement required.
I recall that Al gave a great response where this came up in the earlier
thread, to point out the alternatives using the SERIN modifiers to combine
bytes that arrive via a SERIN command.
The << operators are explained in the stamp manual v1.9 on pg 246. This
use of << to combine bytes is elementary computer science.
Here are some links that lead into a huge amount of free material on stamp
programming:
http://www.stampsinclass.com
http://www.hth.com/losa.htm
http://www.geocities.com/stampic/links.htm#top12
http://www.seetron.com/links.htm
http://www.al-williams.com/wd5gnr/stampfaq.htm
http://www.emesystems.com/BS2links.htm
-- Tracy Allen
Electronically Monitored Ecosystems
http://www.emesystems.com