bits of arrays
Lee Marshall
Posts: 106
im a spin noob, having done the bulk of my prop programming in asm, however, i have recently come to notice how useful spin could be for rapid testing of stuff.
this question quite possibly has come up before, but i cant find it.
say, for instance, i define a long in SPIN....
if i were to do this:
that should SET bit 13 of a_long
what if i define an array?:
and i did:
wouldnt that set all the bits in element 10 to 1??
what if i wanted to manipulate a single bit of an array element??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I hear and I forget. I see and I remember. I do and I understand
-Confucius
Post Edited (Mr Crowley) : 11/3/2007 6:36:09 AM GMT
this question quite possibly has come up before, but i cant find it.
say, for instance, i define a long in SPIN....
VAR long a_long
if i were to do this:
PUB Main a_long[noparse][[/noparse]13]~~
that should SET bit 13 of a_long
what if i define an array?:
VAR long an_array[noparse][[/noparse]300]
and i did:
PUB Main an_array[noparse][[/noparse]10]~~
wouldnt that set all the bits in element 10 to 1??
what if i wanted to manipulate a single bit of an array element??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I hear and I forget. I see and I remember. I do and I understand
-Confucius
Post Edited (Mr Crowley) : 11/3/2007 6:36:09 AM GMT
Comments
As Mike already pointed out, there is another very specific aplication for "[noparse][[/noparse]" "]"..
To your other question: See the thread on the ~ operator. Here Chip explains a VERY nice side effect of it, generally overlooked and thus underused
It helps to think of xxx~~ to work like xxx := TRUE
Eidt: Killed some typos...
Post Edited (deSilva) : 11/3/2007 2:53:25 PM GMT
No, it will treat a_long as an array of longs, set all bits of what it thinks is element 13 of your array, and corrupt something in memory.
Those 'bit arrays' are only valid for the special purpose registers, INA, OUTA etc.
Correct, all bits of element 10 will be set.
To manipulate individual bits you can use &, |, ^ operators and their equivalent &=, |=, ^= assignments. The |< operator can also be used to good effect. If you wanted to set bit 7 in an_array[noparse][[/noparse]10], one way is ...
an_array[noparse][[/noparse]10] |= |< 7
You can create routines to abstract bit handling ...
Perhaps a better point of view is a programmer's: arrays are data structures that need this sort of code to work. And procede to explain 'this sort of code'. So the Prop compiler doesn't jump through your hoop. Get over it and get on with it.
The Prop doesn't have a stack, doesn't have a multiply or divide in assembly, doesn't auto-increment nor auto-decrement, and so on. Those are our handicaps, now run with it. So people write Forths and float32s, use buffers, write BS2's or graphics programs for tv sets ... and if you ask them, they all use arrays, stacks, multiplies and divides, and incrementing pointers up the wazoo.
No matter what language, the programmer must usually do some things that the language doesn't directly support. Also, no matter what language, the programmer must understand and live with the limitations of the language being used to properly solve the problem. This is what libraries are for.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
····BYTE[noparse]/noparse]@WordVar[noparse][[/noparse]x[noparse][[/noparse]y]
you could write
····WordVar[noparse][[/noparse]x].[noparse][[/noparse]y]
where the period (.) is a dereferencing operator for bytes. Likewise, a colon (:) could dereference words, and a double colon (::) could dereference longs.
This would also come in handy for linked lists or binary trees, viz:
····Tree[noparse][[/noparse]left]:[noparse][[/noparse]left]:[noparse][[/noparse]right] := Leaf
where left and right are constants equal to 0 and 1, respectively.
-Phil
The concept you and me are talking about is "abstraction" - or put into another more philosophical dress: "What is reality?"
You and me know what a computer does: it switches transistors - somehow....
How come it can compute square roots??
Don't tell a mathematician (I happen to be one...) that drawing square roots is just switching transistors!
So what we do in computerscience is defining levels of abstraction, giving names to them taken from other areas, or just creating new things and giving them new names... Think of the "telephone"..It makes no sense to start an argument whether a telephone is really a "tele-phone".
So we invent "logical gates" (which is no longer easy at the low voltages (1V) we use nowadays inside high end computers), "storage words", "jump instructions", "floating point operations"... This can be considered as "buttom-up abstraction"
Starting in the mid-50th (with some fore-runners) computer scientists used abstraction layers top-down rather then bottom-up. The talked of "loops" rather than "jumps", "arrays", "stacks" and "queues" rather than "memory vectors", "lists" rather than "total mess from multiple pointers"
This was possible by the success of compiler construction techniques, which allowed it to define (and even debug!) algorithms in terms of the application (problem) domain without any reference to the underlying hardware.
This could only work with implementions fully suporting the taken "metaphors", further supporting this by symbolic debug aids, as bounds checking.
An "array" is the computer science concept of mathematical matrices, useful for many numerical and graph oriented algorithms, not always useful for other stuff. Its main charactistic is its two-dimensionality (=rank), or - more generalized- its arbitrary rank. A "one-dimensional" array is a very special case.
There are two or three methods how to map the "arrays" of a computer language into machine memory, the most common is to allocate them either row-wise or column-wise and add a descriptor somewhere in memory as a "proxy" so you still can tease it apart again.
Note that even if the "bounds" of the array are static and thus known by the compiler at compile time in the module where an "array" is defined, this is no longer the case after the array is used as a parameter to a subroutine. Believe it or not: This has been a major topic in compiler construction and language design for more than 10 years!
I think is has become obvious now, that - as SPIN does nothing of all these complex things - there are no "arrays" in SPIN at all.
I will not take offence using the term "indexing" when accessing the n-th element after a reference element (pointer) . This is exactly as it is used in machine code.
And this is the situation with SPIN, and I don't want to be demissive, but just for the sake of clarity: There is nothing of what is called "data structure" since the era of Wirth in SPIN. SPIN's data metaphor is the machine code metaphor of a linear memory vector, sectioned by specific marks (labels).
is shorthand for
nothing more, nothing less.
Post Edited (deSilva) : 11/3/2007 6:49:57 PM GMT
Personally, I like the mental framework of Atari ST Basic's file structure for arrays: random = fixed length fields, variable = fixed length indexes to sequential variable length strings (yup, they're pinned). In either case, prefigure what you need X times y times datasize choice. Allocate what you need. Or find another way to solve the program.
Maybe what we should do is to stop beating around the theoretical bushes and start to document the implementations of data structures we run into around here. Sort of like Oldbitcollector's hardware cookbook, except for programmers. (Even money says Chip Gracey has the neatest stuff.)
But I see - day for day - people here that have the most extraordinary concepts of what could be an "array" in SPIN, as they will not read the manual, and try to transfer their knowledge from a language they used or use on another machine.
I still think the best way to help is making them aware that there simply are no arrays in SPIN
From your perspective, "Its main charactistic is its two-dimensionality", I can understand better now why you say there are no arrays, however, for anyone who considers an indexable linear list to be "an array", they can see all the evidence before them that there are arrays in Spin.
Maybe those people's definitions of arrays are "most extraordinary concepts" from your perspective, but from theirs there is absolutely nothing wrong with considering VAR long myArray[noparse][[/noparse] 10 ] as an array. Telling people there are no arrays in Spin is not going to change their view of what they see and perceive in my opinion. They have no care of what underlies Spin; it looks like what they'd call an array, it works like one, it can be used like one, so as far as they are concerned it is one.
You're also right; people take what they see as arrays in other languages, look at Spin, see that something looks remarkably the same ( one would be hard pushed to determine any difference from the two definitions and usage in C or Spin of a one-dimensional array if unfamiliar with either language ), and therefore consider Spin to have arrays. I don't see a problem with that.
I cannot see any benefit in telling programmers that there are no such things as arrays in Spin; they'll simply disagree. It's a loosing battle unless you want to repeat the explanation you gave here, and even then the majority will most likely still keep calling what they have arrays and believing they are
hmm...maybe i should write an alternative to spin and a compiler thats compatible with linux........
also, a little question. when spin does math, does it use the log/antilog tables for stuff like * or /, or does it just do loads of adds?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I hear and I forget. I see and I remember. I do and I understand
-Confucius
Post Edited (Lee Marshall) : 11/3/2007 8:59:24 PM GMT
If you refer to Floating Point, the matter is that multiplication is quite trivial in an mantissa/exponent format and does not take longer than addition...
-Phil
There are several things necessary here:
1. Documentation for SPIN should describe accurately what the [noparse]/noparse construct does.
2. Programmers using SPIN should read the documentation.
3. Forum members should not expect all subscribers to have thoroughly read the documentation, nor expect them to have full understanding of the implications of the contents thereof. There are those of us who read the manual front to back before using something (like me) and there are those who don't. We must all have some patience for those who learn from making mistakes, or learn by doing. Many very intelligent people learn that way. My wife is that way. Trust me, I've had to learn some patience and understanding!
4. Don't forget that many of us who add entries in this forum are here to help. Many people come and go and many don't have a history of all of the discussion on this forum nor an understanding of how easily to find the answers. Let's all be polite and point them in the right direction in a way that does not discourage them from discovering the wonders of the Propeller!
I'll get off my box now.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
Post Edited (Ken Peterson) : 11/3/2007 11:22:12 PM GMT
Being not a native speaker I of course have fomed my prejudices with some words, so I just consulted the Webster's:
I get the impression that - although some two-dimensional undertone is noticeable in all definitions - the basis meaning seems more to point to an orderly shape of identical elements, thus allowing the phrase "one-dimensional array", which always sounded funny to me...
However my point was never language, but the situation that SPIN stretches the meaning of general computer science terms (as object,...) to a great deal...
Post Edited (deSilva) : 11/4/2007 12:03:01 AM GMT
If a forum member has expectations for the SPIN language that are driven by knowledge of other languages, we must simply point out the differences as fact and save our opinions, lest we confuse and discourage our fellow members.
I would suggest that rather than bemoaning the shortcomings of SPIN in relation to general computer science theory, one should try to describe how some common constructs can be implemented using the means available in the SPIN language. A good programmer can figure out how to do anything with any language. The first step is to understand and accept it for what it is.
In my experience as an engineer, management does not care a damn about theory. It's all about deadlines and budgets. Give me the facts and I'll design accordingly. Sometimes you have to forget about how something should work theoretically and concentrate about how it works in fact.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
Post Edited (Ken Peterson) : 11/4/2007 12:13:59 AM GMT
But I think this forum should not only be a helpdesk for the repeating trouble of our new members, but also give the more experienced a chance for some new insight...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?
"Primitive" means: plain, simple, absolute minimum. KISS. But, having multiple equal elements, we can create very sophisticated solutions. The whole is more then the sum of the part. And that makes the propeller superior. You can create a "whole" within a single chip.
The concept of array follows from the need of having more complex data structures the just natural numbers. But natural numbers arise from the need to have more complex element than just a bit. We can not answer the question, if a bit is a very simple array or if an array is a very complex multitude of bits. But we have to decide the question. After doing this, we have to argue according to and consistent with this decision. In advance of every discussion we have to make clear, which point of view we own, otherwise, we can take, but will only have misunderstanding.
As an example: Complex numbers can be seen as an agglomeration of natural numbers, combined with certain rules of computation or natural numbers can be seen as complex numbers with imaginary part equal zero. A balance needs just numbers, no need to see them as complex numbers. But, trigonometry turn to a simple task, when we see the arguments as complex numbers.
So, the question is not, it SPIN knows array, but if we interpret the code as being an embodyment of such an array.
The memory model is plain.
To me arose the question: why do we call the main memory "hub memory"? To me, a hub memory is a memory cell, fused with the round robbing hub. I think it was a unfortunate choice to use this word, for it will lead to confusion.
Post Edited (ErNa) : 11/4/2007 12:25:55 PM GMT
When you use "data structures" you want to "abstract" from the machine level. E.g. Your language (compiler) might allow "bit arrays",
note that SPIN in fact has 16 bit arrays! Then you say: but they must not exceed 32 elements. o.k. this is fine. But you stand in the rain when you need 33...
The second concept behind "data structures" is their recursive character. You must be able to build them up. An array is a one or many dmensional ordered set of "other" data elements. This is only superflously the case with SPIN vectors, which can consist of bytes, words, and longs only.
The trick is that they do not form an ensemble of elements built up from, but they are a whole consecutive piece of memory, allowing the selection of smaller sections in a very efficient way, as they do not abstract from the hardware but use intrinsic similarities.
Post Edited (deSilva) : 11/4/2007 12:04:26 PM GMT