Propeller Manual

Table Of Contents
2: Spin Language Reference – WORD
Addressing Main Memory
As Figure 2-5 suggests, main memory is really just a set of contiguous bytes (see “data as
bytes” row) that can also be read as words (2-byte pairs) when done properly. In fact, the
above example shows that even the addresses are calculated in terms of bytes. This concept
is a consistent theme for any commands that use addresses.
Main memory is ultimately addressed in terms of bytes regardless of the size of value you are
accessing; byte, word, or long. This is advantageous when thinking about how bytes, words,
and longs relate to each other, but it may prove problematic when thinking of multiple items
of a single size, like words.
For this reason, the
WORD designator has a very handy feature to facilitate addressing from a
word-centric perspective. Its BaseAddress field when combined with the optional Offset field
operates in a base-aware fashion.
Imagine accessing words of memory from a known starting point (the BaseAddress). You
may naturally think of the next word or words as being a certain distance from that point (the
Offset). While those words are indeed a certain number of “bytes” beyond a given point, it’s
easier to think of them as a number of “words” beyond a point (i.e., the 4
th
word, rather than
the word that starts beyond the 6
th
byte). The WORD designator treats it properly by taking the
Offset value (units of words), multiplies it by 2 (number of bytes per word), and adds that
result to the BaseAddress to determine the correct word of memory to read. It also clears the
lowest bit of BaseAddress to ensure the address referenced is a word-aligned one.
So, when reading values from the
MyData list, word[@MyData][0] reads the first word value,
word[@MyData][1] reads the second word value, and word[@MyData][2] reads the third.
If the Offset field were not used, the above statements would have to be something like
word[@MyData], word[@MyData+2], and word[@MyData+4], respectively. The result is the same,
but the way it’s written may not be as clear.
For more explanation of how data is arranged in memory, see the
DAT section’s Declaring
Data(Syntax 1) on page 100.
An Alternative Memory Reference
There is yet another way to access the data from the code example above; you could
reference the data symbols directly. For example, this statement reads the first word of the
MyData list:
Temp := MyData[0]
...and these statements read the second and third words of MyData:
Propeller Manual v1.1 · Page 231