Declaring Data (Syntax 1)

Data is declared with a specific alignment and size (BYTEWORD, or LONG) to indicate how it should be stored in memory. The location where data is actually stored depends on the structure of the object and the application it is compiled into since data is included as part of the compiled code.

For example:

DAT
  byte 64, "A", "String", 0
  word $FFC2, 75000
  long $44332211, 32

The first thing on line two of this example, BYTE, indicates the data following it should be byte-aligned and byte-sized. At compile time, the data following BYTE, 64, "A", etc., is stored in program memory a byte at a time starting at the next available location. Line three specifies word-aligned and word-sized data. Its data, $FFC2 and 75000, will begin at the next word boundary position following the data that appeared before it; with any unused bytes from the previous data filled with zeros to pad up to the next word boundary. The fourth line specifies long-aligned and long-sized data; its data will be stored at the next long boundary following the word-aligned data that appeared before it, with zero-padded words leading up to that boundary. The following table shows what this looks like in memory (shown in hexadecimal).

Example Data In Memory:

L

0

1

2

3

4

5

W

0

1

2

3

4

5

6

7

8

9

10

11

B

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

D

40

41

53

74

72

69

6E

67

00

00

C2

FF

F8

24

00

00

11

22

33

44

20

00

00

00

L = longs, W = words, B = bytes, D = data

The first nine bytes (0 – 8) are the byte data from line one; $40 = 64 (decimal), $41 = "A", $53 = "S", etc. Byte 9 is padded with zero to align the first word of word-aligned data, $FFC2, at byte 10. Bytes 10 and 11 (word 5) contain the first word-sized value, $FFC2, stored in low-byte-first format as $C2 and $FF. Bytes 12 and 13 (word 6) is the lowest word of 75000; more on this later. Bytes 14 and 15 (word 7) are zero padded to align the first long of long-aligned data, $44332211. Bytes 16 through 19 (long 5) contain that value in low-byte-first format. Finally, bytes 20 through 23 (long 6) contains the second long of data, 32, in low-byte-first format.

You may have noticed that the value 75000 was specified as a word-sized one. The number 75000 in hexadecimal is $124F8, but since that's larger than a word, only the lowest word ($24F8) of the value was stored. This resulted in word 6 (bytes 12 and 13) containing $F8 and $24, and word 7 (bytes 14 and 15) containing $00 and $00 due to the padding for the following long-aligned values.

This phenomenon, whether or not it is intentional, occurs for byte-aligned/byte-sized data as well, for example:

DAT
  byte $FFAA, $BB995511

...results in only the low bytes of each value, $AA and $11 being stored in consecutive locations.

Occasionally, however, it is desirable to store an entire large value as smaller elemental units that are not necessarily aligned according to the size of the value itself. To do this, specify the value's size just before the value itself.

DAT
  byte word $FFAA, long $BB995511 

This example specifies byte-aligned data, but a word-sized value followed by a long-sized value. The result is that the memory contains $AA and $FF, consecutively, and following it, $11, $55, $99 and $BB.

If we modify line three of the first example above as follows:

word $FFC2, long 75000 

...then we'd end up with $F8, $24, $01, and $00 occupying bytes 12 through 15. Byte 15 is the upper byte of the value and it just happens to be immediately left of the next long boundary so no additional zero-padded bytes are needed for the next long-aligned data.

Optionally, the Symbol field of syntax 1 can be included to "name" the data. This makes referencing the data from a PUB or PRI block easy. For example:

DAT
  MyData byte $FF, 25, %1010

PUB GetData | Temp
  Temp := MyData[0] 'Get first byte of data table 

This example creates a data table called MyData that consists of bytes $FF, 25 and %1010. The public method, GetData, reads the first byte of MyData from main memory and stores it in its local variable, Temp.

You can also use the BYTEWORD, and LONG declarations to read main memory locations. For example:

DAT
  MyData byte $FF, 25, %1010 

PUB GetData | Temp
  Temp := BYTE[@MyData][0] 'Get first byte of data table 

This example is similar to the previous one except that it uses the BYTE declaration to read the value stored at the address of MyData. Refer to BYTEWORD, and LONG for more information on reading and writing main memory.

Declaring Repeating Data

Data items may be repeated by using the optional Count field. For example:

DAT
  MyData byte 64, $AA[8], 55 

The above example declares a byte-aligned, byte-sized data table, called MyData, consisting of the following ten values: 64, $AA, $AA, $AA, $AA, $AA, $AA, $AA, $AA, 55. There were eight occurrences of $AA due to the [8] in the declaration immediately after it.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.