Difference between revisions of "Auto Assembler:TRY EXCEPT"

From Cheat Engine
Jump to navigation Jump to search
(First draft)
 
m (Added note about position)
 
(5 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
Next, at the end of {$try} you will want to force a jump to a location indicating success; if you do not, then the code after {$except} will be executed.
 
Next, at the end of {$try} you will want to force a jump to a location indicating success; if you do not, then the code after {$except} will be executed.
  
Also note that the exception may break from any line, so make sure that you exception code can handle unexpected register values.
+
Also note that the exception may break from any line in the {$try}/{$except} block, so make sure that your exception code can handle unexpected register values.
  
 +
How does it work? It works by assembling an exception handler in the game and then register it using addvectoredexception handler. Then, it adds a list of eip/rip ranges and where to jump to on exception. So, when an exception happens it checks if it's in the list, and if so, change eip/rip to the exception address.
  
  
Line 17: Line 18:
 
mov eax,[ecx+10]
 
mov eax,[ecx+10]
  
jmp short @f
+
jmp short noerror
  
 
{$except}
 
{$except}
Line 23: Line 24:
 
xor eax,eax
 
xor eax,eax
  
@@:
+
noerror:
 +
 
 +
Benefits:
 +
 
 +
- There should be hardly any performance hit as long as no exception happens.
 +
 
 +
 
 +
Remarks:
 +
 
 +
{$try} is actually a label.
 +
Meaning, you should not use anonymous labels (i.e. @@) with forward (@f) and backwards (@b) that are supposed to jump over the {$try}/{$except} block because the @f and @b will jump to the {$try} label rather your intended anonymous label.
 +
 
 +
{$try} and {$except} must be at the beginning of the line to be recognized

Latest revision as of 17:35, 14 October 2022

CE 6.8 added {$try}/{$except} to auto assembler scripts.

Essentially, any code included in the {$try}/{$except} block will jump to the {$EXCEPT} part when any kind of exception happens within the block.

This means, you will want to put the code likely to result in an error between {$try} and {$except}.

Next, at the end of {$try} you will want to force a jump to a location indicating success; if you do not, then the code after {$except} will be executed.

Also note that the exception may break from any line in the {$try}/{$except} block, so make sure that your exception code can handle unexpected register values.

How does it work? It works by assembling an exception handler in the game and then register it using addvectoredexception handler. Then, it adds a list of eip/rip ranges and where to jump to on exception. So, when an exception happens it checks if it's in the list, and if so, change eip/rip to the exception address.


In this example, if any error occurs - for example, ecx is zero or ecx+10 is unreadable -, eax will be set to zero:

{$try}

mov eax,[ecx+10]

jmp short noerror

{$except}

xor eax,eax

noerror:

Benefits:

- There should be hardly any performance hit as long as no exception happens.


Remarks:

{$try} is actually a label. Meaning, you should not use anonymous labels (i.e. @@) with forward (@f) and backwards (@b) that are supposed to jump over the {$try}/{$except} block because the @f and @b will jump to the {$try} label rather your intended anonymous label.

{$try} and {$except} must be at the beginning of the line to be recognized