The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: ecube on March 18, 2009, 08:59:54 PM

Title: SEH
Post by: ecube on March 18, 2009, 08:59:54 PM
can I use seh macros through my program to prevent any crashes? and are there any big disadvantages?
Title: Re: SEH
Post by: ToutEnMasm on March 18, 2009, 09:06:22 PM

Hello,
Quote
there any big disadvantages?
Not,you don't lose time or other things.They are more useful when writing the code.
When the code is finish and tested they aren't very usefull.
Title: Re: SEH
Post by: Tedd on March 23, 2009, 11:30:49 AM
There's a small overhead for setting up each handler (one for each procedure) but that's about all.
But if you care enough to catch the exceptions, then it shouldn't be an issue.

They are still useful after development, unless you assume no-one will ever give your program bad input, or nothing will ever go wrong.
Title: Re: SEH
Post by: BlackVortex on March 23, 2009, 04:51:43 PM
Are there any macros like that in the masm32 package ?  Couldn't find any. Or some examples ?

I'm looking for something hassle-free, like the try/except/finally directives of some HLL languages.
Title: Re: SEH
Post by: ecube on March 23, 2009, 05:18:54 PM
seh.inc


sSEH STRUCT
OrgEsp            DD ?
OrgEbp            DD ?
SaveEip           DD ?
sSEH ENDS

;---- MACROs ----
InstSehFrame MACRO ContinueAddr
ASSUME FS : NOTHING

   IFNDEF  SehStruct
        SehStruct EQU 1
      .DATA
      SEH    sSEH <>
   ENDIF
   
.CODE
MOV  SEH.SaveEip, ContinueAddr
MOV  SEH.OrgEbp, EBP
PUSH OFFSET SehHandler
PUSH FS:[0]
MOV  SEH.OrgEsp, ESP
MOV  FS:[0], ESP
ENDM

KillSehFrame MACRO
POP  FS:[0]
ADD  ESP, 4
ENDM

.CODE
SehHandler PROC C pExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD

MOV  EAX, pContext
ASSUME EAX : PTR CONTEXT

PUSH SEH.SaveEip
POP  [EAX].regEip
PUSH SEH.OrgEsp
POP  [EAX].regEsp
PUSH SEH.OrgEbp
POP [EAX].regEbp

MOV  EAX, ExceptionContinueExecution

RET
SehHandler ENDP



usage
InstSehFrame <OFFSET not_under_VMware>
push   edx
      push   ecx
      push   ebx

      mov    eax, 'VMXh'
      mov    ebx, 0         ; any value but not the MAGIC VALUE
      mov    ecx, 10        ; get VMWare version
      mov    edx, 'VX'      ; port number

      in     eax, dx        ; read port
                            ; on return EAX returns the VERSION
      cmp    ebx, 'VMXh'    ; is it a reply from VMWare?
      jne  not_under_VMware ; set return value

      pop    ebx
      pop    ecx
      pop    edx
KillSehFrame
    ;Vmware found

not_under_VMware:       
KillSehFrame


also http://www.rohitab.com/sourcecode/seh.html

the code I posted above I forgot where i got
Title: Re: SEH
Post by: BlackVortex on March 23, 2009, 07:19:39 PM
Awesome cube, nice finds there. Just used the first one and worked great the first time. Also, no registers are changed after setting a new handler !