News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Assert

Started by jj2007, January 10, 2014, 03:02:20 AM

Previous topic - Next topic

jj2007

Hi folks,

I am testing an ASSERT-type macro. The principle (nicely explained here) is that a program should shout or otherwise react if a condition is not fulfilled. That is C/C++ - in assembler it can in general be reduced to the simple question "is eax TRUE or FALSE?"

And the idea is of course that you can easily switch the check off once the code is tested.

Usage would be simply ChkZ rv(GetTickCount) or ChkNZ sval("0")

Long example:

include \masm32\MasmBasic\MasmBasic.inc        ; download
ExternDef MbError0:NEAR
usedeb=1                                ; switch error checks on/off

ChkNZ MACRO args:VARARG
  @CatStr(<;>, <args>)
  if usedeb
        test eax, eax
        je MbError0                ; trigger runtime error à la Assert?
  endif
ENDM

ChkZ MACRO args
LOCAL tmp$
  @CatStr(<;>, <args>)
  if usedeb
    .if eax                        ; or show a Messagebox, and continue or cancel?
         pushad
         tmp$ CATSTR <Chr$("## non-zero eax in line >, %@Line, < ##")>
         invoke MessageBox, 0, tmp$, 0, MB_OKCANCEL
         cmp eax, IDCANCEL
         je MbError0                ; will show formatted GetLastError and ExitProcess
         popad
    .endif
  endif
ENDM

  Init
  ChkZ rv(GetTickCount)
  Inkey Str$("GetTickCount returned %i", eax)
  Exit
end start


So in case of failure, ChkZ/ChkNZ would somehow react, unless usedeb=0 (in this case, no code would be generated).

Question: What is better,
- going straight to a runtime error handler, show GetLastError and ExitProcess
or
- show a MessageBox and leave the option to continue?

What about the names ChkZ and ChkNZ? "Assert" would be misleading imho because it requires a condition as first arg. Any better ideas?

NoCforMe

Quote from: jj2007 on January 10, 2014, 03:02:20 AM
Question: What is better,
- going straight to a runtime error handler, show GetLastError and ExitProcess
or
- show a MessageBox and leave the option to continue?

Yes.

IOW, maybe we want both. How 'bout ChkM() for the message-box flavor, or some such?
Assembly language programming should be fun. That's why I do it.