What CPU registers are to be restored at the end of an asm procedure in Delphi

断了今生、忘了曾经 提交于 2019-12-03 17:31:31

问题


When writing a Delphi procedure or function in assembly code, which registers must be saved and restored to the original value at the end of the procedure?

When calling another Delphi procedure or function from (inline) assembly code, what can I expect that other function to do with the registers? Which registers will be restored to their original values and which may not?

(Obviously, the same answer would apply to both questions)

I am assuming the default calling convention of Delphi. I know that EAX is used for 32-bit return values. And looking at the asm code in SysUtils.pas, it seems that EBX, ESI and EDI are pushed and restored, but the others are not. I cannot find any documentation about this, though.


回答1:


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




回答2:


I don't know if the docs are up to date, but you should have a look at Using Inline Assembly Code (Win32 Only) at the Embarcardero Wiki:

Quote:

In general, the rules of register use in an asm statement are the same as those of an external procedure or function. An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely modify the EAX, ECX, and EDX registers. On entry to an asm statement, EBP points to the current stack frame and ESP points to the top of the stack. Except for ESP and EBP, an asm statement can assume nothing about register contents on entry to the statement.



来源:https://stackoverflow.com/questions/3557500/what-cpu-registers-are-to-be-restored-at-the-end-of-an-asm-procedure-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!