Shop OBEX P1 Docs P2 Docs Learn Events
Simple array question... — Parallax Forums

Simple array question...

KyeKye Posts: 2,200
edited 2009-01-09 23:27 in Propeller 1
Hello, I've been trying to figure out how to access an array in asm for some time now and I can't seem to find out how to do so.

I know how to acess an array in the main memory, but how do I access an array within a cog's ram.

I've been·using this code so far.

                        mov     counter,         #25
checkKeyArray           mov     buffer,          #keyArray
                        add     buffer,          counter
 
                        mov     buffer,          ??????????????
                      
                        ' More code here.
 
                        djnz    counter,         #checkKeyArray 

The idea is that I intialize a counter to the number of longs in the array (26) minus 1 (25).

Then I add the index of the array to a storage buffer.

And then I add the the value of the counter to the buffer, which will give me the memory location within the ram of the cog I am running the code on where the element I want in the array exist.

But how do I use the value in buffer,·which is now a pointer to the element in the array I want, to get that element?

I think I'm just missing something. There should be a simple command to do this.·The write and read operation to the main memory are the only aviable operators that do what I want...except that they don't operate on cog ram...

Thank you for your help.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Comments

  • KyeKye Posts: 2,200
    edited 2008-12-26 23:33
    Wait, would this work?

                            mov     counter,         #25
    checkKeyArray           movs    arrayPointer,    #keyArray
                            add     arrayPointer,    counter
    arrayPointer            mov     buffer,          0
     
                            ' More code here..
     
                            djnz    counter,         #checkKeyArray
    

    I'm not sure if this type of self modifying code is safe to use...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,

    Post Edited (Kye) : 12/26/2008 11:40:05 PM GMT
  • mynet43mynet43 Posts: 644
    edited 2008-12-26 23:46
    Here's an example to access an array defined in the spin program:

    VAR
    
      long  cog, delay
    
      long  cmd, chnl, nsamples, data          '7 longs (3 longs, 8 words)
    
    PUB in(channel)
    
    '' Read the current sample from an ADC channel (0..7)
    
      chnl := channel
    
      do_cmd(_single, @chnl)        ' get single sample from channel
    
      return data.word[noparse][[/noparse]channel]
    
    PRI do_cmd(adc_command, argptr)
    
      cmd := adc_command << 16 + argptr                 'write command and pointer to shared memory loc
      repeat while cmd                                  'wait for command to be cleared, signifying completion
    
    ' ...
    
                            mov     t3, arg0                'load channel number, 0..7
                            shl     t3, #1                  'mult by 2 for word offset in data[noparse][[/noparse] ]
                            add     t3, #12                 'add 12 for 3 long offset (cmd + chnl + nsamples)
                            mov     t1,par                  'reset sample pointer
                            add     t1, t3                  'add offset, t1 now points to data[noparse][[/noparse]channel]
                            wrword  t2,t1                   'write sample to data[noparse][[/noparse]channel]
    
    
    



    Note the "data" variable in the VAR statement should be an array of 4 longs. For some reason, the insert code directive dropped this[noparse]:([/noparse]

    Let me know if you need more info, these are only code snippets...


    Jim
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-26 23:55
    You have to use self-modifying code to access arrays. The only thing you need more is a NOP before the "mov buffer,0" because the "mov" is fetched before the "add" finishes. The "nop allows the "add" to finish so the address will be correct. Sometimes you can rearrange the code to put an unrelated instruction before an instruction that's modified. In your case, you're running the loop backwards starting at keyArray+25. Better might be
                             movs  checkKeyArray,   #keyArray
                             mov    counter,             #25
    checkKeyArray   mov    buffer,                0-0
                             add     checkKeyArray,  #1
                             ' ...
                             djnz    counter,             #checkKeyArray
    


    For storing into the array, you need to use the destination field like
                             movd  checkKeyArray,  #keyArray
                             mov    counter,             #25
    checkKeyArray   mov    0-0,                   buffer
                             add     checkKeyArray,  incrByOne
                             ' ...
                             djnz    counter,             #checkKeyArray
                             ' ...
    incrByOne          long    %1_000000000   ' A one in the destination field
    
  • KyeKye Posts: 2,200
    edited 2008-12-27 00:33
    Ah,·thanks very much mike.

    Maybe there should be an instruction that does just this though?... =)



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-27 05:10
    Mike Green said...
    You have to use self-modifying code to access arrays.
    
    
    There's no indirect addressing, no address modification of any kind?· I'm still studying the book, and haven't attempted any Propeller assembly yet, but I had only just begun to suspect these deficiencies.· You've got to use self-modifying code?· And it has no registers either -- all instructions work storage-to-storage (and the book calls storage locations "registers", which usage I thought passé several·decades ago).

    Do I read the book correctly?· I find these things very difficult to believe.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-27 05:39
    Carl,
    These are very simple RISC (reduced instruction set) computers these cogs are. They have very limited storage (512 32-bit words of which 16 are special purpose). Their main purpose is to provide software defined peripherals that can do "bit bang" I/O at pretty high speed which they do. Most of the access to anything like an array is to shared (hub) memory where there is a level of indirection using what is functionally an I/O instruction (RDxxxx/WRxxxx).

    Maybe I'm jaded having programmed pretty much every kind of computer from the 1960's to the present and used all kinds of programming languages as well, but I don't find the Propeller at all strange or deficient for the kind of applications it seemed to have been developed for.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-27 06:01
    Mike Green said...

    Maybe I'm jaded having programmed pretty much every kind of computer from the 1960's to the present and used all kinds of programming languages as well
    Maybe I'm jaded, too, having programmed pretty much every kind of computer from the late 1950s to the present and used all kinds of programming languages as well.· Having to learn COBOL·for the sole reason that I had to disassemble and understand·the·MVS Cobol compiler in order to insert code dynamically in compiled programs in order to intercept I/O calls for Y2K conversion was truly dispiriting, though intellectually challenging (the interception, not the COBOL).· And did you ever use APL?· I did.· Now that's really strange -- but we differ, and anyway we're talking about assembly languages, of which I must have used twenty or more, starting with FAP and progressing to MAP on IBM 709s, vacuum tube machines.· Then there was SOAP -- but I digress.· Indexing would be difficult without registers, but indirect addressing, at least, surely would have been easy to implement.

    If this is a RISC machine, then apparently the Propeller Assembly Language is an interpreted language too, in the same sense that the System/360 instruction set was implemented by interpretation in microcode.· That, if true, would have made indirection even easier to implement.

    Happy New Year anyway.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • rjo_rjo_ Posts: 1,825
    edited 2009-01-08 02:36
    Kye,

    How are you doing? Did your question get answered?

    Mike, I don't like the term "self-modifying code."... what you are actually doing is synthesizing an instruction, using information not available at compile time... I'm not going to start a protest over it... but the verbiage implies a level of complexity that is way beyond what is actually happening... (it is kind of scary and misleading to hear that you have to use self-modifying code to address an array). What you are actually doing is "synthesizing an instruction" ... "creating a routine" ... "using the Prop-wise approach to array manipulation" ... anything but "self-modifying code" [noparse]:)[/noparse]

    Carl... you were programming when I was born. I took one computer course in college... fortran... and quickly decided that computers would never be very useful because "who in their right mind would use punch cards and then wait a week to see any output?" I have really only been exposed to 2 assembly languages... the 68000 and PASM.

    So my track record on the op/ed issues isn't perfect...And my computing experience is nothing to crow about.

    Like you, I initially rebelled at the idea that I had to use self-modifying code just to access an array. The mistake I was making was that I was looking at the Prop as a microprocessor... which it is of course... but more than that it is a micro-controller. It is built to address a very specific set of design constraints as efficiently as possible.

    It takes a while to see the absolute beauty and some of the reasons for the design decisions.

    As you know, every instruction you add to an assembly language adds some latency to the responsiveness of the chip... at least under some circumstances of usage. And any strategy you use to cope with this fact also imposes penalties... make too many compromises and there goes your usable bandwidth.

    I personally think there are too many instructions.


    Rich
  • Paul BakerPaul Baker Posts: 6,351
    edited 2009-01-08 19:38
    Kye said...

    Ah,·thanks very much mike.

    Maybe there should be an instruction that does just this though?... =)



    The next Propeller has some form of indexing built into the cog, not a replacement of the current system but an alternate method. Since it's proprietary information at the moment, I left all notes and reports on it's exact operation with Parallax, so I can't provide details. But Chip has described it's operation in the forums, and a bit of diligent searching on search.parallax.com should provide some insight. Of course it will be full documented upon release of the chip.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker


    Post Edited (Paul Baker) : 1/8/2009 7:47:48 PM GMT
  • KyeKye Posts: 2,200
    edited 2009-01-08 19:45
    Ah that's nice.

    For a good example of array acess also check out my keyboard driver. It acess a 2D array made up of bytes... stored in longs (that's why its 2D). Its a bit hard to follow but it's a very good example.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mparkmpark Posts: 1,305
    edited 2009-01-09 19:56
    Paul, you left Parallax?!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2009-01-09 21:03
    Yeah about a month and a half ago, but I'll still be a presence on the forums, just not quite as much as before.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
  • mparkmpark Posts: 1,305
    edited 2009-01-09 23:27
    That's too bad. I can't imagine leaving the Shangri-la that I envision Parallax to be.
Sign In or Register to comment.