PDA

View Full Version : declaring and accessing arrays / lookup tables


dfitz
12-09-07, 08:26 AM
Hi,

I've been trawling through the forums to find an easy answer to my problem, and I'm not sure if i'm missing something or if it's too late on a Sunday night, in either case I have a couple of questions that are of importance to me and I imagine others at some point.

1) I'm trying to define a simple lookup table of values (just a one dimensional array) of length 255. I was initially trying to make this an array of floats, but I can settle for ints and just divide by 10 (one decimal point resolution for my numbers i'm looking up) when I access them. I've tried :
VARIABLE DMD_TABLE 255 ALLOT
to allocate initially and then to write values to the array I was trying:
0 CELL+
2 CELL+
4 CELL+
7 CELL+
10 CELL+
13 CELL+
16 CELL+
.
.
.
DMD_TABLE !

BUT, the plug-a-pod was crashing due to some memory accessing problem i'd say.
So in short, I have 255 numbers that don't change during my program (so they could be constants) and I need to know how to declare them.

2) I'd like to write a simple function that accesses the array and returns the value for a particular row, which I would have though would just be as simple as something like:
the_variable_name index_value + @


All help appreciated.
thanks.
Daniel

RMDumse
12-09-07, 08:56 AM
You're almost there.

You create a head or an address for the array. There are several ways. If you use the VARIABLE and xx ALLOT as you've shown, you have to remember the zeroth value in the array is the variable itself. This array is in RAM then, and difficult to initialize. It's not a table of constants which is what it looks like you want.

So instead, why not just CREATE a head.

Now the easiest way to get things into memory (other than an explicit store, is to "common" them in. However we will be using P, because we will be working in program space.

CREATE DMD_TABLE
0 P,
2 P,
4 P,
7 P,
( etc.
EEWORD ( optional move to flash

Now if you want the xth element it's x DMD_TABLE + P@.

You could make a word, INX_INTO_DMD_TABLE which was defined as DMD_TABLE + P@. Feed it an index, and it draw your value from the table to the stack.

dfitz
12-10-07, 07:30 AM
that worked a treat.

thanks