Shop OBEX P1 Docs P2 Docs Learn Events
Long aligned string — Parallax Forums

Long aligned string

iammeriammer Posts: 4
edited 2013-01-20 09:19 in Propeller 1
Hi all,

Quick question maybe someone can help with. I would like to put a long-aligned string in hub memory with each character taking a byte. In my DAT block, if I do:
label long "string"
I get the string long aligned, but it uses 1 long per character.

If I do:
label byte "string"
I get a byte per character, but the string is not long aligned.

I see per the manual you can override alignment, but it looks like you can only do that with a smaller type. i.e.
label byte long "string"
This still aligns to a byte, what I would like to do is something like
label long byte "string"
but, this does not seem to be valid syntax

Does anyone have an idea how to do this?

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-01-20 08:15
    It's possible to long align an array of characters (bytes). However, doing so is not compatible with native string commands and standard string conventions.
    myString long "s", "t", "r", "i", "n", "g" 
    

    You really should use a byte array to define a string. Why do you need a long aligned character?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-01-20 08:24
    The "org" statement will long align the following variable.
    org
    label byte "string"
    

    I'm pretty sure there are other ways to do this but I don't recall how.
  • iammeriammer Posts: 4
    edited 2013-01-20 08:31
    Mike/Duane,

    Thanks.
    [COLOR=#333333][FONT=Parallax]myString long "s", "t", "r", "i", "n", "g"[/FONT][/COLOR]
    
    seems to be the same thing as
    myString long "string"
    

    What I am looking for is for the characters to be bytes but the string itself to be long-aligned (i.e. the first character starts at a memory address divisible by 4)

    Looks like Duane's idea worked perfectly. Thanks.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-01-20 08:49
    DAT
    
    label     long
              byte "string",0
    
  • Mike GMike G Posts: 2,702
    edited 2013-01-20 08:57
    A string can not be long aligned as a string is an array of bytes and not necessarily divisible by 4. You mean pack 4 characters in a long. Don't forget that longs are stored as little endian; LSB first.

    I'm a little confused though, what is the difference between
    DAT
          org
    label byte "string"
    

    and simply
    label byte "string", 0
    

    both are byte data types...
  • Mike GMike G Posts: 2,702
    edited 2013-01-20 09:05
    This is what I mean by little endian
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
    
    OBJ
      pst             : "Parallax Serial Terminal"
    
    DAT
      mystring    long
                  byte  "string", 0
      workspace long
                byte    $0[16]
    
    PUB Main
        pst.Start(115_200)
        pause(500)
    
        DisplayMemory(@mystring, strsize(@mystring), false)
                      
        workspace := $31323334  '1234
        
        DisplayMemory(@workspace, strsize(@workspace), false)
    
    
    PUB DisplayMemory(addr, len, isHex) | j
      pst.str(string(13,"-----------------------------------------------------",13))
      pst.str(string(13, "      "))
      repeat j from 0 to $F
        pst.hex(j, 2)
        pst.char($20)
      pst.str(string(13, "      ")) 
      repeat j from 0 to $F
        pst.str(string("-- "))
    
      pst.char(13) 
      repeat j from 0 to len
        if(j == 0)
          pst.hex(0, 4)
          pst.char($20)
          pst.char($20)
          
        if(isHex)
          pst.hex(byte[addr + j], 2)
        else
          pst.char($20)
          if(byte[addr+j] == 0)
            pst.char($20)
          pst.char(byte[addr+j])
    
        pst.char($20) 
        if((j+1) // $10 == 0) 
          pst.char($0D)
          pst.hex(j+1, 4)
          pst.char($20)
          pst.char($20)  
      pst.char(13)
      
      pst.char(13)
      pst.str(string("Start: "))
      pst.dec(addr)
      pst.str(string(" Len: "))
      pst.dec(len)
      pst.str(string(13,"-----------------------------------------------------",13,13))
      
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-01-20 09:17
    Mike G wrote: »
    A string can not be long aligned as a string is an array of bytes and not necessarily divisible by 4.

    My interpretation of the problem was just to get the first character "s" long aligned. As you say, the other characters (or 3/4 of them) will not be long aligned.

    I imagine iammer wants to pass the start of the string to a cog running PASM using the "par" register.

    Beau showed another way (IMO better) to long align the first character of a string of bytes.
  • iammeriammer Posts: 4
    edited 2013-01-20 09:19
    Hi Mike,

    Yes, I want to pack 4 characters into a long. What I am looking to do is read 4 bytes of a string at a time using a rdlong instruction. To do that I need the first byte of the string to be long aligned, as rdlong can only read from long aligned addresses. Org is used to set a reference point for symbols in cog memory. And it looks like it long-aligns as well since pasm code must be long aligned.

    I ended up doing it like Beau's example, since that is a lot cleaner.

    Thanks for the note on the endianess. Little endian is what I want.
Sign In or Register to comment.