代码之家  ›  专栏  ›  技术社区  ›  Andreas Rejbrand

返回长字符串的Delphi汇编函数

  •  2
  • Andreas Rejbrand  · 技术社区  · 15 年前

    我正在努力学习Delphi中的内联汇编编程,为此我发现 this article 非常有帮助。

    现在我想写一个汇编函数,返回一个长字符串,特别是一个 AnsiString (为了简单起见)。我已经写信了

    function myfunc: AnsiString;
    asm
      // eax = @result
      mov edx, 3
      mov ecx, 1252
      call System.@LStrSetLength
      mov [eax + 0], ord('A')
      mov [eax + 1], ord('B')
      mov [eax + 2], ord('C')
    end;
    

    说明:

    返回字符串的函数有一个不可见的 var result: AnsiString (在本例中)参数,因此,在函数的开头, eax edx ecx System._LStrSetLength . 实际上,我知道

      _LStrSetLength(@result, 3, 1252)
    

    windows-1252 代码页。

    那么,我知道 eax公司 the address of the first character of the string

    更新

    myfunc ,一名雇员 NewAnsiString LStrSetLength . 我不禁怀疑它们是否都是正确的,因为它们不会弄乱upp-Delphi对字符串的内部处理(引用计数、自动释放等)。

    2 回复  |  直到 14 年前
        1
  •  3
  •   A.Bouchez    15 年前

    你必须使用一些:

    function myfunc: AnsiString;
    asm
      push eax // save @result
      call system.@LStrClr
      mov    eax,3                 {Length}
    {$ifdef UNICODE}
      mov    edx,1252 // code page for Delphi 2009/2010
    {$endif}
      call   system.@NewAnsiString
      pop edx
      mov [edx],eax
      mov [eax],$303132
    end;
    

    它将返回一个'210'字符串。。。

    在2009年之前,最好使用{$ifdef UNICODE}块使代码与Delphi版本兼容。

        2
  •  1
  •   Andreas Rejbrand    15 年前

    在A.Bouchez的出色帮助下,我成功地修改了自己的代码,使用了 LStrSetLength :

    function myfunc: AnsiString;
    asm
    
      push eax
    
      // eax = @result
      mov edx, 3
      mov ecx, 1252
      call System.@LStrSetLength
    
      pop eax
    
      mov ecx, [eax]
    
      mov [ecx], 'A'
      mov [ecx] + 1, 'B'
      mov [ecx] + 2, 'C'
    
    end;