Accessing Bytes of Larger-Sized Symbols (Syntax 4)

In PUB and PRI blocks, syntax 4 of BYTE is used to read or write byte-sized components of word-sized or long-sized variables. For example:

VAR
  word WordVar
  long LongVar

PUB Main
  WordVar.byte := 0              'Set first byte of WordVar to 0 
  WordVar.byte[0] := 0           'Same as above
  WordVar.byte[1] := 100         'Set second byte of WordVar to 100
  LongVar.byte := 25             'Set first byte of LongVar to 25
  LongVar.byte[0] := 25          'Same as above
  LongVar.byte[1] := 50          'Set second byte of LongVar to 50
  LongVar.byte[2] := 75          'Set third byte of LongVar to 75
  LongVar.byte[3] := 100         'Set fourth byte of LongVar to 100

This example accesses the byte-sized components of both WordVar and LongVar, individually. The comments indicate what each line is doing. At the end of the Main method, WordVar will equal 25,600 and LongVar will equal 1,682,649,625.

The same techniques can be used to reference byte-sized components of word-sized or long-sized data symbols.

PUB Main | Temp
  Temp := MyData.byte[0]         'Read low byte of MyData word 0
  Temp := MyData.byte[1]         'Read high byte of MyData word 0
  MyList.byte[3] := $12          'Write high byte of MyList long 0
  MyList.byte[4] := $FF          'Write low byte of MyList long 1

DAT
  MyData word $ABCD, $9988       'Word-sized/aligned data
  MyList long $FF998877, $EEEE   'Long-sized/aligned data

The first and second executable lines of Main read the values $CD and $AB, respectively, from MyData. The third line writes $12 to the high byte of the long in element 0 of MyList, resulting in a value of $12998877. The fourth line writes $FF to the byte at index 4 in MyList (the low byte of the long in element 1), resulting in a value of $EEFF.

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