How to pass pointers with object address
Hello,
I'm trying to return more then one pointer.
I took the sample code from the manual.
Simple main program:
Object code:
I'm trying to return more then one pointer.
I took the sample code from the manual.
Simple main program:
CON
_clkmode = xtal1 + pll16x ' 80 MHz
_xinfreq = 5_000_000
'***************************************
' Debug Terminal Definitions
'***************************************
_Trm_Rx = 31
_Trm_Tx = 30
_Trm_Baud = 115200
'***************************************
OBJ
'***************************************
Trm : "SerialMirror"
Txt : "SubTxt"
'***************************************
PUB main | Cog_Trm, Ptr_Txt, Idx
'***************************************
WaitCnt(5 * 80_000_000 + Cnt)
Cog_Trm := Trm.Start(_Trm_Rx, _Trm_Tx, 0, _Trm_Baud)
WaitCnt(1 * 80_000_000 + Cnt)
Trm.Str(string("Debug Terminal started in Cog: "))
Trm.dec(Cog_Trm)
Trm.CrLf
WaitCnt(1 * 80_000_000 + Cnt)
Ptr_Txt := Txt.Start
Trm.Str(string("Ptr_Txt= "))
Trm.Hex(Ptr_Txt,4)
Trm.CrLf
Trm.CrLf
Trm.Str(string("Main: "))
Trm.CrLf
REPEAT Idx FROM 0 TO 2
Trm.Str(Ptr_Txt[Idx])
REPEAT
Object code:
''**************************************
''
'' SubTxt.spin
''
''**************************************
DAT
Str1 byte "Hello.", 0
Str2 byte "This is an example", 0
Str3 byte "of strings in a DAT block.",0
StrAddr word @Str1, @Str2, @Str3
OBJ
'***************************************
Trm : "SerialMirror"
'***************************************
PUB Start | Addr_Str, Idx
Addr_Str := @@StrAddr
Trm.Str(string("Addr_Str= "))
Trm.Hex(Addr_Str,4)
Trm.CrLf
Trm.Str(string("SubTxt: "))
Trm.CrLf
REPEAT Idx FROM 0 TO 2
Trm.Str(@@StrAddr[Idx])
Trm.CrLf
Trm.CrLf
Return @@StrAddr

Comments
You should use word[Ptr_Txt][Idx] to access the word elements of the array. These are object offsets, so you need to used the @@ operator to get the absolute addresses. So your call to the Str method from the main method should be Trm.Str(@@word[Ptr_Txt][Idx]). I realize this is very confusing, but that's how Spin handles object offsets.
I alterd the program at your instructions but unfortunately, It did not gave the expected results.
PUB Initialize | Idx repeat Idx from 0 to 2 StrAddr[Idx] := @@StrAddr[Idx]Call Txt.Initialize once from your main method before you access the array, and use Trm.Str(word[Ptr_Txt][Idx]) to print the strings.Neither unfortunaly,
I tried even:
PUB Init | Idx StrAddr[0] := @@Str1 StrAddr[1] := @@Str2 StrAddr[2] := @@Str3with no success
My simple solution to that delima is to have the start method return the highest string index for the object, and then include a text() method that provides the address of the string for a given index. Like this:
You can print a string with:
repeat idx from 0 to 2 term.str(messages.text(idx)) term.tx(CR)I tested and this works.
The example I gave is a simplyfied one.
I want to accomplish a general method how one object may access variables, defined in another object.
In my project thera are several types of variables, strings, bytes, words longs and arrays in main memory.
These are not well alligned so I like to see a method whereby these vars in an object are accessable by the main program or even by another object.
char *ops[] = {"++", "+=", "+", "--", "-=", ... "?", "===", "==", 0};I use CSPIN to convert it to Spin, and it generates the following code. CSPIN adds an offset of 16 to generate absolute addresses for the top object. However, in other objects I have to add the offsets at run time, so I use the following routine to initialize the pointers.PUB start | ptr ptr := @ops repeat while long[ptr] long[ptr] += @@0 - 16 ptr += 4This routine subtracts the offset of 16 that is used for the top object and adds the object address, which is given by @@0.This is great.It works indeed ! Especially the @@0 was a revelation to me.
In retrospect quite logical.
Obviously you have good understanding how spin works. For me this was a mystery.but yiu solved it. Thanks
JonnyMac,
You are right when with a few vars. But, I hope you agree, Dave Hein answered to my expectations.
Thanks both for your contributions.
Henk