A better way to share data between SPIN and ASM
dnaddor
Posts: 26
Hi y'all.
I've never liked how I shared data between SPIN and ASM. I often have lots of different sized structures, and it is too easy to make a mistake in the offsets (like forgetting to multiply by 4). I prefer to access all variables by name.
Today, I figured out an easy way to do this. The "trick" is to have a DAT section with this line:
Memory long @Memory
and then start a cog with this:
COGSTART(@CogThread, @Memory - Memory)
Now in ASM, you can access data easily by using PAR and adding the offset to the variable.
I included a simple test program. All it does is print 100+200=300.
I've never liked how I shared data between SPIN and ASM. I often have lots of different sized structures, and it is too easy to make a mistake in the offsets (like forgetting to multiply by 4). I prefer to access all variables by name.
Today, I figured out an easy way to do this. The "trick" is to have a DAT section with this line:
Memory long @Memory
and then start a cog with this:
COGSTART(@CogThread, @Memory - Memory)
Now in ASM, you can access data easily by using PAR and adding the offset to the variable.
I included a simple test program. All it does is print 100+200=300.
spin
2K
Comments
long Something
PUB
SomethingAddress := @Something
cognew(@entry,0)
DAT
entry
....
somethingAddress long 0
The idea is that you embed the addresses of all the data you want to acess into the image for the cog to run. Once this is done you can simply use the addresses in getting data.
As in:
rdlong buffer, somethingAddress
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Did they release a new version?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
long Something
PUB
SomethingAddress := @Something
cognew(@entry,0)
DAT
entry
....
somethingAddress long 0
is because it gets tedious when you have lots of variables:
DAT
EntryNumber long 0
HashIndex long 0
ImJustAByte byte 0
BigTable long 0 [noparse][[/noparse]16]
:
I would have to create a second variable for the address of each of these. I would have to create an assignment for each of these. With my method, I only have one copy of each variable. I also don't waste any additional RAM.
The example is excellent. Very easy to read. Of course the object will be a singleton, but that is often useful anyway for device drivers.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
You are using way more memory from all of that.
Enjoy your new trick.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,