Assembly, step by step
Graham Stabler
Posts: 2,510
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
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
spin
401B
Comments
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
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
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
/John
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:
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:
Graham
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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?"
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Parallax Forums - If you're ready to learn, we're ready to help.
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
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
·
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
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://forums.parallax.com/showthread.php?p=650217
meow, i have my own topic now? (sorta)
Graham
Post Edited (Graham Stabler) : 5/15/2007 11:38:13 PM GMT
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
BTW: I type as I'm thinking, so please don't take any offense at my writing style
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 ;-)
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
What about to talk some about things like this:
Just only an idea.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Regards.
Alberto.
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···········