News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Error Handling

Started by zekyr, August 09, 2011, 03:27:52 AM

Previous topic - Next topic

zekyr

hello! I was wondering how everyone here personally handles simple error messages and stuff :)

I remember in C if you could setup if statements like

if ( !function_call() ) return 1; //if function returns 0 then return

but its expanded version with macros is a little chunky for each function call

call function_call
.if eax == 0
     ret
.endif


i saw... i think it was qword do something cool with one of his while loops! thats why im curious

    .while 1
        invoke GetMessage,       ADDR msg, 0,0,0
        .break .if eax == 0 || eax == -1  ;whats going on here!
        invoke TranslateMessage, ADDR msg
        invoke DispatchMessage,  ADDR msg
    .endw


something like that

i tried writing jmp ERR .if eax == 0 but it wouldnt compile :(

dedndave

for the message loop, i just exit if that particular error occurs
which, i have yet to see it occur   :P
        pushad                                 ;make room for the MSG structure
        xor     edi,edi
        mov     ebp,esp
        jmp short mLoop1

mLoop0: INVOKE  TranslateMessage,ebp
        INVOKE  DispatchMessage,ebp

mLoop1: INVOKE  GetMessage,ebp,edi,edi,edi
        neg     eax                            ;exit only
        shr     eax,1                          ;if 0 or -1
        jnz     mLoop0

        pop     eax                            ;adjust the stack so that MSG.wParam
        JMP     ExitProcess                    ;is the parm for ExitProcess

if/elseif/endif, while/endw, and case/switch may be nice for former c programmers
but writing raw code is always more flexible   :U

jj2007

Quote from: dedndave on August 09, 2011, 10:26:18 AM
        neg     eax                            ;exit only
        shr     eax,1                          ;if 0 or -1
        jnz     mLoop0

Cute :U
This one is same size:
@@:
inc eax
je @B  ; -1 ->0
dec eax  ; 0->1 ->0
je @B

dedndave

        inc     eax
        shr     eax,1
        jnz     mLoop0


one byte smaller   :P

sorry - i shouldn't do this stuff in the campus   :red

jj2007

Quote from: dedndave on August 09, 2011, 04:45:04 PM
        inc     eax
        shr     eax,1
        jnz     mLoop0


one byte smaller   :P

sorry - i shouldn't do this stuff in the campus   :red

Who cares :bg

It is also pretty fast - 2 cycles only:
Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)

Results for eax=12345:
204     cycles for neg
289     cycles for inc+dec
246     cycles for test+inc
241     cycles for dec+inc
207     cycles for inc+shr

Results for eax=-12345:
204     cycles for neg
266     cycles for inc+dec
276     cycles for test+inc
269     cycles for dec+inc
216     cycles for inc+shr

qWord

^^
nice - optimization of a loop that is calling a blocking function   :U
FPU in a trice: SmplMath
It's that simple!

dedndave

Quote from: qWord on August 09, 2011, 09:25:58 PM
^^
nice - optimization of a loop that is calling a blocking function
:lol
i wasn't really going for fast - i was going for small

as for the values that are returned by GetMessage, i have it on good authority that they are always positive if no error
however, i am unable to verify this via ms documentation

if that assumption holds, the code could be
        dec     eax
        jns     mLoop0

qWord

Quote from: dedndave on August 09, 2011, 09:44:28 PMif that assumption holds, the code could be
        dec     eax
        jns     mLoop0

in this case I would use this:
test eax,eax
jg @loop


BTW: from msdn about messages range:
Quote from: msdnGreater than 0xFFFF      Reserved by the system.
FPU in a trice: SmplMath
It's that simple!

baltoro

#8
Hilarious ! This is what happens when you ask a bunch of geniuses a simple question,...  :eek
ZEKYR,
There are an almost unlimited number of ways in which to handle an error. For a simple demo application, here at the MASM forum most of the resident geniuses use a simple MessageBox. What amazes me is the number of beginner programmers who never even check return values. Typically, you see the same types of errors over and over.
I, however, prefer to create as much bloat as possible, so I will often write a log file. This is really only useful in a large complicated project, where the user can perform a number of different operations and long, tedious calculations.
The key concept is,...not to make an error within your error handling block,... :eek
I can't tell you how many times I've done this,...it's extremely annoying, because it's usually the last place you look,...
Baltoro

dedndave

 :bg
the nature of assembly programmers
we love to analyze the crap out of everything
if that wasn't part of our personality, we'd be writing in c

baltoro

#10
...Pretty funny,... :green

It never ceases to amaze me how really talented and knowledgeable you guys are,...
Baltoro

jj2007

Quote from: qWord on August 09, 2011, 10:03:11 PM
Quote from: dedndave on August 09, 2011, 09:44:28 PMif that assumption holds, the code could be
        dec     eax
        jns     mLoop0

in this case I would use this:
test eax,eax
jg @loop



Bloat alarm!!! :dazzled:

dedndave

 :bdg
qWord writes good stuff - leave him be

jj2007

Quote from: dedndave on August 09, 2011, 11:09:24 PM
:bdg
qWord writes good stuff - leave him be

qWord is one of the best, but teasing him will drive him to even higher performance. Better than trying desperately to hit the Ballmer Peak :green

redskull

Though more conceptual than technical, you shouldn't be using a 'ret' in the event of an error; that just bounces a problem back to into a flawed system.  If you are serious about reliable software, then you should be using ExitProcess() or, even better, RaiseException().

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government