pTAL Conversion Guide

Expressions
pTAL Conversion Guide527302-002
13-2
Results After Overflow
Results After Overflow
The result of the code in Example 13-1 on page 13-2, which assumes that overflow
traps are disabled, is different for TNS and native processes:
Check for overflow after all operations that could produce overflow in those portions of
your program that run with overflow traps disabled. Code Example 13-1 on page 13-2
as in Example 13-2 on page 13-2.
Storing a value into a 16-bit variable and then referencing the variable does not ensure
that the referenced value is a maximum of 16 bits in length. The object code might not
write the value to memory, or might write the value to memory but use the value from a
native register, rather than reading the value from memory.
In Example 13-3 on page 13-3, after the statement j :=i + 1 executes, the object
code might not write the value of j to memory because its value can be tested from a
native register. In this case, i + 1 is 32,768, the value of the conditional expression is
FALSE, and the test fails. If the object code writes the value of j to memory and then
reads it back from memory, it uses -32,768, which pTAL sign-extends, and the IF
statement succeeds because -32,768 is less than 0.
TNS Native
i+1 -32,768 32,768
i+1 > 0 FALSE TRUE
Example 13-1. Overflow Results That Vary For TNS and Native Processes
INT i := 32767;
IF i + 1 > 0 THEN ... ! Performs signed addition
Example 13-2. Handling Overflow Results That Vary For TNS and Native
Processes
INT i := 32767;
i := i + 1;
IF $OVERFLOW THEN
BEGIN
! Handle Overflow
END
ELSE
BEGIN
IF i > 0 THEN
BEGIN
...
END
ELSE ...
END;