Shop OBEX P1 Docs P2 Docs Learn Events
Newbie assembly questions — Parallax Forums

Newbie assembly questions

william chanwilliam chan Posts: 1,326
edited 2008-09-02 18:34 in Propeller 1
Consider the simple ASM code below
said...

PUB AddNums(Num1,Num2)
Num1_ := Num1
Num2_ := Num2
cognew(@entry, @Num1)

DAT
org
entry add Num1_,Num2_ 'Add Num1 to Num2
wrlong Num1_, par 'Return result to Num1
CogId CogNum 'Get COG ID
CogStop CogNum 'Stop this COG

CogNum long 0 'Reserved variables
Num1_ long 0
Num2_ long 0

Questions.

1. Does "par" stand for "Propeller Accumulator"?
2. Does the return value always have to be the 1st parameter?

Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my

Comments

  • tpw_mantpw_man Posts: 276
    edited 2008-09-02 03:06
    PAR stands for Parameter Register. It holds a 16 bit value, with the lower 2 bits wired to 0. It is set to the second parameter of cognew() when the cog starts up. what exactly do you mean by the return value being the first parameter?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I am 1011, so be surprised!


    Advertisement sponsored by dfletch:
    Come and join us on the Propeller IRC channel for fast and easy help!
    Channel: #propeller
    Server: irc.freenode.net or freenode.net
    If you don't want to bother installing an IRC client, use Mibbit. www.mibbit.com
    tongue.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-02 03:09
    PAR is a special register that holds the 2nd parameter to COGNEW while the cog is running.. Look it up in the Propeller manual for details.
    There's no "return value" from an assembly routine since the assembly routine doesn't really return. It just keeps running or the cog stops.
    This value (a 14 bit number positioned to work as the address of a long or a table of longs) passed to the cog is used to pass information into and out of the cog.
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-02 04:13
    But this line shows that the Return Value from Par is copied back to the 1st parameter Num1_

    wrlong Num1_, par     'Return result to Num1
    
    



    Does it always have to be the first parameter?

    But curiously after

    cognew(@entry, @Num1)
    
    



    there is no

    return Num1_

    I wonder why....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • potatoheadpotatohead Posts: 10,261
    edited 2008-09-02 04:28
    In the cognew:

    @entry refers to the address in HUB memory where an assembly language program, to be loaded in the COG exists. When the cognew is executed, the COG loads it's memory with the code located at @entry. @Num1 "points to" the address in the HUB that is the container for the value of Num1. This address, with the lower two bits set to 0, gets loaded into the PAR register as a single passed value to the assembly language program running in the COG. The assembly language program then can read this value and use it to fetch additional information from the HUB in a programmatic way. Say, values are stored sequentially in the HUB. Passing the first one is all that is really needed in the COG for the program to then fetch additional ones, as offsets from the first one given in PAR. ...or, perhaps nothing is passed to the COG as it knows where it needs to look in the HUB. In that case, the PAR register really does not matter and most of the time people just then pass a zero.

    PAR is read only to the assembly language program running on the COG.

    in the add:

    There is no accumulator, in the traditional sense of this word used to describe a working register!

    When you add two numbers on a register based CPU, you do something like:

    1 load accumulator with operand

    2 add accumulator to another operand in memory

    3 store accumulator to memory.

    There would be two operands somewhere in memory as well, ignoring immediate (operand part of instruction) type of addressing.

    On the Propeller, an add happens between two addresses, meaning all of that just ends up being:

    1 add destination operand, source operand.

    Still, there are two operands in memory, but only one add instruction. When the add is complete, the destination will contain the sum, eliminating the need to explicitly load and store.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!

    Chat in real time with other Propellerheads on IRC #propeller @ freenode.net
  • potatoheadpotatohead Posts: 10,261
    edited 2008-09-02 04:30
    "wrlong Num1_, par 'Return result to Num1" <--- these are backwards!! (tricky, and do look at the manual on those, as opposed to a mov)

    Really, this instructs the COG to write the value contained in Num1_, to the HUB address contained in PAR.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!

    Chat in real time with other Propellerheads on IRC #propeller @ freenode.net
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-02 06:59
    Ok. I get it now.

    wrlong goes backwards compared to mov.

    I wonder why Chip decided to design it backwards....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-02 13:15
    Mike,

    How to do

    waitcnt(clkfreq/200 + cnt)
    
    



    in Parallax assembly?

    Sorry for so many questions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-09-02 13:27
    Wrlong is backwards because of the way instructions are constructed in PASM. Only the source field can be an immediate value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-09-02 16:46
    william chan said...
    Mike,

    How to do

    waitcnt(clkfreq/200 + cnt)
    
    



    in Parallax assembly?

    Sorry for so many questions.

    No Problem,

    I'm not going to give you the code for division, you can get that from Propeller Guts: http://forums.parallax.com/showthread.php?p=572669
    So assuming the variable·delay has the contents of clkfreq/200:

    ...
      mov tcnt, cnt            'make a copy of counter
      add delay, tcnt          'calc cycle count to wait for
      waitcnt tcnt, delay      'wait until cycle tcnt occurs, then add delay (this automatically preps tcnt to the next waitcnt)
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Nick McClickNick McClick Posts: 1,003
    edited 2008-09-02 18:10
    Tack on a question to that;

    In spin, if I do a waitcnt(clkfreq * 5 + cnt)
    if the cnt register is just about to wrap, will I get unexpected results?

    Same in PASM?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Concentrate on understanding the problem, not applying the tool
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-09-02 18:13
    No because the counter is 32 bit, any math will roll over the answer just as the counter will do, so long as the delta is less than 2^32.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Nick McClickNick McClick Posts: 1,003
    edited 2008-09-02 18:34
    That's what I thought. I don't know why I pictured (clkfreq * 5 + cnt) rolling into a negative number & never being reached. I guess waitcnt is smart enough to know not to use two's complement.

    Good beginner point, though, that waitcnt can't wait beyond 2^32 clocks. I've done a;
    repeat 500
         waitcnt(clkfreq + cnt)
    
    


    To wait for significant periods of time

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Concentrate on understanding the problem, not applying the tool
Sign In or Register to comment.