![]() |
AW: In Asm-Prozedur eine Exception auslösen
@jaenicke
Zitat:
Danke für den Hinweis, werde mich im Laufe des Tages darum kümmern, jetzt erstmal 2-3 Stunden schlafen. |
AW: In Asm-Prozedur eine Exception auslösen
@Paule32.jk
Zitat:
|
AW: In Asm-Prozedur eine Exception auslösen
Zitat:
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; |
AW: In Asm-Prozedur eine Exception auslösen
I am trying here to explain and language barrier is playing huge role.
This will work on x64 for this exact procedure
Code:
BUT will fail on many other places and different situations or for just little more complex assembly above that system.@RaiseExcept !!!
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 ; 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. |
AW: In Asm-Prozedur eine Exception auslösen
OK, there much to say about this but really i don't like write essays :stupid:
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:
Yet again, use that on your risk !
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 ; Edit: to fix an extra pop useless in that example but will cause problem in real life code. |
AW: In Asm-Prozedur eine Exception auslösen
Zitat:
Funny thing. Just before entering the DP I looked, how
Delphi-Quellcode:
is translated to asm, found the following
PROCEDURE TestRaise;
const sErr:String='Meine Meldung'; begin raise Exception.Create(sErr); end;
Code:
copied it into my test-procedure ... and it worked.
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 Then i wanted to post it and saw that did the same. Again thank you |
AW: In Asm-Prozedur eine Exception auslösen
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:
Check the assembly part in CPU window, and will see this gem
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;
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 ![]() Zitat:
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. |
AW: In Asm-Prozedur eine Exception auslösen
Hier, in #24 sind die Prozeduren für die ich das haben wollte.
Noch einmal vielen Dank für die Hilfestellungen. ![]() |
AW: In Asm-Prozedur eine Exception auslösen
@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 ... |
AW: In Asm-Prozedur eine Exception auslösen
Zitat:
Example
Code:
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)
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; and that if i understand your comment right and we are talking about Self like This in other languages. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:37 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 by Thomas Breitkreuz