Recent

Author Topic: Inline asm problem with assign  (Read 18376 times)

Rekumkacz

  • New Member
  • *
  • Posts: 21
Inline asm problem with assign
« on: June 23, 2015, 10:13:36 am »
Hi and wellcome

I have play with inline asm in Lazarus and i have a problem with assign  %)

In Pascal code:
var:='Feed Me';

In IDA pro is look like
mov     ebx, offset _$UNIT1$_Ld2 ; "Feed Me"
lea     eax, [ebp+var_18]
call    FPC_ANSISTR_DECR_REF

Delphi function is LStrLAsg and etc.

I want reuse this code in next Project but when i write
{$ASMMODE intel}
asm
...
lea     eax, var1
call    FPC_ANSISTR_DECR_REF
...
end;
the FPC_ANSISTR_DECR_REF function is not recognized. How i can use it?
« Last Edit: June 23, 2015, 10:16:27 am by Rekumkacz »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12598
  • FPC developer.
Re: Inline asm problem with assign
« Reply #1 on: June 23, 2015, 10:21:59 am »
Do you enable ansistrings in your assembler program with {$H+} ?

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Inline asm problem with assign
« Reply #2 on: June 23, 2015, 10:27:30 am »
It is nothing changed.
unit1.pas(63,14) Error: Unknown identifier "FPC_ANSISTR_DECR_REF"

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Inline asm problem with assign
« Reply #3 on: June 23, 2015, 10:36:59 am »
You could just declare it external. It's a compiler proc so it might be a little strangely handled for inline assembly. Try:

Code: [Select]
Procedure my_fpc_AnsiStr_Decr_Ref (Var S : Pointer);external name 'FPC_ANSISTR_DECR_REF';

Then just call my_fpc_AnsiStr_Decr_Ref

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Inline asm problem with assign
« Reply #4 on: June 23, 2015, 11:22:01 am »
The error is gone but the function is not working. Now i have this code
begin
  var_1:='Text1';
  var_2:='Text2';

  {$ASMMODE intel}
  asm
    lea     eax,[var_2]
    mov  edx,dword ptr [var_1]
    call    my_fpc_AnsiStr_Decr_Ref
  end;

  edit1.Text:=var_1;
  edit2.Text:=var_2;
end;                  

rvk

  • Hero Member
  • *****
  • Posts: 6925
