Code Issue - Method vs. "Main"
Jon Keinath
Posts: 146
I'm having an issue moving some of my code from my "main" method do a sub method. If I do everything in the main method it works as I would expect. The "main" structure is below:
If I use the following program structure the outputs in x0 through x7 are not what I would expect.
A separate PUB (and COG) takes the values in x0 through x7 and displays them on a matrix led board. For some reason x0 is correct in both instances but the remainder of the x terms are incorrect.
Any suggestions?
DAT lH byte %10001, %10001, %10001, %11111, %10001, %10001, %10001, %00000 VAR long x[8] long x0, x1, x2, x3, x4, x5, x6, x7 PUB go x0 := ((lH[0] & |<0)<<0)+((lH[0] & |<1)<<2)+((lH[0] & |<2)<<4)+((lH[0] & |<3)<<6)+((lH[0] & |<4)<<8) x1 := ((lH[1] & |<0)<<0)+((lH[1] & |<1)<<2)+((lH[1] & |<2)<<4)+((lH[1] & |<3)<<6)+((lH[1] & |<4)<<8) x2 := ((lH[2] & |<0)<<0)+((lH[2] & |<1)<<2)+((lH[2] & |<2)<<4)+((lH[2] & |<3)<<6)+((lH[2] & |<4)<<8) x3 := ((lH[3] & |<0)<<0)+((lH[3] & |<1)<<2)+((lH[3] & |<2)<<4)+((lH[3] & |<3)<<6)+((lH[3] & |<4)<<8) x4 := ((lH[4] & |<0)<<0)+((lH[4] & |<1)<<2)+((lH[4] & |<2)<<4)+((lH[4] & |<3)<<6)+((lH[4] & |<4)<<8) x5 := ((lH[5] & |<0)<<0)+((lH[5] & |<1)<<2)+((lH[5] & |<2)<<4)+((lH[5] & |<3)<<6)+((lH[5] & |<4)<<8) x6 := ((lH[6] & |<0)<<0)+((lH[6] & |<1)<<2)+((lH[6] & |<2)<<4)+((lH[6] & |<3)<<6)+((lH[6] & |<4)<<8) x7 := ((lH[7] & |<0)<<0)+((lH[7] & |<1)<<2)+((lH[7] & |<2)<<4)+((lH[7] & |<3)<<6)+((lH[7] & |<4)<<8)
If I use the following program structure the outputs in x0 through x7 are not what I would expect.
PUB go convert(lH) PUB convert(letter) x0 := (((letter[0] & |<0)<<0)+((letter[0] & |<1)<<2)+((letter[0] & |<2)<<4)+((letter[0] & |<3)<<6)+((letter[0] & |<4)<<8)) x1 := (((letter[1] & |<0)<<0)+((letter[1] & |<1)<<2)+((letter[1] & |<2)<<4)+((letter[1] & |<3)<<6)+((letter[1] & |<4)<<8)) x2 := (((letter[2] & |<0)<<0)+((letter[2] & |<1)<<2)+((letter[2] & |<2)<<4)+((letter[2] & |<3)<<6)+((letter[2] & |<4)<<8)) x3 := (((letter[3] & |<0)<<0)+((letter[3] & |<1)<<2)+((letter[3] & |<2)<<4)+((letter[3] & |<3)<<6)+((letter[3] & |<4)<<8)) x4 := (((letter[4] & |<0)<<0)+((letter[4] & |<1)<<2)+((letter[4] & |<2)<<4)+((letter[4] & |<3)<<6)+((letter[4] & |<4)<<8)) x5 := (((letter[5] & |<0)<<0)+((letter[5] & |<1)<<2)+((letter[5] & |<2)<<4)+((letter[5] & |<3)<<6)+((letter[5] & |<4)<<8)) x6 := (((letter[6] & |<0)<<0)+((letter[6] & |<1)<<2)+((letter[6] & |<2)<<4)+((letter[6] & |<3)<<6)+((letter[6] & |<4)<<8)) x7 := (((letter[7] & |<0)<<0)+((letter[7] & |<1)<<2)+((letter[7] & |<2)<<4)+((letter[7] & |<3)<<6)+((letter[7] & |<4)<<8))
A separate PUB (and COG) takes the values in x0 through x7 and displays them on a matrix led board. For some reason x0 is correct in both instances but the remainder of the x terms are incorrect.
Any suggestions?
Comments
Lawson
Andy
I think I almost understand that, kuro.
All except the x0[n++].
We are moving bytes into longs, right?
One byte into each long?
Is that how it reads?
So what happends is we read a byte from the character definition, transform that into a word (table access) and assign it to a long. Does that make sense?
n is an alias for result which starts at zero.
Yes sir, although I skipped over the intermediate word conversion.
Thank you.
It was the :n in the PUB declaration that I am lacking.
That colon shows up in Ariba's Convert pub also.
PUB convert(letter) : i | b
I have not seen that before.
Richard
the colon is another trick of spin.
You can RETURN a value from your spin-method. lets say RETURN 10 or RETURN VarName. But you also can us a implicit existing variable named RESULT. So instead of RETURN 10 you can write RESULT:=10.
and if you do not like the name RESULT for that implicit existing variable wich gets returned you can RENAME it.
and that does that colon after the Method-name
so
PUB test | myvar
myvar:=10
RETURN myVar
is the same as
PUB test | myVar
RESULT:=myVar
is the same as
PUB test : myReturn | myVar
myVar:=10
myReturn:=myVar
And in opposite to all your local variables like myVar (wich are NOT initialized) RESULT is inirtialized as 0.
Enjoy!
Mike