pTAL Guidelines for TAL Programmers
Coding Guidelines
pTAL Guidelines for TAL Programmers—527256-002
2-16
Address Arithmetic
pTAL also supports high-level data type conversions. For details, see Address-
Conversion Routines on page 3-1.
Address Arithmetic
Guideline: Use arithmetic operations on pointers only if the result of the operation
yields another address or if the result is the number of elements between two pointers.
In TAL, you can manipulate 16-bit addresses as INT values and 32-bit extended
addresses as INT(32) values; therefore, you can perform any arithmetic operation on
an address and store the result in a pointer.
In pTAL, the data types of address values are distinct from the data types of other
values. pTAL restricts operations that you can perform on addresses. The result of all
operations on addresses is either another address or, if you subtract two addresses, an
integer whose value is the number of bytes or words between the two addresses.
Table 2-3 on page 2-17 shows all valid pTAL arithmetic operations on addresses. pTAL
reports a type-mismatch error if you attempt to perform any other operations on
addresses.
You can calculate the size of an object in memory by subtracting the address of the
beginning of the object from the address of the end of the object. Use unsigned
operators for nonextended addresses; use signed operators for extended addresses.
To avoid an illegal addition of two addresses, use parentheses:
@src := @srcbuf '+' (@dest '-' @destbuf);
$DBLL(0, BADDR ) EXTADDR STRING .p;
STRING .EXT s := $DBLL(0, @p);
$UDBL(WADDR ) ‘<<‘ 1 EXTADDR INT .p;
INT .EXT s := $UDBL(@p) ‘<<‘ 1;
$DBLL(0, WADDR ) ‘<<‘ 1 EXTADDR INT .p;
INT .EXT s := $DBLL(0, @p) ‘<<‘ 1;
Example 2-11. Subtracting Two Addresses
INT num_bytes,
num_words;
STRING .byte_buff[0:9], .byte_ptr;
INT .word_buff[0:9], .word_ptr;
SCAN byte_buff UNTIL " " -> @byte_ptr;
SCAN word_buff UNTIL " " -> @word_ptr;
num_bytes := @byte_ptr '-' @byte_buff;
num_words := @word_ptr '-' @word_buff;
Table 2-2. Address Conversions (page2of2)
Conversion Expression Result Type Example