AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein In Asm-Prozedur eine Exception auslösen
Thema durchsuchen
Ansicht
Themen-Optionen

In Asm-Prozedur eine Exception auslösen

Ein Thema von Amateurprofi · begonnen am 2. Nov 2023 · letzter Beitrag vom 11. Nov 2023
Antwort Antwort
Seite 3 von 4     123 4      
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#21

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 02:37
@jaenicke
Zitat:
Hattest du diesen Teil seines Codes übersehen?
Nein, aber ich habe nicht gleich realisiert, dass das die Lösung sein könnte.
Danke für den Hinweis, werde mich im Laufe des Tages darum kümmern, jetzt erstmal 2-3 Stunden schlafen.
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#22

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 07:52
@Paule32.jk
Zitat:
ich weiß gerade nicht, was IHR so an der Fragestellung versteht, was ich nicht verstehe...
Für mich (und was der Fragestellende gepostet hat, ist, das er mittels Einzeiler eine Exception bzw. eine System-Routine aufrufen will, OHNE viel Delphi zu verwenden.
Wo habe ich denn geschrieben, dass ich "mittels Einzeiler eine Exception bzw. eine System-Routine aufrufen will" ?
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#23

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 08:11
@jaenicke
Zitat:
Hattest du diesen Teil seines Codes übersehen?
Nein, aber ich habe nicht gleich realisiert, dass das die Lösung sein könnte.
Danke für den Hinweis, werde mich im Laufe des Tages darum kümmern, jetzt erstmal 2-3 Stunden schlafen.
@jaenicke
So, bin wieder da.
Hab mir das angeschaut und mir ist auch wieder eingefallen, was mich gestört hatte, nämlich der Aufruf der Pascal-Routine "GetException", die außerhalb des Asm-Blocks liegt, was ich ja gerade vermeiden wollte.
Nach näherer Betrachtung ließ sich das aber leicht beheben.
Die nachstehende Prozedur funktioniert unter 32Bit, nicht aber unter 64Bit (Wie Kas Ob. auch schon deutlich machte).
Wäre schön, wenn da noch einmal jemand, der das durchschaut, reinschauen könnte.
Delphi-Quellcode:
PROCEDURE Test;
const
   pExClass:ExceptClass=(Exception);
   sErr:String='Meine Meldung';
asm
   {$IfDef CPUX86}
   mov ecx,sErr
   mov dl,1
   mov eax,pExClass
   call Exception.Create
   call System.@RaiseExcept
   {$Else}
   mov r8,sErr
   mov dl,$01
   mov rcx,pExClass
   call Exception.Create // Scheint zu funktionieren
   mov rcx,rax
   call System.@RaiseExcept // Hier krachts. "ACCESS VIOLATION"
   {$EndIf}
end;
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Kas Ob.
Online

Registriert seit: 3. Sep 2023
213 Beiträge
 
#24

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 10:22
I am trying here to explain and language barrier is playing huge role.

This will work on x64 for this exact procedure
Code:
PROCEDURE test;
const
   pExClass:ExceptClass=( Exception );
   sErr:String=' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create // Seems to work
   mov rcx,rax
   call System.@RaiseExcept // Here it crashes. " ACCESS VIOLATION"
   lea rsp,[rbp+$20]
   pop rbp
   pop rbp
   {$EndIf}
end ;
BUT will fail on many other places and different situations or for just little more complex assembly above that system.@RaiseExcept !!!

The reasoning : i put a frame stack for it and so it worked, now if your assembly is using variables or utilizing the stack or even the compiler went ahead and added a frame on its own to build right assembly, we will end up with double frame and whole different mess and that solution become broken again and you should remove that stack i introduced, this is one hand on other hand this can't be guaranteed to be working on future compiler, tested it on XE8 and your test procedure and it is raising stack overflow on my machine not an access violation!
That why i moved the raising call to its own pascal procedure, this guaranteed to have right frame stack and the compiler will handle it right, as i said that in my humble opinion and according to my experience this is the closest for assembly to raise an exception without braking things every time you modify your assembly and guaranteed to be working on different compilers including future ones, also will not send tools like EurekaLog after witch hunting!

I hope that is clear.

ps : this was a good question and might help many who interested.
  Mit Zitat antworten Zitat
Kas Ob.
Online

Registriert seit: 3. Sep 2023
213 Beiträge
 
#25

AW: In Asm-Prozedur eine Exception auslösen

  Alt 5. Nov 2023, 10:32
OK, there much to say about this but really i don't like write essays

You can force the compiler to not introduce a stack frame by using .noframe in the x64 assembly block.

so the code can be like this
Code:
PROCEDURE test;
const
   pExClass : ExceptClass = (Exception);
   sErr : String = ' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   .noframe            //  crucial here
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create
   mov rcx,rax
   call System.@RaiseExcept
   lea rsp,[rbp+$20]
   pop rbp
   {$EndIf}
end ;
Yet again, use that on your risk !

Edit: to fix an extra pop useless in that example but will cause problem in real life code.
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#26

AW: In Asm-Prozedur eine Exception auslösen

  Alt 6. Nov 2023, 05:45
I am trying here to explain and language barrier is playing huge role.

This will work on x64 for this exact procedure
Code:
PROCEDURE test;
const
   pExClass:ExceptClass=( Exception );
   sErr:String=' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create // Seems to work
   mov rcx,rax
   call System.@RaiseExcept // Here it crashes. " ACCESS VIOLATION"
   lea rsp,[rbp+$20]
   pop rbp
   pop rbp
   {$EndIf}
end ;
BUT will fail on many other places and different situations or for just little more complex assembly above that system.@RaiseExcept !!!

The reasoning : i put a frame stack for it and so it worked, now if your assembly is using variables or utilizing the stack or even the compiler went ahead and added a frame on its own to build right assembly, we will end up with double frame and whole different mess and that solution become broken again and you should remove that stack i introduced, this is one hand on other hand this can't be guaranteed to be working on future compiler, tested it on XE8 and your test procedure and it is raising stack overflow on my machine not an access violation!
That why i moved the raising call to its own pascal procedure, this guaranteed to have right frame stack and the compiler will handle it right, as i said that in my humble opinion and according to my experience this is the closest for assembly to raise an exception without braking things every time you modify your assembly and guaranteed to be working on different compilers including future ones, also will not send tools like EurekaLog after witch hunting!

I hope that is clear.

ps : this was a good question and might help many who interested.
Thank you, Kas Ob.
Funny thing.
Just before entering the DP I looked, how

Delphi-Quellcode:
PROCEDURE TestRaise;
const sErr:String='Meine Meldung';
begin
   raise Exception.Create(sErr);
end;
is translated to asm, found the following

Code:
FS_Main.pas.5041: begin
00000000005D3070 55               push rbp
00000000005D3071 4883EC20         sub rsp,$20
00000000005D3075 488BEC          mov rbp,rsp
FS_Main.pas.5042: raise Exception.Create(sErr);
00000000005D3078 488B0D4901E5FF  mov rcx,[rel $ffe50149]
00000000005D307F B201             mov dl,$01
00000000005D3081 4C8B0500460300   mov r8,[rel $00034600]
00000000005D3088 E81307E6FF      call Exception.Create
00000000005D308D 4889C1           mov rcx,rax
00000000005D3090 E8EB73E3FF      call @RaiseExcept
FS_Main.pas.5043: end;
00000000005D3095 488D6520         lea rsp,[rbp+$20]
00000000005D3099 5D              pop rbp
00000000005D309A C3               ret
copied it into my test-procedure ... and it worked.
Then i wanted to post it and saw that did the same.
Again thank you
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Kas Ob.
Online

Registriert seit: 3. Sep 2023
213 Beiträge
 
#27

AW: In Asm-Prozedur eine Exception auslösen

  Alt 6. Nov 2023, 08:47
This is : to whom it concern !

Very simple assembler code, this example shows how assembler code generation is entangled with Pascal part and way outdated and not optimized, though i am using XE8, but i have highly confidence in Embarcadero that they did not touch that part for newer versions.

Code:
PROCEDURE test;
const
   pExClass : ExceptClass = (Exception);
   sErr : String = ' My message ';
asm
    {$IfDef CPUX86}
   mov ecx,sErr
   mov dl ,1
   mov eax,pExClass
   call Exception .Create
   call System.@RaiseExcept
   {$Else}
   .noframe            //  crucial here
   push rbp
   sub rsp,$20
   mov rbp,rsp
   mov r8,sErr
   mov dl ,$01
   mov rcx,pExClass
   call Exception .Create
   mov rcx,rax
   call System.@RaiseExcept
   lea rsp,[rbp+$20]
   pop rbp
   {$EndIf}
end ;


{$STACKFRAMES ON}
function Add1(A,B:NativeInt):NativeInt;
asm
  add A,B
  mov result ,A
end;

{$STACKFRAMES OFF}
function Add2(A,B:NativeInt):NativeInt;
asm
  add A,B
  mov result ,A
end;

{$STACKFRAMES ON}
function Add3(A,B:NativeInt):NativeInt;
asm
  .noframe
  add A,B
  mov result ,A
end;

{$STACKFRAMES OFF}
function Add4(A,B:NativeInt):NativeInt;
asm
  .noframe
  add A,B
  mov result ,A
end;

{$STACKFRAMES OFF}
function Add5(A,B:NativeInt):NativeInt;
asm
  .noframe
 { push rbp   //
  sub rsp,$20 //  Trying to put our own frame with $20 byte window
  mov rbp,rsp //  }

  add A,B
  mov result ,A

  call test   //  calling your working code from previous post and it is OK, try..except will catch and recover form this

 { lea rsp,[rbp+$20]
  pop rbp   }
end;

{$STACKFRAMES OFF}
function Add6(A,B:NativeInt):NativeInt;
asm
  .noframe
  push rbp   //
  sub rsp,$20 //  Trying to put our own frame with $20 byte window
  mov rbp,rsp //

  add A,B
  mov result ,A

  call test   //  calling your working code from previous post and things are broken, on my XE8 try..except doesn't help at all

  lea rsp,[rbp+$20]
  pop rbp
end;
Check the assembly part in CPU window, and will see this gem
Code:
x64
0000000000426310 55               push rbp             // why the stack is needed at all here if we used .noframe
0000000000426311 4883EC10         sub rsp,$10
0000000000426315 488BEC          mov rbp,rsp
AsmException.dpr.87: add A,B
0000000000426318 4801D1           add rcx,rdx
AsmException.dpr.88: mov result ,A
000000000042631B 48894D08         mov [rbp+$08],rcx    //   really !!
AsmException.dpr.89: end;
000000000042631F 488B4508         mov rax,[rbp+$08]    //   WTF
0000000000426323 488D6510         lea rsp,[rbp+$10]
0000000000426327 5D              pop rbp
0000000000426328 C3               ret


x32
AsmException.dpr.86: asm
0041A50C 55               push ebp
0041A50D 8BEC            mov ebp,esp
0041A50F 51               push ecx                  //   WTF, pushing ecx on the stack ??!!!!! why ecx even mentioned or used here at all
AsmException.dpr.87: add A,B
0041A510 01D0             add eax,edx               //   the result is in eax, good, so it is in Result , lets return/exit
AsmException.dpr.88: mov result ,A
0041A512 8945FC          mov [ebp-$04],eax         //   really !!
AsmException.dpr.89: end;
0041A515 8B45FC          mov eax,[ebp-$04]         //   WTF
0041A518 59               pop ecx
0041A519 5D              pop ebp
0041A51A C3               ret

and you will notice many defect and unneeded stack write as both parameters are in register also the result in register meaning there is a wasted 1 full cycle to write and the worse id reading it on the following instruction causing wait on CPU pipe, for what ?!!! for no freaking reason, but this is off topic a little.

Back to exception and stack frames, none of the above followed my request from the compiler to not add a frame ( from documentation https://docwiki.embarcadero.com/RADS..._and_Functions )
Zitat:
Forcibly disables the generation of a stack frame as long as there are no local variables declared and the parameter count <= 4. Use only for leaf functions.
So obviously (with sarcasm ) my parameters are 5 or i am using local variable, unless result is considered as local variable which is... well .....

Anyway back to the topic, in all in above function the assembler added a stack push outside (encapsulating) my frame and my code, in Add6 did broke the test procedure which was working fine !, and that is my point, Add5 is not adding frame and test is working, while Add6 did broke outsider procedure/function "test", see why i am very pessimistic for adding your own frame stack in this very special case where you want to raise an exception.
No raise exception, fine, no problem add as much as you want on the stack, while expecting the stack winder after raising an exception to work out of the box and also to understand your stack use and the conflict with the compiler stack use there is problem, aka there will be blood !

Your test procedure which in its simplistic form doesn't have parameters or result worked fine, using that part with more complex code or lets say useful procedure will behave just like Add6, erratic with unpredictable behavior.


Sorry for the long post and bad Engrish.
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#28

AW: In Asm-Prozedur eine Exception auslösen

  Alt 7. Nov 2023, 01:58
Hier, in #24 sind die Prozeduren für die ich das haben wollte.
Noch einmal vielen Dank für die Hilfestellungen.

https://www.delphipraxis.net/119187-...ml#post1529146
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Benutzerbild von paule32.jk
paule32.jk

Registriert seit: 24. Sep 2022
Ort: Planet Erde
218 Beiträge
 
Delphi 11 Alexandria
 
#29

AW: In Asm-Prozedur eine Exception auslösen

  Alt 10. Nov 2023, 11:21
@Kas Obi:

I think, the stack is need under modern Microsoft Windows.
Because the first two 8 Bit = 16 Bit = 10h is for 64-Bit self-references the Functions, and Procedures.
So the first Argument/Parameter for the Function's/Procedure's begin at Bit 16.
I say to me:
Code:
first := 0 <-- self
second := 1 <-- first Parameter
...
Frag doch einfach
Alles was nicht programmiert werden kann, wird gelötet
  Mit Zitat antworten Zitat
Kas Ob.
Online

Registriert seit: 3. Sep 2023
213 Beiträge
 
#30

AW: In Asm-Prozedur eine Exception auslösen

  Alt 10. Nov 2023, 12:40
@Kas Obi:

I think, the stack is need under modern Microsoft Windows.
Because the first two 8 Bit = 16 Bit = 10h is for 64-Bit self-references the Functions, and Procedures.
So the first Argument/Parameter for the Function's/Procedure's begin at Bit 16.
I say to me:
Code:
first := 0 <-- self
second := 1 <-- first Parameter
...
I am not sure i do understand that %100, but Self will be used and added ONLY in Objects and Classes when the code is used in OOP style, not in orphan procedures/functions like all the mentioned p/f in this thread.

Example
Code:
function MyFunc(A: Integer; B: string): Integer;
begin

end;
 // Internally the compiler will make generate the code exactly as the following

procedure MyFuncAsProc(A: Integer; B: string; out Result: Integer);
begin

end;

------------------------

function MyClass.MyFunc(A: Integer; B: string): Integer;
begin

end;
 // Internally the compiler will make generate the code exactly as the following

procedure MyClass.MyFuncAsProc(const Self: TMyClass; A: Integer; B: string; out Result: Integer);
begin

end;
So using the satck is not for only referencing Self, in fact Self will always be in a register.( in edx on x86/x32, and in rcx on x64)

and that if i understand your comment right and we are talking about Self like This in other languages.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 3 von 4     123 4      


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 11:38 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