Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top