Shop OBEX P1 Docs P2 Docs Learn Events
Memory Array Pointer to Long — Parallax Forums

Memory Array Pointer to Long

sobakavasobakava Posts: 34
edited 2012-06-29 11:01 in Propeller 1
I've tables located in the memory and it is defined like this:
my_table1 byte $10 , $15
byte $12 , $20
.
.

my_table2 byte $17 , $11
byte $13 , $22
.
.

I am accesing this data in this way:
blabla :=  my_table1[ 4 ] * my_table1[ 5 ]



but I want to use another table instead of the first one. I thought I can use a pointer (long) and use it like this:
long my_table_pointer
.
.
.
.
if ( table_nr == 1 )
my_table_pointer := my_table1
if ( table_nr == 2 )
my_table_pointer := my_table2
.
.
[code]blabla := my_table_pointer[ 4 ] * my_table_pointer[ 5 ]



But it seems this approach does not work. I read incorrect data from memory.

What is the most convenient way to do this? I don't want to replicate blabla calculations using if conditions for each table because it requires a lot of memory space.

Thanks!

Comments

  • tonyp12tonyp12 Posts: 1,951
    edited 2012-06-29 07:00
    >blabla := my_table_pointer[ 4 ] * my_table_pointer[ 5 ]

    Will not work.
    Are all the tables the same lenght, say 32 bytes?
    If so put them after each other.

    blabla := my_table1[ 4 + table_nr *32] * my_table1[ 5 + table_nr *32]
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-06-29 08:13
    You can use a pointer if you assign it before the table access -- something like this
    table1pntr := @table1
      table2pntr := @table2
      result := byte[table1pntr][index1] * byte[table2pntr][index2]
    


    Note that when fetching values this way you need to be explicit as to the size of the elements in your table, hence byte[][] was used.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-06-29 10:52
    You could use an array of pointers as follows:
    my_table1 byte $10 , $15
    byte $12 , $20
    .
    .
    my_table2 byte $17 , $11
    byte $13 , $22
    .
    .
    my_table word @my_table1, @my_table2
    .
    .
    blabla := byte[@@my_table[idx1]][4] * byte[@@my_table[idx2]][5]
    
    idx1 and idx2 would have values of 0 or 1. The addresses stored in my_table are object offsets, so you need to use the @@ operator to get the absolute address. The object offsets could be converted to absolute address at the beginning of the program so you would not have to use the @@ operator later on. That would look something like this:
    repeat i from 0 to 1
      my_table[i] := @@my_table[i]
    .
    .
    blabla := byte[my_table[idx1]][4] * byte[my_table[idx2]][5]
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-06-29 11:01
    My previous example may be more complex than you need if idx1 equals idx2, which equals nr. In that case you could use my_table_ptr as follows:
    my_table_ptr := @@my_table[nr-1]
    blabla := byte[my_table_ptr][4] * byte[my_table_ptr][5]
    
Sign In or Register to comment.