Shop OBEX P1 Docs P2 Docs Learn Events
Re MOVS instruction as used in Keyboard.spin file what does it mean.. — Parallax Forums

Re MOVS instruction as used in Keyboard.spin file what does it mean..

OzStampOzStamp Posts: 377
edited 2008-02-25 21:37 in Propeller 1
Hi.

Trying to understand what the code does in· the keyboard.spin object.

Scanning thru the code..
There are 3 spots where the instruction MOVS· napshr,#X· (X is ·3 values 18 or 16 or 13) is
loaded .. I understand that I think..
At also the same locations there is the Call to the NAP routine..

The NAP routine has the napshr... shr instruction in it· ..see below..


nap··················· rdlong· t,#0·················· 'get clkfreq
napshr··············· shr···· t,#18/16/13········· 'shr scales time
···················· ··· min···· t,#3··················· 'ensure waitcnt won't snag
··················· ···· add···· t,cnt·················· 'add cnt to time
··················· ···· waitcnt t,#0·················· 'wait until time elapses (nap)
nap_ret

Nowhere is the napshr register defined not in the Res or long area ?
What am I missing here..?
Is this one of those automatic allocations of variables.. as it is in the·NAP routine..?



cheers·· Ron Mel oz



·

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-23 12:06
    The MOVS instruction moves the first 9 bits of the source register (or the immediate in this case) to the source field of the destination register. So this example moves X to napshr. if your X was 13 then your code would end up like this
    nap                    rdlong  t,#0                   'get clkfreq
    napshr                shr     t,#13          'shr scales time  'the source gets modified by the movs instruction
                             min     t,#3                    'ensure waitcnt won't snag
                             add     t,cnt                   'add cnt to time
                             waitcnt t,#0                   'wait until time elapses (nap)
    nap_ret
    



    What is nice is that it only modifies the source bits. Have a look at the MOVI and MOVD instructions as well. They do the same thing only with the instruction and destination parts of the instruction.

    Just guessing that this is part of a timing or delay routine?

    And very OT but not worth a thread, do you sell the Hydra book in Australia?
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 12:09
    smile.gif NAPSHR is defined exactly at the spot you quoted it! You missed the wood for all the trees.

    Note that the operand part #18/16/13 is just a comment, which shall indicate this wil be filled by one of those values later. However I should consider that as a bad practice -- too tricky!

    There are some remarks wrt self modifying code in my Tutorial...
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 12:28
    Oops sorry I edited this unintentionally ....

    This is an alternative version of NAP in a cleaner style.

        CALL #NAP
        LONG 13
    ....
    nap
            MOVS   :napset,  nap_ret
            ADD     nap_ret, #1
    :napset MOVS  :NAPSHR, 0-0                      
            rdlong  t,#0                      'get clkfreq
    :napshr shr     t,  #0-0               'shr scales time
            min     t,#3                      'ensure waitcnt won't snag
            add     t,cnt                     'add cnt to time
            waitcnt t,#0                      'wait until time elapses (nap)
    nap_ret  ret
    

    Post Edited (deSilva) : 2/23/2008 11:26:39 PM GMT
  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-23 19:59
    I think you've probably got your answer, if it wasn't clear the short answer is that "MOVS, MOVD, and MOVI" are used to modify parts of propeller assembly instructions, so the destination of those instructions will usually be a label in the code somewhere. In this case the keyboard function wants to adjust the parameter of the shift right, so it modifies the code using the MOVS. For some operations, notably indirection through a register, modifying the code is the only way to accomplish it.

    --Chuck
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 21:33
    Chuck, I am glad you jumped in at some occasions. You have a good knowledge of facts and a very "up-to-the-point" style which is highly esteemed here!
  • OzStampOzStamp Posts: 377
    edited 2008-02-23 22:53
    Hi SteveM, De Silva and Chuck.

    Thanx for the pointers.
    I do understand the MOVD MOVI and MOVS instructions.

    What is a little cryptic and still is..read on if you wish..

    Walking thru that Keyboard.spin object just for the purpose of me learning
    as to how it does what is does...I could not see anywhere down the bottom
    where it defines all the LONGS WORDS and RES's .. I could not find the
    NAPSHR variable(REGister)...that is a little odd to me.

    So what from I gather from DeSilva comment is that the NAPSHR in the
    NAP routine· NAP....NAP_RET· it is defined there..see below ther..

    nap···················· rdlong· t,#0·················· 'get clkfreq
    napshr················ shr···· t,#18/16/13········· 'shr scales time
    ······················ ·· min···· t,#3··················· 'ensure waitcnt won't snag
    ···················· ··· add···· t,cnt················· · 'add cnt to time
    ····················· ·· waitcnt t,#0··················· 'wait until time elapses (nap)

    nap_ret················ ret

    To me that shr· t,#18/16/13 also looks like a divide to me .. very cryptic..
    Nowhere in any doc's could I find anything related to that exact syntax.

    I have read DeSilva's asm tutorial numerous times and refer back to it often..
    It is usefull very usefull..
    Also PhiPi tricks and traps is printed out for ready persual at all times...

    The NAP routine to me is a simple PAUSE routine and the way it is written
    seems to me over complicated..I am sure there is a reason for it the way it is
    done but that eludes me presently..

    Laerning and loving it..

    cheers Ron Mel OZ..
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-23 23:01
    Ron,
    The NAP routine is a simple PAUSE routine, but it allows for different pause times by modifying the shift count before calling the routine.

    Yes, the notation "18/16/13" is confusing to the uninitiated. It looks like a divide because that's what the assembler does with it, but it's really a cryptic comment to show the possible values in the immediate source operand. The MIN instruction just ensures that the WAITCNT doesn't hang up because the wait time is too small. Remember that this routine is supposed to pause for a known time regardless of the clock speed being used (within reason).
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 23:04
    Ron,
    your comments are as strange to me as - obviously - my comments to you!.
    You SEE that label NAPSHR don't you? That's it, nothing else.

    I also commented on #18/16/13 and said that is a bad practice as it will confuse the naive.. so it has despite my warning smile.gif

    It is a COMMENT! It will say: Put 18, 16 or 13 here! Tricky! Too tricky

    This line should best read:
    NAPSHR   SHR  T, #0-0  ' copy 18, 16 or 13 to the dest
    


    as most people are now accustomed to the 0-0 which is also a comment however ....

    And: No, a pause routine can't be shorter. Have a look a my alternative: It's even longer (though much clearer)

    Post Edited (deSilva) : 2/23/2008 11:09:06 PM GMT
  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-23 23:10
    Hi Ron,

    Yes it is confusing, but I think you have it, I've highlighted two bits from the code above:
    nap       rdlong  t,#0                   'get clkfreq
    [b]napshr[/b]   shr     t,[b]#18/16/13[/b]           'shr scales time
              min     t,#3                   'ensure waitcnt won't snag
              add     t,cnt                  'add cnt to time
              waitcnt t,#0                   'wait until time elapses (nap)
    nap_ret   ret
    
    


    See the napshr ? That is the label that represents where the SHR instruction is. And the argument #18/16/13 is bogus because it is going to be overwritten by the MOVS instruction. So the programmer has taken the opportunity to add some documentation as to the values that might be placed there, in this case 18, 16 or 13.

    --Chuck
  • OzStampOzStamp Posts: 377
    edited 2008-02-23 23:12
    Thnx Mike

    Just can see that T parameter.. that must be the parameter that holds the Clock speed..
    Good pointer thnx.


    DeSilva read my last post properly... I did thank you and mentioned

    ===================
    So what from I gather from DeSilva comment is that the NAPSHR in the
    NAP routine NAP....NAP_RET it is defined there..see below ther..
    ===================

    cheers ron
  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-23 23:12
    lolz, lots of help here.
    --Chuck
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 23:21
    Oz, for another exercise... I have improved my alternative routine... Do you understand what I do now differently ?
        CALL #NAP
        LONG 13
    ....
    nap
            MOVS   :napshr,  nap_ret
            ADD     nap_ret, #1                      
            rdlong  t,#0                      'get clkfreq
    :napshr shr     t,  0-0               'shr scales time
            min     t,#3                      'ensure waitcnt won't snag
            add     t,cnt                     'add cnt to time
            waitcnt t,#0                      'wait until time elapses (nap)
    nap_ret  ret
    
  • OzStampOzStamp Posts: 377
    edited 2008-02-23 23:21
    thnx Chuck yes it is clear now..

    labels .. labels..

    Can you see though how napshr is not defined anywhere as a REGISTER)

    That is the exact issue that confused me .. understood now..

    Yep lots of help.. thanks again guy's
    ron

    Post Edited (OzStamp) : 2/23/2008 11:52:27 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 23:28
    Oz, EVERYTHING is a "register", code is in registers smile.gif There are no "bytes"... Well this is confusing...

    It's just as in all old computers, starting with the Manchester Mark I

    Post Edited (deSilva) : 2/23/2008 11:37:13 PM GMT
  • OzStampOzStamp Posts: 377
    edited 2008-02-23 23:51
    Hi deSilva.

    I really did mean just REGISTER .. edited my post..

    thnx soory about the confusion..

    cheers ron
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 23:54
    Oz, do you understand the two changes im my posted examples?
  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-24 00:25
    OzStamp said...

    thnx Chuck yes it is clear now..

    labels .. labels..

    Can you see though how napshr is not defined anywhere as a REGISTER ?

    That is the exact issue that confused me .. understood now..

    Hi Ron, I had the same misunderstanding when I started programming in Assembler. If someone is collecting tidbits for an updated FAQ then we should put something like this in it:

    REGISTERS vs CODE vs MEMORY

    Each COG has exactly five hundred and twelve 32 bit words of memory that it can address numbered 0 through 511. By design, the source and destination fields of every assembler instruction are 9 bits which means they can hold the address of any one of these locations in memory. By convention, if a memory location is being used to hold data it is referred to as a "register" however unlike other computers you may have been exposed to there is no specific qualities for the first 496 locations that make them more like registers or more like memory. The last 16 locations however have specific roles and they are discussed in the propeller manual, COG programs and their data are thus restricted to the "lower 496" in practice. Also unlike many architectures the propeller does not have a notion of an "accumulator" or a set of bits which are not present in any memory space. All of the transforming operations available in the instruction set such as ADD, SUB, XOR, AND are just as adept at transforming a memory word that is holding an instruction as they are transforming one that is simply holding data. There are even specific instructions, MOVS, MOVI, and MOVD which are designed to operate on specific parts of a memory location which are associated with the Source, Instruction, and Destination areas of a propeller assembly language instruction. These special move instructions facilitate the development of self modifying code which can make possible some more complicated transfer operations such as move indirect or move with offset.

    Post Edited (Chuck McManis) : 2/24/2008 1:26:13 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-24 00:31
    Chuck,
    What you wrote is good except that some of the registers do have special qualities (the last 16 in memory). Some of them are read-only like INA / INB / CNT / PAR. If these are used as a destination, the actual memory location is used rather than the special register function.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-24 00:45
    Just for fun (and also for the people who sleep with deSilva's Tutorial under their pillow) here the lower half of page 3 of it... there are more pages:
    said...
    Let’s talk about processors – called “COGs” in Propeller lingo.
    What do they do? There is an always correct answer: They execute
    instructions! A standard processor gets these instructions from a
    globally addressed memory (in a so called “von-Neumann-architecture&#8221[noparse];)[/noparse]
    or from a dedicated instruction memory (in a so
    called “Harvard-architecture” – this is the way PICs and AVRs are
    organized!). Having two memories allows to “tune” them according
    to specific needs (e.g. non-volatile, read-only, fast access
    time), and also to access them in parallel!

    A Propeller processor gets its instructions from its internal COG-memory,
    space limited to 496 instructions! Now, please don’t rush
    to give your Prop to your nephew to play with! Remember, you have
    8 of those COGs and the COG-memory is RAM, so it can be reloaded!
    Furthermore, we have 32-bit instructions, giving them much more
    power than a common 8-bit instruction has.

    So it seems we have a flawless von-Neumann architecture, where
    instructions and data lay mixed in one memory. Each instruction is
    32 bits long and the data – is also 32 bits long. Now this is
    funny! Does memory not consist of bytes??

    No, it does not! It consists of tiny electrical charges caught in
    semiconductor structures smile.gif And it is SOMETIMES packaged in sizes
    of eight. COG memory is packaged in sizes of 32. Period!

    We best call those packages “cells” to avoid misunderstandings! So
    we have 496 multi-purpose cells, some will contain our program,
    some data – there are additional 16 cells used as I/O registers;
    we come to that later.

    Post Edited (deSilva) : 2/24/2008 12:51:41 AM GMT

  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-24 01:20
    @mike good point about the last 16 longs, I'll edit it and tell me what you think.

    @deSilva I know you covered it in your tutorial, I observed however that both I and Oz have read your tutorial and we both missed the significance of this. That suggests that the meaning of your message in that section is getting lost in amongst all the words. When I was writing a column for JavaWorld my editor was really good at helping me eliminate any words in my columns that were not part of the message. She helped me understand that the fewer words you use to communicate a point, the more likely it is the point you are making will come across. In the paragraphs you've quoted you talk about no fewer than six concepts von-Neumann vs Harvard architecture, native word length vs byte length, instruction complexity vs word length, multiple cores vs single cores, volatility of RAM vs ROM, and oh by the way there are only 496 usable words of memory. I know it would help me if each concept was covered separately rather than all at once as that would help me absorb each concept better, if they built one upon the other even better. I do appreciate the time and effort you put into getting all of this information into a single document too!

    --Chuck
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-24 02:40
    I can imagine three reasons Parallax is not doing the things we all - living in the forum - see day for day would be helpful for our comrades:

    - They miss manpower
    - They know it will not boost business to teach some hard boiled newbies
    - They think there is enough information available.

    These are guesses...

    When I wrote the Tutorial half a year ago it was done on request of a very specific group, who also requested a little bit of entertainment... And I worked under the false preconception, that everybody here had sufficient experience in SPIN and a working basis with the Propeller, so PASM would be the second step.

    I would do it differently today: I would write a book for the half-wit, starting at least from Adam and Eve....Or I wouldn't I'm sure...
    There is an excellent book about all the Propeller by Andr
  • OzStampOzStamp Posts: 377
    edited 2008-02-24 04:57
    DeSilva wrote to Chuck..

    =========================================================
    It is obvious that you are a trained technical writer. I am not. I am a frustrated entertainer.
    =========================================================

    I (OZ)thought you were a German Shepherd..now this is a double fronted joke ...
    A shepherd is a kind person that looks afer his flock...
    +
    Awhile ago you mentioned that sometimes your on your knees and you pretend to be a canine...

    Sorry guy's could not resists it...

    Relax a little deSilva.. you take it all too serious.. we appreciate what your doing but the "Body Language"
    in your messages has the feel that your a little frustated..and you confirmed that yourself.

    Take care
    Ron Mel OZ
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2008-02-25 21:37
    Review for the Perplexed (ie I think I got it)
    1) every memory location in a cog is a register
    2) which can be referred to by either its address or a label
    3) which may hold code or data
    4) which may be modified by your program
    5) and can be meaningfully reprogrammed with the MOVS, MOVD and MOVI commands.

    [b][img]http://forums.parallax.com/images/smilies/hop.gif[/img] Plea to the Parallax gods:[/b] add to the assembly compiler a repurposing of ?, in a command's operands to signify self-modifying code. 
    
    So, ?line 23, or ?long text line that explains what you want to do, may mean "[noparse][[/noparse]modified] by line 23" or "long text line explanation here". 
    
    Or minimally ?, for just a placeholder indicator.
    
Sign In or Register to comment.