Shop OBEX P1 Docs P2 Docs Learn Events
Array of text strings, how to ?? — Parallax Forums

Array of text strings, how to ??

RogerInHawaiiRogerInHawaii Posts: 87
edited 2009-02-22 20:34 in Propeller 1
I'm trying to construct an array of text strings in Spin. Since that doesn't seem to be directly possible I've taken the approach of declaring the strings separately and then having an array of pointers to those strings. Here's my code:

DAT

        MenuItemQueryMode       byte            "Query Mode", 0
        MenuItemFreeRunning     byte            "Free Running", 0
        MenuItemSnapshot        byte            "Snapshot", 0
        MenuItemCalibrate       byte            "Calibrate", 0

        MenuMain word   @MenuItemQueryMode, @MenuItemFreeRunning, @MenuItemSnapshot, @MenuItemCalibrate, 0




When I look at the code using the Object Info window the MenuItems reside at the following positions (in Hex):

        MenuItemQueryMode        28
        MenuItemFreeRunning       33
        MenuItemSnapshot           40
        MenuItemCalibrate            49




When I run the program I write out some diagnostic information to the Parallax Serial Terminal. The MenuMain (which is my array of pointers to the various MenuItemStrings) happens to reside at location 83 (hex). Looking at the set of consecutive values that reside there I see that it thinks that the menu item strings start at the following positions:

        MenuItemQueryMode        18
        MenuItemFreeRunning       23
        MenuItemSnapshot           30
        MenuItemCalibrate            39




That is, each string pointer is pointing at the wrong location. I seems that each pointer value is exactly 10 Hex less than it should be. And, indeed, when I try to access the strings via the pointers contained within the MenuMain array I get only partial strings, because the pointers are not pointing at te correct starting character for each string.

How do I get this to work as I desire?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-21 23:28
    The compiler doesn't know the actual address of the strings as it compiles them. It figures that out later, after compiling everything. If you use "@" in an expression executed at run-time, Spin uses the actual address, but, if you compile an address value into a constant, the compiler uses the relative address within the object. Within Spin code, you can use the "@@" operator to fetch a relative address and add the base address of the object to it. Read pages 278-9 in the Propeller Manual for a more detailed explanation.
  • RogerInHawaiiRogerInHawaii Posts: 87
    edited 2009-02-22 00:01
    Thanks for the quick and informative reply. I read through that section and think I understand it. I tried:

      VideoScreen.WriteText(@@MenuMain)
    
    



    And that works fine. There should be the number 3 in square brackets after the @@MenuMain, but it doesn't seem to show up in code mode. (VideoScreen.WriteText is my modified version of the FullDuplexSerialPlus object)

    But I have a complication. My intention is to pass MenuMain (i.e. the address of it) to a function which then uses it to index through its elements, getting the address of each of its strings, and use those strings.

    But if I pass the address of MenuMain, as in:

    MyMenuHandler.Show(@MenuMain, 10, 20, 0)
    
    



    when it gets into the Show method all it knows is the absolute address of the object. It doesn't know the object itself.

    What do I do when I pass in something like MenuMain to a method? How do I get it to calculate the correct addresses?



    
    PUB Show (AddressOfMenuStrings, PositionX, PositionY, SelectItemNumber) : Success | Index, ItemAddress, Y
    
      Index := 0
      Y := PositionY
      ItemAddress := @AddressOfMenuStrings[noparse][[/noparse]Index]
      repeat while ItemAddress <> 0
    
        VideoScreen.PositionSet(PositionX, Y)
        VideoScreen.WriteText(ItemAddress)
        ++Y
        ++Index
         ItemAddress := @AddressOfMenuStrings[noparse][[/noparse]Index]
    
    



    So the @ symbol isn't right, and neither apparently is the @@ symbol.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-22 00:53
    Best thing is to fix all the addresses yourself during your program's initialization. You'd do something like:
    x := 0
    repeat while MenuMain[noparse][[/noparse]x] <> 0
       MenuMain[noparse][[/noparse]x] := @@MenuMain[noparse][[/noparse]x]
       x++
    


    This will change all the addresses to absolute addresses rather than offsets from the start of the object's data area.
    The rest of your program can use the corrected addresses.
  • RogerInHawaiiRogerInHawaii Posts: 87
    edited 2009-02-22 20:34
    Thanks, Mike, that did the trick.

    - Roger
Sign In or Register to comment.