pTAL Guidelines for TAL Programmers

Coding Guidelines
pTAL Guidelines for TAL Programmers527256-002
2-17
Incrementing and Decrementing Pointers
Incrementing and Decrementing Pointers
Guideline: Use indexing to increment and decrement pointers.
Following this guideline does not affect your program’s performance, but can make
your program easier to understand and maintain.
Use indexing to increment and decrement pointers, rather than explicitly adding values
to or subtracting values from the addresses in pointers. In Example 2-12 on page 2-18,
all three assignment statements have the same effect. The first assignment, however,
requires that you know exactly how many bytes the compiler allocates for each
occurrence of the array. The second is not affected by changes to the size of the
structure s but is unnecessarily complex.
Table 2-3. Valid Arithmetic Operations on Addresses
General Expression
Result
Type Example Meaning
atype
1
‘+’ INT atype @wp := @wp ‘+n; Increment @WP by n
words.
INT ‘+atype
1
atype @bp := n ‘+’ @bp; Increment @BP by n
bytes.
atype
1
‘-’ INT atype @wp := @wp ‘-n; Subtract n bytes
from a word pointer.
EXTADDR + INT(32) EXTADDR Add 32-bit integer to
an extended pointer.
INT(32) + EXTADDR EXTADDR Add 32-bit integer to
an extended pointer.
EXTADDR - INT(32) EXTADDR Subtract a 32-bit
integer from an
extended pointer.
atype
1
‘-
2
atype
1
INT l := @b2 ‘-’ @b1 Compute bytes or
words between two
byte or word pointers.
EXTADDR - EXTADDR INT(32) l := @e2 ‘-’ @e1 Compute bytes
between two byte
pointers.
1. atype is a word address or byte address in the user data segment, in the user code segment, or in system
globals.
2. Both operands must be the same address type. The result of subtracting two byte addresses or two word
addresses using unsigned subtraction is a 16-bit integer. If the difference can be less than -32768 or greater than
32767, use extended-address operands:
nbytes32 := @xptr - @xbuf;