Shop OBEX P1 Docs P2 Docs Learn Events
Variables: Need clarification — Parallax Forums

Variables: Need clarification

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2007-08-01 18:12 in Propeller 1
I have to admit that I'm getting very lost in variables.
Would someone mind giving me some examples in .spin based on these basic examples?

A$="This is not a number"

A=1

A=A+1

A=A-1

LET B = A

If there is some clarification elsewhere in print, (the prop manual isn't helping)
please point in that direction. Thanks!

Jeff

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The comments and code above are proof that a million monkeys with a million propeller chips *could* write Shakespeare!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-31 15:07
    1) There are no string variables in Spin, but you can use string constants and byte arrays to do some of the same functions. In this case, you'll need to declare a method to do the string copy using primitives included in Spin. Note that there's not checking for overflow of the variable. You could add checking to strcopy if you need to.
    VAR byte a[noparse][[/noparse] 20 ]
    
    PUB strcopy( dst, src)
       bytemove(dst, src, strsize(src)+1)
    
    { now you can do: strcopy(a,string("This is not a number")) }
    



    2) A := 1

    3) A := A + 1 or A += 1 or A++

    4) A := A - 1 or A -= 1 or A--

    5) B : = A
  • deSilvadeSilva Posts: 2,967
    edited 2007-07-31 17:13
    To anticipate a further question:
    .......  string("xxxxxxxxxx") ......
    


    is nearly exactly the same as:
    ......... @s9876543 ........
    
    DAT
    s9876543 BYTE "xxxxxxxxxx",0
    
    



    Edit: STRING is very versatile: You can also write things like :
    string("ABC",10,200,"XYZ",13)
    


    Of course you must not use 0...

    Post Edited (deSilva) : 7/31/2007 5:18:30 PM GMT
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-07-31 22:36
    deSilva said...
    Of course you must not use 0...
    I assume you mean ascii $00, not ascii 48, "0".
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-31 22:45
    Fred,
    Strings in Spin use a zero byte ($00) as a terminator character, like in C and some other programming languages.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-08-01 01:52
    Forgive me... But why does this work?

    VAR byte a[noparse][[/noparse] 20 ]

    [noparse][[/noparse]set the length of a?]

    PUB strcopy( dst, src) [noparse][[/noparse]a function with destination and source?]
    bytemove(dst, src, strsize(src)+1) [noparse][[/noparse]move destination to source +1??]

    { now you can do: strcopy(a,string("This is not a number")) }

    so a now equals "This is not a number" ?

    Lost...

    Oldbitcollector

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The comments and code above are proof that a million monkeys with a million propeller chips *could* write Shakespeare!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-01 02:49
    bytemove requires a byte count as well as the address of the data and the address of where to put it.

    strsize scans the string looking for a zero byte and returns the number of non-zero bytes found before the zero byte which is the length of the string. strsize+1 includes the zero byte in the length.

    The declaration "VAR byte a[noparse][[/noparse] 20 ]" simply allocates a 20 byte block of storage.

    I did make a mistake in the example in the comment. It should be: strcopy(@a,string("This is not a number"))
    The difference is that the "@" operator produces an address (of a) which is what strcopy expects. The "string" function already returns an address.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-01 18:12
    Mike Green said...
    The "string" function already returns an address.
    STRING should be called a "compiler directive" rather then a "function". Note that it has all the limitations constant expressions have..
Sign In or Register to comment.