Shop OBEX P1 Docs P2 Docs Learn Events
Why does DataLoc++ work one way but not another — Parallax Forums

Why does DataLoc++ work one way but not another

dbpagedbpage Posts: 217
edited 2015-12-21 22:18 in Propeller 1
Why does
DataLoc := 0
repeat i from 0 to 10              
  long[VarData][DataLoc] := DataLoc++
Result in
0	0	1	2	3	4	5	6	7	8	9 ...
and
DataLoc := 0
repeat i from 0 to 10              
  long[VarData][DataLoc] := DataLoc
  DataLoc++                        
Result in
0	1	2	3	4	5	6	7	8	9     10 ...

Comments

  • I'm assuming you understand why your second snippet behaves how it does.

    Your first snippet behaves how it does because the right hand side of an assignment operator in Spin is evaluated before the left hand side. The DataLoc in "long[VarData][DataLoc]" is already incremented by the time the address to store the value at is calculated.

    In other words, "long[x] := y" expects x to be above y on the stack. Therefore, y has to be calculated first. The incrementation of DataLoc happens when the y in my example is evaluated, and it is used again after that when the x in my example is evaluated.
  • That explains why
    DataLoc := 0
    repeat i from 0 to 10              
      long[VarData][DataLoc++] := DataLoc
    
    works the same as the second snipet.

    Thanks!
Sign In or Register to comment.