Shop OBEX P1 Docs P2 Docs Learn Events
Array implementation in Assembly — Parallax Forums

Array implementation in Assembly

IratenateIratenate Posts: 7
edited 2011-03-12 20:51 in Propeller 1
I'm interested in implementing an array in assembler code. Is it possible?

I would think memory declaration would be something like:

arrayName res 8

for an array of 8 longs, but how would I reference a specific cell? How would I write to a specific cell?

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-03-12 11:00
    You use memory pointers and self modifying code.

    This is an example given to me by Mike Green
    DAT
    ''Snippet - Self modifying code
                            movd   :moveIt,#rxBuffer     ' initialize destination address
                            movs   :moveIt,#otherBuf     ' initialize source address
                            mov     count,#5            ' number of longs to copy.  Also allows for pipeline delay
    :moveIt                 mov     0-0,0-0              ' "0-0" is just a placeholder
                            add     :moveIt,bothIncr     ' increment both source and destination
                            djnz    count,#:moveIt       ' count down, loop if not yet zero
    
    
    
    bothIncr                long    1<<9 + 1               ' ones line up with source and destination
    
    count                   res     1
    rxBuffer                res     5
    otherBuf                res     5  
    
  • IratenateIratenate Posts: 7
    edited 2011-03-12 12:08
    Thanks for the quick response!

    So if I understand this correctly.....
    The self modifying code treats the value stored at register :moveIt as any other variable.
    movd sets the destination register to the new array 0th spot
    movs sets the source register to the original array 0th spot
    mov count, #5 sets up the loop
    mov 0-0,0-0 executes the mov command on the data previously stored at :moveIt. The 0-0 place holder makes it so the data in the source and destination fields is not overwritten?
    %1000000001 is added to to :moveIt, effectively incrementing both source and destination register locations.
    repeat 5 times.

    Makes sense, somewhat. I think I got it.

    Thanks!

    One more question. Where can I find out more info on the place holder and its uses?
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-03-12 12:20
    >mov 0-0,0-0 executes the mov command on the data previously stored at :moveIt.
    > The 0-0 place holder makes it so the data in the source and destination fields is not overwritten?

    No, that would not completly be correct.

    :moveIt is just a label name for your assembler to know what space in ram you are referring to.

    The assembler think you are creating a command that looks like: mov 0,0
    But before your executed code comes to this position, you have already changed the value 0 and 0
    to ram addresses that the labels RxBuffer and otherBuf represents (eg self modyifing code)

    As pasm uses absolute values for ram location and not the difference from * (here) in a -512 to 512
    and also a fix bitwith(32) for each and every instruction.
    It makes it very ease to point to a new ram location by just increasing that part of the code line.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-03-12 12:34
    @Iratenate:
    I'm not sure whether you understand what's been said or whether I understand what you said.

    Each PASM instruction has a structure. There are some bits that tell the COG what instruction it is, there are some bits which tell what to do with flags and result, there are 9 bits which tell the COG the destination address and there are 9 bits for the source address or a nine bit immediate value (depending on another bit in the instruction) - all in all 32 bits.

    movd simply replaces the destination address of the instruction that's found at the destination address of movd (:moveIt in this case)
    #rxBuffer means that the address of rxBuffer (the COG-RAM address) is stored as destination address.

    movs replaces the source address of the instruction that's found at the destination address of movs.
    #otherBuf means that the address of otherBuf is stored as source address.

    So, when mov 0-0, 0-0 is executed the first time it will find the address of rxBuffer as source and the address of otherBuf as destination, so it will copy the content of rxBuffer to otherBuf. Of course this overwrites whatever was at otherBuf before.
    The instruction in that time is:

    mov otherBuf, rxBuffer

    %x000000001000000001
    %xdddddddddsssssssss

    x=bits 18-31
    d=destination address
    s=source address or immediate value
    So, if you add %000000001000000001 you increment the source and the destination by 1

    So, in the 2nd iteration the instruction at :moveIt is:
    mov (otherBuf+1), (rxBuffer+1)

    The placeholder is nothing else but a convention. It makes it easier to identify code which is meant to be self-modifying. You can write what you want, as its overwritten anyways by movd and movs.
  • IratenateIratenate Posts: 7
    edited 2011-03-12 13:13
    That clears it up quite a bit. The place holder is there for compiling, but during run time the source and destination location is rewritten on the fly. Superb.

    Thanks.
  • AribaAriba Posts: 2,690
    edited 2011-03-12 20:28
    Iratenate wrote: »
    The place holder is there for compiling...

    Not exactly. The place holder is there for the human that reads the code. It just marks the part of the instructions that are modified so you can understand the source code better.
    For the compiler it does not matter, you can set these placeholders to anything (valid), they get anyway overwritten by the code at runtime.

    Andy
  • kwinnkwinn Posts: 8,697
    edited 2011-03-12 20:51
    Iratenate, do keep in mind that cog memory is very limited and that some of it has to be used for program code. If you use hub ram to store your array there is considerably more memory available and cog memory can be used to hold pointers to the hub ram. That would permit larger arrays and even multidimensional arrays (ie ArrayElement [x] [y]
Sign In or Register to comment.