View Full Version : array := array
codeking
02-01-2007, 11:52 PM
Does the assignment operator (:=) work with arrays.· For example, we have array1 and array2.· Each one has 12 bytes.· If we do array1 := array2, will array 2's data be copied to array1?· If not, how do I do this?
Mike Green
02-01-2007, 11:59 PM
The assignment operator does not work with arrays. You can use bytemove/wordmove/longmove to do a copy (look at the manual) or you can do a loop with something like "repeat i from 0 to 11", then "array1[ i] := array2[ i]".
Bergamot
02-02-2007, 12:02 AM
I think the assignment operator would cause both array variables to point to the same array in memory (which does not sound like what you want).
Instead, I'd use
bytemove(@Array1, @Array2, 12)
EDIT: Curse you Mike Green! http://forums.parallax.com/images/smilies/smilewinkgrin.gif
EDIT #2: Is it faster to loop by hand? Is longmove faster than bytemove or wordmove?
Mike Green
02-02-2007, 12:10 AM
Bergamot,
If the array is word or long aligned, wordmove is faster and longmove is fastest since 2 bytes or 4 bytes are moved with two Hub accesses.
Mike
codeking
02-02-2007, 12:17 AM
is there some way I could do something like this?
IF array1 == array2
Mike Green
02-02-2007, 12:47 AM
None of the operators in Spin work on arrays. You could write a subroutine (method) that takes two array addresses and compares them byte by byte and returns either true or false like:
PRI compare(addr1,addr2,size)
repeat size
if byte[addr1++] <> byte[addr2++]
return false
return true
Then you could write "if compare(@array1,@array2,12)"
codeking
02-02-2007, 12:56 AM
Thanks, Mike
asterick
02-02-2007, 02:21 AM
Mike Green said...
Bergamot,
If the array is word or long aligned, wordmove is faster and longmove is fastest since 2 bytes or 4 bytes are moved with two Hub accesses.
Mike
Do RD{WORD|LONG} require that they be on their appropriate boundaries? the docs don't say anything about that.
Bergamot
02-02-2007, 02:30 AM
asterick said...
Mike Green said...
Bergamot,
If the array is word or long aligned, wordmove is faster and longmove is fastest since 2 bytes or 4 bytes are moved with two Hub accesses.
Mike
Do RD{WORD|LONG} require that they be on their appropriate boundaries? the docs don't say anything about that.
I think he meant that it's faster if the words/longs are properly aligned, but works (though more slowly) if they are not.
Regardless, the docs don't seem to address that issue either.
Paul Baker
02-02-2007, 02:35 AM
Any WORD|LONG operation works on aligned boundries only, the Propeller masks out bits of the address to ensure this (last LSB for WORD, last two LSB for LONG).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)
asterick
02-02-2007, 02:36 AM
Good to know. *scampers off to fix a bug in gear*