Re: Inline asm problem with assign
« Reply #5 on: June 23, 2015, 12:06:42 pm »
Maybe my assembler is a bit rusty (it's been about 20 years), but what is this supposed to do?

lea   eax, [var_2]
- Loads the address of var_2 in eax

mov   edx, dword ptr [var_1]
- Loads the first byte from var_1 in edx (?)

I'm not seeing a real exchange of data between variables.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Inline asm problem with assign
« Reply #6 on: June 23, 2015, 12:07:40 pm »
What are you trying to do? That assembly looks like it will free var_2 and probably make whatever it points to nil, and then do nothing with edx.

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Inline asm problem with assign
« Reply #7 on: June 23, 2015, 12:20:28 pm »
I want put var_2 in to var_1, that's all.

var_1:=var_2 in asm

rvk

  • Hero Member
  • *****
  • Posts: 6925
Re: Inline asm problem with assign
« Reply #8 on: June 23, 2015, 12:32:13 pm »
Again, with my very rusty assembler:

Code: [Select]
procedure my_fpc_AnsiStr_Decr_Ref (Var S : Pointer);external name 'FPC_ANSISTR_DECR_REF';
procedure my_fpc_AnsiStr_Incr_Ref (Var S : Pointer);external name 'FPC_ANSISTR_INCR_REF';

procedure TForm1.Button1Click(Sender: TObject);
var
  var_1, var_2: String;
begin
  var_1 := 'Text1';
  var_2 := 'Text2';

  {$ASMMODE intel}
  asm
    lea    eax, [var_1]
    call   my_fpc_ansistr_incr_ref
    lea    eax, [var_2]
    call   my_fpc_ansistr_decr_ref
    mov    eax, [var_2]
    mov    [var_1], eax
  end;

  edit1.Text := var_1;
  edit2.Text := var_2;
end;
« Last Edit: June 23, 2015, 12:41:54 pm by rvk »

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Inline asm problem with assign
« Reply #9 on: June 23, 2015, 12:58:04 pm »
You are my hero. Ok now is working but i am now confused.

What do these functions?
fpc_AnsiStr_Decr_Ref
fpc_AnsiStr_Incr_Ref

Fresh from IDAPro
//var_1 := 'Text1';
mov     ebx, offset _$UNIT1$_Ld3 ; "Text1"
lea     eax, [ebp+var_C]
call    FPC_ANSISTR_DECR_REF

//var_2 := 'Text2';
mov     [ebp+var_C], ebx
mov     ebx, offset _$UNIT1$_Ld4 ; "Text2"
lea     eax, [ebp+var_10]
call    FPC_ANSISTR_DECR_REF
mov     [ebp+var_10], ebx

//var_1:=var_2;
mov     eax, [ebp+var_10]
call    FPC_ANSISTR_INCR_REF
lea     eax, [ebp+var_C]
call    FPC_ANSISTR_DECR_REF
mov     eax, [ebp+var_10]
mov     [ebp+var_C], eax

//edit1.Text := var_1; with edit2.Text := var_2; ???
mov     eax, [ebp+var_8]
mov     eax, [eax+46Ch]
mov     edx, [ebp+var_C]
call    CONTROLS_TCONTROL_$__SETTEXT$TTRANSLATESTRING

My brain will explode  :-[
« Last Edit: June 23, 2015, 01:02:53 pm by Rekumkacz »

rvk

  • Hero Member
  • *****
  • Posts: 6925
Re: Inline asm problem with assign
« Reply #10 on: June 23, 2015, 01:45:10 pm »
What do these functions?
fpc_AnsiStr_Decr_Ref
fpc_AnsiStr_Incr_Ref
As I understand it... The memory locations of variables are reference counted.
Quote
Long strings are based on a reference-counting mechanism, which keeps track of how many string variables are referring to the same string in memory. This reference-counting is used also to free the memory when a string isn't used anymore-that is, when the reference count reaches zero.

I'm still puzzled a bit myself as to why the memory of var_1 should be increased (my_fpc_ansistr_incr_ref) while you do a var_1 := var_2;
(My guess would have been the other way around because the memory pointing to by var_1 is not used anymore)

Maybe somebody with more knowledge of the reference counting system of FPC could explain this mechanism a bit better.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Inline asm problem with assign
« Reply #11 on: June 23, 2015, 01:52:41 pm »
What do these functions?
fpc_AnsiStr_Decr_Ref
fpc_AnsiStr_Incr_Ref
As I understand it... The memory locations of variables are reference counted.
Quote
Long strings are based on a reference-counting mechanism, which keeps track of how many string variables are referring to the same string in memory. This reference-counting is used also to free the memory when a string isn't used anymore-that is, when the reference count reaches zero.

I'm still puzzled a bit myself as to why the memory of var_1 should be increased (my_fpc_ansistr_incr_ref) while you do a var_1 := var_2;
(My guess would have been the other way around because the memory pointing to by var_1 is not used anymore)

Maybe somebody with more knowledge of the reference counting system of FPC could explain this mechanism a bit better.
You assume that mov     eax, [ebp+var_10] points to var_1
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6925
Re: Inline asm problem with assign
« Reply #12 on: June 23, 2015, 02:01:09 pm »
You assume that mov     eax, [ebp+var_10] points to var_1

No, I was assuming eax points to the memory location of var_1 in the following:
Code: [Select]
  lea    eax, [var_1]
  call   my_fpc_ansistr_incr_ref



Code: [Select]
// var_1 := var_2 in asm:
 asm
    lea    eax, [var_1]
    call   my_fpc_ansistr_incr_ref
    lea    eax, [var_2]
    call   my_fpc_ansistr_decr_ref
    mov    eax, [var_2]
    mov    [var_1], eax
  end;

Rekumkacz

  • New Member
  • *
  • Posts: 21
Re: Inline asm problem with assign
« Reply #13 on: June 23, 2015, 02:03:33 pm »
Work too  :o
Code: [Select]
// var_1 := var_2 in asm:
 asm
    mov    eax, [var_2]
    mov    [var_1], eax
  end;

Where can I read about these functions?
« Last Edit: June 23, 2015, 02:05:57 pm by Rekumkacz »

rvk

  • Hero Member
  • *****
  • Posts: 6925
Re: Inline asm problem with assign
« Reply #14 on: June 23, 2015, 02:10:54 pm »
You assume that mov     eax, [ebp+var_10] points to var_1
Ooo, wait... maybe it should have been this:

Code: [Select]
  asm
    mov    eax, [var_2]
    call   my_fpc_ansistr_incr_ref
    lea    eax, [var_1]
    call   my_fpc_ansistr_decr_ref
    mov    eax, [var_2]
    mov    [var_1], eax
  end;

So increase the memory location of the var_2 string (with mov eax, var_2 and my_fpc_ansistr_incr_ref)
and decrease the old string in var_1 with lea eax, [var_1] and my_fpc_ansistr_decr_ref.

Why is for one "mov" used and the other "lea" ??
(* yeah, my asm is very rusty)


Work too  :o
Code: [Select]
// var_1 := var_2 in asm:
 asm
    mov    eax, [var_2]
    mov    [var_1], eax
  end;
That "seems" to works too but you have a problem with the reference counting in that case. Text1 is not freed (until you end your program) and my guess in you can get in trouble when accessing the strings later on.
« Last Edit: June 23, 2015, 02:19:08 pm by rvk »

 

TinyPortal © 2005-2018