Shop OBEX P1 Docs P2 Docs Learn Events
Arrays in ASM? — Parallax Forums

Arrays in ASM?

JavalinJavalin Posts: 892
edited 2008-02-20 17:53 in Propeller 1
Hello All,

How do I do arrays in Propeller ASM?

I've declared:
··· ubxData················ res········ 50·

to store it.··Trying to get the address of the array using:
··· mov···· ubxDataPtr,@ubxData

results in "Source Register cannot exceed $1FF"

I guess thats not the way to do it?· I want to get the address of the beginning of the array into a counter, then increment it as I recieve data into it.

Im probably missing something simple!

Many thanks,

James

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-02-16 12:22
    Javalin said...
    Im probably missing something simple!
    No, just 20% of how the Propeller works smile.gif
    Why not read through my tutorial?

    BTW: It should be
    mov     ubxDataPtr, #ubxData
    


    But that is just the beginning of the story....

    Post Edited (deSilva) : 2/16/2008 12:28:36 PM GMT
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-16 12:28
    The question is does Javalin want the array in the hub or in the cog?
  • JavalinJavalin Posts: 892
    edited 2008-02-16 12:35
    In the Cog! I can do arrays in the HUB.

    Just reading deSilva's tutorial for that extra 20%...!

    J
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-16 12:40
    then your mov should be an rdlong. See the manual for details. Note that the res only reserves space in the cog, not in the hub so you need
    ubxData long/word/byte [noparse][[/noparse]amount of data]

    Also note that you will need a variable in the cog to store the address of the array in smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-16 12:44
    thanks Chaps - think i've got it now...

    Javalin
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-02-16 16:54
    If you want the array in the cog, then MOV is the instruction to use, not RDLONG. The latter accesses the hub. Besides, one would not use RES for an array in the hub anyway, since it doesn't reserve any room there. Indexing arrays in the cog is not as straightforward as it is for the hub, though, since the Propeller doesn't have a true indirect or indexed addressng mode. We have to resort to self-modifying code to pull it off. Here's an example of how one might sum all fifty values from your array:

            mov     ptr,#ubxData   'Point to first array element.
            mov     ctr,#50        'Initialize counter.
            mov     sum,#0         'Initialize sum.
    
    :loop   movs    :add,ptr   'Put pointer value into add instruction's source field.
            add     ptr,#1     'Increment the pointer. (Due to pipelining, there has to be at least one instruction here.)
    :add    add     sum,0-0    'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
            djnz    ctr,#:loop 'Back for another.
    
    
    


    -Phil
  • JavalinJavalin Posts: 892
    edited 2008-02-16 17:19
    Interesting - thanks Phil!
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-16 17:23
    Well, that's why I wrote the "Tutorial", so that Phil need not always repeat those basics.
    Still recommending reading it smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-16 18:40
    deSilva,

    I looked (I think at the right thread) after you said - but could'nt see an obvious ref. I assume its the Assembly06 or Assembly07 PDF's in the tutorial stickey thread?

    J

    Post Edited (Javalin) : 2/16/2008 6:55:33 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-02-16 18:40
    Normally, I wouldn't have bothered. But since misdirection was afoot, I thought it best to correct it forthwith.

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-16 18:45
    Javalin said...
    I looked (I think) after you said - but could'nt see an obvious ref. I assume its the Assembly06 or Assembly07 PDF's in the tutorial stickey thread?
    Its in the stickies and called "deSilva's machine language tutorial" and it will lead you here:
    http://forums.parallax.com/showthread.php?p=668559

    @Phil: Right!
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-16 21:13
    Sorry, I read the answer the wrong way around and thought he wanted the array in the HUB. I'm glad that there are other people around here that can correct me. smile.gif

    Edit: done it again confused.gif

    Post Edited (stevenmess2004) : 2/16/2008 9:57:42 PM GMT
  • JavalinJavalin Posts: 892
    edited 2008-02-16 21:35
    I thought so.

    smile.gif

    J
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-02-16 22:22
    Okay, quiz time. These two programs, both of which execute more quickly than my example above, sum the same array elements and produce the same result. How is that possible?

            movs    :add,#ubxData  'Point to first array element.
            mov     ctr,#50        'Initialize counter.
            mov     sum,#0         'Initialize sum.
    
    :loop   add     :add,#1        'Increment the pointer.
    :add    add     sum,0-0        'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
            djnz    ctr,#:loop     'Back for another.
    
    
    



            movs    :add,#ubxData  'Point to first array element.
            mov     ctr,#50        'Initialize counter.
            mov     sum,#0         'Initialize sum.
    
    :add    add     sum,0-0        'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
            add     :add,#1        'Increment the pointer.
            djnz    ctr,#:add      'Back for another.
    
    
    


    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-17 03:19
    I guess deSilva is not admitted to the quizz? But he already hears the people squall wrt the first box smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-17 17:15
    Ok - so I don't quite geddit.· I've read deSilva's tutorial (which is excellent BTW).· I get that we're using the MOVS/MOVD commands to alter the commands at the labels dynamically to address a block of memory - but I am still having issues.

    How to I write to a COG based array?

    Based on PhilPi's read code above...· For an example i'm trying to·write "1" in each·location and then read it back and sum.··From Phil's read example·I assume that I can swap the "add sum,0-0" to "add 0-0, #1" and change the movS to a movD as·I am·writing to not reading from.
                            movd    :add1,#ubxData      'Point to first array element.
                            mov     ctr,#50             'Initialize counter.
    :add1                   add     0-0,#1              'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
                            add     :add1,#1            'Increment the pointer.
                            djnz    ctr,#:add1          'Back for another.
                            
                            movs    :add2,#ubxData      'Point to first array element.
                            mov     ctr,#50             'Initialize counter.
                            mov     sum,#0              'Initialize sum.
    :add2                   add     sum,0-0             'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
                            add     :add2,#1            'Increment the pointer.
                            djnz    ctr,#:add2          'Back for another.
    
    
    
    

    However, as you've probably guess'd it doesn't work - it·returns·a sum total of "1".· Help?

    thanks,

    James

    PS - deSilva - your EX07C would seem to have a typo on the last line - surely should read "X··· RES 20"???

    (Updated for clarity)

    Post Edited (Javalin) : 2/17/2008 8:30:52 PM GMT
  • JavalinJavalin Posts: 892
    edited 2008-02-17 18:03
    PhilPi,

    I'll take the first stab then - I assume there is an answer though?

    The first example has more instructions and uses a seperate ptr variable then spends time putting it back into ":add". The third example is the tidyest - uses the loop ":add" var and the ptr in the same instruction.

    Marks out of ten?

    James

    Post Edited (Javalin) : 2/18/2008 9:09:00 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-17 18:36
    James,
    (b) wrt the quizz that is Phil's matter smile.gif

    (a) Your example... You are a little bit incoherent... What is "SUM" which you say is "1"? As there is just a part of the program one has to giess.
    You understand that you now always add to UBXDATA and to nothing else, as the "dest" of the ADD instruction is never changed again.... What you modify is the "source" part, so the "SUM" should be 2450 in the end... smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-17 20:30
    deSilva,

    I've updated the post - sorry about that.

    James
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-17 20:36
    I see, but that's definitely confusing, How can you expect to use ADD to fill a cell in COG meory?

    See:
    (a) You have to MOV the value in question
    (b) So it is "dest" of MOV that has to be modified
    (c) That can no longer be done by ADD ... #1 as this modifies "source" only
    (d) So think of a way to add to the "dest" field. (Hint: there is NO special instruction for this!)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-02-18 06:48
    James,

    Regarding your quiz answer: you're right as far as it goes. But look at the last two programs and where the embedded pointer is incremented, with respect to the add instruction itself. The point of the quiz is to identify how these two programs sum the same array elements.

    -Phil
  • hippyhippy Posts: 1,981
    edited 2008-02-18 10:17
    Phil Pilgrim (PhiPi) said...
    :loop   add     :add,#1        'Increment the pointer.
    :add    add     sum,0-0        'Add array element to sum. (0-0 is just a placeholder for the pointer value.)
    
    
    


    Very clever. My first thought was, "that's not right", but of course it is. The comment on the :loop line isn't entirely complete though, but I guess that would have spoilt the fun of the quiz.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-18 20:51
    Counting: That was #1 squalling smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-19 22:04
    All,

    Many thanks for the help on this.· Got it working.

                            mov     ubxCount,#ubxData   ' Point to first array element.
                            movd    :add1,ubxCount      ' move to the dest of the :add1 instruction
                            mov     ctr,#50             ' Initialize counter.
    :add1                   mov     0-0,#1              ' Add array element to sum. (0-0 is just a placeholder for the dest adddress)
                            add     ubxCount,#1         ' Next one
                            movd    :add1,ubxCount      ' update the dest in :add1
                            djnz    ctr,#:add1          ' Back for another.
                                                    
                            
                            movs    :add2,#ubxData      ' Point to first array element.
                            mov     ctr,#50             ' Initialize counter.
                            mov     sum,#0              ' Initialize sum.
    :add2                   add     sum,0-0             ' Add array element to sum. (0-0 is just a placeholder for the Src address)
                            add     :add2,#1            ' Increment the pointer.
                            djnz    ctr,#:add2          ' Back for another.
     
    .....
     
    ubxCount                long        0
    sum                     long        0
    ctr                     long        0
    ubxData                 res         50
    


    :-)

    I'll post the full code tomorrow (for COG and HUB example)·when I get a chance (for those interested).

    James
    ·
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-19 22:12
    Excellent!
    DeSilva noticed of course immediately that the comment on the :add1-line is misleading, but that's just by the way smile.gif

    No you have time to go back to Phil's quizz. He showed an "optimization" there. You already remarked
    that the principle was to substitute a full memory cell by a 9-bit field of an instruction..
    It something similar possible for this pre-setting of the array?
  • JavalinJavalin Posts: 892
    edited 2008-02-20 17:53
    deSilva - I don't think so.· Philpi's optimization seems to be based on that he can increment·the src register with the add instruction.

    I re-looked at my code and I can set the inital address of #asmDataArray (new variable names in the posted example) with "movd :write1,#asmDataArray"·but I then cannot·add to·it without writing a counter variable to it using "movd :write1,#idxCount".·

    The only other way would be using a shift instruction - but that seems messy...

    In the interim i'll post these two examples - which work - but maybe optimizable....

    (Debug_PC.spin and fullduplexserial.spin are needed for these to run as the output is to a serial terminal...)

    Can I have another hint?

    Thanks,

    James

    Post Edited (Javalin) : 2/20/2008 6:02:56 PM GMT
Sign In or Register to comment.