Author Topic: Assert  (Read 2988 times)

jj2007

  • Member
  • *****
  • Posts: 10468
  • Assembler is fun ;-)
    • MasmBasic
Assert
« on: January 10, 2014, 03:02:20 AM »
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

  • Member
  • **
  • Posts: 89
Re: Assert
« Reply #1 on: March 01, 2014, 03:13:30 PM »
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?