[fpc-pascal]Modifying cpu registers while in turbo pascal interrupt routine ?

Klaus Hartnegg klaus at fischer.brain.uni-freiburg.de
Fri Mar 19 12:10:02 CET 2004

> TP7 only uses 16-bit regs, for longints too, hence no need to
> save/restore higher words.

No, programs compiled with Turbo-Pascal 7 with the $G+ option will
use 32 bit registers for longint arithmetic if the program runs on a
386 cpu or higher. They test the cpu-type on startup and store the
result in the global variable 'test8086'. Longint library functions
test this variable to decide which code to use.

But the programs do not not save the upper half of the cpu-registers in 
interrupt procedures, so if the interrupted main program is in the middle of a
longint calculation and the interrupt procedure uses longint as well,
then the calculation in the main programm can get incorrect results.

The attached source demonstrates the problem, however I just noticed
that the error happens much less often if the program runs in a dos
window than when it runs in plain dos.

> >> and it doesn't provide a stack (so your programm will crash even it it
> > doesn't
> >> use longint).
> Of course interrupt handler will have some stack, though it is
> usually rather small (like all things in 16-bit world ;)
> Built-in stack checking won't work properly though indeed.

I think stack checking does report the problem, but the stack can
sometimes be so small that it can hardly store the registers. Yet a
Pascal-Program can easily use a lot of stack, for example if it uses a
big local record variable or strings or if it passes such variables to
another procedure. This can easily overwrite the stack, causing the
program to crash. Using the $M option does not help because what the
interrupt procedure sees is the stack of the interrupted program, not
necessarily the stack of the pascal program.

Summary: If you want your program to be reliable,
don't use the built in interrupt keyword.

I suffered from both problems, that's why I wrote trapint.pas

> > Euhm... why will it crash ?
> > Let me gues... if I call other routines from inside the interrupt routine ?
> No. If you re-enter non-reenterable code like DOS services,
> say try to open some file without special care.

It will crash because the return address on the stack is overwritten
with other data, so when the interrupt procedure finishes, the CPU 
doesn't remember to which address it should return.


-------------- next part --------------
The following section of this message contains a file attachment
prepared for transmission using the Internet MIME message format.
If you are using Pegasus Mail, or any another MIME-compliant system,
you should be able to save it or view it from within your mailer.
If you cannot, please ask your system administrator for assistance.

   ---- File information -----------
     File:  Int-eax.pas
     Date:  19 Mar 2004, 11:29
     Size:  873 bytes.
     Type:  Binary
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Int-eax.pas
Type: application/octet-stream
Size: 873 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20040319/fef6bb75/attachment.obj>

More information about the fpc-pascal mailing list