Einzelnen Beitrag anzeigen

Kas Ob.

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

AW: In Asm-Prozedur eine Exception auslösen

  Alt 6. Nov 2023, 09: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