3.4 Signaling changed registers

When the compiler uses variables, it sometimes stores them, or the result of some calculations, in the processor registers. If you insert assembler code in your program that modifies the processor registers, then this may interfere with the compiler’s idea about the registers. To avoid this problem, Free Pascal allows you to tell the compiler which registers have changed in an asm block. The compiler will then save and reload these registers if it was using them. Telling the compiler which registers have changed is done by specifying a set of register names behind an assembly block, as follows:

end [’R1’, ... ,’Rn’];

Here R1 to Rn are the names of the registers you modify in your assembly code.

As an example:

   movl BP,%eax  
   movl 4(%eax),%eax  
   movl %eax,__RESULT  
   end [’EAX’];

This example tells the compiler that the EAX register was modified.

For assembler routines, i.e., routines that are written completely in assembler, the ABI of the processor & platform must be respected, i.e. the routine itself must know what registers to save and what not, but it can tell the compiler using the same method what registers were changed or not. The compiler will save specified registers to the stack on entry and restore them on routine exit.

The only thing the compiler normally does, is create a minimal stack frame if needed (e.g. when variables are declared). All the rest is up to the programmer.