AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

ASM in Lazarus

Ein Thema von plusplus · begonnen am 30. Jul 2011 · letzter Beitrag vom 2. Sep 2011
Antwort Antwort
plusplus

Registriert seit: 30. Jul 2010
106 Beiträge
 
Delphi 2009 Architect
 
#1

ASM in Lazarus

  Alt 30. Jul 2011, 10:52
Code:
begin
  AInt := Args[i];
  asm push AInt; end;
end;
the code above works in all versions of delphi, I have set {$asmmode intel} but it seams the push is not working, does anyone have an opinion why that could be.

Cheers

PS: I am working x64 windows version of lazarus 0.9.30
Grid Computing made simple - http://xerocoder.com

Geändert von plusplus (30. Jul 2011 um 10:54 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von implementation
implementation

Registriert seit: 5. Mai 2008
940 Beiträge
 
FreePascal / Lazarus
 
#2

AW: ASM in Lazarus

  Alt 30. Jul 2011, 11:26
What means "is not working"?
  Mit Zitat antworten Zitat
plusplus

Registriert seit: 30. Jul 2010
106 Beiträge
 
Delphi 2009 Architect
 
#3

AW: ASM in Lazarus

  Alt 30. Jul 2011, 11:33
What means "is not working"?
well its not pushing the value. I get always 0. Actually i get a silent access violation when I call push. its really weird. is there an alternative command for push in asm?
Grid Computing made simple - http://xerocoder.com

Geändert von plusplus (30. Jul 2011 um 11:43 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von JamesTKirk
JamesTKirk

Registriert seit: 9. Sep 2004
Ort: München
604 Beiträge
 
FreePascal / Lazarus
 
#4

AW: ASM in Lazarus

  Alt 1. Aug 2011, 09:43
Code:
begin
  AInt := Args[i];
  asm push AInt; end;
end;
the code above works in all versions of delphi, I have set {$asmmode intel} but it seams the push is not working, does anyone have an opinion why that could be.

Cheers

PS: I am working x64 windows version of lazarus 0.9.30
I don't have a x64 version available to test, put try to replace "push" with "pushq".

You might also want to read this regarding the calling convention used on 64-Bit Windows systems (also note the second last paragraph about "push" and "pop").

Regards,
Sven
Sven
[Free Pascal Compiler Entwickler]
this post is printed on 100% recycled electrons
  Mit Zitat antworten Zitat
plusplus

Registriert seit: 30. Jul 2010
106 Beiträge
 
Delphi 2009 Architect
 
#5

AW: ASM in Lazarus

  Alt 1. Sep 2011, 15:05
@JamesTKirk
Thank you man, but pushq is an invalid operand.

Below is actually the code that worked in Delphi, and I am trying to port it to Laz.
Weird thing is, x64 push raises a silent exception and on x86 it crashes the app. I have tried many variations and yet no result.

Please if anyone knows what is wrong, help me. Or if anyone has a diff solution for calling dll's dynamically with dynamic parms, please let me know.

I am trying to make this work on Lazaruz 0.90, for x64 and x86 on Win, Lin, and Mac.

Code:
function DynamicDllCallName(Dll: String; const Name: String; HasResult: Boolean; var Returned: Cardinal; const Parameters: array of Pointer): Boolean;
var
  prc: Pointer;
  x, n: Integer;
  p: Pointer;
  dllh: THandle;
begin
  dllh := GetModuleHandle(PChar(Dll));
  if dllh = 0 then begin
    dllh := LoadLibrary(PChar(Dll));
  end;
  if dllh <> 0 then begin
    prc := GetProcAddress(dllh, PChar(Name));
    if Assigned(prc) then begin
      n := High(Parameters);
      if n > -1 then begin
        x := n;
        repeat
          p := Parameters[x];
          asm
            PUSH p
          end;
          Dec(x);
        until x = -1;
      end;
      asm
        CALL prc
      end;
      if HasResult then begin
        asm
          MOV p, EAX
        end;
        Returned := Cardinal(p);
      end else begin
        Returned := 0;
      end;
    end else begin
      Returned := 0;
    end;
    Result := Assigned(prc);
  end else begin
    Result := false;
  end;
end;
Grid Computing made simple - http://xerocoder.com

Geändert von plusplus ( 1. Sep 2011 um 15:08 Uhr)
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#6

AW: ASM in Lazarus

  Alt 1. Sep 2011, 16:01
You’re calling push right in the middle of your high level code – this is almost bound to cause problems because the asm code generated by the compiler from your source code most likely uses the stack, too. You were just lucky that it didn’t break before.
  Mit Zitat antworten Zitat
plusplus

Registriert seit: 30. Jul 2010
106 Beiträge
 
Delphi 2009 Architect
 
#7

AW: ASM in Lazarus

  Alt 1. Sep 2011, 16:32
You’re calling push right in the middle of your high level code – this is almost bound to cause problems because the asm code generated by the compiler from your source code most likely uses the stack, too. You were just lucky that it didn’t break before.
Never had a problem from Delphi 3 on and upwards to Delphi XE, I haven't tested in XE2 but I am sure this code works there too.

I am trying to find a way to do the same in Lazarus. I also tried to make a simple call without any params
(so a simple asm call proc; end; )
and does not work either. I think this is a FPC bug.
Grid Computing made simple - http://xerocoder.com
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#8

AW: ASM in Lazarus

  Alt 1. Sep 2011, 17:46
Never had a problem from Delphi 3 on and upwards to Delphi XE, I haven't tested in XE2 but I am sure this code works there too.
Maybe the Delphi compiler generates more optimized code that keeps everything in registers and avoids the stack for performance reasons?

I am trying to find a way to do the same in Lazarus. I also tried to make a simple call without any params
(so a simple asm call proc; end; )
and does not work either. I think this is a FPC bug.
That’s strange. What kind of procedure are you calling and how is it declared? (calling convention?)

Also: Have you checked what instructions the FPC compiler actually generates? I don’t know if FPC/Lazarus comes with a debugger like the one included in Delphi where you can set a breakpoint somewhere in the source code and then switch to the cpu pane to see the generated asm code... that would be the easiest way. If there isn’t such a thing, you could always put an asm int 3; end; (which acts as a breakpoint) at the beginning of the routine and run it through an ordinary debugger (though it will be more difficult to figure out which opcodes correspond to which instruction in the source code that way). If it really is a bug in FPC, you may spot something here.
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.582 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: ASM in Lazarus

  Alt 1. Sep 2011, 18:22
Never had a problem from Delphi 3 on and upwards to Delphi XE, I haven't tested in XE2 but I am sure this code works there too.
Using 32-bit as target I think so too, but you won't be able to compile this for 64-Bit. Just because you can't put a few pieces assembler code in a procedure. You have to make the whole procedure use assembler in 64-Bit in XE2.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
Benutzerbild von JamesTKirk
JamesTKirk

Registriert seit: 9. Sep 2004
Ort: München
604 Beiträge
 
FreePascal / Lazarus
 
#10

AW: ASM in Lazarus

  Alt 2. Sep 2011, 09:51
Please if anyone knows what is wrong, help me. Or if anyone has a diff solution for calling dll's dynamically with dynamic parms, please let me know.

I am trying to make this work on Lazaruz 0.90, for x64 and x86 on Win, Lin, and Mac.
In that case I'd personally suggest you to take a look at the library libffi, which stands for Foreign Function Interface and seems to allow what you want to achive. As it's used by other projects like Java Native Access (an alternative to JNI) it should be mature enough and it also supports different processor and OS combinations. The only downside is that it seems that you'll need to manually compile it for Win32 and Win64 using the approbiate MinGW environment (the 32 and the 64 bit one).

Regards,
Sven
Sven
[Free Pascal Compiler Entwickler]
this post is printed on 100% recycled electrons
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:17 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz