Author Topic: Are register-saving conventions for assembler procedures not documented?  (Read 565 times)


  • New Member
  • *
  • Posts: 25
I found that regardless of MODE, the push/pop ebx/esi/edi frame is always generated for assembler procedures with pascal calling convention, but never for stdcall. Is safecall changing something? I have not seen. (and when I try to set {$calling safеcall}, the FPC stops with internal error on those turbopascal sources that now compile with $calling pascal/stdcall)
«Signaling changed registers» doesn't seem to change anything for procedures at -Rintel too (but I have not experimented with this a lot).
Also, regardless of {$MODE TP}, registers are not saved in the case of asm inclusions.
In FPC, destroying ebx/esi/edi registers easily disrupts, for example, «for» loops. However, in Turbo Pascal, the programmer did not have to save registers in both cases:
Register-saving conventions
Procedures and functions should preserve the BP, SP, SS, and DS registers. All other registers can be modified.

An asm statement must preserve the BP, SP, SS, and DS registers, but can freely modify the AX, BX, CX, DX, SI, DI, ES, and Flags registers.


  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: Are register-saving conventions for assembler procedures not documented?
« Reply #1 on: January 20, 2020, 10:45:42 pm »
Please note that you're throwing everything together here.

First of the safecall calling convention is only intended for functions that normally return a HRESULT on the non-Pascal side (mainly in the context of Windows COM (as in Component Object Model) programming). So this calling convention is not what you want.

Second the Mode has nothing to do with the calling convention or the prologues of routines. This is only influenced by the calling convention and the nostackframe modifier.

Third if you have an assembly block inside a function you need to specify which registers are modified so that the compiler's register allocator can spill them correctly (this is more efficient than saving/restoring all registers; depending on the architecture there can be many registers (e.g. x86_64 or PPC), also this allows the use of unknown assembly opcodes by manually encoding them). The syntax for this is as follows:

Code: Pascal  [Select][+][-]
  1. begin
  2.   // some Pascal code
  3.   asm
  4.     // let's assume the following:
  5.     mov ebx, ecx
  6.     mov ecx, eax
  7.     mov eax, ebx
  8.   end ['ebx', 'ecx', 'eax']; // these registers where modified
  9.   // some more Pascal code
  10. end;

This is not necessary for pure assembly functions (those consisting only of a asmend block).

Fourth you're quoting the Turbo Pascal calling convention. However 32-bit or 64-bit code is not using the Turbo Pascal calling convention. In fact that calling convention is provided by FPC only on the 16-bit i8086 target. On other platforms you need to adhere to the corresponding ABIs. On nearly all platforms FPC follows the default ABI of the C compiler (aka cdecl). The only exceptions are i386 where there are also the register (default), stdcall and pascal calling conventions, x86_64-win64 with the vectorcall calling convention and m68k with the register (default) calling convention.
When you work with assembly and functions you must learn at least about those calling conventions that you are using for those platforms that you are supporting with assembly.

See also: X86 calling conventions on Wikipedia
« Last Edit: January 20, 2020, 10:52:49 pm by PascalDragon »


TinyPortal © 2005-2018