Shop OBEX P1 Docs P2 Docs Learn Events
Spin assignment operators question? — Parallax Forums

Spin assignment operators question?

rwgast_logicdesignrwgast_logicdesign Posts: 1,464
edited 2013-01-21 23:46 in Propeller 1
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

  • SRLMSRLM Posts: 5,045
    edited 2013-01-21 20:08
    = is "constant assignment", and := is "variable assignment". If I were naming =, I'd probably use "constant initialization".

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-01-21 20:34
    A constant "assignment" is not really an assignment at all, since it is not something that's executed by the Propeller. Rather, it's an equivalence pragma that simply gives a name to a numerical value during compile time. Thus, it would be inappropriate to express it with an assignment operator.

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2013-01-21 20:55
    I think Phil has it.
    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.
  • rwgast_logicdesignrwgast_logicdesign Posts: 1,464
    edited 2013-01-21 21:02
    Oh ok this makes sense I didn't really think of it that way.

    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
    '' PeKit Chp4 I/O & Timing'' Exercise 8
    '' Bin Shift with Buttons
    CON
    
    
      _xinfreq = 5_000_000
      _clkmode = xtal1+pll16x
    
    
    
    
    PUB ShiftLEDsLeft | pattern, divide 
    
    
      dira[16..23]~~
      divide := 16
    
    
        repeat
          if pattern == 0
           pattern := 000000
    
    
          if ina[1] == 1
            divide++
            divide<#=254
          elseif ina[2] == 1
            divide--
            divide#>=1
     
          waitcnt(clkfreq/divide + cnt)
          outa[16..23] := pattern
          pattern >>= 1
    

    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?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-21 21:22
    FIrst of all, all local variables are longs. Since local variables are allocated on the stack, their size doesn't count against the program size or the variable (VAR) block size.

    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).
  • rwgast_logicdesignrwgast_logicdesign Posts: 1,464
    edited 2013-01-21 22:12
    Ok got it that makes total sense now :), thank you guys. It is very important to keep your code as clean and small/fast as possible to me I ma be a bit early in the exercises to be thinking about this but coming from a programming background this kind of stuff is always on my mind.
  • ErlendErlend Posts: 612
    edited 2013-01-21 23:46
    About the :=
    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.
Sign In or Register to comment.