[fpc-pascal] subtract longwords with rollover

Tomas Hajny XHajT03 at hajny.biz
Fri Nov 29 11:57:12 CET 2013

On Fri, November 29, 2013 11:00, Klaus Hartnegg wrote:
> On 28.11.2013 18:32, Tomas Hajny wrote:
>> On Thu, November 28, 2013 18:12, Klaus Hartnegg wrote:
>>> Sorry, I forgot to mention there is {$Q-} in the source.
>> I believe that $Q- covers overflow checks, not range checks.
> Thanks, this showed me the right direction.
> Explanation: I'm trying to port code from Turbo Pascal. There
> word1-word2 does wrap around with {$Q-}{$R+}. And dec(word1) never
> checks for overflow, even with {$Q+}{$R+}.
> FreePascal seems to have a wider definition of Range Check, and the
> inc() and dec() functions do check for overflow.

This is because TP is 16-bit (word), whereas FPC (in particular your
compilation target in FPC) is 32-bit (longword).

> The longint trick prevents runtime error 201, but causes runtime error
> 215 if the longword value does not fit into longint.
> On closer look this is all behaving like documented, just different from
> TurboPascal (maybe it would have worked in TP-compatibility-mode).

{$MODE TP} wouldn't change this. Only the i8086-msdos target (16-bit)
should behave the same way as TP from this point of view.

> The correct fix is to simply add {$R-} to the code wherever it currently
> has {$Q-}. I have just done this, and now it passes the test.

That was indeed one of my proposed solutions ({$R-} is equivalent to


More information about the fpc-pascal mailing list