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
Kas Ob.

Registriert seit: 3. Sep 2023
433 Beiträge
 
#1

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.
Kas
  Mit Zitat antworten Zitat
Amateurprofi

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

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
356 Beiträge
 
Delphi 11 Alexandria
 
#3

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.

Registriert seit: 3. Sep 2023
433 Beiträge
 
#4

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.
Kas
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.397 Beiträge
 
Delphi 12 Athens
 
#5

AW: In Asm-Prozedur eine Exception auslösen

  Alt 10. Nov 2023, 12:46
no, with is only for results greater a Register aka SizeOf(Pointer)
or for Results with managed types, such as string and interface.
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
Benutzerbild von paule32.jk
paule32.jk

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

AW: In Asm-Prozedur eine Exception auslösen

  Alt 10. Nov 2023, 19:41
@kas ob:

ehmm... with self, I mean not the Delphi keyword in Context of Classes/Records.
with self, I mean the internal working Design of Microsoft Windows.
Like himitsu said - it could be for "self" managed Code (COM+).

And I can bet, that Microsoft have a secret Layer'ed File with Numbers (I pointed: 2 ^64 = 18 Trillion GiBytes dataflow. So, each managed Type becomes a (GUID) Number, and as such, all the Component(s) are different and can directly accessed. Instead to use String like in Delphi, and C++ Builder Debug Symbols.
In older Window's, the DLL Functions could be called by Value, and Name.
Frag doch einfach
Alles was nicht programmiert werden kann, wird gelötet
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke
Online

Registriert seit: 10. Jun 2003
Ort: Berlin
9.980 Beiträge
 
Delphi 12 Athens
 
#7

AW: In Asm-Prozedur eine Exception auslösen

  Alt 10. Nov 2023, 22:01
I don't understand what you mean, but the stack layout is documented here:
https://learn.microsoft.com/en-us/cpp/build/stack-usage
Sebastian Jänicke
AppCentral
  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 12:58 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz