13

用汇编代码编写Delphi程序或函数时,程序结束时必须保存哪些寄存器并恢复为原始值?

当从(内联)汇编代码调用另一个 Delphi 过程或函数时,我可以期望其他函数对寄存器做什么?哪些寄存器会恢复到原来的值,哪些不会?

(显然,相同的答案将适用于这两个问题)

我假设 Delphi 的默认调用约定。我知道它EAX用于 32 位返回值。查看 SysUtils.pas 中的 asm 代码,似乎EBX,ESIEDI被推送和恢复,但其他不是。不过,我找不到任何关于此的文档。

4

2 回答 2

11

The three first arguments of a function are given in EAX, EDX, and ECX, respectively. Additional arguments are pushed on the stack. For a method of an object, the Self pointer is always the (invisible) first parameter. The result should be in EAX. For functions returning long strings, the (invisible) last parameter of the function is the pointer to the resulting string (which by itself is a pointer to the first character of the string).

EBX must not be altered (unless you restore it before the end of the procedure/function), and so must not ESP, EBP, ESI, or EDI either.(1) An excellent introduction to Delphi inline ASM used to be found here: http://www.delphi3000.com/articles/article_3766.asp

于 2010-08-24T14:41:52.177 回答
4

我不知道文档是否是最新的,但您应该看看Embarcardero Wiki 上的Using Inline Assembly Code (Win32 Only) :

引用:

一般来说,asm 语句中寄存器的使用规则与外部过程或函数的使用规则相同。asm 语句必须保留 EDI、ESI、ESP、EBP 和 EBX 寄存器,但可以自由修改 EAX、ECX 和 EDX 寄存器。在进入 asm 语句时,EBP 指向当前栈帧,ESP 指向栈顶。除了 ESP 和 EBP 之外,asm 语句不能对进入语句时的寄存器内容做出任何假设。

于 2010-08-24T14:36:09.833 回答