Hello,

It's me again, the guy who keeps trying to fit HLL thinking into an assy-shaped box. :grin:

I'm diddling with Iczelion's tutorials, and I've been doing this a lot:

    ; check for error code

invoke SomeAPIFunction
test eax, eax ; did API return 0 (failure)?
jnz @F
; yes, print error message and abort
invoke MessageBox, NULL, addr SomeErrorMessage, \
addr ErrTitle, MB_OK or MB_ICONWARNING
mov eax, -1 ; return error code
ret
@@:


Now, I'm a big fan of hiding details to reveal intent and/or to encapsulate behavior that might change. This is a fairly trivial example, but it's triviality is exactly what makes it good for posting to a forum. :) So anyway. Try to resist the urge to just say "well, why bother, it doesn't save you anything" for now, because I'm going to use it to more worthy effect later on.

Okay, here's my thinking. I'd like to be able to hide the details of the MessageBox call, like so:

    test eax, eax    ; error?

jnz @F
invoke ErrorMessage, addr SomeErrorMessage
mov eax, -1
ret
;...
ErrorMessage proc pszErrorMessage:LPCSTR
invoke MessageBox, NULL, pszErrorMessage, addr ErrTitle, \
MB_OK or MB_ICONWARNING
ErrorMessage endp
@@:


The advantage to this is (a) it simplifies the main body of code and (b) I can change it to redirect to StdOut or to do nothing very easily. The problem is, it relies on there being a string named ErrTitle in the data segment. I'd like it to be able to handle that part on its own, rather than require the programmer to jam that in every time I want to incorporate this method in a program.

Is that possible? How?

Thanks!

-Chalain
Posted on 2002-04-24 12:43:03 by Chalain
look at the szString macro in the masm32 package
Posted on 2002-04-24 12:53:48 by Hiroshimator
ErrorMessage MACRO pszErrorMessage:REQ, ErrTitle:=<ErrorMessage_Title>


IFNDEF ErrorMessage_Title
const SEGMENT
ErrorMessage_Title db "Oh, Poo!",0
const ENDS
ENDIF

invoke MessageBox, NULL, pszErrorMessage, addr ErrTitle, \
MB_OK or MB_ICONWARNING

ENDM



test eax, eax ; error?
jnz @F
ErrorMessage, addr SomeErrorMessage
mov eax, -1
ret

test eax, eax ; error?
jnz @F
ErrorMessage, addr SomeErrorMessage, addr SomeTitle
mov eax, -1
ret
;...
@@:
Allows custom title, or default.
Posted on 2002-04-24 13:04:44 by bitRAKE
Hrm, "ErrorMessage MACRO" and then "ErrorMessage endp" ? :P.
I'd still make it a proc - it will save some PUSH code bytes if it's used
a bunch of times, and it's not like execution speed is going to be
important.
Posted on 2002-04-24 13:07:17 by f0dder
:tongue: What are you talking about f0dder? :tongue:

It's just hard to see all the errors until it's posted. I'm typing here from work between tasks and it's very common for me to post-edit. Once I see how bad the advise is, I shape it into something half way descent. Also, wouldn't be a shock if it didn't assemble. :eek:
Posted on 2002-04-24 13:15:46 by bitRAKE
I'm talking about the EDIT button of course ;)
Posted on 2002-04-24 13:22:54 by f0dder
Chalain, I'm really fond of reducing the interface between pieces of code through encapsulation and have found macros are really the best way to accomplish this in ASM. It would be nice if we had some more features in a PROC than MASM provides - like true static locals, instead of creating a global. Labels created in the code section that is within a PROC can be scoped (in fact are by default), but if you switch to another segment then the labels are global - they should be scoped as well, imo. Here is an example to show you what I mean:
TestPROC PROC p1:DWORD


_data SEGMENT
value dd 12345678h ; this is !global!
_data ENDS

xor eax,eax
Begin: ; this is scoped label
cmp p1,123
je Exit
inc eax
Exit: ; this is scoped label
ret

TestPROC ENDP
You can probably see how your problem above could be solved within a PROC if all the labels within a PROC were scoped - no matter what segment they are in.
Posted on 2002-04-24 17:24:47 by bitRAKE
BitRAKE, don't worry about making errors, it's a pretty human thing to do :).
I make a fair deal of errors and mistakes myself, and I mostly point out
other peoples mistakes to be helpful...

Damn I wish I could get myself together and write an assembler. I've been
wanting to for a long time. Nah, it wouldn't be anything revolutionary,
but it would be a fun experience. My main problem is probably setting up
a decent tokenizer, and then of course keep on working :).
Posted on 2002-04-25 05:33:00 by f0dder