Spin assignment operators question?
rwgast_logicdesign
Posts: 1,464
So I just noticed something a few hours ago that i have never really thought about, and it got me thinking. In the CON section you assign a value to a constant using a plain old ='s sign, but in the Pub section you have to use a := to assign an operand to a variable. Does anyone know why this is? What is wrong with the good old fashion way of just using an = or an == like C, why does spin seem to use =,==, and :=? I think the := makes your code kind of ugly
Comments
My guess on the logic behind the : in := is so that it matches operators such as *=, *=, +=, and -=. In any case, you probably have slightly fewer unnoticed typos in code with :. It prevents you from making a mistake, and accidentally using things like >:=, etc.
-Phil
In maths the equals sign is not an assignment operation it is a statement of fact. The left side of the equation IS equal to the right side.
So in Spin the equals used for constants is not an assignment but a statement of fact.
That leaves the issue of making assignment and equality testing operators different.
I know this is a totally different topic but I didnt want to start a new thread. Im sorry ive finally started cracking the whip on myself to get through the PE Kit book so I can learn spin well, and I have another quick question as to whats going on behind the scenes..
Take exercise for example
Now when I declare these variables local to the method as I have done above, the program info tells me im using 19 longs for the program and 0 longs in variables. If I declare the same variables as global(i.e Bytes in VAR block), my program grows to 22 longs and and I believe 1 long variables.
Now I remember hearing the prop only deals in longs, so what happens when I use a byte, will spin store 4 bytes in one long, or just store each variable delcared as a byte in its own long, and exactly why does my program grow by 4 longs total when using global Bytes?
When you declare global (VAR) variables as words or bytes, that's what's allocated. The Spin compiler sorts the variables according to their alignment (longs, words, bytes) and allocates all of an alignment group together so there's no wasted space. Longs come first (long aligned), words come next (word aligned), and bytes comes last (byte aligned).
The Prop does deal in words and bytes when they're in hub memory. There are special instructions RDLONG/RDWORD/RDBYTE and WRLONG/WRWORD/WRBYTE that allow reading and writing longs, words, and bytes that are in hub memory. The cog memory is all longs and the Prop's instructions deal directly only with longs (except for the RDxxxx/WRxxxx).
I love it. My first (real) language was BCPL, the predecessor of C. It used := in the same manner. Besides, I think it is intuitively right. It should be used in math as well - like 'X = 5" is kind of a question, whereas X := 5 is more obviously an assignment.