delphiassemblybasm

Store function variable in ASM functions in Delphi


How can I properly store n in functions like the following one ? Because of value in n changes for some reason after I use it once.

function Test(n: Integer): Byte;
asm
  mov eax, n
  add eax, eax
  add eax, n
  mov ecx, eax
  mov ebx, eax
  mov ecx, n
end;

Solution

  • The first argument to the function, n, is stored in eax, so your line

    mov eax, n
    

    is highly strange (move n to n). Also, if you change eax, you change n.

    You could save the argument for future use (since you likely need to alter eax):

    var
      tempN: integer;
    asm
      mov tempN, eax
    

    Also, IIRC, you must not change the value of ebx when writing inline ASM. Hence, you need to surround your code by push ebx and pop ebx.