Shop OBEX P1 Docs P2 Docs Learn Events
bits of arrays — Parallax Forums

bits of arrays

Lee MarshallLee Marshall Posts: 106
edited 2007-11-04 11:58 in Propeller 1
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....
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

  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-03 06:36
    The notation for setting individual bits only works for specific special registers like OUTA and DIRA. It doesn't work for variables, so an_array[noparse][[/noparse] 10 ] can only mean the 11th element of an_array (starting at zero).
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 08:34
    @Lee: I am preaching this for months - with little success: There are no arrays in SPIN! There is a fine notation "[noparse][[/noparse]" and "]" with which you can predefne HUB memory and address any HUB cell, but that's all. There is no intelligence whatever from the compiler with this.

    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
  • hippyhippy Posts: 1,981
    edited 2007-11-03 12:38
    Mr Crowley said...
    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

    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.
    Mr Crowley said...
    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??

    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 ...

    PUB SetBitInLong( ptrToLong, bitNumber )
      long[noparse][[/noparse] ptrToLong ] |= |< bitNumber
    
    
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-11-03 16:50
    deSilva, what's your definition of an array? It seems wrapped up in what the compiler does or doesn't do for its user.

    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.
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-11-03 17:57
    Whether your want to call it an array or not, whatever the SPIN compiler does with [noparse]/noparse serves the purpose of an array for me. One must not assume that using [noparse][[/noparse]x][noparse][[/noparse]y] will give you a two-dimensional array, however, because it will simply add x and y and use that for an index. But, of one needs a two-dimensional array and codes a work-around, then what results IS a two-dimensional array in an abstract sense, if not in a language sense. It comes down to how you conceptualize your problem and your solution, and how much of that solution is directly supported by your language.

    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?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-11-03 18:32
    Actually, variable[noparse][[/noparse]x][noparse][[/noparse]y] won't compile, as well it should not. That said, it would be nice if Spin had a shorthand dereferencing mechanism so that multidimensional arrays and other structures could be expressed in a meaningful way. For example, instead of

    ····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
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 18:38
    @Fred: Are not all our issues rooted in the wrong use of names smile.gif

    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).
    SIZE aVar
    ...
      x := aVar[noparse][[/noparse] aValue ]
    


    is shorthand for
    x := SIZE[noparse][[/noparse]@aVar][noparse][[/noparse]aValue]
    


    nothing more, nothing less.

    Post Edited (deSilva) : 11/3/2007 6:49:57 PM GMT
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-11-03 19:14
    That's an easy one: square roots are successive approximations and your routine's speed is dependent upon your first guess. (Nonmathematician, distilling a lot of noise over the years.)

    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.)
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 19:40
    I feel misunderstood shakehead.gif I myself have no interest in theoretical bushes... I fuly understand how SPIN works (really idea.gif )
    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 smilewinkgrin.gif
  • hippyhippy Posts: 1,981
    edited 2007-11-03 20:16
    It perhaps depends upon what an individual defines an "array" to be.

    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 smile.gif
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 20:25
    If you think so, I can't help it smile.gif
  • Lee MarshallLee Marshall Posts: 106
    edited 2007-11-03 20:45
    To me, an array is a concept on the part of the programmer, which, on the computer's part, only involves RESERVING a certain amount addresses, starting at a base address. as with most programming languages [noparse][[/noparse] ] or () or whatever, offsets the address by whatever number you put in the middle.

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-03 21:36
    When Spin does multiplication or division, it uses a short subroutine that does it a bit at a time. That's 32 loops. The Propeller has some instructions that make division much more efficient than you might think. There's a sample multiplication and division routine in a document called "Propeller Guts" in Graham Stabler's Pretty Good List of links in a sticky thread at the top of the forum's thread list.
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 21:50
    This code is also reprinted in deSilvas "Tutorial" smile.gif

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-11-03 22:40
    Another way to define an array, and the one I was aluding to above, is a one-dimensional (only!!) indexed list. However, each array element can be an address (i.e. a pointer) that can refer to anything, including another array. In such a case, the first subscript retrieves a pointer to the other array, and the second subscript points to a value in that array. (This, BTW, is the way Perl does multi-dimensional arrays.) While this is inefficient for storing statically-allocated two-dimensional arrays, it's more general, in that it can be extended to other data structures. One must do his own bookkeeping, however, since Spin is agnostic as to what a pointer refers to or, for that matter, whether the number in question is a pointer at all, or just a terminal value.

    -Phil
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-11-03 23:13
    Again, my point is that one needs to understand what SPIN does, and implement one's solution accordingly. What SPIN does is provide a variable that can be indexed to subsequent locations. The physical result in memory of the index depends on the type of the base variable. In this way, it can quite simply implement a one-dimensional array. With some fancy programming, one can implement a multi-dimensional array, but not in the way that C supports multi-dimensional arrays.

    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
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-03 23:14
    @Phil: That was the second methode how to implement arrays; as we seem to elaborate things I could also mention the third method using either general linked lists or bit-vectors for sparse arrays.

    Being not a native speaker I of course have fomed my prejudices with some words, so I just consulted the Webster's:
    1 
    a: a regular and imposing grouping or arrangement : order <lined up&#8230;in soldierly array &#8212; Donald Barthelme> 
    b: an orderly listing of jurors impaneled
    2
    a: clothing, attire 
    b: rich or beautiful apparel : finery
    3: a body of soldiers : militia <the baron and his feudal array>
    4: an imposing group : large number <faced a whole array of problems>; also : variety, assortment <a broad array of styles>
    5 a
    (1): a number of mathematical elements arranged in rows and columns 
    (2): a data structure in which similar elements of data are arranged in a table 
    b: a series of statistical data arranged in classes in order of magnitude
    6: a group of elements forming a complete unit <an antenna array>
    



    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
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-11-03 23:26
    @deSilva: wouldn't it be nice if SPIN would support a multi-dimensional array of objects like C++ and Java can? While I'm wishing...even function pointers would be nice. Oh well, we are talking about a microcontroller with a built-in language. Let's lower our expectations a bit. I have full understanding of your arguments, but in the context of programming the Propeller, one must concentrate on implementation rather than theory.

    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
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-04 00:04
    Yes Ken, please do smile.gif

    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...
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-11-04 00:13
    I agree. It needs to be both. One should be aware as to the context of the original question, and phrase one's responses accordingly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-04 10:26
    That's not a problem. When I flop, Hippy will paraphrase me smilewinkgrin.gif
  • ErNaErNa Posts: 1,749
    edited 2007-11-04 11:35
    The propeller is a very primitive complex piece of silicon.
    "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
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-04 11:58
    ErNa's posting gave me the chance for another elaboration on my point of view, promised to be the last one, in this thread at least...

    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
Sign In or Register to comment.