Shop OBEX P1 Docs P2 Docs Learn Events
Assembly, step by step — Parallax Forums

Assembly, step by step

Graham StablerGraham Stabler Posts: 2,507
edited 2009-09-20 15:42 in Propeller 1
Putting some money where my mouth is here is step one of a series of simple assembly programs to illustrate some basic assembly ideas and to ease you into assembly. My feeling is that the assembly for beginners requires a little more background in assembly than many have.

Step 1: Flash an LED

You will learn:

1. How to start an assembly cog
2. A simple way to load a variable into that cog on start up with no funny business (very handy)
3. How to set a pin as an output
4. One way to make a pin high
5. How to use waitcnt in assembly

DO read the comments
DO confer with the manual
DO ask questions if you really don't get anything

I'd appreciate glaring errors and suggestions for alternative methods to be emailed/messaged to me to avoid confusion in the thread and to keep in short and sweet.

Cheers,

Graham
«1

Comments

  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-25 23:25
    Step 2: Setting pins

    You will learn:

    1. How to create masks for single and multiple pins from variables.
    2. How to set up the dira register using these masks
    3. How to set outputs high with OR and a mask
    4. How to set outputs low with ANDN and a mask
    5. How to toggle outputs with XOR and a mask
    6. How to use muxc and muxnc to set and clear bits and a mask

    The demo flashes 4 leds, two alternatively and a pair together, I used the vga leds on the demoboard.

    Graham

    Post Edited (Graham Stabler) : 4/26/2007 12:31:33 AM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-25 23:25
    Step 3: Hub to cog data transfer

    You will learn:

    1. How to pass an address for hub variables to assembly code on start up.
    2. How to load these variables from hub to cog within assembly using rdlong.
    3. That the addresses are byte wise in assembly
    4. How to write functions that can be used in spin to modify the global hub variables.
    5. How these might be used in a simple spin function

    Again we are still just flashing leds but this time we can alter the duty (brightness) and frequency. As a excersise you may consider writing spin functions for the object to control these two parameters more easily.

    Graham

    Post Edited (Graham Stabler) : 4/26/2007 8:23:26 PM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-25 23:25
    Step 4: Cog to hub data transfer

    You will learn:

    1. Review how to pass an address for hub variables to assembly code on start up.
    2. How to load data into hub ram with wrlong

    Yet more flashing LEDs in a knight-rider style this time.

    But really just remember that the variable that is loaded into hub ram could have been generated by an ADC routine or could have come from an accelerometer or other sensor or any process going on in that cog. It is also a good way to debug programs as you can echo variables in assembly to the terminal or TV in the spin program.

    Graham

    Post Edited (Graham Stabler) : 5/16/2007 9:22:19 AM GMT
  • JohnBFJohnBF Posts: 107
    edited 2007-04-26 01:14
    This is great. Thanks! I wanted to take a small step and make a pin toggle a finite number of times. But with this code the pin doesn't stop. What am I doing wrong?

    /John
  • potatoheadpotatohead Posts: 10,253
    edited 2007-04-26 04:00
    I don't see you decrementing cycles, for one, thus it stays at 10.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-26 07:34
    Potatohead, the djnz command decrements cycles and jumps if not zero.

    John, your code looks fine except that you add 9 to time for no good reason, it isn't enough cycles for the first waitcnt so I suspect it messes things up.

    try:

    Toggle                mov      dira, Pin        
                          mov      Time, cnt        
                          add      Time, Delay         
    :loop                 waitcnt  Time, Delay      
                          xor      outa, Pin        
                          djnz     Cycles, #:loop  
    
    



    So to reiterate the waitcnt command,

    waitcnt target, delta

    It waits until cnt = target and then adds delta to target. So the process is:

    1. load target variable with cnt
    2. add delta to target variable
    3. waitcnt target, delta

    The delta field of waitcnt just means you can avoid step 2 after the first delay.

    Another point is that you may want to close the cog after your pulses:

                CogId      CogNum          ' Get COG ID
                CogStop  CogNum          ' Stop this COG
    
    
    CogNum      long        0
    
    
    



    Graham
  • crgwbrcrgwbr Posts: 614
    edited 2007-04-26 11:44
    This should definitely be a sticky.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I would love to change the world, but they won't give me the source code

    People say that if you play Microsoft CD's backwards, you hear satanic things, but that's nothing, because if you play them forwards, they install Windows.

    I spent a minute looking at my own code by accident. I was thinking "What the heck is this guy doing?"
  • potatoheadpotatohead Posts: 10,253
    edited 2007-04-26 14:35
    It does!

    That's other CPU's still getting in the way! I'm now very interested in the waitcnt behavior...

    If it misses the mark, shouldn't it hang?
  • CJCJ Posts: 470
    edited 2007-04-26 14:44
    it will wait until CNT comes back around (~53 secs @ 80Mhz)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • potatoheadpotatohead Posts: 10,253
    edited 2007-04-26 15:27
    Thanks.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-26 20:22
    Step 3 now posted. Next, how to get data out of the cog!

    Graham
  • JohnBFJohnBF Posts: 107
    edited 2007-04-26 23:44
    Graham,

    Thanks very much for the explanation. Adding the 9 comes from the example code on page 340 of the Propeller Manual. That's the one line I didn't understand.

    /John
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-05-04 02:22
    >>Next, how to get data out of the cog!
    Simple. WRLONG Value, # Address
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-04 03:07
    Remember that you're using the immediate form of the WRLONG instruction which only allows operands from 0-511. If you address is above that, it won't work, you'll have to have the address in some other location and reference that like "WRLONG Value,LongWithAddress".
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-04 08:27
    part 4 comming soon, just have to get the weekend over with.

    Graham
  • Brian_BBrian_B Posts: 840
    edited 2007-05-15 10:04
    Graham,
    Any new sample code yet ? This has been very helpful :- )

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thank's Brian


    www.truckwiz.com

    ·"Imagination is more important than knowledge..." ·· Albert Einstein

    http://www.diycalculator.com/subroutines.shtml· My favorite website ( Bet you can't guess why)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-15 11:29
    Sorry Brian, busy at work last week but I'll get the next installment done tonight.

    Graham
  • ALIBEALIBE Posts: 299
    edited 2007-05-15 11:51
    this thread is fantastic. It should be sticky.

    Graham, thanks for this kick-off - very helpful for prop asm beginners like me.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "any small object, accidentally dropped, goes and hides behind a larger object."

    ·
    ALIBE - Artificial LIfe BEing. In search of building autonoumous land robot
  • KaioKaio Posts: 253
    edited 2007-05-15 13:43
    Graham,

    I would recommend to add this thread to your thread index at the first position of section "Programming the propeller". So asm beginners could start with it, before they be frustrated looking in other links.

    @ALIBE
    Then you could find this here which is already sticky.
    http://forums.parallax.com/showthread.php?p=609066

    Thomas
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-15 13:47
    seconded on the stickiness. i stink at asm and this is helping a lot!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-15 15:19
    I think there are already too many sticky threads (that was why I started the index originally) so I'll just add it to the index as per Thomas' suggestion.

    Graham
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-15 23:26
    I've uploaded part 4 and I have added thread to thread index

    Post Edited (Graham Stabler) : 5/15/2007 11:38:13 PM GMT
  • JohnBFJohnBF Posts: 107
    edited 2007-05-16 00:49
    Another great chapter. Thanks! Why stop now - any chance for a part 5?

    A question: is it really necessary to declare time as a local variable in the demo program's start method? It's only used in the cog2hub's ASM routine, which declares it itself.

    /John
  • simonlsimonl Posts: 866
    edited 2007-05-16 08:54
    Hey Graham, you're helping me loads with this! Until you started posting these steps I didn't really know where to start - thank you smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-16 09:22
    John,

    I may do some more parts if I can decide what to cover, possibly the use of wc,wz etc.

    The variables declared in the demo start function were throwbacks from the previous lesson, I've removed them now. They would of course have no effect on the variables defined in the assembly cog and you could have both doing different things.

    Graham
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-16 09:23
    Brian, I'm not sure what you are missing but part 4 is in post 4.

    Graham
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-17 20:18
    Have been absent (with leave) for more than a month. I regret that.

    But, what do I find when I'm coming back?!

    Yess! That ASM tutorial that I needed so badly.

    Thanks a lot to Graham and all that makes this site so great. Mike included.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-17 23:46
    My pleasure.

    As there seems to be interest are there any requests for topics of further lessons, I think all the IF_NC type stuff can cause confusion but perhaps there are more fundemental things that need to be addressed first.

    Graham
  • BTXBTX Posts: 674
    edited 2007-05-18 03:02
    Hi Graham.
    What about to talk some about things like this:
                   cmp      variable,number    wc,wz      ' Compare for variable =< number
     if_nc_and_nz  jmp      #greater
                   .
                   . 'do something here if variable is =< than number
                   .
    greater        . 'do something here if variable is > than number
                   .
                   .         
    

    Just only an idea.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • GennadyGennady Posts: 34
    edited 2007-07-01 17:27
    I am new to this forum (and to the Propeller redface.gif ), and this tutorial helped me tremendously. Thanks a lot !

    I have a few questions, though:

    Step3: method 'Start' is defined with the local variable time which doesn't seem to be used anywere (and not initialized).

    ········· What can be consiquences of this?

    Step 4:·start_time in 'Start' is not in a list of local variables, initialized and not used. Again, what are the consequences?

    Again, thanks a lot for the tutorial.

    Gennady···········
Sign In or Register to comment.