Shop OBEX P1 Docs P2 Docs Learn Events
Bubble Sort PASM - Page 2 — Parallax Forums

Bubble Sort PASM

2»

Comments

  • ShawnaShawna Posts: 508
    edited 2013-07-14 11:40
    So then would these be valid operations using calculus during a mov command.
    mov temp1,Sort + i
     mov temp2,Sort + [i-1]
    
    Sort Res 11
    

    And if the above is valid than are these two pieces of code equivalent.
    'Spin
    long i
    long sort[11]
    
    
    i := 10
            If Sort[i] < Sort[i-1]
                  'do something
    
    
    'PASM
            mov temp1,Sort + i
            mov temp2,Sort + [i-1]
            
            cmps temp1,temp2  wc
       if_c 'do something
    
    
    temp1 long 0
    temp2 long 0
    i     long 10
    Sort  Res  11
    

    It has been complicated to for me the whole time.:smile:

    Thanks
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-07-14 11:54
    >So then would these be valid operations using calculus during a mov command. ?

    NO, there are no calculations and/or indirect addressing allowed.
    You have to change the source or destination pointer by poking around in the instruction itself.

    If there for some reason where no outerloop (most of the time there is)
    eg this part is only run through once since cognew
    you could remove the first 7 lines of code as there is no need to reset the values
    and have the compiler give you the first run to start out with (+1 is added at compile and not at runtime)
            mov   t1,#9            ' loop 
    lable1  cmps  sort+1,sort wc   ' c=1 if sort+1 is less than sort
    lable2  if_c  mov t2,sort      ' copy sort to temp
    lable3  if_c  mov sort,sort+1  ' copy sort+1 to sort
    lable4  if_c  mov sort+1,t2    ' copy temp to sort+1
            add   lable1,mybit9_0  ' add 1 to both source and dest field
            add   lable2,#1        ' add 1 to source field
            add   lable3,mybit9_0  ' add 1 to both source and dest field
            add   lable4,mybit9    ' add 1 to dest field
            djnz  t1,#lable1       ' loop 9 times after that never run this part again
    
  • ShawnaShawna Posts: 508
    edited 2013-07-14 15:00
    Holy Smile it just clicked I think!
    I could not figure out what you were doing with the movs and movd and then the labels. label1 ........ label4. mov destination of that line and then mov source of that line to the operator of the labeled line. Thats slick, I still haven't muddled my way completly through the code, but that is a pretty big piece that I wasn't grasping. Is that what is meant by self mod code?
    I am sure you were either yelling at me or laughing at me through your computer. Compile time vrs run-time helps me with the calculus part.
    Thanks for the patience.
  • Cluso99Cluso99 Posts: 18,069
    edited 2013-07-14 17:32
    In running these pieces of code I realize there are many ways to be really efficient in the use of cog RAM:

    e.g. moving code into body section of Kuroneko program snippet

    Sometimes it gets a bit cryptic and I am far from understanding how to maximize the use of each and every register within a code/data block of cog memory.

    Does anyone have an example of extremely (or the most) powerful program(s) ever loaded into a single cog? With 512 longs there has to be a limit but who has reached it and know that they can not pack another bit of functionality within 2K of logic?

    There should be some sort of Parallax Hall of Fame for generally acknowledged 'super-cog' examples that the novice can shoot for.

    This is probably a ridiculous piece of rhetoric but I am curious.

    sm
    While it is not easy to read, I think Chip's Spin Interpreter would have to win hands down for the most powerful and full use f the cog space. The code is very convoluted to squeeze everything in!
    I have extended the original to sequeeze/hide a zero footprint LMM style trace/debugger into the shadow registers.
  • ShawnaShawna Posts: 508
    edited 2013-07-14 19:00
    Hey Tony and the rest of you guys thanks for your help.
    From what I have learned I have been able to go from a command like this.

    mov sort +[12/3 + 2 * 4^6], temp

    which obviously worked in my head, but no where else.:smile:

    to this
            mov t4,#11
            movs  lable5,#axArray
            movd  lable5,#sort
    axsortloop
    lable5  mov 0-0,0-0
            add lable5,mybit9_0
    
    
            djnz  t4,#axsortloop
    
    

    Which moves my running ax array from my accelerometer into my sort array to be sorted.

    It replaces this.
            mov sort + 0 ,axArray + 0
            mov sort + 1 ,axArray + 1
            mov sort + 2 ,axArray + 2
            mov sort + 3 ,axArray + 3
            mov sort + 4 ,axArray + 4
            mov sort + 5 ,axArray + 5
            mov sort + 6 ,axArray + 6
            mov sort + 7 ,axArray + 7
            mov sort + 8 ,axArray + 8
            mov sort + 9 ,axArray + 9
            mov sort + 10,axArray + 10
    

    It doesn't save much space, but every little bit counts, I am going to jam all of this in Jason's 9DOF object.
    I need to figure out how to time sections of code in PASM, I suspect that the latter code is actually faster because there are no DJNZ commands but I am not sure.
    I suspect timing the code in PASM probably isn't much different than doing it in spin, I just haven't tried it yet.

    Thanks Again
    Shawn
  • kuronekokuroneko Posts: 3,623
    edited 2013-07-14 19:03
    This won't work as expected, you need at least one insn between the one modifying and the one being modified (there are exceptions when you expect the resulting behaviour but here it's essential).
            mov t4,#11
            movs  lable5,#axArray
            [COLOR="#FF0000"]movd  lable5,#sort[/COLOR]
    axsortloop
    lable5 [COLOR="#FF0000"] mov 0-0,0-0[/COLOR]
            add lable5,mybit9_0
    
            djnz  t4,#axsortloop
    
    Just reorder the setup phase:
            movs  lable5,#axArray
            movd  lable5,#sort
            [COLOR="#FFA500"]mov t4,#11[/COLOR]
    axsortloop
    lable5  mov 0-0,0-0
            add lable5,mybit9_0
    
            djnz  t4,#axsortloop
    
    And yes, the loop version is actually slower but shorter. It's up to you to find a healthy balance.
  • ShawnaShawna Posts: 508
    edited 2013-07-14 19:17
    Ah, you caught it.
    That is why it wasn't working, I just got done playing with it and it was missing my first mov command. I see what you did there I will have to try that real quick.
  • ShawnaShawna Posts: 508
    edited 2013-07-14 19:21
    Kuroneko, that did fix the problem.
    But what about this, it seems to work and saves 2 lines of code?
               mov t4,#11 
    axsortloop
    lable5  mov sort,axarray
              add lable5,mybit9_0
    
    
              djnz  t4,#axsortloop
    
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-07-14 20:00
    It works , but you never reset the values for future runs.
             mov t4,#11 
    axsortloop
    lable5   mov sort,axarray
             add lable5,mybit9_0
             djnz  t4,#axsortloop 
             movs  lable5,#axArray  ' restore 
             movd  lable5,#sort     ' restore
    

    Setting/Resetting the values before the loop and using 0 (eg 0-0) at intended locations
    to show that this code will self modify is the preferred way.
  • ShawnaShawna Posts: 508
    edited 2013-07-14 20:14
    Nice catch Tony, I see what your saying about resetting the code. The next time I ran through the loop it would have been really messed up.
    That would have really screwd with me when I tried to implement it in the 9DOF object.
  • lonesocklonesock Posts: 917
    edited 2013-07-22 10:37
    A quick way in PASM to get 2 values sorted (v1 <= v2):
    mov tmp, v1
    max v1, v2
    min v2, tmp
    
    Don't forget to use mins / maxs if you want to sort signed integers. And a median filter is a great idea over a regular moving average for noisy signal data. It excels at removing shot noise while still preserving hard edges (for things like looking at a square wave...the moving average will delay and soften the edges, while the median filter only delays them).

    Jonathan
Sign In or Register to comment.