The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: zekyr on August 09, 2011, 03:27:52 AM

Title: Error Handling
Post by: zekyr on August 09, 2011, 03:27:52 AM
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 :(
Title: Re: Error Handling
Post by: dedndave on August 09, 2011, 10:26:18 AM
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
Title: Re: Error Handling
Post by: jj2007 on August 09, 2011, 12:29:20 PM
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
Title: Re: Error Handling
Post by: 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
Title: Re: Error Handling
Post by: jj2007 on August 09, 2011, 09:03:50 PM
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
Title: Re: Error Handling
Post by: qWord on August 09, 2011, 09:25:58 PM
^^
nice - optimization of a loop that is calling a blocking function   :U
Title: Re: Error Handling
Post by: dedndave on August 09, 2011, 09:44:28 PM
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
Title: Re: Error Handling
Post by: 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


BTW: from msdn (http://msdn.microsoft.com/en-us/library/ms644931(v=vs.85).aspx) about messages range:
Quote from: msdnGreater than 0xFFFF      Reserved by the system.
Title: Re: Error Handling
Post by: baltoro on August 09, 2011, 10:18:29 PM
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,...
Title: Re: Error Handling
Post by: dedndave on August 09, 2011, 10:22:39 PM
 :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
Title: Re: Error Handling
Post by: baltoro on August 09, 2011, 10:30:58 PM
...Pretty funny,... :green

It never ceases to amaze me how really talented and knowledgeable you guys are,...
Title: Re: Error Handling
Post by: jj2007 on August 09, 2011, 10:49:26 PM
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:
Title: Re: Error Handling
Post by: dedndave on August 09, 2011, 11:09:24 PM
 :bdg
qWord writes good stuff - leave him be
Title: Re: Error Handling
Post by: jj2007 on August 09, 2011, 11:18:32 PM
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 (http://xkcd.com/323/) :green
Title: Re: Error Handling
Post by: redskull on August 09, 2011, 11:38:19 PM
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
Title: Re: Error Handling
Post by: baltoro on August 09, 2011, 11:44:25 PM
REDSKULL,   
You're probably right.
I often find myself in that phase of writing software where I'm absolutely certain my code is generating errors (and, by that I mean, the code is not doing what I expected). In that case, I want the application to keep running until I terminate it, and I want as much information about its execution as I can get before I rewrite and re-compile.
I think when you are finally past that chaotic phase, you clean up your code and remove all the code blocks you wrote just to get it to work right,...and make it bulletproof,...then you add a more reliable system error checking mechanism.
You probably write pretty clean code. I'm exactly the opposite. I thrash around alot,...
Title: Re: Error Handling
Post by: redskull on August 10, 2011, 12:54:21 AM
Quote from: baltoro on August 09, 2011, 11:44:25 PM
when you are finally past that chaotic phase, you clean up your code and remove all the code blocks you wrote just to get it to work right,...and make it bulletproof,...then you add a more reliable system error checking mechanism.

I find that while a good idea in theory, there's never time or money to do this; the squeaky wheel gets the grease, so as long as it works it ships.  The bug doesn't appear until months later when you've forgotten everything and it takes weeks to track down.  If you throw an exception at the first sign of danger (and, conversely, rarely catch anything), then as soon as it breaks you know when, where, and why.  But more importantly, you can't ignore it  :U
Title: Re: Error Handling
Post by: baltoro on August 10, 2011, 01:12:08 AM
Ahhhh,...yes, that makes alot of sense.

Hey,...here's a question,...since all you genuises are reading this,...
This is a very common error,...I've done this in C++ probably millions of times,...and, I would imagine it is even worse in assembly language: dereferencing an invalid pointer. In assembly code it would be dereferencing an invalid address,...one that fails an access check, or, an address to memory that has been released or the data is no longer valid. These things are insidious,...especially in COM, where the application just crashes, and, you have no idea why.
So, the question is: what is the best way to handle an error that you generate when trying to dereference an invalid address ??? Or, worse,...the error you get when trying to write to an invalid address,...and you generate an access violation,...
Here's an interesting post from Raymond Chen's Blog: IsBadXxxPtr Should Really Be Called CrashProgramRandomly
(http://blogs.msdn.com/b/oldnewthing/archive/2006/09/27/773741.aspx)
I could never find a great way to deal with this problem. In C++, you set the pointer value to an invalid value, like zero, which can be easily tested. You could do the same thing in assembly language, but, you'd be guaranteed to crash if you didn't first attempt to test the invalid address.
Title: Re: Error Handling
Post by: zekyr on August 10, 2011, 02:47:55 AM
Quote from: baltoro on August 09, 2011, 10:18:29 PM
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.

There really is a lot of nice stuff here :) I write code usually without error checking but I figured I have to grow up sometime! and I wanted to know if there was some compact ways / preferred ways of doing simple error checking stuff; especially since I was having a lot of trouble getting winsocks in icezlion's winsock tutorials working: I was trying to write udp stuff instead of tcp so i wasn't following exactly. I finally got it by the way! just had to do a lot of stepping through code with olly! :)
Title: Re: Error Handling
Post by: dedndave on August 10, 2011, 03:06:27 AM
you can use the forum search tool for "SEH" (structured exception handling)
as i understand it, that is how c compilers handle errors - you should feel right at home
Title: Re: Error Handling
Post by: jj2007 on August 10, 2011, 07:28:45 AM
Quote from: baltoro on August 10, 2011, 01:12:08 AM
dereferencing an invalid address

In development phase, by far the best option is to set Olly as your default debuffer: Options/Debugging/Just-in-time.
So when the error occurs, Olly pops up, you find yourself inside your own code, and Olly shows you exactly what happened.
Title: Re: Error Handling
Post by: baltoro on August 10, 2011, 04:27:50 PM
JOCHEN,
Quote from: JOCHENIn development phase, by far the best option is to set Olly as your default debuffer: Options/Debugging/Just-in-time.
So when the error occurs, Olly pops up, you find yourself inside your own code, and Olly shows you exactly what happened.

EXCELLENT,...thanks. I'd never thought of that. Maybe, this will make me a less crappy programmer. :bg