Shop OBEX P1 Docs P2 Docs Learn Events
How to Declare a string as a constant in SPIN? — Parallax Forums

How to Declare a string as a constant in SPIN?

william chanwilliam chan Posts: 1,326
edited 2008-09-11 04:44 in Propeller 1
Can a string be declared as a constant or it's impossible?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my

Comments

  • PraxisPraxis Posts: 333
    edited 2008-09-10 12:46
    DAT
            strHelloWorld             BYTE "HelloWorld",0
    
    




    Cheers
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-10 13:19
    Thanks a million.

    P.S. When is your production going to start?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • PraxisPraxis Posts: 333
    edited 2008-09-10 13:21
    Hi William,

    We did the OEM design work for that project, the customer will sub the production to production house in Penang.

    Cheers
  • PraxisPraxis Posts: 333
    edited 2008-09-10 13:22
    PS I forgot to ask, do you stock the new Professional proto board?
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-10 13:55
    You already expert in Propeller, no need the PPDB.


    I tried
    serial.str(string(strHelloWorld)) and serial.str(strHelloWorld)
    
    



    but both cannot send out the string.

    What gives?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • PraxisPraxis Posts: 333
    edited 2008-09-10 14:16
    serial.str(@strHelloWorld)
    
    



    Note the string constant must end with a Zero i.e. BYTE "HelloWorld",0

    If you want to add a carriage return to it then

    BYTE "HelloWorld",13,0

    Cheers
  • PraxisPraxis Posts: 333
    edited 2008-09-10 14:17
    william chan said...

    You already expert in Propeller, no need the PPDB

    Need something to play with lah.
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-09-10 14:21
    You can use

    serial.str(string"Hello World", $0D))

    or

    serial.str(@StrHelloWorld)

    (the @ is required to indicate it is a pointer to the memory)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-10 17:36
    The string pseudofunction requires parens:

    serial.str(string("Hello World", $0D))

    Also (and please correct me if I'm wrong), unless you're using the same string from more than one place in your program, or need to modify the string during execution, there's probably no advantage to using the DAT form.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • BradCBradC Posts: 2,601
    edited 2008-09-10 17:44
    Phil Pilgrim (PhiPi) said...
    The string pseudofunction requires parens:

    serial.str(string("Hello World", $0D))

    Also (and please correct me if I'm wrong), unless you're using the same string from more than one place in your program, or need to modify the string during execution, there's probably no advantage to using the DAT form.

    -Phil

    You are absolutely correct. For every STRING() the compiler places an independent copy in the Bytecode.
    Using a string in DAT and referencing it multiple times is _far_ more efficient.

    Remember kiddies .. strings from STRING() are all null terminated, so make sure to remember the ",0" in the DAT declaration or you might find your strings run on past what you are expecting.*!%@&#*($}}}}}}}}}}}}}}}}^Z^Z^Z^Z^Z^Z

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pull my finger!
  • william chanwilliam chan Posts: 1,326
    edited 2008-09-11 01:06
    I did

    serial.str(string("Hello World")
    
    



    without the $0D at the back (is that a null?) and it still works! without any extra exclamations.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-11 01:13
    The $0D is a carriage return. The "Hello World" will usually display as you expect, but the next item will directly follow the "d" rather than appear on the next line. You do need balanced parentheses ... one close parenthesis for each open parenthesis.
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-09-11 01:16
    The $0D is a carriage return (newline). (see an ASCII code chart for the character set $00-$1F which are control characters)
  • BradCBradC Posts: 2,601
    edited 2008-09-11 04:44
    william chan said...
    I did

    serial.str(string("Hello World")
    
    



    without the $0D at the back (is that a null?) and it still works! without any extra exclamations.

    The null is '0' and is inserted by the compiler when you use STRING(). You only need to add it manually when you
    declare a string in the DAT section

    DAT

    mystring byte "This is my string",0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pull my finger!
Sign In or Register to comment.