News:

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

Assembly time SWITCH macro, different behaviour ML/JWasm

Started by jj2007, December 31, 2009, 10:32:24 AM

Previous topic - Next topic

jj2007

I was playing with an assembly time SWITCH macro and got stuck. Assembly fails for ml, while JWasm assembles the code but runs the non-cases, too. Really tricky. Knowing that some here know a lot more about macros than I do, I post the testbed hoping to get some feedback...
I am not sure if the idea is feasible at all, but am curious to see what you think.

.nolist
include \masm32\include\masm32rt.inc
.listall
aSwitch MACRO arg   ; assembly time switch
  SwitchArg = arg
  ctcase = 0
  echo ------------- Switch -------------
ENDM
aCase MACRO carg
LOCAL SwON
  if SwitchArg eq carg
SwON = 1
  else
SwON = 0
  endif
  tmp$ CATSTR <** aCASE=>, <carg>, <, flagged >, %SwON, < with ct=>, %ctcase, < **>
  % echo tmp$
  if ctcase
tmp$ CATSTR <elseif >, %SwON
% echo tmp$
tmp$
echo BLAAAAAAAAAAAAAAA never seen
  else
tmp$ CATSTR <if >, %SwON
% echo tmp$
tmp$
echo BLAAA #2, visible for ML but not JWasm
  endif
  ctcase = ctcase + 1
ENDM
aEndsw MACRO
  tmp$ equ <endif>
  echo endif ------------
  tmp$ ; if commented, the code will assemble in ml but run both cases
ENDM

.code
start:
aSwitch 1
aCase 0
print "Case 0", 13, 10
aCase 1
print "Case 1", 13, 10
aEndsw
exit

end start

jj2007

To illustrate the problem further, here the bottleneck:
include \masm32\include\masm32rt.inc
.code
start:
MyEndif equ endif
if 1
MsgBox 0, "Test", "Hi", MB_OK
MyEndif
exit
end start

This assembles fine, in ml and Jwasm. Replace the if 1 with if 0, and an error will be thrown by both assemblers: unmatched block nesting. In other words, the if 0 prevents the equate or macro from being used.

jj2007

... and the solution:

include \masm32\include\masm32rt.inc

SwitchArg = 1 ; the option we choose
UseNothing = 0 ; option 0: do nothing
UseEdit = 1 ; option 1: create an edit control
UseListbox = 2 ; option 2: create a listbox

_Switch equ if 0
_Case equ elseif SwitchArg eq
_Endsw equ endif

.code
start:
%_Switch
%_Case UseEdit
print "Case Edit", 13, 10
%_Case UseListbox
print "Case Listbox", 13, 10
%_Case UseNothing
print "Case nothing", 13, 10
%_Endsw
exit

end start


Might look nice inside a WM_CREATE handler :bg

Works fine with ml 6.14, ml 6.15 and ml 9.0 but unfortunately not with JWasm.

Happy New MMX Year,
Jochen