Advertisement
WarPie90

Untitled

May 2nd, 2022
1,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.93 KB | None | 0 0
  1. program mul_func;
  2. {------------------------------------------------------------------------------]
  3.  Shows how to implement a native cdecl function within lape itself,
  4.  using lapes "special" argument passing
  5.  The goal of this example - Implement the following native method:
  6.  -----------------------------------------------------------------
  7.   | procedure Mul(const P: PParamArray; const Res: Pointer); cdecl;
  8.   | begin
  9.   |   PInt32(Res)^ := PInt32(P^[0])^ * PInt32(P^[1])^;
  10.   | end;
  11.  Lape implements argument passing as just a pointer to it's stack at the current
  12.  offset -argcount or something, that's the whole PParamArray deal.
  13.  While the result is just a direct pointer to the output variable.
  14. [------------------------------------------------------------------------------}
  15. {$I slackasm/assembler.pas}
  16. {$X+}
  17.  
  18.  
  19. function MulFunc(): Pointer;
  20. var
  21.   assembler: TSlackASM;
  22. begin
  23.   with assembler := TSlackASM.Create() do
  24.   try
  25.     // prologue
  26.     code += _push(ebp);
  27.     code += _mov(esp, ebp);
  28.     (*
  29.     // load real args
  30.     code += _mov(ebp+12, edx);           // Result pointer
  31.     code += _mov(ebp+08, ebx);           // argz (ptr to array [WORD] of Pointer)
  32.  
  33.     // load lape args
  34.     code += _mov(ref(ebx), eax);         // deref ebx into eax
  35.     code += _mov(ref(eax), eax);         // mov contents of first arg to eax
  36.  
  37.     code += _add(imm(4),   ebx);         // offset by size of pointer
  38.     code += _mov(ref(ebx), ecx);         // deref ebx into eax
  39.     code += _mov(ref(ecx), ecx);         // mov contents of second arg to ecx
  40.  
  41.     // implementation
  42.     code += _imul(ecx, eax);
  43.  
  44.     // result
  45.     code += _mov(eax, ref(edx));
  46.     *)
  47.     // epilogue
  48.     code += _pop(ebp);
  49.  
  50.     code += _ret;
  51.  
  52.     Result := Finalize();
  53.   finally
  54.     WriteLn(Code);
  55.     Free();
  56.   end;
  57. end;
  58.  
  59. var
  60.   mul: external function(x,y:Int32): Int32;
  61. begin
  62.   mul := MulFunc();
  63.  
  64.   WriteLn mul(1000, 5);
  65.  
  66.   FreeMethod(@mul);
  67. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement