first newbie assembly program.
darkxceed
Posts: 34
Hello,
First I like to thank deSilva and potatohead's nice assembly tutorial.
Even is potatohead's tutorial was simple, I still learn't things.
Now my first program will be a sort of capacity meter.
I first load a capacitor with a pinout(in serie with a resitor ofcourse).
Then I set the same pin to input and wait till the the capacitor drops below the treshold of logic "1".
I then want to fill an array with measured data, this will al be done in assembly.
A program in spin will set the array size which the assembly program will fill with measurements if it is 'called'.
Below is the program in assembly:
pub Main(value)
· cognew(@Toggle, value)
dat
·········· ·· org 0
Toggle··· ·mov dira, #$1···················· 'set pin 1 output
············· xor outa, #$1···················· 'set pin 1 high
············· mov Time, cnt···················· 'copy system counter to Time
············· waitcnt Time, Delay············· 'wait a small delay.
············· mov dira, #$0···················· 'set pin 1 as input
············· mov Time, cnt···················· 'copy system counter to Time
:loop····· ·mov Pin, ina····················· 'copy value form pin 1 to Pin
············· xor Pin, #$1··· wc··············· 'look if Pin value has changed from 1
············· if_nc jmp #:loop················· 'loop while 'c' is "0"
············· mov TotalT, cnt·················· 'copy system counter to TotalT
············· sub TotalT, Time················· 'substrac Time from TotalT
············· wrlong value, TotalT
·······
'defined data
Delay········ long 20
'ungedefined data
TotalT······· res· 1
Time········· res· 1
Pin·········· res· 1
value······· res· 1
Now I want to know what the difference is between the 'defined' and the 'undefined data' except that it already has a value?
Why not make all the data defined, and I guess they are all in cog memory because I can only read and write data with wrlong and rdlong(ofcourse also the word and byte).
Please can you give me your opinion about my programm, maybe there is something wrong or good [noparse]:)[/noparse].
·
First I like to thank deSilva and potatohead's nice assembly tutorial.
Even is potatohead's tutorial was simple, I still learn't things.
Now my first program will be a sort of capacity meter.
I first load a capacitor with a pinout(in serie with a resitor ofcourse).
Then I set the same pin to input and wait till the the capacitor drops below the treshold of logic "1".
I then want to fill an array with measured data, this will al be done in assembly.
A program in spin will set the array size which the assembly program will fill with measurements if it is 'called'.
Below is the program in assembly:
pub Main(value)
· cognew(@Toggle, value)
dat
·········· ·· org 0
Toggle··· ·mov dira, #$1···················· 'set pin 1 output
············· xor outa, #$1···················· 'set pin 1 high
············· mov Time, cnt···················· 'copy system counter to Time
············· waitcnt Time, Delay············· 'wait a small delay.
············· mov dira, #$0···················· 'set pin 1 as input
············· mov Time, cnt···················· 'copy system counter to Time
:loop····· ·mov Pin, ina····················· 'copy value form pin 1 to Pin
············· xor Pin, #$1··· wc··············· 'look if Pin value has changed from 1
············· if_nc jmp #:loop················· 'loop while 'c' is "0"
············· mov TotalT, cnt·················· 'copy system counter to TotalT
············· sub TotalT, Time················· 'substrac Time from TotalT
············· wrlong value, TotalT
·······
'defined data
Delay········ long 20
'ungedefined data
TotalT······· res· 1
Time········· res· 1
Pin·········· res· 1
value······· res· 1
Now I want to know what the difference is between the 'defined' and the 'undefined data' except that it already has a value?
Why not make all the data defined, and I guess they are all in cog memory because I can only read and write data with wrlong and rdlong(ofcourse also the word and byte).
Please can you give me your opinion about my programm, maybe there is something wrong or good [noparse]:)[/noparse].
·
Comments
mov Time, cnt 'copy system counter to Time
waitcnt Time, Delay 'wait a small delay.
That's going to be around a 50 second delay. You need something like -
mov tmp,cnt
add tmp,delay
waitcnt, tmp,...
For "cognew(@Toggle, value)" you need to pass in the address of whaere the 'value' variable is "cognew(@Toggle, @value)", and when it comes to returning a result ...
wrlong value, TotalT
You have the registers reversed, and where you will be writing to ( ultimately the Spin 'value' variable ) has its address held in PAR, so ...
wrlong TotalT,PAR
As to the difference between initialised and uninitialised, anything with 'res' doesn't take up space in hub memory / Eeprom image so saves space. If you had 100 registers all initialised with "long 0" that would take up 100 longs ( 400 bytes ) of Hub / Eeeprom. If each were defined as 'res 1' no space would be taken up, but what they were initialised with would be 'random values'.
xor Pin, #$1 wc 'look if Pin value has changed from 1
if_nc jmp #:loop 'loop while 'c' is "0"
You'll probably need to use 'test Pin,#$1 WC" there.
Plus, after you've returned your result using 'wrlong'; what next ? The code execution just continues into 'nothingness'. You'll need a 'jmp #...' to re-run the program or to stop it doing anything else ( 'jmp #$' ), or a 'cogstop' to close the entire Cog down.
For "cognew(@Toggle, value)" you need to pass in the address of whaere the 'value' variable is "cognew(@Toggle, @value)", and when it comes to returning a result ...
If I define a value[noparse][[/noparse]1000] in spin and use the cognew(@Toggle, @value) command.
When starting with wrlong command, it will start at value[noparse][[/noparse]0] then value[noparse][[/noparse]1] etc... ofcourse I have to skip 4 bytes every time.
But what happens if I go to value[noparse][[/noparse]1100]?
nothing or will I overwrite some code in the hubram?
You have the registers reversed, and where you will be writing to ( ultimately the Spin 'value' variable ) has its address held in PAR, so ...
I don't understand 'wrlong TotalT, PAR', TotalT has a number that will be placed at value[noparse][[/noparse]0] for the first time.
How do I let the propeller know that PAR is the place for value[noparse][[/noparse]0]?
As for the res and other variables, the 'long' is hubram and the 'res' is cogram?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
The stars go out, time stops, and the universe gets sucked into a black hole. Bad things happen - Yes, code ( though more likely other data ) will be corrupted and cause no end of problems. Don't forget that with "VAR value[noparse][[/noparse] 1000 ]" you can only access "value[noparse][[/noparse] 0 ]" through to "value[noparse][[/noparse] 999 ]" ( not entirely true to be a pedant, but the stars go out, time stops, etc unless you intend to do this and have catered for it ).
I don't understand 'wrlong TotalT, PAR', TotalT has a number that will be placed at value[noparse][[/noparse] 0 ] for the first time. How do I let the propeller know that PAR is the place for value[noparse][[/noparse] 0 ]?
That comes from the "cognew(...,@value)"; the second parameter is placed into PAR when the PASM code starts, so what PAR holds is the address (@) of 'value'. The 'wrlong TotalT,PAR" says put the value at the location indicated by the address held in PAR. In spin it's the equivalent of writting -
PAR := @value
long[noparse][[/noparse] PAR ] := TotalT
which becomes "long[noparse][[/noparse] @value ] := TotalT", thus, "value := TotalT"
As for the res and other variables, the 'long' is hubram and the 'res' is cogram?
Both are in Cog RAM. Cog RAM the only thing within a Cog which can be accessed other than through the rdlong and wrlong instructions.
But in addition "long" will also be in Hub RAM but RES will not be ( for the sake of simplicity ).
Could this be working?
con
·_clkmode = xtal1 +pll16x
·_xinfreq = 5_000_000
VAR
long A[noparse][[/noparse]1000]
pub Main
· cognew(@Toggle,@A)
·
dat
·····
Toggle······· mov adres, PAR
first········ mov dira, #$1···················· 'set pin 1 output
············· xor outa, #$1···················· 'set pin 1 high
············· mov Time, cnt···················· 'copy system counter to Time
············· add Time, #90····················· 'small delay
············· waitcnt Time, Delay·············· 'wait a small delay.
············· mov dira, #$0···················· 'set pin 1 as input
············· mov Time, cnt···················· 'copy system counter to Time
loop········· mov Pin, ina····················· 'copy value form pin 1 to Pin
············· test Pin, #$1······ wc··········· 'look if Pin value has changed from 1
············· if_nc jmp #loop················· 'loop while 'c' is not·"0"
············· mov TotalT, cnt·················· 'copy system counter to TotalT
············· sub TotalT, Time················· 'substrac Time from TotalT
············· wrlong TotalT, adres············· 'write value of TotalT @ pos '0' of hubmem
············· add adres, #4···················· 'add 4 to adres which points to PAR
············· djnz Size,#first················ 'substract 1 from Size and if not '0' jump to first
·············
'defined data
Delay········ long 20
TotalT······· long 0
Size········· long 512
'undefined data
Time········· res· 1
Pin·········· res· 1
Adres········ res· 1
When I want to stop the cog, which instruction must I use?
Cogstop(@Toggle)?
or stop it with
Cogstop(cog~ -1) in spin,·I will have to make a small endless loop at the end of the assembly code.
Is it possible to let the cog start @Toggle while it is running in the small loop at the end of the assembly withought stopping the cog.
This will ofcourse be faster that when I want to load the cog with instructions to start.
·
1. In asm after djnz
cogid Adres ' store cog id in adres
cogstop Adress ' stop this cog
2. In asm after djnz
stop jmp stop
in spin change cognew to id := cognew
then work out when cog is done and call
cogstop(id)
the 1st is easier and you dont have to work out when the cog is done - though you need to anywhere to know when you can use the data in A
3. Change the asm so it loops, i.e. after djnz
mov Size, #512
jmp Toggle
this will keep reading, filling up A and then reseting back to the start of A and filling again
Questions remain:
'PinSet' it sets·pin 16 as output, it has value $10001 I guess. But wil other pins be unchanged because is defines pin 16 as 'out' and the rest as 'in'?
This also counts when I set pin 16 as input, all the pins will be set to·input, but only 16 has to be set as input(rest must be unchanged). And does the line "Test PinSet,ina·· wc" works? It·must look if pin 16 goes low·, else repeat loop.
dat
············· org 0
Toggle······· mov adres, PAR··················· 'copy hubmem adres to 'adres'
first········ mov dira, PinSet················· 'set pin 16 output
············· mov outa, PinSet················· 'set pin 16 high
············· mov Time, cnt···················· 'copy system counter to Time
············· add Time, #40···················· 'add 40cycles to wait
············· waitcnt Time, Delay·············· 'wait a small delay.
············· mov dira, #0····················· 'set pin 16 as input
············· mov Time, cnt···················· 'copy system counter to Time
loop········· test PinSet, ina······ wc········ 'test if the pin input goes low
············· if_c jmp #loop··················· 'loop while 'c' is not "0"
············· mov TotalT, cnt·················· 'copy system counter to TotalT
············· sub TotalT, Time················· 'substrac Time from TotalT
············· wrlong TotalT, adres············· 'write value of TotalT @ pos '0' of hubmem
············· add adres, #4···················· 'add 4 to adres which points to PAR
············· djnz Size,#first················· 'substract 1 from Size and if not '0' jump to first
············· mov Size, #511
············· jmp Toggle
············· fit 496
·············
'gedefined data
Delay········ long 40
TotalT······· long 0
Size········· long 511
PinSet······· long |<16
'Mask········· long $10001
'ungedefined data
Time········· res· 1
Pin·········· res· 1
Adres········ res· 1