Shop OBEX P1 Docs P2 Docs Learn Events
Copying arrays — Parallax Forums

Copying arrays

UghaUgha Posts: 543
edited 2009-02-17 04:55 in General Discussion
Whats the best way to copy one array into another?

Would this work:
put array1, array2

Or would it only copy the first element?

What if I wanted to exclude an element from the copy... is there an easier way to do that than just a long for-loop?

Also... if I have this situation:
array1 var byte(10)
array2 var byte(10)
array3 var byte(10)
 
if num = 1 then
    ... long loop and other stuff that requires knowledge of which array it is...
    array1(0) = 99
elseif num = 2 then
    ... another long loop ...
    array2(0) = 99
elseif num = 3 then
    ... a third long loop...
    array3(0) = 99
endif
 

Can I somehow transfer the address of the array into a placeholder or something with each if then do a single block of code on that placeholder?
This is an example of how·I first thought it'd work.

array1 var byte(10)
array2 var byte(10)
array3 var byte(10)
arrayX var byte
 
if num = 1 then
    arrayX = @array1
elseif num = 2 then
    arrayX = @array2
elseif num = 3 then
    arrayX = @array3
endif
 
... Long loop ..
arrayX(0) = 99

This of course doesn't work, but perhaps it'll give you a better understanding of what I'm asking.

Thanks in advance for your help.

PS: I'd run a bunch of tests on my own but I won't have access to my SX for at least a day and I want to work on code now.

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2009-02-13 07:41
    In SX/B 2.0 you can do this:

    PUT array1, array2(X) TO array2(Y)
    


    I actually looked at the compiled output for this:

    PUT array1, array2
    


    ...and it seems to do what you want (good to know). Analyzing the generated code shows that PUT will move as many bytes as are in array2, so this should be equal to or less than the size of array1. For example, if you had these declarations:

    array1          VAR     Byte (3)
    array2          VAR     Byte (8)
    


    The copy is fine but the first five elements of array2 become corrupted. In the attached graphic I've marked array1 in green and array2 in red -- note that the first five values of array2 have gone from 0, 1, 2, 3, 4 to 3, 4, 5, 6, 7 (test code is attached).

    Finally, SX/B has an internal array called __RAM(), which is the entire RAM space, so you can do this:

    pntr = @array2
    __RAM(pntr) = 99
    


    Of course, you can modify pntr after the initial assignment to get to different elements of the array.

    Post Edited (JonnyMac) : 2/16/2009 5:54:17 AM GMT
    585 x 357 - 163K
  • mojorizingmojorizing Posts: 249
    edited 2009-02-16 05:28
    With regard to your intention for a "placeholder" you can use indirect addressing
    'arrayX(0) = 99
    'becomes this
    
     \mov FSR,arrayX
     \mov W,#$99
     \mov IND,W
    

    ·Bean, you might want to look at this.· To make this work, I needed to use #$99 and not #99 as expected for my data type to get the compiler to give me 99 decimal. I'm using ver. 3.2.92h

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Bad spellers of the world untie!

    Post Edited (mojorizing) : 2/17/2009 4:54:45 AM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2009-02-16 05:59
    Mojo: You code seems to work fine. It shows as 63 (hex) in the a register array but when aliased and used in a watch directive it's fine.
  • mojorizingmojorizing Posts: 249
    edited 2009-02-17 04:55
    Yep, JonnyMac, there's no issue with the data type.

    Thanks,
    kevin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Bad spellers of the world untie!
Sign In or Register to comment.