Shop OBEX P1 Docs P2 Docs Learn Events
PASM code question — Parallax Forums

PASM code question

10gigbill10gigbill Posts: 79
edited 2011-08-03 16:50 in Propeller 1
New at PASM. Need some help. I would like to pass 4 ascii characters (numbers) to a pasm obj and have them converted to binary and added together . So I thought I would start simple and just try to add some numbers and have the pasm obj return a value. See attached code.
Something is not quite right….
adder_test.spinadder.spin

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-02 13:49
    Hi 10gigbill,

    I think it's not so much a problem of the PASM but of the SPIN-part

    The first method of SPIN-program starts after booting the propeller.
    If this first method is executed the cog is shutdown

    this means
    PUB summit is executed with zeros and thats all
    method "start" and method "what" never execute
    {{ this is adder  July 29 2011
    accepts 2 32 bit words and adds them }}
    CON
    
    VAR
      long cog
      long total  
      long num1
      long num2
    
    
    Pub Main
      summit (10,5)
      start
      'need some kind of feedback here to tell if the result is stored in total
      'or a call of what to give back the sum
       
    PUB summit(no1,no2)
      num1 := no1
      num2 := no2
    
    PUB start
     cog :=  cognew(@init_asm, @total) +1
       
    PUB what : cow
     cow := total   
      
    DAT
    
    {{  Assembly language  }}
    
                  org       0
    init_asm
                  
    gohere
                  mov       p,   par           'save the start address in p     
                  mov       t1,  par
                  add       t1,  #4            'get the starting address of the params
                  rdlong    v1,  t1            ' v1 is now first number
                  add       t1,  #4            ' get second numbers address
                  rdlong    v2,  t1            'v2 is added to first number
                  add       v1,  v2
                 
                  wrlong    v1,  p              'write v1 to p
    
    v1     res   1
    v2     res   1
    t1     res   1        
    p      res   1
    

    So I would add Debug-output to PST.EXE with using FullDUplexSerial
    to see what the program flow does

    keep the questions coming
    best regards

    Stefan
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-08-02 13:49
    The cog does it's math and shuts down before you hand it the numbers to add.

    If you just swap the submit() and start() method calls, then add a wait between the start and the what() call, it should work.
  • 10gigbill10gigbill Posts: 79
    edited 2011-08-02 18:25
    Thanks guys that helped explain the intermittent software problem. I made a few changes and it now adds 4 numbers ok. I would like to have the PASM object running in its on cog available at any time with a single call.
    Could I change PUB summit(no1,no2,no3,no4) to PUB summit(no1,no2,no3,no4):cow
    So then I would only make one call?


    Billadder4.spinadder_test.spin
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-02 18:53
    @Bill: Before it becomes an issue for you, you should terminate your PASM code in some way (endless loop/cog shutdown). The way it is now it keeps executing data after the wrlong (whatever managed to get into cog RAM) and more often than not has unwanted side-effects.
  • 10gigbill10gigbill Posts: 79
    edited 2011-08-02 20:58
    Thanks for the help. I made a few changes and it seems to be working as I wanted.
    I have spent many long hours to get this far. If it weren't for you guys helping out I don't think
    I could have found all the answers in the two propeller books I have. I will attach my latest.
    Thank you for your support

    Billadder5_test.spinadder5.spin
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-02 22:36
    That's what this forum is for: Answering questions you can't answer yourself (after having done your "homework" meaning trying to solve the problem yourself for some time)

    As you are working with PASM do you know PASD?

    keep the questions coming
    best regards

    Stefan
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-08-03 08:52
    If you want to tighten up your code, here are some tips:

    1) The :loop label can be moved down a line to remove an unneeded instruction.
    2) You don't need both the init_asm and gohere label, they are both the same address.
    3) As your submit method executes, your PASM cog it already adding the existing values. A more appropriate way to do this might be to have the PASM code wait for a "submit" variable to be set, or the fourth variable to be a non-zero or non-negative-one.
    4) In the same idea as the previous tip, returning a "done" flag would be worthwhile because, all you do it grab the total, as soon as you have set the values to add. I am surprised the timing works correctly. It seems the first run through of the submit method might actually return an incorrect result. This will be absolutely a necesity when you create longer PASM algorithms.

    Something like this:
    VAR
      long cog
      long total  
      long done
      long num1
      long num2
      long num3
      long num4
    
    PUB start
      done := -1 ' mark as not ready
      cog :=  cognew(@init_asm, @done) +1
     
    PUB summit(no1,no2,no3,no4): cow
      num1 := no1
      num2 := no2
      num3 := no3
      num4 := no4
      done := 0
      repeat until (done)  ' stay here until PASM completes its code.
      cow := total
    
    
    DAT
    
    {{  Assembly language  }}
    
                  ORG
    init_asm
                  mov       doneP, par           'save the start address in doen pointer
                  mov       p, doneP
                  sub       p, #4                ' get total location
    
    :loop         rdlong    nothing,  doneP WZ ' read done pointer (use a nothing address because we don't care about the value)
                                               ' you could add a wait in here to save some power, but it will slow down the initiation
            IF_NZ jmp       #:loop             ' if not ready, keep waiting
                  mov       t1,  par
                  add       t1,  #4            'get the starting address of the params
                  rdlong    v1,  t1            ' v1 is now first number
                  add       t1,  #4            ' get second numbers address
                  rdlong    v2,  t1            ' v2 is added to first number
                  add       v1,  v2
                  add       t1,  #4
                  rdlong    v2,  t1            'get 3rd number
                  add       v1,  v2
                  add       t1,  #4
                  rdlong    v2,  t1            'get 4th number
                  add       v1,  v2        
                 
                  wrlong    v1,  p              'write v1 to p
                  wrlong    negone, doneP       ' flag as done
                  jmp       #:loop
                  
    
    negone        LONG      -1
                  
    v1            res
    v2            res
    t1            res        
    p             res
    doneP         res
    nothing       res
    
                  FIT
    
  • 10gigbill10gigbill Posts: 79
    edited 2011-08-03 12:15
    Thanks Bobb
    you are right. I have added more code, as my original goal was to have a PASM object to convert
    4 octal digits to binary. and it does return a wrong answer the first time. I will try to make your
    recommended changes and will post it here a little later... thanks again... this is all a great educational
    adventure.

    Bill
  • 10gigbill10gigbill Posts: 79
    edited 2011-08-03 16:50
    OK now check this. it just returns Zero. But it looks nice... my result is not getting passed back. hummmm?
    I'm think the octal to binary convert code is OK it was returning the right answer (on second call) before.
    something not quite in the right place...?

    Billadder8_test.spinadder8.spin edit... 10minutes later... I think I got it, have to add 4 to par since we stuck in "done"
    and add 4 to the first number fetch.. now is working...
Sign In or Register to comment.